2021-02-17 12:49:52 +08:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.services.borgmatic;
|
2022-05-03 00:31:14 +00:00
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
2023-02-05 14:48:46 +01:00
|
|
|
|
2024-08-30 00:46:39 +02:00
|
|
|
repository =
|
|
|
|
with lib.types;
|
|
|
|
submodule {
|
2023-08-07 00:15:57 +08:00
|
|
|
options = {
|
2024-08-30 00:46:39 +02:00
|
|
|
path = lib.mkOption {
|
2023-08-07 00:15:57 +08:00
|
|
|
type = str;
|
|
|
|
description = ''
|
|
|
|
Path to the repository
|
|
|
|
'';
|
|
|
|
};
|
2024-08-30 00:46:39 +02:00
|
|
|
label = lib.mkOption {
|
2023-08-07 00:15:57 +08:00
|
|
|
type = str;
|
|
|
|
description = ''
|
|
|
|
Label to the repository
|
|
|
|
'';
|
2024-12-10 20:26:33 +01:00
|
|
|
};
|
2023-08-07 00:15:57 +08:00
|
|
|
};
|
|
|
|
};
|
2024-08-30 00:46:39 +02:00
|
|
|
cfgType =
|
|
|
|
with lib.types;
|
|
|
|
submodule {
|
2023-02-05 14:48:46 +01:00
|
|
|
freeformType = settingsFormat.type;
|
2023-08-07 00:15:57 +08:00
|
|
|
options = {
|
2024-08-30 00:46:39 +02:00
|
|
|
source_directories = lib.mkOption {
|
2024-08-04 11:56:25 +08:00
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
2023-02-05 14:48:46 +01:00
|
|
|
description = ''
|
2023-08-07 00:15:57 +08:00
|
|
|
List of source directories and files to backup. Globs and tildes are
|
|
|
|
expanded. Do not backslash spaces in path names.
|
2023-02-05 14:48:46 +01:00
|
|
|
'';
|
2023-08-07 00:15:57 +08:00
|
|
|
example = [
|
|
|
|
"/home"
|
|
|
|
"/etc"
|
|
|
|
"/var/log/syslog*"
|
|
|
|
"/home/user/path with spaces"
|
|
|
|
];
|
2023-02-05 14:48:46 +01:00
|
|
|
};
|
2024-08-30 00:46:39 +02:00
|
|
|
repositories = lib.mkOption {
|
2024-08-04 11:56:25 +08:00
|
|
|
type = listOf repository;
|
|
|
|
default = [ ];
|
2023-02-05 14:48:46 +01:00
|
|
|
description = ''
|
2023-08-07 00:15:57 +08:00
|
|
|
A required list of local or remote repositories with paths and
|
|
|
|
optional labels (which can be used with the --repository flag to
|
|
|
|
select a repository). 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.
|
2023-02-05 14:48:46 +01:00
|
|
|
'';
|
|
|
|
example = [
|
2023-08-07 00:15:57 +08:00
|
|
|
{
|
|
|
|
path = "ssh://user@backupserver/./sourcehostname.borg";
|
|
|
|
label = "backupserver";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
path = "/mnt/backup";
|
|
|
|
label = "local";
|
|
|
|
}
|
2023-02-05 14:48:46 +01:00
|
|
|
];
|
2024-12-10 20:26:33 +01:00
|
|
|
};
|
2023-02-05 14:48:46 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-05-03 00:31:14 +00:00
|
|
|
cfgfile = settingsFormat.generate "config.yaml" cfg.settings;
|
2023-02-05 14:48:46 +01:00
|
|
|
in
|
|
|
|
{
|
2021-02-17 12:49:52 +08:00
|
|
|
options.services.borgmatic = {
|
2024-08-30 00:46:39 +02:00
|
|
|
enable = lib.mkEnableOption "borgmatic";
|
2021-02-17 12:49:52 +08:00
|
|
|
|
2024-08-30 00:46:39 +02:00
|
|
|
settings = lib.mkOption {
|
2023-02-05 14:48:46 +01:00
|
|
|
description = ''
|
2021-02-17 12:49:52 +08:00
|
|
|
See https://torsion.org/borgmatic/docs/reference/configuration/
|
|
|
|
'';
|
2023-02-05 14:48:46 +01:00
|
|
|
default = null;
|
2024-08-30 00:46:39 +02:00
|
|
|
type = lib.types.nullOr cfgType;
|
2023-02-05 14:48:46 +01:00
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:39 +02:00
|
|
|
configurations = lib.mkOption {
|
2023-02-05 14:48:46 +01:00
|
|
|
description = ''
|
|
|
|
Set of borgmatic configurations, see https://torsion.org/borgmatic/docs/reference/configuration/
|
|
|
|
'';
|
|
|
|
default = { };
|
2024-08-30 00:46:39 +02:00
|
|
|
type = lib.types.attrsOf cfgType;
|
2021-02-17 12:49:52 +08:00
|
|
|
};
|
2024-03-02 12:25:00 +01:00
|
|
|
|
2024-08-30 00:46:39 +02:00
|
|
|
enableConfigCheck = lib.mkEnableOption "checking all configurations during build time" // {
|
|
|
|
default = true;
|
|
|
|
};
|
2021-02-17 12:49:52 +08:00
|
|
|
};
|
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
configFiles =
|
2024-08-30 00:46:39 +02:00
|
|
|
(lib.optionalAttrs (cfg.settings != null) { "borgmatic/config.yaml".source = cfgfile; })
|
|
|
|
// lib.mapAttrs' (
|
|
|
|
name: value:
|
2024-03-02 12:25:00 +01:00
|
|
|
lib.nameValuePair "borgmatic.d/${name}.yaml" {
|
|
|
|
source = settingsFormat.generate "${name}.yaml" value;
|
|
|
|
}
|
|
|
|
) cfg.configurations;
|
|
|
|
borgmaticCheck =
|
|
|
|
name: f:
|
|
|
|
pkgs.runCommandCC "${name} validation" { } ''
|
|
|
|
${pkgs.borgmatic}/bin/borgmatic -c ${f.source} config validate
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
in
|
2024-08-30 00:46:39 +02:00
|
|
|
lib.mkIf cfg.enable {
|
2021-02-17 12:49:52 +08:00
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
warnings =
|
|
|
|
[ ]
|
2024-08-30 00:46:39 +02:00
|
|
|
++
|
|
|
|
lib.optional (cfg.settings != null && cfg.settings ? location)
|
2024-03-02 12:25:00 +01:00
|
|
|
"`services.borgmatic.settings.location` is deprecated, please move your options out of sections to the global scope"
|
2024-08-30 00:46:39 +02:00
|
|
|
++
|
|
|
|
lib.optional (lib.catAttrs "location" (lib.attrValues cfg.configurations) != [ ])
|
2024-03-02 12:25:00 +01:00
|
|
|
"`services.borgmatic.configurations.<name>.location` is deprecated, please move your options out of sections to the global scope";
|
2023-08-07 00:15:57 +08:00
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
environment.systemPackages = [ pkgs.borgmatic ];
|
2021-02-17 12:49:52 +08:00
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
environment.etc = configFiles;
|
2021-02-17 12:49:52 +08:00
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
systemd.packages = [ pkgs.borgmatic ];
|
2023-04-17 23:10:23 +08:00
|
|
|
|
2024-03-02 12:25:00 +01:00
|
|
|
# Workaround: https://github.com/NixOS/nixpkgs/issues/81138
|
|
|
|
systemd.timers.borgmatic.wantedBy = [ "timers.target" ];
|
|
|
|
|
2024-08-30 00:46:39 +02:00
|
|
|
system.checks = lib.mkIf cfg.enableConfigCheck (lib.mapAttrsToList borgmaticCheck configFiles);
|
2024-03-02 12:25:00 +01:00
|
|
|
};
|
2021-02-17 12:49:52 +08:00
|
|
|
}
|