Skip to content

Commit 1f23d54

Browse files
committed
copy fonts to source and serve local fonts
1 parent 60a368b commit 1f23d54

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

freshen.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
},
5656
{
5757
"name": "piped",
58+
"update_scripts": [{
59+
"attr_path": "piped-frontend.passthru.fonts"
60+
}],
5861
"tests": [{
5962
"attr_path": "piped-test",
6063
"disable_sandbox": true

piped-frontend/default.nix

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
, lib
33
, buildNpmPackage
44
, pnpm
5+
, fetchurl
6+
, writeScript
57
}:
68

79
let
@@ -17,6 +19,16 @@ let
1719

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

22+
fonts = builtins.mapAttrs (url: hash: fetchurl {inherit url hash;}) (builtins.fromJSON (builtins.readFile ./fonts.json));
23+
copyFonts = builtins.concatStringsSep "\n" (lib.mapAttrsToList (
24+
url: font: let
25+
fontPath = "dist/fonts/" + (builtins.substring (builtins.stringLength "https://fonts.gstatic.com/") (-1) url);
26+
in ''
27+
mkdir -p $(dirname ${fontPath})
28+
cp ${font} ${fontPath}
29+
''
30+
)
31+
fonts);
2032
in
2133

2234
buildNpmPackage {
@@ -27,11 +39,39 @@ buildNpmPackage {
2739

2840
npmDeps = pnpmDeps;
2941

42+
postBuild = ''
43+
echo "postBuild"
44+
mkdir -p dist/fonts
45+
46+
${copyFonts}
47+
48+
sed -i "s#https://fonts.gstatic.com#/fonts#g" dist/assets/*
49+
ls -l dist/fonts/
50+
'';
51+
3052
installPhase = ''
3153
cp dist $out -r
3254
'';
3355

34-
passthru.hashUpdate = doPnpmDeps "";
56+
passthru = {
57+
hashUpdate = doPnpmDeps "";
58+
fonts = writeScript "update-font-hashes" ''
59+
#!/usr/bin/env bash
60+
61+
tmp=$(mktemp -d)
62+
cp -r "${src}" "$tmp/src"
63+
chmod -R 750 $tmp/src
64+
(
65+
cd "$tmp/src"
66+
${pnpm}/bin/pnpm install
67+
echo "extracting"
68+
bash ${./extract-font-hashes.sh} > fonts.json
69+
cat fonts.json
70+
)
71+
cp "$tmp/src/fonts.json" "piped-frontend/fonts.json"
72+
rm -fr "$tmp"
73+
'';
74+
};
3575

3676
meta = {
3777
homepage = "https://github.com/TeamPiped/piped";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env nix-shell
2+
#!nix-shell -i bash -p curl nix
3+
4+
fonts=($(grep -hPo "https://fonts\.gstatic\.com/s[^)]*" -r node_modules/ | sort | uniq))
5+
echo '{'
6+
for font in "${fonts[@]}"; do
7+
hash=$(nix-hash --to-sri --type sha256 $(nix-prefetch-url "$font" 2>/dev/null))
8+
if [ "$font" = "${fonts[-1]}" ]; then
9+
echo ' "'"$font"'"': '"'"$hash"'"'
10+
else
11+
echo ' "'"$font"'"': '"'"$hash"'",'
12+
fi
13+
done
14+
echo '}'

piped-frontend/fonts.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"https://fonts.gstatic.com/s/materialiconsround/v109/LDItaoyNOAY6Uewc665JcIzCKsKc_M9flwmM.otf": "sha256-utheVFS2KIEEzgOAbDcyO82PFF4wlOcnhgFzrIyRBi4=",
3+
"https://fonts.gstatic.com/s/roboto/v48/KFOMCnqEu92Fr1ME7kSn66aGLdTylUAMQXC89YmC2DPNWubEbVmUiA8.ttf": "sha256-Z6CSYilO1dB4PF8qsg2Wc2VMLePQOHBHN+v+noJu1hM="
4+
}

0 commit comments

Comments
 (0)