From a71502aff04a5b04f9e0d3aeceb61eab8eab2ed5 Mon Sep 17 00:00:00 2001 From: Finn Landweber Date: Sat, 2 Mar 2024 12:24:17 +0100 Subject: [PATCH 1/2] nixos/borgmatic: added test --- nixos/tests/all-tests.nix | 1 + nixos/tests/borgmatic.nix | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 nixos/tests/borgmatic.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 8193c3dfe840..39e95609ddfe 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -154,6 +154,7 @@ in { bootspec = handleTestOn ["x86_64-linux"] ./bootspec.nix {}; boot-stage1 = handleTest ./boot-stage1.nix {}; borgbackup = handleTest ./borgbackup.nix {}; + borgmatic = handleTest ./borgmatic.nix {}; botamusique = handleTest ./botamusique.nix {}; bpf = handleTestOn ["x86_64-linux" "aarch64-linux"] ./bpf.nix {}; bpftune = handleTest ./bpftune.nix {}; diff --git a/nixos/tests/borgmatic.nix b/nixos/tests/borgmatic.nix new file mode 100644 index 000000000000..70ad43e8bd35 --- /dev/null +++ b/nixos/tests/borgmatic.nix @@ -0,0 +1,24 @@ +import ./make-test-python.nix ({ pkgs, ... }: +{ + name = "borgmatic"; + nodes.machine = { ... }: { + services.borgmatic = { + enable = true; + settings = { + source_directories = [ "/home" ]; + repositories = [ + { + label = "local"; + path = "/var/backup"; + } + ]; + keep_daily = 7; + }; + }; + }; + + testScript = '' + machine.succeed("borgmatic rcreate -e none") + machine.succeed("borgmatic") + ''; +}) From 9d94b98e46f8cf5f8abdcdc3cf0a271071922bc8 Mon Sep 17 00:00:00 2001 From: Finn Landweber Date: Sat, 2 Mar 2024 12:25:00 +0100 Subject: [PATCH 2/2] nixos/borgmatic: refactor added configuration check at built time added borgmatic.enableConfigCheck whether or not it'll be executed. --- nixos/modules/services/backup/borgmatic.nix | 49 +++++++++++++-------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/nixos/modules/services/backup/borgmatic.nix b/nixos/modules/services/backup/borgmatic.nix index b27dd2817120..9cc0085c4c8a 100644 --- a/nixos/modules/services/backup/borgmatic.nix +++ b/nixos/modules/services/backup/borgmatic.nix @@ -76,29 +76,42 @@ in default = { }; type = types.attrsOf cfgType; }; + + enableConfigCheck = mkEnableOption (lib.mdDoc "checking all configurations during build time") // { default = true; }; }; - config = mkIf cfg.enable { + config = + let + configFiles = + (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; + borgmaticCheck = name: f: pkgs.runCommandCC "${name} validation" { } '' + ${pkgs.borgmatic}/bin/borgmatic -c ${f.source} config validate + touch $out + ''; + in + mkIf cfg.enable { - warnings = [] - ++ optional (cfg.settings != null && cfg.settings ? location) - "`services.borgmatic.settings.location` is deprecated, please move your options out of sections to the global scope" - ++ optional (catAttrs "location" (attrValues cfg.configurations) != []) - "`services.borgmatic.configurations..location` is deprecated, please move your options out of sections to the global scope" - ; + warnings = [] + ++ optional (cfg.settings != null && cfg.settings ? location) + "`services.borgmatic.settings.location` is deprecated, please move your options out of sections to the global scope" + ++ optional (catAttrs "location" (attrValues cfg.configurations) != []) + "`services.borgmatic.configurations..location` is deprecated, please move your options out of sections to the global scope" + ; - environment.systemPackages = [ pkgs.borgmatic ]; + 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; + environment.etc = configFiles; - systemd.packages = [ pkgs.borgmatic ]; + systemd.packages = [ pkgs.borgmatic ]; - # Workaround: https://github.com/NixOS/nixpkgs/issues/81138 - systemd.timers.borgmatic.wantedBy = [ "timers.target" ]; - }; + # Workaround: https://github.com/NixOS/nixpkgs/issues/81138 + systemd.timers.borgmatic.wantedBy = [ "timers.target" ]; + + system.checks = mkIf cfg.enableConfigCheck (mapAttrsToList borgmaticCheck configFiles); + }; }