Commit Graph

70 Commits

Author SHA1 Message Date
Ilan Joselevich
944a258aeb switch-to-configuration-ng: handle transition to socket activation
When a service gains a `.socket` unit, the socket is not yet active, so
the `active_cur.contains_key(socket)` check is false and the service is
queued in `units_to_start`. That races with the socket unit being started
by `sockets.target`, producing "Socket service already active, refusing"
or "no socket activation file descriptors found" errors.

Treat the service as socket-activated whenever the socket is absent now
but present in the new configuration, leaving it to be activated on demand
by the newly started socket rather than started directly.

Fixes: https://github.com/NixOS/nixpkgs/pull/510391#issuecomment-4273511176
Assisted-by: Claude:claude-opus-4-8
2026-06-20 18:40:21 +03:00
Ilan Joselevich
ad3e95b093 switch-to-configuration-ng: handle socket-activated Accept=yes services
A per-connection instance spawned by an Accept=yes socket (e.g.
foo@1234.service) has an implicit socket of foo.socket, but the implicit
name was derived from the template base name including the trailing "@",
yielding foo@.socket. That never matches an active unit, so such running
instances were treated as non-socket-activated and the activation script
tried to start them directly on a configuration switch. That fails
because there is no connection socket to pass, sending the instance into
a restart loop.

Strip the trailing "@" so the implicit socket resolves correctly; the
running instances are then stopped and their socket restarted, leaving
systemd to spawn fresh instances for new connections.

Resolves: https://github.com/NixOS/nixpkgs/issues/533205
Assisted-by: Claude:claude-opus-4-8
2026-06-19 16:09:44 +03:00
r-vdp
69dc41f083 nixos/preSwitchChecks: actually set errexit inside check bodies
The previous `if ! ( ... )` wrapper put the check body in a context
where bash ignores `set -e`, so a failing command in the middle of a
check would not abort it and the switch would proceed. Run the
subshell outside any conditional and capture $? explicitly instead.
2026-06-04 02:18:34 +03:00
r-vdp
663a59e0b6 nixos/activation: run user nixos-activation.service exactly once per switch
stc-ng starts every active target (including default.target) and then
explicitly restarts nixos-activation.service. As a Type=oneshot without
RemainAfterExit the unit is inactive after login, so the default.target
start job re-runs it via Wants=, and the explicit restart runs it again
(or, depending on ordering, SIGTERMs the currently running script and re-runs it).

Set RemainAfterExit=yes so target starts are a no-op for an already-run
activation, and restartIfChanged=false so the unit-diff pass leaves it
alone when the script changes. The explicit restart in stc-ng remains
the single trigger per switch. Print that restart so it is visible in
the switch output, and drop it from the "NOT restarting" list.

Extend the user-activation-scripts test to assert the activation is
only run once, and never killed.
2026-05-23 12:50:40 +02:00
r-vdp
6ced06a1b3 switch-to-configuration-ng: rework user-unit migration candidate selection
The previous "FragmentPath not under /etc" deny-list swept up units that
were never managed by a per-user manager (e.g. dbus-broker, whose
FragmentPath systemd reports under /run/current-system/sw/share via
systemd.packages) and missed nothing it should have caught, but for the
wrong reason.

Make the intent explicit. A unit is a migration candidate iff it is
active, the new generation defines it in /etc/systemd/user, and either
  * its FragmentPath is under $XDG_CONFIG_HOME/systemd/user (the
    home-manager case; ~/.config shadows /etc, so we must wait for
    sd-switch to remove the copy), or
  * its FragmentPath is anywhere else outside /etc and the previous
    generation did not have it in /etc (package-shipped units found via
    $XDG_DATA_HOME / $XDG_DATA_DIRS, e.g. ~/.nix-profile/share; /etc
    outranks these so it wins on daemon-reload).

The "previous generation did not have it" guard keeps units that have
always been in /etc, but whose FragmentPath systemd reports elsewhere, out
of the candidate set, and the existing now_etc check verifies /etc
actually won before acting. Compare FragmentPath by parent directory
instead of string prefix while here.

Covered by a new switch-test case that seeds a unit in
~/.local/share/systemd/user.
2026-05-22 12:42:19 +02:00
r-vdp
76c8d45099 switch-to-configuration-ng: honour X-* directives in user-unit migration pass
The post-activation pass added in 5cc82c4922 to handle units migrating
from a per-user manager (home-manager) to NixOS unconditionally restarts
or starts any candidate. dbus-broker.service explicitly opts out of
restarts via reloadIfChanged because restarting the session bus kills
running clients; the second pass ignored that and restarted it anyway.

Apply the same X-ReloadIfChanged / X-RestartIfChanged / RefuseManualStop /
RefuseManualStart / X-OnlyManualStart checks that handle_modified_unit
performs, so a migrated unit is reloaded, skipped, restarted or started
as its directives require.

