{ 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 UTC"; Persistent = true; }; hourly = { OnCalendar = "hourly UTC"; Persistent = true; }; daily = { OnCalendar = "daily UTC"; Persistent = true; }; monthly = { OnCalendar = "monthly UTC"; Persistent = true; }; yearly = { OnCalendar = "yearly UTC"; 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 = { "/boot" = { device = "boot"; fsType = "zfs"; }; "/nix" = { device = "ssd-raid0/local/nix"; fsType = "zfs"; }; "/root" = { device = "ssd-raid1/safe/home-root"; fsType = "zfs"; neededForBoot = true; }; "/var/lib/systemd" = { device = "ssd-raid1/local/var-lib-systemd"; fsType = "zfs"; neededForBoot = true; }; "/var/lib/nixos" = { device = "ssd-raid1/local/var-lib-nixos"; fsType = "zfs"; neededForBoot = true; }; "/var/lib/unbound" = { device = "ssd-raid1/local/var-lib-unbound"; fsType = "zfs"; }; "/var/lib/dhcp" = { device = "ssd-raid1/local/var-lib-dhcp"; fsType = "zfs"; }; "/var/lib/chrony" = { device = "ssd-raid1/local/var-lib-chrony"; fsType = "zfs"; }; "/var/lib/samba" = { device = "ssd-raid1/local/var-lib-samba"; fsType = "zfs"; }; "/var/lib/prometheus2" = { device = "ssd-raid1/local/var-lib-prometheus2"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/var/lib/grafana" = { device = "ssd-raid1/local/var-lib-grafana"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/var/lib/loki" = { device = "ssd-raid1/local/var-lib-loki"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/srv/tftp" = { device = "ssd-raid1/local/srv-tftp"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/var/log" = { device = "ssd-raid1/local/var-log"; fsType = "zfs"; }; "/home" = { device = "hdd-raid6/safe/home"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/home/gkleen" = { device = "hdd-raid6/safe/home/gkleen"; fsType = "zfs"; options = [ "zfsutil" ]; }; "/home/mherold" = { device = "hdd-raid6/safe/home/mherold"; fsType = "zfs"; options = [ "zfsutil" ]; }; }; 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 hdd-raid6/safe ${zfs}/bin/zfs set com.sun:auto-snapshot=false hdd-raid6/safe/home/mherold/eos/base ${zfs}/bin/zfs set com.sun:auto-snapshot=true ssd-raid1/safe ${zfs}/bin/zfs set com.sun:auto-snapshot=true boot ''; }; }; 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); services.zfs.trim.enable = false; services.zfs.autoScrub = { enable = true; interval = "Sun *-*-1..7 04:00:00"; }; services.zfs.zed.settings = { ZED_SYSLOG_SUBCLASS_EXCLUDE = "history_event"; }; }; }