diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2021-05-29 15:18:04 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2021-05-29 15:18:04 +0200 |
| commit | 333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a (patch) | |
| tree | 82f9c823b587c0c423192eeed026f172f52eb431 /hosts/surtr/zfs.nix | |
| parent | 28e1cd3729bd39e34c5c0aabda3b5d53d4877133 (diff) | |
| download | nixos-333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a.tar nixos-333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a.tar.gz nixos-333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a.tar.bz2 nixos-333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a.tar.xz nixos-333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a.zip | |
surtr: zfs snapshots
Diffstat (limited to 'hosts/surtr/zfs.nix')
| -rw-r--r-- | hosts/surtr/zfs.nix | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/hosts/surtr/zfs.nix b/hosts/surtr/zfs.nix new file mode 100644 index 00000000..72cc79e3 --- /dev/null +++ b/hosts/surtr/zfs.nix | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | { pkgs, config, ... }: | ||
| 2 | let | ||
| 3 | snapshotNames = ["frequent" "hourly" "daily" "monthly" "yearly"]; | ||
| 4 | snapshotCount = { | ||
| 5 | frequent = 24; | ||
| 6 | hourly = 24; | ||
| 7 | daily = 30; | ||
| 8 | monthly = 12; | ||
| 9 | yearly = 5; | ||
| 10 | }; | ||
| 11 | snapshotTimerConfig = { | ||
| 12 | frequent = { OnCalendar = "*:0/5"; Persistent = true; }; | ||
| 13 | hourly = { OnCalendar = "hourly"; Persistent = true; }; | ||
| 14 | daily = { OnCalendar = "daily"; Persistent = true; }; | ||
| 15 | monthly = { OnCalendar = "monthly"; Persistent = true; }; | ||
| 16 | yearly = { OnCalendar = "yearly"; Persistent = true; }; | ||
| 17 | }; | ||
| 18 | snapshotDescr = { | ||
| 19 | frequent = "few minutes"; | ||
| 20 | hourly = "hour"; | ||
| 21 | daily = "day"; | ||
| 22 | monthly = "month"; | ||
| 23 | yearly = "year"; | ||
| 24 | }; | ||
| 25 | |||
| 26 | zfs = config.boot.zfs.package; | ||
| 27 | |||
| 28 | autosnapPackage = pkgs.zfstools.override { inherit zfs; }; | ||
| 29 | in { | ||
| 30 | config = { | ||
| 31 | fileSystems = { | ||
| 32 | "/nix" = | ||
| 33 | { device = "surtr/local/nix"; | ||
| 34 | fsType = "zfs"; | ||
| 35 | }; | ||
| 36 | |||
| 37 | "/root" = | ||
| 38 | { device = "surtr/safe/home-root"; | ||
| 39 | fsType = "zfs"; | ||
| 40 | neededForBoot = true; | ||
| 41 | }; | ||
| 42 | |||
| 43 | "/var/lib/systemd" = | ||
| 44 | { device = "surtr/local/var-lib-systemd"; | ||
| 45 | fsType = "zfs"; | ||
| 46 | neededForBoot = true; | ||
| 47 | }; | ||
| 48 | |||
| 49 | "/var/log" = | ||
| 50 | { device = "surtr/local/var-log"; | ||
| 51 | fsType = "zfs"; | ||
| 52 | }; | ||
| 53 | |||
| 54 | "/home" = | ||
| 55 | { device = "surtr/safe/home"; | ||
| 56 | fsType = "zfs"; | ||
| 57 | }; | ||
| 58 | }; | ||
| 59 | |||
| 60 | systemd.services = | ||
| 61 | let mkSnapService = snapName: { | ||
| 62 | name = "zfs-snapshot-${snapName}"; | ||
| 63 | value = { | ||
| 64 | description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; | ||
| 65 | after = [ "zfs-import.target" ]; | ||
| 66 | serviceConfig = { | ||
| 67 | Type = "oneshot"; | ||
| 68 | ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; | ||
| 69 | }; | ||
| 70 | restartIfChanged = false; | ||
| 71 | |||
| 72 | preStart = '' | ||
| 73 | ${zfs}/bin/zfs set com.sun:auto-snapshot=true surtr/safe | ||
| 74 | ''; | ||
| 75 | }; | ||
| 76 | }; | ||
| 77 | in builtins.listToAttrs (map mkSnapService snapshotNames); | ||
| 78 | |||
| 79 | systemd.timers = | ||
| 80 | let mkSnapTimer = snapName: { | ||
| 81 | name = "zfs-snapshot-${snapName}"; | ||
| 82 | value = { | ||
| 83 | wantedBy = [ "timers.target" ]; | ||
| 84 | timerConfig = snapshotTimerConfig.${snapName}; | ||
| 85 | }; | ||
| 86 | }; | ||
| 87 | in builtins.listToAttrs (map mkSnapTimer snapshotNames); | ||
| 88 | }; | ||
| 89 | } | ||
