diff options
Diffstat (limited to 'hosts/vidhar/zfs.nix')
-rw-r--r-- | hosts/vidhar/zfs.nix | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/hosts/vidhar/zfs.nix b/hosts/vidhar/zfs.nix new file mode 100644 index 00000000..3beef836 --- /dev/null +++ b/hosts/vidhar/zfs.nix | |||
@@ -0,0 +1,108 @@ | |||
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 UTC"; Persistent = true; }; | ||
13 | hourly = { OnCalendar = "hourly UTC"; Persistent = true; }; | ||
14 | daily = { OnCalendar = "daily UTC"; Persistent = true; }; | ||
15 | monthly = { OnCalendar = "monthly UTC"; Persistent = true; }; | ||
16 | yearly = { OnCalendar = "yearly UTC"; 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 | "/boot" = | ||
33 | { device = "boot"; | ||
34 | fsType = "zfs"; | ||
35 | }; | ||
36 | |||
37 | "/nix" = | ||
38 | { device = "ssd-raid0/local/nix"; | ||
39 | fsType = "zfs"; | ||
40 | }; | ||
41 | |||
42 | "/root" = | ||
43 | { device = "ssd-raid1/safe/home-root"; | ||
44 | fsType = "zfs"; | ||
45 | neededForBoot = true; | ||
46 | }; | ||
47 | |||
48 | "/var/lib/systemd" = | ||
49 | { device = "ssd-raid1/local/var-lib-systemd"; | ||
50 | fsType = "zfs"; | ||
51 | neededForBoot = true; | ||
52 | }; | ||
53 | |||
54 | "/var/lib/nixos" = | ||
55 | { device = "ssd-raid1/local/var-lib-nixos"; | ||
56 | fsType = "zfs"; | ||
57 | neededForBoot = true; | ||
58 | }; | ||
59 | |||
60 | "/var/log" = | ||
61 | { device = "ssd-raid1/local/var-log"; | ||
62 | fsType = "zfs"; | ||
63 | }; | ||
64 | |||
65 | "/home" = | ||
66 | { device = "hdd-raid6/safe/home"; | ||
67 | fsType = "zfs"; | ||
68 | }; | ||
69 | }; | ||
70 | |||
71 | systemd.services = | ||
72 | let mkSnapService = snapName: { | ||
73 | name = "zfs-snapshot-${snapName}"; | ||
74 | value = { | ||
75 | description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; | ||
76 | after = [ "zfs-import.target" ]; | ||
77 | serviceConfig = { | ||
78 | Type = "oneshot"; | ||
79 | ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; | ||
80 | }; | ||
81 | restartIfChanged = false; | ||
82 | |||
83 | preStart = '' | ||
84 | ${zfs}/bin/zfs set com.sun:auto-snapshot=true hdd-raid6/safe | ||
85 | ${zfs}/bin/zfs set com.sun:auto-snapshot=true ssd-raid1/safe | ||
86 | ${zfs}/bin/zfs set com.sun:auto-snapshot=true boot | ||
87 | ''; | ||
88 | }; | ||
89 | }; | ||
90 | in builtins.listToAttrs (map mkSnapService snapshotNames); | ||
91 | |||
92 | systemd.timers = | ||
93 | let mkSnapTimer = snapName: { | ||
94 | name = "zfs-snapshot-${snapName}"; | ||
95 | value = { | ||
96 | wantedBy = [ "timers.target" ]; | ||
97 | timerConfig = snapshotTimerConfig.${snapName}; | ||
98 | }; | ||
99 | }; | ||
100 | in builtins.listToAttrs (map mkSnapTimer snapshotNames); | ||
101 | |||
102 | services.zfs.trim.enable = false; | ||
103 | services.zfs.autoScrub = { | ||
104 | enable = true; | ||
105 | interval = "Sun *-*-1..7 04:00:00"; | ||
106 | }; | ||
107 | }; | ||
108 | } | ||