Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch-to-configuration: Better handling of socket-activated units #359724

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nixos/lib/systemd-lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ in rec {
'' else "")
+ optionalString (def ? stopIfChanged && !def.stopIfChanged) ''
X-StopIfChanged=false
'' + optionalString (def ? notSocketActivated && def.notSocketActivated) ''
X-NotSocketActivated=true
'' + attrsToSection def.serviceConfig);
};

Expand Down
12 changes: 12 additions & 0 deletions nixos/lib/systemd-unit-options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,18 @@ in rec {
'';
};

notSocketActivated = mkOption {
type = types.bool;
default = false;
description = ''
If set, a changed unit is never assumed to be
socket-activated on configuration activation, even if
it might have associated socket units. Instead, the unit
will be restarted (or stopped/started) as if it had no
associated sockets.
'';
};

startAt = mkOption {
type = with types; either str (listOf str);
default = [];
Expand Down
5 changes: 3 additions & 2 deletions nixos/modules/services/hardware/udev.nix
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@ in
fi
'';

systemd.services.systemd-udevd =
{ restartTriggers = [ config.environment.etc."udev/rules.d".source ];
systemd.services.systemd-udevd = {
restartTriggers = [ config.environment.etc."udev/rules.d".source ];
notSocketActivated = true;
};

};
Expand Down
7 changes: 7 additions & 0 deletions nixos/modules/system/activation/switch-to-configuration.pl
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ sub handle_modified_unit { ## no critic(Subroutines::ProhibitManyArgs, Subroutin
}
}

if (parse_systemd_bool(\%new_unit_info, "Service", "X-NotSocketActivated", 0)) {
# If the unit explicitly opts out of socket
# activation, restart it as if it weren't (but do
# restart its sockets, that's fine):
$socket_activated = 0;
}

# If the unit is not socket-activated, record
# that this unit needs to be started below.
# We write this to a file to ensure that the
Expand Down
1 change: 1 addition & 0 deletions nixos/modules/system/boot/networkd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,7 @@ let
config.environment.etc."systemd/networkd.conf".source
];
aliases = [ "dbus-org.freedesktop.network1.service" ];
notSocketActivated = true;
};

networking.iproute2 = mkIf (cfg.config.addRouteTablesToIPRoute2 && cfg.config.routeTables != { }) {
Expand Down
10 changes: 10 additions & 0 deletions pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const RELOAD_LIST_FILE: &str = "/run/nixos/reload-list";
// `stopIfChanged = true` is ignored, switch-to-configuration will handle `restartIfChanged =
// false` and `reloadIfChanged = true`. This is the same as specifying a restart trigger in the
// NixOS module.
// In addition, switch-to-configuration will handle notSocketActivated=true to disable treatment
// of units as "socket-activated" even though they might have any associated sockets.
//
// The reload file asks this program to reload a unit. This is the same as specifying a reload
// trigger in the NixOS module and can be ignored if the unit is restarted in this activation.
Expand Down Expand Up @@ -583,6 +585,8 @@ fn handle_modified_unit(
} else {
// If this unit is socket-activated, then stop the socket unit(s) as well, and
// restart the socket(s) instead of the service.
// We count as "socket-activated" any unit that doesn't declare itself not so
// via X-NotSocketActivated, that has any associated .socket units.
let mut socket_activated = false;
if unit.ends_with(".service") {
let mut sockets = if let Some(Some(Some(sockets))) = new_unit_info.map(|info| {
Expand Down Expand Up @@ -634,6 +638,12 @@ fn handle_modified_unit(
}
}
}
if parse_systemd_bool(new_unit_info, "Service", "X-NotSocketActivated", false) {
// If the unit explicitly opts out of socket
// activation, restart it as if it weren't (but do
// restart its sockets, that's fine):
socket_activated = false;
}

// If the unit is not socket-activated, record that this unit needs to be started
// below. We write this to a file to ensure that the service gets restarted if
Expand Down