Polish erase-home-darlings.clj

This commit is contained in:
Madeleine Sydney
2024-12-12 13:59:43 -07:00
parent 0350752a50
commit 684e78e936
6 changed files with 243 additions and 138 deletions

View File

@@ -26,6 +26,21 @@ in {
type = with types; listOf anything;
default = [];
};
rollbackTo = mkOption {
type = types.str;
};
archiveTo = mkOption {
type = types.str;
default = "/persist/previous/home";
};
dataset = mkOption {
type = types.str;
};
archiveLimit = mkOption {
type = types.ints.positive;
default = 3;
};
};
};
@@ -34,33 +49,58 @@ in {
zfs
];
boot.initrd.systemd.services.erase-darlings = {
description = "Rollback filesystem to a blank state on boot";
boot.initrd.systemd.services.erase-darlings =
let service = {
description = "Rollback filesystem to a blank state on boot";
wantedBy = [
"initrd.target"
];
after = [
# "zfs-import.service"
"zfs-import-rpool.service"
];
before = [
"sysroot.mount"
];
path = [ pkgs.zfs ];
unitConfig.DefaultDependencies = "no";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = /* bash */ ''
zfs rollback -r rpool/local/root@blank \
&& echo ">> >> rollback complete << <<"
'';
};
in if config.boot.initrd.systemd.enable
then service
else throw "sydnix.impermanence currently requires config.boot.initrd.systemd.enable'!";
systemd.services.erase-home-darlings = {
description = "Rollback home to a blank state on boot";
wantedBy = [
"initrd.target"
"multi-user.target"
];
after = [
# "zfs-import.service"
"zfs-import-rpool.service"
"home.mount"
];
before = [
"sysroot.mount"
];
path = [ pkgs.zfs ];
unitConfig.DefaultDependencies = "no";
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}" -n "${toString cfg.archiveLimit}" --dataset "${cfg.dataset}" --rollback-to "${cfg.rollbackTo}"'';
};
script = /* bash */ ''
zfs rollback -r rpool/local/root@blank \
&& echo ">> >> rollback complete << <<"
'';
stopIfChanged = false;
restartIfChanged = false;
};
environment.persistence."/persist" = {
directories = cfg.directories;
files = cfg.files;
directories = cfg.directories;
files = cfg.files;
};
};
}