about summary refs log tree commit diff
path: root/nixos/modules/services/backup
diff options
context:
space:
mode:
authorajs124 <git@ajs124.de>2023-08-24 01:31:58 +0200
committerajs124 <git@ajs124.de>2023-08-28 15:17:37 +0200
commitdbb69f82c6d3ef080f42f478fc366445539df757 (patch)
tree7f981332e674d9049a6e506ab77bd014629f3aee /nixos/modules/services/backup
parent4732cbf3f88f451760345e9cdb54babbb543e22e (diff)
downloadnixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar.gz
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar.bz2
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar.lz
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar.xz
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.tar.zst
nixlib-dbb69f82c6d3ef080f42f478fc366445539df757.zip
nixos/restic: add wrapper scripts that set parameters for backup
and use in test
Diffstat (limited to 'nixos/modules/services/backup')
-rw-r--r--nixos/modules/services/backup/restic.nix30
1 files changed, 29 insertions, 1 deletions
diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix
index 60afb15ee879..70be32aa8c38 100644
--- a/nixos/modules/services/backup/restic.nix
+++ b/nixos/modules/services/backup/restic.nix
@@ -260,6 +260,16 @@ in
             Restic package to use.
           '';
         };
+
+        createWrapper = lib.mkOption {
+          type = lib.types.bool;
+          default = true;
+          description = ''
+            Whether to generate and add a script to the system path, that has the same environment variables set
+            as the systemd service. This can be used to e.g. mount snapshots or perform other opterations, without
+            having to manually specify most options.
+          '';
+        };
       };
     }));
     default = { };
@@ -316,7 +326,8 @@ in
           in
           nameValuePair "restic-backups-${name}" ({
             environment = {
-              RESTIC_CACHE_DIR = "%C/restic-backups-${name}";
+              # not %C, because that wouldn't work in the wrapper script
+              RESTIC_CACHE_DIR = "/var/cache/restic-backups-${name}";
               RESTIC_PASSWORD_FILE = backup.passwordFile;
               RESTIC_REPOSITORY = backup.repository;
               RESTIC_REPOSITORY_FILE = backup.repositoryFile;
@@ -376,5 +387,22 @@ in
           timerConfig = backup.timerConfig;
         })
         config.services.restic.backups;
+
+    # generate wrapper scripts, as described in the createWrapper option
+    environment.systemPackages = lib.mapAttrsToList (name: backup: let
+      extraOptions = lib.concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
+      resticCmd = "${backup.package}/bin/restic${extraOptions}";
+    in pkgs.writeShellScriptBin "restic-${name}" ''
+      set -a  # automatically export variables
+      ${lib.optionalString (backup.environmentFile != null) "source ${backup.environmentFile}"}
+      # set same environment variables as the systemd service
+      ${lib.pipe config.systemd.services."restic-backups-${name}".environment [
+        (lib.filterAttrs (_: v: v != null))
+        (lib.mapAttrsToList (n: v: "${n}=${v}"))
+        (lib.concatStringsSep "\n")
+      ]}
+
+      exec ${resticCmd} $@
+    '') (lib.filterAttrs (_: v: v.createWrapper) config.services.restic.backups);
   };
 }