Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions freshen.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
},
{
"name": "piped",
"update_scripts": [{
"attr_path": "piped-frontend.passthru.fonts"
}],
"tests": [{
"attr_path": "piped-test",
"disable_sandbox": true
Expand Down
42 changes: 41 additions & 1 deletion piped-frontend/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
, lib
, buildNpmPackage
, pnpm
, fetchurl
, writeScript
}:

let
Expand All @@ -17,6 +19,16 @@ let

pnpmDeps = doPnpmDeps (builtins.fromJSON (builtins.readFile ./npmDepsHash.json));

fonts = builtins.mapAttrs (url: hash: fetchurl {inherit url hash;}) (builtins.fromJSON (builtins.readFile ./fonts.json));
copyFonts = builtins.concatStringsSep "\n" (lib.mapAttrsToList (
url: font: let
fontPath = "dist/fonts/" + (builtins.substring (builtins.stringLength "https://fonts.gstatic.com/") (-1) url);
in ''
mkdir -p $(dirname ${fontPath})
cp ${font} ${fontPath}
''
)
fonts);
in

buildNpmPackage {
Expand All @@ -27,11 +39,39 @@ buildNpmPackage {

npmDeps = pnpmDeps;

postBuild = ''
echo "postBuild"
mkdir -p dist/fonts

${copyFonts}

sed -i "s#https://fonts.gstatic.com#/fonts#g" dist/assets/*
ls -l dist/fonts/
'';

installPhase = ''
cp dist $out -r
'';

passthru.hashUpdate = doPnpmDeps "";
passthru = {
hashUpdate = doPnpmDeps "";
fonts = writeScript "update-font-hashes" ''
#!/usr/bin/env bash

tmp=$(mktemp -d)
cp -r "${src}" "$tmp/src"
chmod -R 750 $tmp/src
(
cd "$tmp/src"
${pnpm}/bin/pnpm install
echo "extracting"
bash ${./extract-font-hashes.sh} > fonts.json
cat fonts.json
)
cp "$tmp/src/fonts.json" "piped-frontend/fonts.json"
rm -fr "$tmp"
'';
};

meta = {
homepage = "https://github.com/TeamPiped/piped";
Expand Down
14 changes: 14 additions & 0 deletions piped-frontend/extract-font-hashes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl nix

fonts=($(grep -hPo "https://fonts\.gstatic\.com/s[^)]*" -r node_modules/ | sort | uniq))
echo '{'
for font in "${fonts[@]}"; do
hash=$(nix-hash --to-sri --type sha256 $(nix-prefetch-url "$font" 2>/dev/null))
if [ "$font" = "${fonts[-1]}" ]; then
echo ' "'"$font"'"': '"'"$hash"'"'
else
echo ' "'"$font"'"': '"'"$hash"'",'
fi
done
echo '}'
4 changes: 4 additions & 0 deletions piped-frontend/fonts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"https://fonts.gstatic.com/s/materialiconsround/v109/LDItaoyNOAY6Uewc665JcIzCKsKc_M9flwmM.otf": "sha256-utheVFS2KIEEzgOAbDcyO82PFF4wlOcnhgFzrIyRBi4=",
"https://fonts.gstatic.com/s/roboto/v48/KFOMCnqEu92Fr1ME7kSn66aGLdTylUAMQXC89YmC2DPNWubEbVmUiA8.ttf": "sha256-Z6CSYilO1dB4PF8qsg2Wc2VMLePQOHBHN+v+noJu1hM="
}