nixos/restic: add option to inhibit going to sleep

This commit is contained in:
Max Hausch 2024-03-17 17:05:59 +01:00
parent e86fd8c2ee
commit a8038698d3
No known key found for this signature in database
3 changed files with 33 additions and 1 deletions

View File

@ -211,6 +211,8 @@
- Nemo is now built with gtk-layer-shell support, note that for now it will be expected to see nemo-desktop - Nemo is now built with gtk-layer-shell support, note that for now it will be expected to see nemo-desktop
listed as a regular entry in Cinnamon Wayland session's window list applet. listed as a regular entry in Cinnamon Wayland session's window list applet.
- `restic` module now has an option for inhibiting system sleep while backups are running, defaulting to off (not inhibiting sleep), available as [`services.restic.backups.<name>.inhibitsSleep`](#opt-services.restic.backups._name_.inhibitsSleep).
- Support for *runner registration tokens* has been [deprecated](https://gitlab.com/gitlab-org/gitlab/-/issues/380872) - Support for *runner registration tokens* has been [deprecated](https://gitlab.com/gitlab-org/gitlab/-/issues/380872)
in `gitlab-runner` 15.6 and is expected to be removed in `gitlab-runner` 18.0. Configuration of existing runners in `gitlab-runner` 15.6 and is expected to be removed in `gitlab-runner` 18.0. Configuration of existing runners
should be changed to using *runner authentication tokens* by configuring should be changed to using *runner authentication tokens* by configuring

View File

@ -83,6 +83,15 @@ in
''; '';
}; };
inhibitsSleep = mkOption {
default = false;
type = types.bool;
example = true;
description = ''
Prevents the system from sleeping while backing up.
'';
};
repository = mkOption { repository = mkOption {
type = with types; nullOr str; type = with types; nullOr str;
default = null; default = null;
@ -299,7 +308,14 @@ in
(name: backup: (name: backup:
let let
extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions; extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
resticCmd = "${backup.package}/bin/restic${extraOptions}"; inhibitCmd = concatStringsSep " " [
"${pkgs.systemd}/bin/systemd-inhibit"
"--mode='block'"
"--who='restic'"
"--what='sleep'"
"--why=${escapeShellArg "Scheduled backup ${name}"} "
];
resticCmd = "${optionalString backup.inhibitsSleep inhibitCmd}${backup.package}/bin/restic${extraOptions}";
excludeFlags = optional (backup.exclude != []) "--exclude-file=${pkgs.writeText "exclude-patterns" (concatStringsSep "\n" backup.exclude)}"; excludeFlags = optional (backup.exclude != []) "--exclude-file=${pkgs.writeText "exclude-patterns" (concatStringsSep "\n" backup.exclude)}";
filesFromTmpFile = "/run/restic-backups-${name}/includes"; filesFromTmpFile = "/run/restic-backups-${name}/includes";
doBackup = (backup.dynamicFilesFrom != null) || (backup.paths != null && backup.paths != []); doBackup = (backup.dynamicFilesFrom != null) || (backup.paths != null && backup.paths != []);

View File

@ -4,6 +4,7 @@ import ./make-test-python.nix (
let let
remoteRepository = "/root/restic-backup"; remoteRepository = "/root/restic-backup";
remoteFromFileRepository = "/root/restic-backup-from-file"; remoteFromFileRepository = "/root/restic-backup-from-file";
remoteInhibitTestRepository = "/root/restic-backup-inhibit-test";
remoteNoInitRepository = "/root/restic-backup-no-init"; remoteNoInitRepository = "/root/restic-backup-no-init";
rcloneRepository = "rclone:local:/root/restic-rclone-backup"; rcloneRepository = "rclone:local:/root/restic-rclone-backup";
@ -66,6 +67,12 @@ import ./make-test-python.nix (
find /opt -mindepth 1 -maxdepth 1 ! -name a_dir # all files in /opt except for a_dir find /opt -mindepth 1 -maxdepth 1 ! -name a_dir # all files in /opt except for a_dir
''; '';
}; };
inhibit-test = {
inherit passwordFile paths exclude pruneOpts;
repository = remoteInhibitTestRepository;
initialize = true;
inhibitsSleep = true;
};
remote-noinit-backup = { remote-noinit-backup = {
inherit passwordFile exclude pruneOpts paths; inherit passwordFile exclude pruneOpts paths;
initialize = false; initialize = false;
@ -190,6 +197,13 @@ import ./make-test-python.nix (
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"', 'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
) )
# test that the inhibit option is working
server.systemctl("start --no-block restic-backups-inhibit-test.service")
server.wait_until_succeeds(
"systemd-inhibit --no-legend --no-pager | grep -q restic",
5
)
''; '';
} }
) )