From 74eeb75af1ea94c2f639a843172ee2fc82f5cf54 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 10 Feb 2024 13:43:31 +0200 Subject: [PATCH 1/2] systemd: add support for upholds and upheldBy --- nixos/lib/systemd-lib.nix | 10 +++++++++- nixos/lib/systemd-unit-options.nix | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix index 347ee7303936..c9cca619ed70 100644 --- a/nixos/lib/systemd-lib.nix +++ b/nixos/lib/systemd-lib.nix @@ -242,7 +242,7 @@ in rec { ln -sfn '${name}' $out/'${name2}' '') (unit.aliases or [])) units)} - # Create .wants and .requires symlinks from the wantedBy and + # Create .wants, .upholds and .requires symlinks from the wantedBy, upheldBy and # requiredBy options. ${concatStrings (mapAttrsToList (name: unit: concatMapStrings (name2: '' @@ -250,6 +250,12 @@ in rec { ln -sfn '../${name}' $out/'${name2}.wants'/ '') (unit.wantedBy or [])) units)} + ${concatStrings (mapAttrsToList (name: unit: + concatMapStrings (name2: '' + mkdir -p $out/'${name2}.upholds' + ln -sfn '../${name}' $out/'${name2}.upholds'/ + '') (unit.upheldBy or [])) units)} + ${concatStrings (mapAttrsToList (name: unit: concatMapStrings (name2: '' mkdir -p $out/'${name2}.requires' @@ -289,6 +295,8 @@ in rec { { Requires = toString config.requires; } // optionalAttrs (config.wants != []) { Wants = toString config.wants; } + // optionalAttrs (config.upholds != []) + { Upholds = toString config.upholds; } // optionalAttrs (config.after != []) { After = toString config.after; } // optionalAttrs (config.before != []) diff --git a/nixos/lib/systemd-unit-options.nix b/nixos/lib/systemd-unit-options.nix index 9c69bda471bb..9a0fedeb0b2d 100644 --- a/nixos/lib/systemd-unit-options.nix +++ b/nixos/lib/systemd-unit-options.nix @@ -74,6 +74,15 @@ in rec { ''; }; + upheldBy = mkOption { + default = []; + type = types.listOf unitNameType; + description = lib.mdDoc '' + Keep this unit running as long as the listed units are running. This is a continuously + enforced version of wantedBy. + ''; + }; + wantedBy = mkOption { default = []; type = types.listOf unitNameType; @@ -147,6 +156,20 @@ in rec { ''; }; + upholds = mkOption { + default = []; + type = types.listOf unitNameType; + description = lib.mdDoc '' + Configures dependencies similar to Wants=, but as long as this unit is up, all units + listed in Upholds= are started whenever found to be inactive or failed, and no job is + queued for them. While a Wants= dependency on another unit has a one-time effect when + this units started, a Upholds= dependency on it has a continuous effect, constantly + restarting the unit if necessary. This is an alternative to the Restart= setting of + service units, to ensure they are kept running whatever happens. The restart happens + without delay, and usual per-unit rate-limit applies. + ''; + }; + after = mkOption { default = []; type = types.listOf unitNameType; From fd5664871fb33e2a9f8e0b023e568958aa959ad8 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 10 Feb 2024 22:25:24 +0200 Subject: [PATCH 2/2] review --- nixos/doc/manual/release-notes/rl-2405.section.md | 4 ++++ nixos/lib/systemd-unit-options.nix | 8 +------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2405.section.md b/nixos/doc/manual/release-notes/rl-2405.section.md index 05c06ebcac1b..e11524f0de8a 100644 --- a/nixos/doc/manual/release-notes/rl-2405.section.md +++ b/nixos/doc/manual/release-notes/rl-2405.section.md @@ -286,6 +286,10 @@ The pre-existing [services.ankisyncd](#opt-services.ankisyncd.enable) has been m [fileSystems.overlay](#opt-fileSystems._name_.overlay.lowerdir). See also the [NixOS docs](#sec-overlayfs). +- systemd units can now specify the `Upholds=` and `UpheldBy=` unit dependencies via the aptly + named `upholds` and `upheldBy` options. These options get systemd to enforce that the + dependencies remain continuosly running for as long as the dependent unit is in a running state. + - `stdenv`: The `--replace` flag in `substitute`, `substituteInPlace`, `substituteAll`, `substituteAllStream`, and `substituteStream` is now deprecated if favor of the new `--replace-fail`, `--replace-warn` and `--replace-quiet`. The deprecated `--replace` equates to `--replace-warn`. - A new hardening flag, `zerocallusedregs` was made available, corresponding to the gcc/clang option `-fzero-call-used-regs=used-gpr`. diff --git a/nixos/lib/systemd-unit-options.nix b/nixos/lib/systemd-unit-options.nix index 9a0fedeb0b2d..df05d165d9e8 100644 --- a/nixos/lib/systemd-unit-options.nix +++ b/nixos/lib/systemd-unit-options.nix @@ -160,13 +160,7 @@ in rec { default = []; type = types.listOf unitNameType; description = lib.mdDoc '' - Configures dependencies similar to Wants=, but as long as this unit is up, all units - listed in Upholds= are started whenever found to be inactive or failed, and no job is - queued for them. While a Wants= dependency on another unit has a one-time effect when - this units started, a Upholds= dependency on it has a continuous effect, constantly - restarting the unit if necessary. This is an alternative to the Restart= setting of - service units, to ensure they are kept running whatever happens. The restart happens - without delay, and usual per-unit rate-limit applies. + Keeps the specified running while this unit is running. A continuous version of `wants`. ''; };