commit 832628ff443a8315009f8b13b54599dfcaf6b9b0
parent 9d5e27b539023f9e150f49fcbf94682b24e588fa
Author: Francesco Saccone <francesco@francescosaccone.com>
Date: Wed, 26 Mar 2025 13:22:32 +0100
refactor: put stagit script inside script.nix
Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>
Diffstat:
2 files changed, 85 insertions(+), 73 deletions(-)
diff --git a/hosts/server/default.nix b/hosts/server/default.nix
@@ -4,6 +4,9 @@
inputs,
...
}:
+let
+ scripts = import ./scripts.nix { inherit config pkgs inputs; };
+in
rec {
imports = [
./disk-config.nix
@@ -148,79 +151,7 @@ rec {
owner = "Francesco Saccone";
url = "git://${networking.domain}/${name}";
};
- hooks.postReceive =
- let
- destDir = "/var/tmp/stagit";
- cacheFile = "${destDir}/.htmlcache";
- reposDir = config.modules.git.directory;
- flags = builtins.concatStringsSep " " [
- "-c ${cacheFile}"
- "-u https://${networking.domain}/git/${name}/"
- ];
-
- script = pkgs.writeShellScriptBin "stagit" ''
- # Define is_force=1 if 'git push -f' was used
- null_ref="0000000000000000000000000000000000000000"
- is_force=0
- while read -r old new ref; do
- ${pkgs.sbase}/bin/test "$old" = $null_ref && continue
- ${pkgs.sbase}/bin/test "$new" = $null_ref && continue
-
- has_revs=$(${pkgs.git}/bin/git rev-list "$old" "^$new" | \
- ${pkgs.sbase}/bin/sed 1q)
-
- if ${pkgs.sbase}/bin/test -n "$has_revs"; then
- is_force=1
- break
- fi
- done
-
- # If is_force = 1, remove commits and cache file
- if ${pkgs.sbase}/bin/test $is_force = "1"; then
- ${pkgs.sbase}/bin/rm -f ${cacheFile}
- ${pkgs.sbase}/bin/rm -rf ${reposDir}/${name}/commit
- fi
-
- ${pkgs.sbase}/bin/mkdir -p ${destDir}/${name}
- cd ${destDir}/${name}
- ${pkgs.stagit}/bin/stagit ${flags} ${reposDir}/${name}
- ${pkgs.stagit}/bin/stagit-index ${reposDir}/*/ \
- > ${destDir}/index.html
-
- # Make the log.html file the index page
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/${name}/log.html \
- ${destDir}/${name}/index.html
-
- # Symlink favicon.png, logo.png and stagit.css from site
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/256.png \
- ${destDir}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/32.png \
- ${destDir}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/stagit.css \
- ${destDir}/style.css
-
- # Symlink favicon.png, logo.png and style.css in repos from
- # index
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/favicon.png \
- ${destDir}/${name}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/logo.png \
- ${destDir}/${name}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/style.css \
- ${destDir}/${name}/style.css
- '';
- in
- "${script}/bin/stagit";
+ hooks.postReceive = scripts.stagit { inherit name; };
}
);
daemon = {
diff --git a/hosts/server/scripts.nix b/hosts/server/scripts.nix
@@ -0,0 +1,81 @@
+{
+ config,
+ pkgs,
+ inputs,
+}:
+{
+ stagit =
+ { name }:
+ let
+ destDir = "/var/tmp/stagit";
+ cacheFile = "${destDir}/.htmlcache";
+ reposDir = config.modules.git.directory;
+ flags = builtins.concatStringsSep " " [
+ "-c ${cacheFile}"
+ "-u https://${config.networking.domain}/git/${name}/"
+ ];
+
+ script = pkgs.writeShellScriptBin "stagit" ''
+ # Define is_force=1 if 'git push -f' was used
+ null_ref="0000000000000000000000000000000000000000"
+ is_force=0
+ while read -r old new ref; do
+ ${pkgs.sbase}/bin/test "$old" = $null_ref && continue
+ ${pkgs.sbase}/bin/test "$new" = $null_ref && continue
+
+ has_revs=$(${pkgs.git}/bin/git rev-list "$old" "^$new" | \
+ ${pkgs.sbase}/bin/sed 1q)
+
+ if ${pkgs.sbase}/bin/test -n "$has_revs"; then
+ is_force=1
+ break
+ fi
+ done
+
+ # If is_force = 1, remove commits and cache file
+ if ${pkgs.sbase}/bin/test $is_force = "1"; then
+ ${pkgs.sbase}/bin/rm -f ${cacheFile}
+ ${pkgs.sbase}/bin/rm -rf ${reposDir}/${name}/commit
+ fi
+
+ ${pkgs.sbase}/bin/mkdir -p ${destDir}/${name}
+ cd ${destDir}/${name}
+ ${pkgs.stagit}/bin/stagit ${flags} ${reposDir}/${name}
+ ${pkgs.stagit}/bin/stagit-index ${reposDir}/*/ \
+ > ${destDir}/index.html
+
+ # Make the log.html file the index page
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/${name}/log.html \
+ ${destDir}/${name}/index.html
+
+ # Symlink favicon.png, logo.png and stagit.css from site
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/icon/256.png \
+ ${destDir}/favicon.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/icon/32.png \
+ ${destDir}/logo.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/stagit.css \
+ ${destDir}/style.css
+
+ # Symlink favicon.png, logo.png and style.css in repos from
+ # index
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/favicon.png \
+ ${destDir}/${name}/favicon.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/logo.png \
+ ${destDir}/${name}/logo.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/style.css \
+ ${destDir}/${name}/style.css
+ '';
+ in
+ "${script}/bin/stagit";
+}