From 1f23d540027cd4c9f01cc5f21633d6f2a71b1242 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 30 Dec 2025 15:24:11 +0100 Subject: [PATCH] copy fonts to source and serve local fonts --- freshen.json | 3 ++ piped-frontend/default.nix | 42 ++++++++++++++++++++++++++- piped-frontend/extract-font-hashes.sh | 14 +++++++++ piped-frontend/fonts.json | 4 +++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 piped-frontend/extract-font-hashes.sh create mode 100644 piped-frontend/fonts.json diff --git a/freshen.json b/freshen.json index 6ca0d9b..56b5c05 100644 --- a/freshen.json +++ b/freshen.json @@ -55,6 +55,9 @@ }, { "name": "piped", + "update_scripts": [{ + "attr_path": "piped-frontend.passthru.fonts" + }], "tests": [{ "attr_path": "piped-test", "disable_sandbox": true diff --git a/piped-frontend/default.nix b/piped-frontend/default.nix index b6aae22..ceef211 100644 --- a/piped-frontend/default.nix +++ b/piped-frontend/default.nix @@ -2,6 +2,8 @@ , lib , buildNpmPackage , pnpm +, fetchurl +, writeScript }: let @@ -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 { @@ -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"; diff --git a/piped-frontend/extract-font-hashes.sh b/piped-frontend/extract-font-hashes.sh new file mode 100644 index 0000000..f4467a3 --- /dev/null +++ b/piped-frontend/extract-font-hashes.sh @@ -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 '}' diff --git a/piped-frontend/fonts.json b/piped-frontend/fonts.json new file mode 100644 index 0000000..723da05 --- /dev/null +++ b/piped-frontend/fonts.json @@ -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=" +}