blob: ab7fb7de0f274b5e68de1dbe6aed8a0f406cbb80 (
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
{
pkgs,
lib,
config,
mkInstanceServices,
...
}:
let
# kopia policy json definition
compressionType = lib.types.enum [
"none"
"deflate-best-compression"
"deflate-best-speed"
"deflate-default"
"gzip"
"gzip-best-compression"
"gzip-best-speed"
"pgzip"
"pgzip-best-compression"
"pgzip-best-speed"
"s2-better"
"s2-default"
"s2-parallel-4"
"s2-parallel-8"
"zstd"
"zstd-better-compression"
"zstd-fastest"
];
policyListType = lib.mkOption {
type = lib.types.listOf policySubmodule;
default = [];
};
policyType = lib.mkOption {
type = lib.types.nullOr policySubmodule;
default = null;
};
policySubmodule = lib.types.submodule {
options = {
path = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path for which the policy applies";
};
retention = {
keepLatest = lib.mkOption {
type = lib.types.int;
default = 5;
description = "Number of latest snapshots to keep.";
};
keepHourly = lib.mkOption {
type = lib.types.int;
default = 48;
description = "Number of hourly snapshots to keep.";
};
keepDaily = lib.mkOption {
type = lib.types.int;
default = 7;
description = "Number of daily snapshots to keep.";
};
keepWeekly = lib.mkOption {
type = lib.types.int;
default = 4;
description = "Number of weekly snapshots to keep.";
};
keepMonthly = lib.mkOption {
type = lib.types.int;
default = 3;
description = "Number of monthly snapshots to keep.";
};
keepAnnual = lib.mkOption {
type = lib.types.int;
default = 0;
description = "Number of yearly snapshots to keep.";
};
};
files = {
ignoreDotFiles = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [
".gitignore"
".kopiaignore"
];
description = "List of files to source ignore lists from.";
};
noParentDotFiles = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = false;
description = "Do not use parent ignore dot files.";
};
ignoreCacheDirs = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = false;
description = "Ignore cache directories.";
};
maxFileSize = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Maximum file size to include in backup.";
};
oneFileSystem = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Stay in parent filesystem when finding files.";
};
};
errorHandling = {
ignoreFileErrors = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Ignore errors reading ignore files.";
};
ignoreDirectoryErrors = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Ignore errors reading directories.";
};
ignoreUnknownTypes = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Ignore unknown file types.";
};
};
compression = {
compressorName = lib.mkOption {
type = compressionType;
default = "none";
description = "Name of the compressor to use.";
};
onlyCompress = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "List of file extensions to compress.";
};
noParentOnlyCompress = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Do not use parent only compress list.";
};
neverCompress = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "List of file extensions to never compress.";
};
noParentNeverCompress = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Do not use parent never compress list.";
};
minSize = lib.mkOption {
type = lib.types.int;
default = 0;
description = "Minimum file size to compress.";
};
maxSize = lib.mkOption {
type = lib.types.int;
default = 0;
description = "Maximum file size to compress.";
};
};
metadataCompression = {
compressorName = lib.mkOption {
type = compressionType;
default = "zstd-fastest";
description = "Name of the compressor to use.";
};
};
splitter = {
algorithm = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Name of the splitter algorithm to use.";
};
};
# FIXME: add action definition afterward (maybe implement it during implement at zfs, btrfs snapshot)
osSnapshots = {
volumeShadowCopy = {
enable = lib.mkOption {
type = lib.types.nullOr (
lib.types.enum [
"never"
"always"
"when-available"
"inherit"
]
);
default = null;
description = "Enable volume shadow copy";
};
};
};
logging = {
directories = {
snapshotted = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Log detail when a directory is snapshotted";
};
ignored = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Log detail when a directory is ignored";
};
};
entries = {
snapshotted = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Log detail when an entry is snapshotted";
};
ignored = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Log detail when an entry is ignored";
};
};
};
upload = {
# maxParallelSnapshots - GUI only
maxParallelFileReads = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Maximum number of parallel file reads(GUI Only)";
};
parallelUploadAboveSize = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = "Use parallel uploads above size(GUI Only)";
};
};
};
};
instanceType = lib.types.submodule {
options = {
policy = policyListType;
};
};
# application logic
jsonFormat = (pkgs.formats.json { });
# generate policy name for policy file generation
mkPolicyName =
user: hostname: path:
"${user}@${hostname}${if path != null then ":${path}" else ""}";
mkPolicyFile = policy: (jsonFormat.generate "kopia-policy.json" policy);
mkInstancePolicyService =
name: instance:
let
policies = lib.lists.foldr (
policy: a:
a
// {
"${mkPolicyName instance.user config.networking.hostName policy.path}" = lib.attrsets.filterAttrs (
n: v: n != "path"
) policy;
}
) { } instance.policy;
policyFile = mkPolicyFile (
policies
// lib.optionalAttrs (config.services.kopia.globalPolicy != null) {
"(global)" = config.services.kopia.globalPolicy;
}
);
in
(lib.attrsets.nameValuePair "kopia-policy-${name}" {
description = "Kopia policy setup";
wants = [ "kopia-repository-${name}.service" ];
wantedBy = [ "kopia-snapshot-${name}.service" ];
after = [ "kopia-repository-${name}.service" ];
before = [ "kopia-snapshot-${name}.service" ];
restartTriggers = [ policyFile ];
script = ''
${pkgs.kopia}/bin/kopia policy import --from-file=${policyFile}
'';
serviceConfig = {
Type = "oneshot";
User = instance.user;
WorkingDirectory = "~";
SetLoginEnvironment = true;
};
});
in
{
options.services.kopia = {
globalPolicy = policyType;
instances = lib.mkOption {
type = lib.types.attrsOf instanceType;
};
};
config = lib.mkIf config.services.kopia.enable {
systemd.services = mkInstanceServices config.services.kopia.instances mkInstancePolicyService;
};
}
|