Skip to content

Commit

Permalink
nixos/restic: add option to inhibit going to sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
cheriimoya committed Jul 8, 2024
1 parent e86fd8c commit a803869
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nixos/doc/manual/release-notes/rl-2411.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
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
Expand Down
18 changes: 17 additions & 1 deletion nixos/modules/services/backup/restic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
type = with types; nullOr str;
default = null;
Expand Down Expand Up @@ -299,7 +308,14 @@ in
(name: backup:
let
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)}";
filesFromTmpFile = "/run/restic-backups-${name}/includes";
doBackup = (backup.dynamicFilesFrom != null) || (backup.paths != null && backup.paths != []);
Expand Down
14 changes: 14 additions & 0 deletions nixos/tests/restic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ./make-test-python.nix (
let
remoteRepository = "/root/restic-backup";
remoteFromFileRepository = "/root/restic-backup-from-file";
remoteInhibitTestRepository = "/root/restic-backup-inhibit-test";
remoteNoInitRepository = "/root/restic-backup-no-init";
rcloneRepository = "rclone:local:/root/restic-rclone-backup";

Expand Down Expand Up @@ -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
'';
};
inhibit-test = {
inherit passwordFile paths exclude pruneOpts;
repository = remoteInhibitTestRepository;
initialize = true;
inhibitsSleep = true;
};
remote-noinit-backup = {
inherit passwordFile exclude pruneOpts paths;
initialize = false;
Expand Down Expand Up @@ -190,6 +197,13 @@ import ./make-test-python.nix (
'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
)
'';
}
)

0 comments on commit a803869

Please sign in to comment.