1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-11 05:39:44 +01:00
home-manager/tests/modules/programs/borgmatic/basic-configuration.nix
Damien Cassou 27a92b54f3
borgmatic: improve support for version 1.8.0
The configuration file format of borgmatic has changed in version
1.8.0:
https://projects.torsion.org/borgmatic-collective/borgmatic/src/branch/main/NEWS

This commit makes Home Manager generate borgmatic's configuration file
using the new format.

Even though the NEWS file indicates that old configuration files are
compatible, this is not 100% the case: empty sections work fine in old
borgmatic but stop working in new ones. I've reported the bug upstream
by email as I couldn't create an account on the forge.
2024-04-10 21:26:26 +02:00

126 lines
3.6 KiB
Nix

{ config, pkgs, ... }:
let
boolToString = bool: if bool then "true" else "false";
backups = config.programs.borgmatic.backups;
in {
programs.borgmatic = {
enable = true;
backups = {
main = {
location = {
sourceDirectories = [ "/my-stuff-to-backup" ];
repositories = [ "/mnt/disk1" "/mnt/disk2" ];
extraConfig = {
one_file_system = true;
exclude_patterns = [ "*.swp" ];
};
};
storage = {
encryptionPasscommand = "fetch-the-password.sh";
extraConfig = { checkpoint_interval = 200; };
};
retention = {
keepWithin = "14d";
keepSecondly = 12;
extraConfig = { prefix = "hostname"; };
};
consistency = {
checks = [
{
name = "repository";
frequency = "2 weeks";
}
{
name = "archives";
frequency = "4 weeks";
}
];
extraConfig = { prefix = "hostname"; };
};
output = { extraConfig = { color = false; }; };
hooks = {
extraConfig = { before_actions = [ "echo Starting actions." ]; };
};
};
};
};
test.stubs.borgmatic = { };
nmt.script = ''
config_file=$TESTED/home-files/.config/borgmatic.d/main.yaml
assertFileExists $config_file
declare -A expectations
expectations[source_directories[0]]="${
builtins.elemAt backups.main.location.sourceDirectories 0
}"
expectations[repositories[0]]="${
builtins.elemAt backups.main.location.repositories 0
}"
expectations[repositories[1]]="${
builtins.elemAt backups.main.location.repositories 1
}"
expectations[one_file_system]="${
boolToString backups.main.location.extraConfig.one_file_system
}"
expectations[exclude_patterns[0]]="${
builtins.elemAt backups.main.location.extraConfig.exclude_patterns 0
}"
expectations[encryption_passcommand]="${backups.main.storage.encryptionPasscommand}"
expectations[checkpoint_interval]="${
toString backups.main.storage.extraConfig.checkpoint_interval
}"
expectations[keep_within]="${backups.main.retention.keepWithin}"
expectations[keep_secondly]="${
toString backups.main.retention.keepSecondly
}"
expectations[prefix]="${backups.main.retention.extraConfig.prefix}"
expectations[checks[0].name]="${
(builtins.elemAt backups.main.consistency.checks 0).name
}"
expectations[checks[0].frequency]="${
(builtins.elemAt backups.main.consistency.checks 0).frequency
}"
expectations[checks[1].name]="${
(builtins.elemAt backups.main.consistency.checks 1).name
}"
expectations[checks[1].frequency]="${
(builtins.elemAt backups.main.consistency.checks 1).frequency
}"
expectations[prefix]="${backups.main.consistency.extraConfig.prefix}"
expectations[color]="${boolToString backups.main.output.extraConfig.color}"
expectations[before_actions[0]]="${
builtins.elemAt backups.main.hooks.extraConfig.before_actions 0
}"
yq=${pkgs.yq-go}/bin/yq
for filter in "''${!expectations[@]}"; do
expected_value="''${expectations[$filter]}"
actual_value="$($yq ".$filter" $config_file)"
if [[ "$actual_value" != "$expected_value" ]]; then
fail "Expected '$filter' to be '$expected_value' but was '$actual_value'"
fi
done
one_file_system=$($yq ".one_file_system" $config_file)
if [[ $one_file_system != "true" ]]; then
fail "Expected one_file_system to be true but it was $one_file_system"
fi
'';
}