Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

Merge pull request #198239 from jacobgreenleaf/jacobg-borg-inhibit

nixos/borgbackup: Add option for inhibiting sleep

authored by Ryan Lahfa and committed by GitHub 4428f9f5 810e9ccf

+51 -4
+8
nixos/doc/manual/from_md/release-notes/rl-2305.section.xml
··· 70 70 </listitem> 71 71 <listitem> 72 72 <para> 73 + <literal>borgbackup</literal> module now has an option for 74 + inhibiting system sleep while backups are running, defaulting 75 + to off (not inhibiting sleep), available as 76 + <link linkend="opt-services.borgbackup.jobs._name_.inhibitsSleep"><literal>services.borgbackup.jobs.&lt;name&gt;.inhibitsSleep</literal></link>. 77 + </para> 78 + </listitem> 79 + <listitem> 80 + <para> 73 81 The EC2 image module no longer fetches instance metadata in 74 82 stage-1. This results in a significantly smaller initramfs, 75 83 since network drivers no longer need to be included, and
+2
nixos/doc/manual/release-notes/rl-2305.section.md
··· 28 28 29 29 - `carnix` and `cratesIO` has been removed due to being unmaintained, use alternatives such as [naersk](https://github.com/nix-community/naersk) and [crate2nix](https://github.com/kolloch/crate2nix) instead. 30 30 31 + - `borgbackup` module now has an option for inhibiting system sleep while backups are running, defaulting to off (not inhibiting sleep), available as [`services.borgbackup.jobs.<name>.inhibitsSleep`](#opt-services.borgbackup.jobs._name_.inhibitsSleep). 32 + 31 33 - The EC2 image module no longer fetches instance metadata in stage-1. This results in a significantly smaller initramfs, since network drivers no longer need to be included, and faster boots, since metadata fetching can happen in parallel with startup of other services. 32 34 This breaks services which rely on metadata being present by the time stage-2 is entered. Anything which reads EC2 metadata from `/etc/ec2-metadata` should now have an `after` dependency on `fetch-ec2-metadata.service` 33 35
+21 -4
nixos/modules/services/backup/borgbackup.nix
··· 19 19 concatStringsSep " " 20 20 (mapAttrsToList (x: y: "--keep-${x}=${toString y}") cfg.prune.keep); 21 21 22 - mkBackupScript = cfg: '' 22 + mkBackupScript = name: cfg: pkgs.writeShellScript "${name}-script" ('' 23 + set -e 23 24 on_exit() 24 25 { 25 26 exitStatus=$? ··· 61 62 ${optionalString (cfg.prune.prefix != null) "--glob-archives ${escapeShellArg "${cfg.prune.prefix}*"}"} \ 62 63 $extraPruneArgs 63 64 ${cfg.postPrune} 64 - ''; 65 + ''); 65 66 66 67 mkPassEnv = cfg: with cfg.encryption; 67 68 if passCommand != null then ··· 73 74 mkBackupService = name: cfg: 74 75 let 75 76 userHome = config.users.users.${cfg.user}.home; 76 - in nameValuePair "borgbackup-job-${name}" { 77 + backupJobName = "borgbackup-job-${name}"; 78 + backupScript = mkBackupScript backupJobName cfg; 79 + in nameValuePair backupJobName { 77 80 description = "BorgBackup job ${name}"; 78 81 path = with pkgs; [ 79 82 borgbackup openssh 80 83 ]; 81 - script = mkBackupScript cfg; 84 + script = "exec " + optionalString cfg.inhibitsSleep ''\ 85 + ${pkgs.systemd}/bin/systemd-inhibit \ 86 + --who="borgbackup" \ 87 + --what="sleep" \ 88 + --why="Scheduled backup" \ 89 + '' + backupScript; 82 90 serviceConfig = { 83 91 User = cfg.user; 84 92 Group = cfg.group; ··· 338 346 {manpage}`systemd.timer(5)` 339 347 which triggers the backup immediately if the last trigger 340 348 was missed (e.g. if the system was powered down). 349 + ''; 350 + }; 351 + 352 + inhibitsSleep = mkOption { 353 + default = false; 354 + type = types.bool; 355 + example = true; 356 + description = lib.mdDoc '' 357 + Prevents the system from sleeping while backing up. 341 358 ''; 342 359 }; 343 360
+20
nixos/tests/borgbackup.nix
··· 99 99 environment.BORG_RSH = "ssh -oStrictHostKeyChecking=no -i /root/id_ed25519"; 100 100 }; 101 101 102 + sleepInhibited = { 103 + inhibitsSleep = true; 104 + # Blocks indefinitely while "backing up" so that we can try to suspend the local system while it's hung 105 + dumpCommand = pkgs.writeScript "sleepInhibited" '' 106 + cat /dev/zero 107 + ''; 108 + repo = remoteRepo; 109 + encryption.mode = "none"; 110 + startAt = [ ]; 111 + environment.BORG_RSH = "ssh -oStrictHostKeyChecking=no -i /root/id_ed25519"; 112 + }; 113 + 102 114 }; 103 115 }; 104 116 ··· 204 216 client.wait_for_unit("network.target") 205 217 client.systemctl("start --wait borgbackup-job-commandFail") 206 218 client.succeed("systemctl is-failed borgbackup-job-commandFail") 219 + 220 + with subtest("sleepInhibited"): 221 + server.wait_for_unit("sshd.service") 222 + client.wait_for_unit("network.target") 223 + client.fail("systemd-inhibit --list | grep -q borgbackup") 224 + client.systemctl("start borgbackup-job-sleepInhibited") 225 + client.wait_until_succeeds("systemd-inhibit --list | grep -q borgbackup") 226 + client.systemctl("stop borgbackup-job-sleepInhibited") 207 227 ''; 208 228 })