fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Philipp Kühnborgmatic: preparing upcoming borgmatic changea4218048

master
7.6 KiB260 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.borgmatic;
7
8  yamlFormat = pkgs.formats.yaml { };
9
10  mkNullableOption = args:
11    lib.mkOption (args // {
12      type = lib.types.nullOr args.type;
13      default = null;
14    });
15
16  cleanRepositories = repos:
17    map (repo:
18      if builtins.isString repo then {
19        path = repo;
20      } else
21        removeNullValues repo) repos;
22
23  mkRetentionOption = frequency:
24    mkNullableOption {
25      type = types.int;
26      description =
27        "Number of ${frequency} archives to keep. Use -1 for no limit.";
28      example = 3;
29    };
30
31  extraConfigOption = mkOption {
32    type = yamlFormat.type;
33    default = { };
34    description = "Extra settings.";
35  };
36
37  repositoryOption = types.submodule {
38    options = {
39      path = mkOption {
40        type = types.str;
41        example = "ssh://myuser@myrepo.myserver.com/./repo";
42        description = "Path of the repository.";
43      };
44
45      label = mkOption {
46        type = types.nullOr types.str;
47        default = null;
48        example = "remote";
49        description = ''
50          Short text describing the repository. Can be used with the
51          `--repository` flag to select a repository.
52        '';
53      };
54    };
55  };
56
57  consistencyCheckModule = types.submodule {
58    options = {
59      name = mkOption {
60        type = types.enum [ "repository" "archives" "data" "extract" ];
61        description = "Name of consistency check to run.";
62        example = "repository";
63      };
64
65      frequency = mkNullableOption {
66        type = types.strMatching "([[:digit:]]+ .*)|always";
67        description = "Frequency of this type of check";
68        example = "2 weeks";
69      };
70    };
71  };
72
73  configModule = types.submodule ({ config, ... }: {
74    config.location.extraConfig.exclude_from =
75      mkIf config.location.excludeHomeManagerSymlinks
76      (mkAfter [ (toString hmExcludeFile) ]);
77    options = {
78      location = {
79        sourceDirectories = mkOption {
80          type = types.listOf types.str;
81          description = "Directories to backup.";
82          example = literalExpression "[config.home.homeDirectory]";
83        };
84
85        repositories = mkOption {
86          type = types.listOf (types.either types.str repositoryOption);
87          apply = cleanRepositories;
88          example = literalExpression ''
89            [
90              {
91                "path" = "ssh://myuser@myrepo.myserver.com/./repo";
92                "label" = "server";
93              }
94              {
95                "path" = "/var/lib/backups/local.borg";
96                "label" = "local";
97              }
98            ]
99          '';
100          description = ''
101            List of local or remote repositories with paths and optional labels.
102          '';
103        };
104
105        excludeHomeManagerSymlinks = mkOption {
106          type = types.bool;
107          description = ''
108            Whether to exclude Home Manager generated symbolic links from
109            the backups. This facilitates restoring the whole home
110            directory when the Nix store doesn't contain the latest
111            Home Manager generation.
112          '';
113          default = false;
114          example = true;
115        };
116
117        extraConfig = extraConfigOption;
118      };
119
120      storage = {
121        encryptionPasscommand = mkNullableOption {
122          type = types.str;
123          description = "Command writing the passphrase to standard output.";
124          example =
125            literalExpression ''"''${pkgs.password-store}/bin/pass borg-repo"'';
126        };
127        extraConfig = extraConfigOption;
128      };
129
130      retention = {
131        keepWithin = mkNullableOption {
132          type = types.strMatching "[[:digit:]]+[Hdwmy]";
133          description = "Keep all archives within this time interval.";
134          example = "2d";
135        };
136
137        keepSecondly = mkRetentionOption "secondly";
138        keepMinutely = mkRetentionOption "minutely";
139        keepHourly = mkRetentionOption "hourly";
140        keepDaily = mkRetentionOption "daily";
141        keepWeekly = mkRetentionOption "weekly";
142        keepMonthly = mkRetentionOption "monthly";
143        keepYearly = mkRetentionOption "yearly";
144
145        extraConfig = extraConfigOption;
146      };
147
148      consistency = {
149        checks = mkOption {
150          type = types.listOf consistencyCheckModule;
151          default = [ ];
152          description = "Consistency checks to run";
153          example = literalExpression ''
154            [
155              {
156                name = "repository";
157                frequency = "2 weeks";
158              }
159              {
160                name = "archives";
161                frequency = "4 weeks";
162              }
163              {
164                name = "data";
165                frequency = "6 weeks";
166              }
167              {
168                name = "extract";
169                frequency = "6 weeks";
170              }
171            ];
172          '';
173        };
174
175        extraConfig = extraConfigOption;
176      };
177
178      output = { extraConfig = extraConfigOption; };
179
180      hooks = { extraConfig = extraConfigOption; };
181    };
182  });
183
184  removeNullValues = attrSet: filterAttrs (key: value: value != null) attrSet;
185
186  hmFiles = builtins.attrValues config.home.file;
187  hmSymlinks = (lib.filter (file: !file.recursive) hmFiles);
188  hmExcludePattern = file: ''
189    ${config.home.homeDirectory}/${file.target}
190  '';
191  hmExcludePatterns = lib.concatMapStrings hmExcludePattern hmSymlinks;
192  hmExcludeFile = pkgs.writeText "hm-symlinks.txt" hmExcludePatterns;
193
194  writeConfig = config:
195    generators.toYAML { } (removeNullValues ({
196      source_directories = config.location.sourceDirectories;
197      repositories = config.location.repositories;
198      encryption_passcommand = config.storage.encryptionPasscommand;
199      keep_within = config.retention.keepWithin;
200      keep_secondly = config.retention.keepSecondly;
201      keep_minutely = config.retention.keepMinutely;
202      keep_hourly = config.retention.keepHourly;
203      keep_daily = config.retention.keepDaily;
204      keep_weekly = config.retention.keepWeekly;
205      keep_monthly = config.retention.keepMonthly;
206      keep_yearly = config.retention.keepYearly;
207      checks = config.consistency.checks;
208    } // config.location.extraConfig // config.storage.extraConfig
209      // config.retention.extraConfig // config.consistency.extraConfig
210      // config.output.extraConfig // config.hooks.extraConfig));
211in {
212  meta.maintainers = [ maintainers.DamienCassou ];
213
214  options = {
215    programs.borgmatic = {
216      enable = mkEnableOption "Borgmatic";
217
218      package = mkPackageOption pkgs "borgmatic" { };
219
220      backups = mkOption {
221        type = types.attrsOf configModule;
222        description = ''
223          Borgmatic allows for several named backup configurations,
224          each with its own source directories and repositories.
225        '';
226        example = literalExpression ''
227          {
228            personal = {
229              location = {
230                sourceDirectories = [ "/home/me/personal" ];
231                repositories = [ "ssh://myuser@myserver.com/./personal-repo" ];
232              };
233            };
234            work = {
235              location = {
236                sourceDirectories = [ "/home/me/work" ];
237                repositories = [ "ssh://myuser@myserver.com/./work-repo" ];
238              };
239            };
240          };
241        '';
242      };
243    };
244  };
245
246  config = mkIf cfg.enable {
247    assertions = [
248      (lib.hm.assertions.assertPlatform "programs.borgmatic" pkgs
249        lib.platforms.linux)
250    ];
251
252    xdg.configFile = with lib.attrsets;
253      mapAttrs' (configName: config:
254        nameValuePair ("borgmatic.d/" + configName + ".yaml") {
255          text = writeConfig config;
256        }) cfg.backups;
257
258    home.packages = [ cfg.package ];
259  };
260}