1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.borgmatic;
7 settingsFormat = pkgs.formats.yaml { };
8
9 cfgType = with types; submodule {
10 freeformType = settingsFormat.type;
11 options.location = {
12 source_directories = mkOption {
13 type = listOf str;
14 description = mdDoc ''
15 List of source directories to backup (required). Globs and
16 tildes are expanded.
17 '';
18 example = [ "/home" "/etc" "/var/log/syslog*" ];
19 };
20 repositories = mkOption {
21 type = listOf str;
22 description = mdDoc ''
23 Paths to local or remote repositories (required). Tildes are
24 expanded. Multiple repositories are backed up to in
25 sequence. Borg placeholders can be used. See the output of
26 "borg help placeholders" for details. See ssh_command for
27 SSH options like identity file or port. If systemd service
28 is used, then add local repository paths in the systemd
29 service file to the ReadWritePaths list.
30 '';
31 example = [
32 "ssh://user@backupserver/./sourcehostname.borg"
33 "ssh://user@backupserver/./{fqdn}"
34 "/var/local/backups/local.borg"
35 ];
36 };
37 };
38 };
39
40 cfgfile = settingsFormat.generate "config.yaml" cfg.settings;
41in
42{
43 options.services.borgmatic = {
44 enable = mkEnableOption (mdDoc "borgmatic");
45
46 settings = mkOption {
47 description = mdDoc ''
48 See https://torsion.org/borgmatic/docs/reference/configuration/
49 '';
50 default = null;
51 type = types.nullOr cfgType;
52 };
53
54 configurations = mkOption {
55 description = mdDoc ''
56 Set of borgmatic configurations, see https://torsion.org/borgmatic/docs/reference/configuration/
57 '';
58 default = { };
59 type = types.attrsOf cfgType;
60 };
61 };
62
63 config = mkIf cfg.enable {
64
65 environment.systemPackages = [ pkgs.borgmatic ];
66
67 environment.etc = (optionalAttrs (cfg.settings != null) { "borgmatic/config.yaml".source = cfgfile; }) //
68 mapAttrs'
69 (name: value: nameValuePair
70 "borgmatic.d/${name}.yaml"
71 { source = settingsFormat.generate "${name}.yaml" value; })
72 cfg.configurations;
73
74 systemd.packages = [ pkgs.borgmatic ];
75
76 # Workaround: https://github.com/NixOS/nixpkgs/issues/81138
77 systemd.timers.borgmatic.wantedBy = [ "timers.target" ];
78 };
79}