1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.borgmatic;
7 settingsFormat = pkgs.formats.yaml { };
8
9 repository = with types; submodule {
10 options = {
11 path = mkOption {
12 type = str;
13 description = ''
14 Path to the repository
15 '';
16 };
17 label = mkOption {
18 type = str;
19 description = ''
20 Label to the repository
21 '';
22 };
23 };
24 };
25 cfgType = with types; submodule {
26 freeformType = settingsFormat.type;
27 options = {
28 source_directories = mkOption {
29 type = nullOr (listOf str);
30 default = null;
31 description = ''
32 List of source directories and files to backup. Globs and tildes are
33 expanded. Do not backslash spaces in path names.
34 '';
35 example = [ "/home" "/etc" "/var/log/syslog*" "/home/user/path with spaces" ];
36 };
37 repositories = mkOption {
38 type = nullOr (listOf repository);
39 default = null;
40 description = ''
41 A required list of local or remote repositories with paths and
42 optional labels (which can be used with the --repository flag to
43 select a repository). Tildes are expanded. Multiple repositories are
44 backed up to in sequence. Borg placeholders can be used. See the
45 output of "borg help placeholders" for details. See ssh_command for
46 SSH options like identity file or port. If systemd service is used,
47 then add local repository paths in the systemd service file to the
48 ReadWritePaths list.
49 '';
50 example = [
51 { path="ssh://user@backupserver/./sourcehostname.borg"; label="backupserver"; }
52 { path="/mnt/backup"; label="local"; }
53 ];
54 };
55 };
56 };
57
58 cfgfile = settingsFormat.generate "config.yaml" cfg.settings;
59in
60{
61 options.services.borgmatic = {
62 enable = mkEnableOption "borgmatic";
63
64 settings = mkOption {
65 description = ''
66 See https://torsion.org/borgmatic/docs/reference/configuration/
67 '';
68 default = null;
69 type = types.nullOr cfgType;
70 };
71
72 configurations = mkOption {
73 description = ''
74 Set of borgmatic configurations, see https://torsion.org/borgmatic/docs/reference/configuration/
75 '';
76 default = { };
77 type = types.attrsOf cfgType;
78 };
79 };
80
81 config = mkIf cfg.enable {
82
83 warnings = []
84 ++ optional (cfg.settings != null && cfg.settings ? location)
85 "`services.borgmatic.settings.location` is deprecated, please move your options out of sections to the global scope"
86 ++ optional (catAttrs "location" (attrValues cfg.configurations) != [])
87 "`services.borgmatic.configurations.<name>.location` is deprecated, please move your options out of sections to the global scope"
88 ;
89
90 environment.systemPackages = [ pkgs.borgmatic ];
91
92 environment.etc = (optionalAttrs (cfg.settings != null) { "borgmatic/config.yaml".source = cfgfile; }) //
93 mapAttrs'
94 (name: value: nameValuePair
95 "borgmatic.d/${name}.yaml"
96 { source = settingsFormat.generate "${name}.yaml" value; })
97 cfg.configurations;
98
99 systemd.packages = [ pkgs.borgmatic ];
100
101 # Workaround: https://github.com/NixOS/nixpkgs/issues/81138
102 systemd.timers.borgmatic.wantedBy = [ "timers.target" ];
103 };
104}