summary refs log tree commit diff
path: root/nixos/modules/services/backup/borgmatic.nix
blob: 5ee036e68c7bc6aba21d500d583f96d8dca2171f (plain) (blame)
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.borgmatic;
  settingsFormat = pkgs.formats.yaml { };

  cfgType = with types; submodule {
    freeformType = settingsFormat.type;
    options.location = {
      source_directories = mkOption {
        type = listOf str;
        description = mdDoc ''
          List of source directories to backup (required). Globs and
          tildes are expanded.
        '';
        example = [ "/home" "/etc" "/var/log/syslog*" ];
      };
      repositories = mkOption {
        type = listOf str;
        description = mdDoc ''
          Paths to local or remote repositories (required). 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 = [
          "ssh://user@backupserver/./sourcehostname.borg"
          "ssh://user@backupserver/./{fqdn}"
          "/var/local/backups/local.borg"
        ];
      };
    };
  };

  cfgfile = settingsFormat.generate "config.yaml" cfg.settings;
in
{
  options.services.borgmatic = {
    enable = mkEnableOption (mdDoc "borgmatic");

    settings = mkOption {
      description = mdDoc ''
        See https://torsion.org/borgmatic/docs/reference/configuration/
      '';
      default = null;
      type = types.nullOr cfgType;
    };

    configurations = mkOption {
      description = mdDoc ''
        Set of borgmatic configurations, see https://torsion.org/borgmatic/docs/reference/configuration/
      '';
      default = { };
      type = types.attrsOf cfgType;
    };
  };

  config = mkIf cfg.enable {

    environment.systemPackages = [ pkgs.borgmatic ];

    environment.etc = (optionalAttrs (cfg.settings != null) { "borgmatic/config.yaml".source = cfgfile; }) //
      mapAttrs'
        (name: value: nameValuePair
          "borgmatic.d/${name}.yaml"
          { source = settingsFormat.generate "${name}.yaml" value; })
        cfg.configurations;

    systemd.packages = [ pkgs.borgmatic ];

    # Workaround: https://github.com/NixOS/nixpkgs/issues/81138
    systemd.timers.borgmatic.wantedBy = [ "timers.target" ];
  };
}