Fully fixup rolling datasets

Signed-off-by: magic_rb <magic_rb@redalder.org>
This commit is contained in:
magic_rb 2023-11-03 20:10:48 +01:00
parent 729e68b80e
commit 99005cd768
No known key found for this signature in database
GPG key ID: 08D5287CC5DDCA0E
2 changed files with 27 additions and 47 deletions

View file

@ -4,7 +4,7 @@ source @out@/share/functions/get_old_datasets
export "PATH=$PATH:@runtimePath@" export "PATH=$PATH:@runtimePath@"
if [[ "$#" != 3 ]] ; then if [[ "$#" != 2 ]] ; then
echo "This script requires precisely 2 arguments!" echo "This script requires precisely 2 arguments!"
exit 1 exit 1
fi fi
@ -21,7 +21,7 @@ zfs rollback "$dataset@blank" && echo "rollback complete"
while IFS="\n" read gen while IFS="\n" read gen
do do
printf "$gen < $(("$generation" - "$to_keep")) => " printf -- "$gen < $(("$generation" - "$to_keep")) => "
if [ "$gen" -lt "$(("$generation" - "$to_keep"))" ]; then if [ "$gen" -lt "$(("$generation" - "$to_keep"))" ]; then
zfs destroy -r "$dataset/$gen" zfs destroy -r "$dataset/$gen"
echo "destroyed" echo "destroyed"

View file

@ -1,50 +1,27 @@
{ inputs, ... }: { inputs, lib, ... }:
let
inherit (lib)
filterAttrs
mapAttrsToList
concatStringsSep
;
in
{ {
flake.overlays.rolling_datasets = flake.overlays.rolling_datasets =
final: prev: final: prev:
let let
writeTextFile = writeShellScript = name: text:
{ name # the name of the derivation prev.writeTextFile {
, text inherit name;
, executable ? false # run chmod +x ? executable = true;
, destination ? "" # relative path appended to $out eg "/bin/foo" text = ''
, checkPhase ? "" # syntax checks, e.g. for scripts #!/bin/sh
, env ? {} ${text}
}:
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}
''; '';
checkPhase = '' checkPhase = ''
${final.stdenv.shell} -n $out/bin/${name} ${prev.stdenv.shellDryRun} "$target"
''; '';
}; };
in in
{ {
rolling_datasets = rolling_datasets =
@ -56,11 +33,14 @@
export runtimeShell=${prev.runtimeShell} \ export runtimeShell=${prev.runtimeShell} \
runtimePath=${prev.lib.makeBinPath (with prev; [ utillinux zfs ])} runtimePath=${prev.lib.makeBinPath (with prev; [ utillinux zfs ])}
for binary in ${./bin}/* ; do ${concatStringsSep "\n" (mapAttrsToList (name: _: ''
output_binary="$out/bin/$(basename "$binary")" {
binary=${writeShellScript name (builtins.readFile (./. + "/bin/${name}"))}
output_binary="$out/bin/${name}"
substituteAll "$binary" "$output_binary" substituteAll "$binary" "$output_binary"
chmod +x "$output_binary" chmod +x "$output_binary"
done }
'') (filterAttrs (_: v: v == "regular") (builtins.readDir ./bin)))}
for function in ${./functions}/* ; do for function in ${./functions}/* ; do
substituteAll "$function" "$out/share/functions/$(basename "$function")" substituteAll "$function" "$out/share/functions/$(basename "$function")"