rewrite erase-home-darlings service in clojure

This commit is contained in:
Madeleine Sydney
2024-12-11 19:22:16 -07:00
parent 1e5f745767
commit 28b2aece65
2 changed files with 89 additions and 37 deletions

View File

@@ -129,8 +129,7 @@ builtins.mapAttrs mkHost (builtins.readDir ./hosts)
boot.initrd.systemd.initrdBin = with pkgs; [
zfs
coreutils
gnugrep
gawk
babashka
];
boot.initrd.systemd.services.erase-darlings = {
@@ -168,48 +167,17 @@ builtins.mapAttrs mkHost (builtins.readDir ./hosts)
after = [
"home.mount"
];
path = [ pkgs.zfs pkgs.coreutils pkgs.gnugrep pkgs.gawk pkgs.util-linux ];
path = [ pkgs.zfs pkgs.babashka ];
# unitConfig.DefaultDependencies = "no";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let script = ./erase-home-darlings.clj;
in ''${pkgs.babashka}/bin/bb "${script}" 3'';
};
stopIfChanged = false;
restartIfChanged = false;
script = /* bash */ ''
set -xe
set -o pipefail
command -v findmnt || echo "can't find findmnt on PATH"
command -v awk || echo "can't find awk on PATH"
# findmnt -no TARGET rpool/darlings/persist
PREVDIR=$(findmnt -no TARGET rpool/darlings/persist)/previous
# PREVDIR=/sysroot/persist/previous
echo "PREVDIR=$PREVDIR"
mkdir -p $PREVDIR/home
[ -e $PREVDIR/home/3 ] && rm -rf $PREVDIR/home/3
[ -e $PREVDIR/home/2 ] && mv $PREVDIR/home/2 $PREVDIR/home/3
[ -e $PREVDIR/home/1 ] && mv $PREVDIR/home/1 $PREVDIR/home/2
DIFFFILE="$(mktemp "erase-home-darlings-zfs-diff-XXXXXX")"
zfs diff -HF rpool/local/home@blank rpool/local/home \
> "$DIFFFILE"
awk '{if ($2 == "F" || $2 == "/") print $3}' < "$DIFFFILE" \
| while read -r line; do
DEST="$PREVDIR/home/1/$(dirname "$line")/$(basename "$line")"
mkdir -p "$(dirname "$DEST")"
mv "$line" "$DEST" && \
echo "$line -> $DEST"
done \
&& echo ">> previous home backed up to $PREVDIR/home/1 <<"
zfs rollback -r rpool/local/home@blank \
&& echo ">> home rolled back <<"
'';
};
# boot.loader.grub = {