-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
Expand file tree
/
Copy pathborgmatic.nix
More file actions
199 lines (185 loc) · 6.12 KB
/
borgmatic.nix
File metadata and controls
199 lines (185 loc) · 6.12 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.borgmatic;
settingsFormat = pkgs.formats.yaml { };
postgresql = config.services.postgresql.package;
mysql = config.services.mysql.package;
requireSudo =
s:
s ? postgresql_databases
&& lib.any (d: d ? username && !(d ? password) && !(d ? pg_dump_command)) s.postgresql_databases;
addRequiredBinaries =
s:
s
// (lib.optionalAttrs (s ? postgresql_databases && s.postgresql_databases != [ ]) {
postgresql_databases = map (
d:
let
as_user = if d ? username && !(d ? password) then "${pkgs.sudo}/bin/sudo -u ${d.username} " else "";
in
{
pg_dump_command =
if d.name == "all" && (!(d ? format) || isNull d.format) then
"${as_user}${postgresql}/bin/pg_dumpall"
else
"${as_user}${postgresql}/bin/pg_dump";
pg_restore_command = "${as_user}${postgresql}/bin/pg_restore";
psql_command = "${as_user}${postgresql}/bin/psql";
}
// d
) s.postgresql_databases;
})
// (lib.optionalAttrs (s ? mariadb_databases && s.mariadb_databases != [ ]) {
mariadb_databases = map (
d:
{
mariadb_dump_command = "${mysql}/bin/mariadb-dump";
mariadb_command = "${mysql}/bin/mariadb";
}
// d
) s.mariadb_databases;
})
// (lib.optionalAttrs (s ? mysql_databases && s.mysql_databases != [ ]) {
mysql_databases = map (
d:
{
mysql_dump_command = "${mysql}/bin/mysqldump";
mysql_command = "${mysql}/bin/mysql";
}
// d
) s.mysql_databases;
});
repository =
with lib.types;
submodule {
options = {
path = lib.mkOption {
type = str;
description = ''
Path to the repository
'';
};
label = lib.mkOption {
type = str;
description = ''
Label to the repository
'';
};
};
};
cfgType =
with lib.types;
submodule {
freeformType = settingsFormat.type;
options = {
source_directories = lib.mkOption {
type = listOf str;
default = [ ];
description = ''
List of source directories and files to backup. Globs and tildes are
expanded. Do not backslash spaces in path names.
'';
example = [
"/home"
"/etc"
"/var/log/syslog*"
"/home/user/path with spaces"
];
};
repositories = lib.mkOption {
type = listOf repository;
default = [ ];
description = ''
A required list of local or remote repositories with paths and
optional labels (which can be used with the --repository flag to
select a repository). Tildes are expanded. Multiple repositories are
backed up to in sequence. Borg placeholders can be used. See the
output of "borg help placeholders" for details. See ssh_command for
SSH options like identity file or port. If systemd service is used,
then add local repository paths in the systemd service file to the
ReadWritePaths list.
'';
example = [
{
path = "ssh://user@backupserver/./sourcehostname.borg";
label = "backupserver";
}
{
path = "/mnt/backup";
label = "local";
}
];
};
};
};
cfgfile = settingsFormat.generate "config.yaml" (addRequiredBinaries cfg.settings);
anycfgRequiresSudo =
requireSudo cfg.settings || lib.any requireSudo (lib.attrValues cfg.configurations);
in
{
options.services.borgmatic = {
enable = lib.mkEnableOption "borgmatic";
settings = lib.mkOption {
description = ''
See <https://torsion.org/borgmatic/docs/reference/configuration/>
'';
default = null;
type = lib.types.nullOr cfgType;
};
configurations = lib.mkOption {
description = ''
Set of borgmatic configurations, see <https://torsion.org/borgmatic/docs/reference/configuration/>
'';
default = { };
type = lib.types.attrsOf cfgType;
};
enableConfigCheck = lib.mkEnableOption "checking all configurations during build time" // {
default = true;
};
};
config =
let
configFiles =
(lib.optionalAttrs (cfg.settings != null) {
"borgmatic/config.yaml".source = cfgfile;
})
// lib.mapAttrs' (
name: value:
lib.nameValuePair "borgmatic.d/${name}.yaml" {
source = settingsFormat.generate "${name}.yaml" (addRequiredBinaries value);
}
) cfg.configurations;
borgmaticCheck =
name: f:
pkgs.runCommandCC "${name} validation" { } ''
${pkgs.borgmatic}/bin/borgmatic -c ${f.source} config validate
touch $out
'';
in
lib.mkIf cfg.enable {
warnings =
[ ]
++
lib.optional (cfg.settings != null && cfg.settings ? location)
"`services.borgmatic.settings.location` is deprecated, please move your options out of sections to the global scope"
++
lib.optional (lib.catAttrs "location" (lib.attrValues cfg.configurations) != [ ])
"`services.borgmatic.configurations.<name>.location` is deprecated, please move your options out of sections to the global scope";
environment.systemPackages = [ pkgs.borgmatic ];
environment.etc = configFiles;
systemd.packages = [ pkgs.borgmatic ];
systemd.services.borgmatic.path = [ pkgs.coreutils ];
systemd.services.borgmatic.serviceConfig = lib.optionalAttrs anycfgRequiresSudo {
NoNewPrivileges = false;
CapabilityBoundingSet = "CAP_DAC_READ_SEARCH CAP_NET_RAW CAP_SETUID CAP_SETGID";
};
# Workaround: https://github.com/NixOS/nixpkgs/issues/81138
systemd.timers.borgmatic.wantedBy = [ "timers.target" ];
system.checks = lib.mkIf cfg.enableConfigCheck (lib.mapAttrsToList borgmaticCheck configFiles);
};
}