diff --git a/overlays/rolling_datasets/bin/roll_dataset b/overlays/rolling_datasets/bin/roll_dataset index 2ba7cfc..ff79d01 100644 --- a/overlays/rolling_datasets/bin/roll_dataset +++ b/overlays/rolling_datasets/bin/roll_dataset @@ -4,7 +4,7 @@ source @out@/share/functions/get_old_datasets export "PATH=$PATH:@runtimePath@" -if [[ "$#" != 3 ]] ; then +if [[ "$#" != 2 ]] ; then echo "This script requires precisely 2 arguments!" exit 1 fi @@ -21,7 +21,7 @@ zfs rollback "$dataset@blank" && echo "rollback complete" while IFS="\n" read gen do - printf "$gen < $(("$generation" - "$to_keep")) => " + printf -- "$gen < $(("$generation" - "$to_keep")) => " if [ "$gen" -lt "$(("$generation" - "$to_keep"))" ]; then zfs destroy -r "$dataset/$gen" echo "destroyed" diff --git a/overlays/rolling_datasets/default.nix b/overlays/rolling_datasets/default.nix index aded02a..34c07ad 100644 --- a/overlays/rolling_datasets/default.nix +++ b/overlays/rolling_datasets/default.nix @@ -1,50 +1,27 @@ -{ inputs, ... }: +{ inputs, lib, ... }: +let + inherit (lib) + filterAttrs + mapAttrsToList + concatStringsSep + ; +in { flake.overlays.rolling_datasets = final: prev: let - writeTextFile = - { name # the name of the derivation - , text - , executable ? false # run chmod +x ? - , destination ? "" # relative path appended to $out eg "/bin/foo" - , checkPhase ? "" # syntax checks, e.g. for scripts - , env ? {} - }: - final.runCommand name - ({ inherit text executable; - passAsFile = [ "text" ]; - # Pointless to do this on a remote machine. - preferLocalBuild = true; - allowSubstitutes = false; - } // env) - '' - n=$out${destination} - mkdir -p "$(dirname "$n")" - - if [ -e "$textPath" ]; then - mv "$textPath" "$n" - else - echo -n "$text" > "$n" - fi - - ${checkPhase} - - (test -n "$executable" && chmod +x "$n") || true - ''; - writeShellScriptBin = name : text : env : - writeTextFile { - inherit name; - executable = true; - destination = "/bin/${name}"; - text = '' - #!${final.runtimeShell} - ${text} + writeShellScript = name: text: + prev.writeTextFile { + inherit name; + executable = true; + text = '' + #!/bin/sh + ${text} ''; - checkPhase = '' - ${final.stdenv.shell} -n $out/bin/${name} - ''; - }; + checkPhase = '' + ${prev.stdenv.shellDryRun} "$target" + ''; + }; in { rolling_datasets = @@ -56,11 +33,14 @@ export runtimeShell=${prev.runtimeShell} \ runtimePath=${prev.lib.makeBinPath (with prev; [ utillinux zfs ])} - for binary in ${./bin}/* ; do - output_binary="$out/bin/$(basename "$binary")" + ${concatStringsSep "\n" (mapAttrsToList (name: _: '' + { + binary=${writeShellScript name (builtins.readFile (./. + "/bin/${name}"))} + output_binary="$out/bin/${name}" substituteAll "$binary" "$output_binary" chmod +x "$output_binary" - done + } + '') (filterAttrs (_: v: v == "regular") (builtins.readDir ./bin)))} for function in ${./functions}/* ; do substituteAll "$function" "$out/share/functions/$(basename "$function")"