Covered by new switch-test specialisations for reloadIfChanged and
restartIfChanged = false.
2026-05-22 12:42:19 +02:00
Paul Haerle
f4e70f2708 switch-to-configuration-ng: restart changed user units on switch (#507588) 2026-04-21 18:47:11 +00:00
r-vdp
17773e7fa7 nixos/tests/switch-test: cover user-unit migration to NixOS
Simulate a home-manager-style unit in ~/.config/systemd/user and verify
switch-to-configuration takes it over after the per-user activation
drops it (both stopped and left-running variants), and leaves it alone
while still shadowed.
2026-04-21 15:45:37 +02:00
r-vdp
106c49d2e4 nixos/tests/switch-test: cover user-unit restart on switch
Add a lingering test user plus specialisations covering stop-start,
stopIfChanged=false, reloadIfChanged, reloadTriggers, and removal for
systemd.user.services so the new user-scope diff/restart path in
switch-to-configuration-ng is exercised.
2026-04-21 15:44:15 +02:00
r-vdp
49c05db108 nixos/switchable-system: improve switch-inhibitor pre-check
The check ran `realpath /run/current-system` under errexit, so a
missing current-system symlink aborted the script.
Drop the realpath calls (the -f test and jq already follow symlinks)
and use a static store path for the empty fallback instead of mktemp/trap.

Also exempt dry-activate, which makes no state changes and was being
blocked from showing its diff, and let jq fail loudly on malformed
inhibitor JSON instead of silently treating it as empty.
2026-04-16 10:50:02 +02:00
Jared Baur
f36a79d39b switch-to-configuration-ng: handle mount/automount changes more robustly
Mount options changing for the same mountpoint means we perform a unit
reload as opposed to a unit restart. If a mount unit is not active (for
example, with a mount unit that is failing), then we need to start the
unit instead. Since we do some of the work that the fstab generator
already does, we parse /etc/fstab and derive mount unit names that we
should start ourselves. We cannot expect those units to be present in
/etc/systemd/system, however, which we currently do in order to
determine whether we should _not_ start the inactive unit.
2026-02-15 15:17:26 -08:00
r-vdp
3408d1a4a9 nixos/switchable-system: improve switch inhibitors
Improvements:
1. Turn the nix value into an attrset so that every inhibitor has a name
   that we can match between generations
2. Write the attrset to a file as JSON
3. When checking, we load the JSON files from both the current and the
   new generation into a jq pipeline and match up the keys.
   We output a dict with a value for every key that is present in both
   generations with a different value.
4. Build in error handling for different corner cases (missing files,
   non-JSON content)
2026-01-09 11:59:26 +02:00
Robert Hensing
41b98b4a8a nixos: Shuffle definitions and deprecate system.extraSystemBuilderCmds
Shuffle:
The definitions are now combined into a single option. Since they have
no interdependencies, that's ok, but you may notice this trivial change
by a changed hash, and analyzing with nix-diff.

Deprecation:
Use the option `system.systemBuilderCommands` instead.
2025-11-12 21:00:32 +01:00
Jared Baur
fe6a73c0f7 nixosTests.switchTest: assert automount units are handled appropriately 2025-09-14 16:11:13 -07:00
Jared Baur
477b8796f9 nixosTests.switchTest: assert switch-to-configuration ignores generated units 2025-09-14 14:06:20 -07:00
Grimmauld
a48bc46a3e nixos/tests/switchTest: migrate to systemd.settings.Manager 2025-07-28 11:24:23 +02:00
Jared Baur
c59d4343f9 treewide: remove Perl-based switch-to-configuration 2025-06-11 08:56:42 -07:00
Silvan Mosberger
374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00
r-vdp
1aa7e7a131 switch-to-configuration: add a test to make sure that we don't block on the lockfile
We should exit with a clear error message instead.
2025-02-17 15:47:08 +01:00
r-vdp
e6bc540ce0 nixos/activation: pass the action as an argument to the pre-switch-checks
This allows for instance to reject switching into a configuration, while
still allowing to reboot into that same configuration.

This can be useful for instance to reject switching to a configuration
with a new systemd major version, but setting that same configuration as
the new boot default with `switch-to-configuration boot` is fine.
2025-01-28 11:40:38 +01:00
r-vdp
113dbd79de nixos/activation: improve preSwitchChecks
A couple of improvements:

1. Avoid the generally discouraged apply argument to options, as it has
   quite weird semantics
2. Avoid issues when a user calls a preSwitchCheck `script`, which
   would've been silently overridden by the existing implementation.
   Reliance on a special attribute name like that is bound to lead to a
   very-hard-to-debug problem for someone at some point
3. Use writeShellApplication so that the preSwitchChecks are checked by
   shellcheck and and so that they run with basic bash guardrails
4. Fix shellcheck issue (testing the value of $?)
5. Add a positive preSwitchCheck to the nixos test, to make sure that
   that works as intended
2025-01-23 15:51:37 +01:00
r-vdp
779c710476 nixos/switch-test: use the runTest handler to improve eval performance
The newer runTest handler uses a single nixpkgs instance to eval all the
specialisations, reducing the memory usage and eval time of the test
drastically compared to handleTest which creates a new nixpkgs instance
for every specialisation.
2024-12-11 10:06:03 +01:00
Jared Baur
6e192c4489 nixos/activation: Add pre-switch checks
Add an option for shell script fragments that are ran before switching
to a new NixOS system configuration (pre installation of bootloader or
system activation). Also add a new subcommand for
switch-to-configuration called "check" that will cause the program to
always exit after checks are ran.
2024-11-21 13:02:54 -08:00
Will Fancher
f92ec1bc93 nixos/tests/switchTest: Add test for dbus reloading 2024-11-04 04:12:52 -05:00
Will Fancher
a7cda6835f nixos/tests/switchTest: Remove spurious dbus reload checks
Previously, the base system had grub in `systemPackages` but the
specialisations did not. This is because of a few factors:

- Grub was enabled in the nixos config
- Specialisations set `grub.device = "nodev"` (see: system/activation/no-clone.nix)
- Grub is added to `systemPackages` depending on whether `devices == ["nodev"]`

This meant that switching between the base system and a specialisation
always changed `system-path.drv`, which is a reload trigger for
dbus. With grub disabled in this test, this no longer happens.
2024-11-04 04:12:52 -05:00
Will Fancher
40b7674b98 nixos/tests/switchTest: Test no boot loader 2024-11-04 03:33:26 -05:00
Jared Baur
f6fed8b831 nixos/switch-test: add test for multi-line unit values 2024-09-21 08:28:03 -07:00
K900
932903acff nixos/tests/switch-test: enable switch-to-configuration 2024-09-08 17:09:01 +03:00
Emily
73f34448ca nixos/tests/switch-test: simplify enableNg setting 2024-09-06 08:35:43 +01:00
Jared Baur
32bf051ba4 nixos/switch-to-configuration: add new implementation
This adds an implementation of switch-to-configuration that allows for
closer interaction with the lifecycle of systemd units by using DBus
APIs directly instead of using systemctl. It is disabled by default, but
can be enabled by specifying `{ system.switch = { enable = false; enableNg = true; }; }`.
2024-05-10 16:33:06 -07:00
Jared Baur
3cdbad2ef1 nixos/switch-to-configuration: fix dbus implementation used for test
Makes the switch-test work with both dbus server implementations.
2024-04-29 21:41:07 -07:00
K900
2975aecbcb nixos/tests/switch-test: s/dbus.service/dbus-broker.service/g
Fixes the test after the default DBus implementation change.
2024-04-06 23:21:06 +03:00
Janne Heß
358347e8b6 nixos/switchTest: Also test swap devices 2023-09-22 10:26:11 +02:00
Janne Heß
85c1c30fd9 nixos/switch-to-configuration: Never unmount / or /nix
Also adds a huge test for fstab handling
2023-09-22 10:26:10 +02:00
Janne Heß
c3e6412260 nixos/switchTest: Also test restarting from aborted switches 2023-09-13 17:11:32 +02:00
Janne Heß
e0717ce857 nixos/switchTest: Also test systemd restarts 2023-09-13 17:04:54 +02:00
Janne Heß
211e2d738b nixos/switchTest: Also test init interface version 2023-09-13 16:56:01 +02:00
Janne Heß
d1c1335908 nixos/switch-to-configuration: Test more action things 2023-09-13 16:42:19 +02:00
Janne Heß
eb831f759b nixos/stc: Improve mount unit handling
We should sometimes restart the units rather than reloading them so the
changes are actually applied. / and /nix are explicitly excluded because
there was some very old issue where these were unmounted. I don't think
this will affect many people since most people use fstab mounts instead
but I plan to adapt this behavior for fstab mounts as well in the future
(once I wrote a test for the fstab thingies).
2023-08-21 09:07:14 +02:00
Janne Heß
7937c5816d nixos/switchTest: Also check for base unit modifications 2023-08-13 14:25:24 +02:00
oddlama
d073105d6b nixos/switch-to-configuration: fix ignoring of template unit specialization dropins 2023-07-23 13:16:58 +02:00
Robert Hensing
772d6076e8 nixos: Add system.activatable flag for images that are pre-activated 2023-06-28 14:06:28 +02:00
Robert Hensing
89664199e1 nixos/tests/switch-test.nix: Fix warnings 2023-06-28 12:49:34 +02:00
Weathercold
7874b995fb nixos/switch-test: add test for services starting with dash
Ensures that services starting with dash can be reloaded.
2022-08-20 16:43:25 -04:00
Janne Heß
2473cce829 nixos/switchTest: Also test boot/switch actions 2022-03-20 13:04:24 +01:00
Janne Heß
5c00fe6b1b nixos/switchTest: Also test the os-release parser 2022-03-20 13:01:11 +01:00
Janne Heß
bc58430068 nixos/switch-to-configuration: Fix reloading of stopped services 2022-03-11 14:05:19 +01:00
Janne Heß
c96180c53f nixos/switch-to-configuration: Ignore some unit keys
Some unit keys don't need to restart the service to make them effective.
Reduce the amount of service restarts by ignoring these keys
2022-03-11 13:30:03 +01:00
Janne Heß
acb535fb61 nixos/switchTest: Also test targets 2022-03-11 13:30:03 +01:00
Janne Heß
f6ad15fd8c nixos/switchTest: Make checks more precise 2022-03-03 20:56:37 +01:00