fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Philipp Kühnborgmatic: preparing upcoming borgmatic changea4218048

master
4.0 KiB139 linesraw
1{ config, pkgs, ... }:
2
3let
4
5  boolToString = bool: if bool then "true" else "false";
6  backups = config.programs.borgmatic.backups;
7
8in {
9  programs.borgmatic = {
10    enable = true;
11    backups = {
12      main = {
13        location = {
14          sourceDirectories = [ "/my-stuff-to-backup" ];
15          repositories = [
16            "/mnt/disk1"
17            { path = "/mnt/disk2"; }
18            {
19              path = "/mnt/disk3";
20              label = "disk3";
21            }
22          ];
23          extraConfig = {
24            one_file_system = true;
25            exclude_patterns = [ "*.swp" ];
26          };
27        };
28
29        storage = {
30          encryptionPasscommand = "fetch-the-password.sh";
31          extraConfig = { checkpoint_interval = 200; };
32        };
33
34        retention = {
35          keepWithin = "14d";
36          keepSecondly = 12;
37          extraConfig = { prefix = "hostname"; };
38        };
39
40        consistency = {
41          checks = [
42            {
43              name = "repository";
44              frequency = "2 weeks";
45            }
46            {
47              name = "archives";
48              frequency = "4 weeks";
49            }
50          ];
51
52          extraConfig = { prefix = "hostname"; };
53        };
54
55        output = { extraConfig = { color = false; }; };
56
57        hooks = {
58          extraConfig = { before_actions = [ "echo Starting actions." ]; };
59        };
60      };
61    };
62  };
63
64  test.stubs.borgmatic = { };
65
66  nmt.script = ''
67    config_file=$TESTED/home-files/.config/borgmatic.d/main.yaml
68    assertFileExists $config_file
69
70    declare -A expectations
71
72    expectations[source_directories[0]]="${
73      builtins.elemAt backups.main.location.sourceDirectories 0
74    }"
75    expectations[repositories[0].path]="${
76      (builtins.elemAt backups.main.location.repositories 0).path
77    }"
78    expectations[repositories[1].path]="${
79      (builtins.elemAt backups.main.location.repositories 1).path
80    }"
81    expectations[repositories[2].path]="${
82      (builtins.elemAt backups.main.location.repositories 2).path
83    }"
84    expectations[repositories[2].label]="${
85      (builtins.elemAt backups.main.location.repositories 2).label
86    }"
87    expectations[one_file_system]="${
88      boolToString backups.main.location.extraConfig.one_file_system
89    }"
90    expectations[exclude_patterns[0]]="${
91      builtins.elemAt backups.main.location.extraConfig.exclude_patterns 0
92    }"
93
94    expectations[encryption_passcommand]="${backups.main.storage.encryptionPasscommand}"
95    expectations[checkpoint_interval]="${
96      toString backups.main.storage.extraConfig.checkpoint_interval
97    }"
98
99    expectations[keep_within]="${backups.main.retention.keepWithin}"
100    expectations[keep_secondly]="${
101      toString backups.main.retention.keepSecondly
102    }"
103    expectations[prefix]="${backups.main.retention.extraConfig.prefix}"
104
105    expectations[checks[0].name]="${
106      (builtins.elemAt backups.main.consistency.checks 0).name
107    }"
108    expectations[checks[0].frequency]="${
109      (builtins.elemAt backups.main.consistency.checks 0).frequency
110    }"
111    expectations[checks[1].name]="${
112      (builtins.elemAt backups.main.consistency.checks 1).name
113    }"
114    expectations[checks[1].frequency]="${
115      (builtins.elemAt backups.main.consistency.checks 1).frequency
116    }"
117    expectations[prefix]="${backups.main.consistency.extraConfig.prefix}"
118    expectations[color]="${boolToString backups.main.output.extraConfig.color}"
119    expectations[before_actions[0]]="${
120      builtins.elemAt backups.main.hooks.extraConfig.before_actions 0
121    }"
122
123    yq=${pkgs.yq-go}/bin/yq
124
125    for filter in "''${!expectations[@]}"; do
126      expected_value="''${expectations[$filter]}"
127      actual_value="$($yq ".$filter" $config_file)"
128
129      if [[ "$actual_value" != "$expected_value" ]]; then
130        fail "Expected '$filter' to be '$expected_value' but was '$actual_value'"
131      fi
132    done
133
134    one_file_system=$($yq ".one_file_system" $config_file)
135    if [[ $one_file_system != "true" ]]; then
136       fail "Expected one_file_system to be true but it was $one_file_system"
137    fi
138  '';
139}