feat: implement rollback on boot for zfs
This commit is contained in:
parent
6af773f6e1
commit
07b12c6171
@ -1,16 +1,66 @@
|
|||||||
{ config, lib, ... }:
|
{ config, lib, ... }:
|
||||||
let
|
let
|
||||||
inherit (lib) mkIf mkEnableOption;
|
inherit (lib)
|
||||||
|
concatStringsSep
|
||||||
|
mkIf
|
||||||
|
mkEnableOption
|
||||||
|
mkOption
|
||||||
|
mkBefore
|
||||||
|
;
|
||||||
|
inherit (lib.types)
|
||||||
|
bool
|
||||||
|
str
|
||||||
|
listOf
|
||||||
|
;
|
||||||
cfg = config.ataraxia.filesystems.zfs;
|
cfg = config.ataraxia.filesystems.zfs;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.ataraxia.filesystems.zfs = {
|
options.ataraxia.filesystems.zfs = {
|
||||||
enable = mkEnableOption "Root on zfs";
|
enable = mkEnableOption "Root on zfs";
|
||||||
|
# Zfs clean root
|
||||||
|
eraseOnBoot = {
|
||||||
|
enable = mkOption {
|
||||||
|
type = bool;
|
||||||
|
default = config.persist.enable;
|
||||||
|
description = "Rollback zfs datasets on boot";
|
||||||
|
};
|
||||||
|
snapshots = mkOption {
|
||||||
|
type = listOf str;
|
||||||
|
default = [ ];
|
||||||
|
example = [ "rpool/nixos/root@empty" ];
|
||||||
|
description = ''
|
||||||
|
A list of dataset snapshots to rollback on boot.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config =
|
||||||
persist.state.files = [
|
let
|
||||||
"/etc/zfs/zpool.cache"
|
script = concatStringsSep "\n" (
|
||||||
];
|
map (x: ''
|
||||||
};
|
${config.boot.zfs.package}/bin/zfs rollback -r ${x} && echo ">>> rollback ${x} <<<"
|
||||||
|
'') cfg.eraseOnBoot.snapshots
|
||||||
|
);
|
||||||
|
in
|
||||||
|
mkIf cfg.enable {
|
||||||
|
boot.initrd = mkIf cfg.eraseOnBoot.enable {
|
||||||
|
postDeviceCommands = mkIf (!config.boot.initrd.systemd.enable) (mkBefore script);
|
||||||
|
|
||||||
|
systemd.services.rollback = mkIf config.boot.initrd.systemd.enable {
|
||||||
|
description = "Rollback zfs datasets to a pristine state on boot";
|
||||||
|
wantedBy = [ "initrd.target" ];
|
||||||
|
requires = [ "zfs-import-rpool.service" ];
|
||||||
|
after = [ "zfs-import-rpool.service" ];
|
||||||
|
before = [ "sysroot.mount" ];
|
||||||
|
unitConfig.DefaultDependencies = "no";
|
||||||
|
serviceConfig.Type = "oneshot";
|
||||||
|
script = script;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
persist.state.files = [
|
||||||
|
"/etc/zfs/zpool.cache"
|
||||||
|
];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user