summaryrefslogtreecommitdiff
path: root/modules/borgsnap/default.nix
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2022-11-01 22:43:25 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2022-11-01 22:43:25 +0100
commit7448d3431fcfc05f9b7991e337b02083300a99db (patch)
tree0664ebc0f666ef404881ac7287198ff44c0ba9e0 /modules/borgsnap/default.nix
parent234eab10a84e7ffb8bbc32f5e2f94495d3c9aedd (diff)
downloadnixos-7448d3431fcfc05f9b7991e337b02083300a99db.tar
nixos-7448d3431fcfc05f9b7991e337b02083300a99db.tar.gz
nixos-7448d3431fcfc05f9b7991e337b02083300a99db.tar.bz2
nixos-7448d3431fcfc05f9b7991e337b02083300a99db.tar.xz
nixos-7448d3431fcfc05f9b7991e337b02083300a99db.zip
...
Diffstat (limited to 'modules/borgsnap/default.nix')
-rw-r--r--modules/borgsnap/default.nix106
1 files changed, 106 insertions, 0 deletions
diff --git a/modules/borgsnap/default.nix b/modules/borgsnap/default.nix
new file mode 100644
index 00000000..f4c0eec4
--- /dev/null
+++ b/modules/borgsnap/default.nix
@@ -0,0 +1,106 @@
1{ config, pkgs, lib, flakeInputs, hostName, ... }:
2
3with lib;
4
5let
6 borgsnap = flakeInputs.mach-nix.lib.${config.nixpkgs.system}.buildPythonPackage rec {
7 pname = "borgsnap";
8 src = ./borgsnap;
9 version = "0.0.0";
10 ignoreDataOutdated = true;
11
12 requirements = ''
13 atomicwrites
14 pyprctl
15 python-unshare
16 python-dateutil
17 '';
18 postInstall = ''
19 wrapProgram $out/bin/borgsnap \
20 --prefix PATH : ${makeBinPath (with pkgs; [config.boot.zfs.package util-linux borgbackup])}:${config.security.wrapperDir}
21 '';
22
23 providers.python-unshare = "nixpkgs";
24 overridesPre = [
25 (self: super: { python-unshare = super.python-unshare.overrideAttrs (oldAttrs: { name = "python-unshare-0.2.1"; version = "0.2.1"; }); })
26 ];
27
28 _.tomli.buildInputs.add = with pkgs."python3Packages"; [ flit-core ];
29 };
30
31 cfg = config.services.borgsnap;
32in {
33 options = {
34 services.borgsnap = {
35 enable = mkEnableOption "borgsnap service";
36
37 target = mkOption {
38 type = types.str;
39 };
40
41 archive-prefix = mkOption {
42 type = types.str;
43 default = "yggdrasil.${hostName}.";
44 };
45
46 extraConfig = mkOption {
47 type = with types; attrsOf str;
48 default = {
49 halfweekly = "8";
50 monthly = "-1";
51 };
52 };
53
54 verbosity = mkOption {
55 type = types.int;
56 default = config.services.zfssnap.verbosity;
57 };
58
59 sshConfig = mkOption {
60 type = with types; nullOr str;
61 default = null;
62 };
63
64 keyfile = mkOption {
65 type = with types; nullOr str;
66 default = null;
67 };
68
69 extraCreateArgs = mkOption {
70 type = with types; listOf str;
71 default = [];
72 };
73 extraCheckArgs = mkOption {
74 type = with types; listOf str;
75 default = [];
76 };
77 };
78 };
79
80 config = mkIf cfg.enable {
81 warnings = mkIf (!config.services.zfssnap.enable) [
82 "borgsnap will do nothing if zfssnap is not enabled"
83 ];
84
85 services.zfssnap.config.exec = {
86 check = "${borgsnap}/bin/borgsnap --verbosity=${toString cfg.verbosity} --target ${escapeShellArg cfg.target} --archive-prefix ${escapeShellArg cfg.archive-prefix} check --cache-file /run/zfssnap-prune/archives-cache.json ${escapeShellArgs cfg.extraCheckArgs}";
87 cmd = "${borgsnap}/bin/borgsnap --verbosity=${toString cfg.verbosity} --target ${escapeShellArg cfg.target} --archive-prefix ${escapeShellArg cfg.archive-prefix} create ${escapeShellArgs cfg.extraCreateArgs}";
88 } // cfg.extraConfig;
89
90 systemd.services."zfssnap-prune" = {
91 serviceConfig = {
92 Environment = [
93 "BORG_BASE_DIR=/var/lib/borg"
94 "BORG_CONFIG_DIR=/var/lib/borg/config"
95 "BORG_CACHE_DIR=/var/lib/borg/cache"
96 "BORG_SECURITY_DIR=/var/lib/borg/security"
97 "BORG_KEYS_DIR=/var/lib/borg/keys"
98 "BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes"
99 "BORG_HOSTNAME_IS_UNIQUE=yes"
100 ] ++ optional (!(isNull cfg.sshConfig)) "BORG_RSH=\"${pkgs.openssh}/bin/ssh -F ${pkgs.writeText "config" cfg.sshConfig}\""
101 ++ optional (!(isNull cfg.keyfile)) "BORG_KEY_FILE=${cfg.keyfile}";
102 RuntimeDirectory = "zfssnap-prune";
103 };
104 };
105 };
106}