2025-03-10 18:42:32 +03:00
|
|
|
{ config, lib, ... }:
|
|
|
|
let
|
2025-03-12 14:28:40 +03:00
|
|
|
inherit (lib)
|
|
|
|
concatStringsSep
|
|
|
|
mkIf
|
|
|
|
mkEnableOption
|
|
|
|
mkOption
|
|
|
|
mkBefore
|
|
|
|
;
|
|
|
|
inherit (lib.types)
|
|
|
|
bool
|
|
|
|
str
|
|
|
|
listOf
|
|
|
|
;
|
2025-03-10 18:42:32 +03:00
|
|
|
cfg = config.ataraxia.filesystems.zfs;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.ataraxia.filesystems.zfs = {
|
|
|
|
enable = mkEnableOption "Root on zfs";
|
2025-03-12 15:41:05 +03:00
|
|
|
mountpoints = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
A list of absolute paths to ZFS dataset mountpoints.
|
|
|
|
These paths will be automatically filtered out from the directories persisted through
|
|
|
|
persist module to prevent conflicts with ZFS's native mount management. Any matching entries
|
|
|
|
in the persistence list will be removed.
|
|
|
|
'';
|
|
|
|
};
|
2025-03-12 14:28:40 +03:00
|
|
|
# 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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2025-03-10 18:42:32 +03:00
|
|
|
};
|
|
|
|
|
2025-03-12 14:28:40 +03:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-06-07 17:35:07 +03:00
|
|
|
boot.kernelParams = [
|
|
|
|
"zfs.metaslab_lba_weighting_enabled=0"
|
|
|
|
];
|
|
|
|
|
2025-03-12 15:41:19 +03:00
|
|
|
services.zfs = {
|
|
|
|
autoScrub.enable = true;
|
|
|
|
autoScrub.interval = "monthly";
|
|
|
|
trim.enable = true;
|
|
|
|
trim.interval = "weekly";
|
|
|
|
};
|
|
|
|
|
2025-03-12 14:28:40 +03:00
|
|
|
persist.state.files = [
|
|
|
|
"/etc/zfs/zpool.cache"
|
|
|
|
];
|
|
|
|
};
|
2025-03-10 18:42:32 +03:00
|
|
|
}
|