summaryrefslogtreecommitdiff
path: root/hosts/surtr/zfs.nix
blob: 3cbd0cf0c53bb982213722371b66d084e074b115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{ 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 = {
      "/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/lib/nixos" =
        { device = "surtr/local/var-lib-nixos";
          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);

    services.zfs.trim.enable = false;
    services.zfs.autoScrub = {
      enable = true;
      interval = "Sun *-*-1..7 04:00:00";
    };
  };
}