summaryrefslogtreecommitdiff
path: root/modules/zfssnap/default.nix
blob: 23041c36a38d3ae0048e4c1e047632f42d886199 (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
102
103
104
105
106
107
108
109
110
111
112
113
{ config, pkgs, lib, flakeInputs, ... }:

with lib;

let
  zfssnap = flakeInputs.mach-nix.lib.${config.nixpkgs.system}.buildPythonPackage rec {
    pname = "zfssnap";
    src = ./zfssnap;
    version = "0.0.0";
    ignoreDataOutdated = true;

    requirements = ''
      pyxdg
      pytimeparse
      python-dateutil
    '';
    postInstall = ''
      wrapProgram $out/bin/zfssnap \
        --prefix PATH : ${makeBinPath [config.boot.zfs.package]}
    '';
  };

  cfg = config.services.zfssnap;
in {
  options = {
    services.zfssnap = {
      enable = mkEnableOption "zfssnap service";

      config = mkOption {
        type = types.submodule {
          options = {
            keep = mkOption {
              type = with types; attrsOf str;
              default = {
                within = "15m";
                "5m" = "48";
                "15m" = "32";
                hourly = "48";
                "4h" = "24";
                "12h" = "12";
                daily = "62";
                halfweekly = "32";
                weekly = "24";
                monthly = "-1";
              };
            };
            exec = mkOption {
              type = with types; attrsOf str;
              default = {};
            };
          };
        };
      };

      snapInterval = mkOption {
        type = types.str;
        default = "*-*-* *:00/5:00";
      };

      verbosity = mkOption {
        type = types.int;
        default = 2;
      };

      extraPruneArgs = mkOption {
        type = with types; listOf str;
        default = [];
      };
      extraAutosnapArgs = mkOption {
        type = with types; listOf str;
        default = [];
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services."zfssnap" = {
      description = "Create automatic ZFS snapshots";
      after = [ "zfs-import.target" ];
      wants = [ "zfssnap-prune.service" ];
      before = [ "zfssnap-prune.service" ];
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${zfssnap}/bin/zfssnap --verbosity=${toString cfg.verbosity} autosnap ${escapeShellArgs cfg.extraAutosnapArgs}";

        LogRateLimitIntervalSec = 0;
      };
    };
    systemd.services."zfssnap-prune" = {
      description = "Prune automatic ZFS snapshots";
      after = [ "zfs-import.target" "zfssnap.service" ];
      serviceConfig = {
        Type = "oneshot";
        ExecStart = let
          mkSectionName = name: strings.escape [ "[" "]" ] (strings.toUpper name);
          zfssnapConfig = generators.toINI { inherit mkSectionName; } cfg.config;
        in "${zfssnap}/bin/zfssnap --verbosity=${toString cfg.verbosity} prune --config=${pkgs.writeText "zfssnap.ini" zfssnapConfig} ${escapeShellArgs cfg.extraPruneArgs}";

        LogRateLimitIntervalSec = 0;
      };
    };

    systemd.timers."zfssnap" = {
      wantedBy = ["timers.target"];
      timerConfig = {
        OnCalendar = cfg.snapInterval;
        Persistent = true;
      };
    };

    environment.systemPackages = [zfssnap];
  };
}