]> git.immae.eu Git - perso/Immae/Config/Nix.git/commitdiff
Improve assets generation
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 27 Aug 2022 17:03:22 +0000 (19:03 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 9 Apr 2023 15:16:27 +0000 (17:16 +0200)
modules/private/environment.nix
modules/private/websites/tools/assets/default.nix
modules/private/websites/tools/games/codenames/immae-assets.patch [new file with mode: 0644]
modules/private/websites/tools/games/codenames/node-packages.nix
modules/private/websites/tools/games/terraforming-mars/immae-assets.patch [new file with mode: 0644]
modules/private/websites/tools/games/terraforming-mars/terraforming-mars.nix
pkgs/webapps/apache-theme/theme/style.css

index ae00298039b2921ac74f97ab369fec84197a0255..1cc3b6cc315752657b603321337245786c7c0ef1 100644 (file)
@@ -908,6 +908,8 @@ in
             default = {};
             type = attrsOf (submodule {
               options = {
+                assetType = mkOption { type = enum ["tgz" "url" "googleFont"]; default = "url"; description = "Type of asset"; };
+                tgzRemoveComponents = mkOption { type = int; default = 0; description = "Remove components when extracting"; };
                 url = mkOption { type = str; description = "URL to fetch"; };
                 sha256 = mkOption { type = str; description = "Hash of the url"; };
               };
index 0eb476d6746071e5c6d5a5702e8296a513486338..1f5f1a4bd2d702af740fde3d9ac3c3f07641e194 100644 (file)
@@ -1,13 +1,47 @@
 { lib, pkgs, config, ... }:
 let
   cfg = config.myServices.websites.tools.assets;
-  assets_urls = lib.mapAttrs (k: v: pkgs.fetchurl v) config.myEnv.tools.assets;
+  fetchFont = v: pkgs.runCommand "fetch-font" {
+    outputHashAlgo = "sha256";
+    outputHashMode = "recursive";
+    outputHash = v.sha256;
+  } ''
+    mkdir -p $out
+    ${pkgs.curl}/bin/curl -k --output $out/font.css -H "User-Agent: Firefox/100.0" "${v.url}"
+    cat $out/font.css | grep -o "https://[^ )]*" | while read url; do
+      filename=$(echo "$url" | sed -e "s@.*/@@g")
+      ${pkgs.curl}/bin/curl -k --output "$out/$filename" "$url"
+      sed -i -e "s@$url@./$filename@" "$out/font.css"
+    done
+  '';
+  fetchTgz = v: pkgs.runCommand "fetch-tgz" {
+    outputHashAlgo = "sha256";
+    outputHashMode = "recursive";
+    outputHash = v.sha256;
+  } ''
+    mkdir -p $out
+    cd $out
+    ${pkgs.curl}/bin/curl -L -k "${v.url}" | tar -xz --strip-components=${builtins.toString v.tgzRemoveComponents}
+  '';
+  fetchAsset = v:
+    if v.assetType == "googleFont"
+    then fetchFont v
+    else if v.assetType == "tgz"
+    then fetchTgz v
+    else pkgs.fetchurl { url = v.url; sha256 = v.sha256; };
+  assets_urls = lib.mapAttrs (k: fetchAsset) config.myEnv.tools.assets;
   assets = pkgs.runCommand "assets" {} (''
     mkdir -p $out
     cp -a ${./static}/* $out/
   '' + builtins.concatStringsSep "\n"
     (lib.mapAttrsToList (k: v: ''
-      install -D -m644 -T ${v} $out/${k}
+      if [ -d "${v}" ]; then
+        mkdir -p "$out/$(dirname "${k}")"
+        cp -a "${v}" "$out/${k}"
+        chmod -R u+rwX "$out/${k}"
+      else
+        install -D -m644 -T "${v}" "$out/${k}"
+      fi
     '') assets_urls));
 in
 {
diff --git a/modules/private/websites/tools/games/codenames/immae-assets.patch b/modules/private/websites/tools/games/codenames/immae-assets.patch
new file mode 100644 (file)
index 0000000..c53ab97
--- /dev/null
@@ -0,0 +1,15 @@
+diff --git a/src/index.html b/src/index.html
+index 2559a37..84a0d64 100644
+--- a/src/index.html
++++ b/src/index.html
+@@ -6,8 +6,8 @@
+     <meta name="description" content="Online cooperative codenames game. Play with two or more players.">
+     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
+     <meta name="theme-color" content="#ffffff">
+-    <link href="https://fonts.googleapis.com/css?family=Exo+2&display=swap" rel="stylesheet">
+-    <link href="https://unpkg.com/ionicons@4.5.10-0/dist/css/ionicons.min.css" rel="stylesheet">
++    <link href="https://assets.immae.eu/fonts/Exo2_swap/font.css" rel="stylesheet">
++    <link href="https://assets.immae.eu/ionicons/4.5.10-0/dist/css/ionicons.min.css" rel="stylesheet">
+     <link rel="manifest" href="/codenames.manifest">
+       <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
+       <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
index 02595c5ece6601245fed70af7f104904fb0f7521..1fb2db3c079cdbbe2c8de21f70ff8d87f5d51dd7 100644 (file)
@@ -6448,6 +6448,7 @@ let
         chmod -R u+w $out
         cd $out
         patch -p1 < ${./codenames.patch}
+        patch -p1 < ${./immae-assets.patch}
       '';
     dependencies = [
       sources."@babel/code-frame-7.0.0"
diff --git a/modules/private/websites/tools/games/terraforming-mars/immae-assets.patch b/modules/private/websites/tools/games/terraforming-mars/immae-assets.patch
new file mode 100644 (file)
index 0000000..684a7df
--- /dev/null
@@ -0,0 +1,13 @@
+diff --git a/assets/index.html b/assets/index.html
+index 220192834..d77a0fc80 100644
+--- a/assets/index.html
++++ b/assets/index.html
+@@ -2,7 +2,7 @@
+ <html>
+     <head>
+         <link
+-            href="https://fonts.googleapis.com/css?family=Ubuntu&display=swap"
++            href="https://assets.immae.eu/fonts/Ubuntu_swap/font.css"
+             rel="stylesheet"
+         />
+         <link rel="stylesheet" type="text/css" media="all" href="/styles.css" />
index 502a553007a3abd7cb5f14fc9a05ca10e31a933d..b7c2c8c0d8463229d45287c29a3f5797ea3d1aba 100644 (file)
@@ -23,6 +23,7 @@ let
     cp -r ${source} ./source
     chmod -R u+w source
     cd source
+    patch -p1 < ${./immae-assets.patch}
     ln -s ${packages.package}/lib/node_modules/terraforming-mars/node_modules .
     npm run build
     mkdir $out
index 4d25c9fb7dc8515b4ab344a6286c980df842747b..e46139f471ab5fa1ecbe0e6a3660371ba52b67fe 100644 (file)
@@ -5,7 +5,7 @@
     Theme name: Apaxy
     Theme author: @adamwhitcroft
 \*------------------------------------*/
-@import url('//fonts.googleapis.com/css?family=Open+Sans');
+@import url('//assets.immae.eu/fonts/OpenSans/font.css');
 /* Have to use @import for the font, as you can only specify a single stylesheet */
 * {
        margin:0;