{ pkgs, config, ... }: let snapshotNames = ["frequent" "hourly" "daily" "monthly" "yearly"]; snapshotCount = { frequent = 24; hourly = 24; daily = 30; monthly = 12; yearly = 5; }; snapshotTimerConfig = { frequent = { OnCalendar = "*:0/5"; Persistent = true; }; hourly = { OnCalendar = "hourly"; Persistent = true; }; daily = { OnCalendar = "daily"; Persistent = true; }; monthly = { OnCalendar = "monthly"; Persistent = true; }; yearly = { OnCalendar = "yearly"; Persistent = true; }; }; snapshotDescr = { frequent = "few minutes"; hourly = "hour"; daily = "day"; monthly = "month"; yearly = "year"; }; zfs = config.boot.zfs.package; autosnapPackage = pkgs.zfstools.override { inherit zfs; }; in { config = { fileSystems = { "/nix" = { device = "surtr/local/nix"; fsType = "zfs"; }; "/root" = { device = "surtr/safe/home-root"; fsType = "zfs"; neededForBoot = true; }; "/var/lib/systemd" = { device = "surtr/local/var-lib-systemd"; fsType = "zfs"; neededForBoot = true; }; "/var/log" = { device = "surtr/local/var-log"; fsType = "zfs"; }; "/home" = { device = "surtr/safe/home"; fsType = "zfs"; }; }; systemd.services = let mkSnapService = snapName: { name = "zfs-snapshot-${snapName}"; value = { description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; after = [ "zfs-import.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; }; restartIfChanged = false; preStart = '' ${zfs}/bin/zfs set com.sun:auto-snapshot=true surtr/safe ''; }; }; in builtins.listToAttrs (map mkSnapService snapshotNames); systemd.timers = let mkSnapTimer = snapName: { name = "zfs-snapshot-${snapName}"; value = { wantedBy = [ "timers.target" ]; timerConfig = snapshotTimerConfig.${snapName}; }; }; in builtins.listToAttrs (map mkSnapTimer snapshotNames); }; }