Commit Graph

100 Commits

Author SHA1 Message Date
Weathercold
8fb63401af
nixos/switch-to-configuration: fix units starting with dash
Fix units starting with `-` being recognized as options.
2022-08-12 18:36:15 -04:00
Stig Palmquist
4c2764c69c
nixos/switch-to-configuration: replace Net::DBus with busctl
Call dbus by using `$cur_systemd/busctl --json=...` and core modules
JSON::PP and IPC::Cmd to slim down dependencies for baseSystem.

perlPackages.NetDBus pulls in quite a few other dependencies, like
XML::Twig, LWP, and HTTP::Daemon. These are not really neccecary for
s-t-c, and some of them have caused issues particularly with cross
builds after updates to perlPackages.
2022-08-04 00:08:26 +02:00
Ingo Blechschmidt
c3428419e0 nixos/switch-to-configuration: Provider better error message in cross-compiling situations 2022-04-05 16:52:09 +02:00
Janne Heß
85874efcb0
nixos/switch-to-configuration: Make perlcritic happy 2022-03-20 13:04:23 +01:00
Janne Heß
9c494b5773
nixos/switch-to-configuration: Snake-case all variables
Also add a lot of comments and reorder some assignments
2022-03-20 13:03:39 +01:00
Janne Heß
67f84b4b87
nixos/switch-to-configuration: Snake-case all subroutines and add comments 2022-03-20 13:03:19 +01:00
Janne Heß
23ea9965bb
nixos/switch-to-configuration: Enforce consistent quoting 2022-03-20 13:01:11 +01:00
Janne Heß
bdcd558812
nixos/switch-to-configuration: Get rid of all postfixes and unlesses 2022-03-20 13:01:11 +01:00
Naïm Favier
9bdd2f852c
nixos/switch-to-configuration: fix installBootLoader escaping
Use a quoted heredoc to inject installBootLoader safely into the script,
and restore the previous invocation of `system` with a single argument so
that shell commands keep working.
2022-03-14 02:12:52 +01:00
Janne Heß
461c1c9e86
nixos/switch-to-configuration: Use parenthesis on all calls 2022-03-11 14:05:20 +01:00
Janne Heß
bc58430068
nixos/switch-to-configuration: Fix reloading of stopped services 2022-03-11 14:05:19 +01:00
Janne Heß
3052d3aa50
nixos/switch-to-configuration: Fix restarting by activation script
This bug is so obscure and unlikely that I was honestly not able to
properly write a test for it. What happens is that we are calling
handleModifiedUnit() with $unitsToStart=\%unitsToRestart. We do this to
make sure that the unit is stopped before it's started again which is
not possible by regular means because the stop phase is already done
when calling the activation script.

recordUnit() still gets $startListFile, however which is the wrong file.
The bug would be triggered if an activation script requests a service
restart for a service that has `stopIfChanged = true` and
switch-to-configuration is killed before the restart phase was run. If
the script is run again, but the activation script is not requesting
more restarts, the unit would be started instead of restarted.
2022-03-11 13:30:03 +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
Peter Hoeg
d44916d12f nixos/activation: allowed quoted values in /etc/os-release 2022-03-09 17:07:48 +08:00
Janne Heß
1def557525
nixos/switch-to-configuration: Document and test socket-activated services 2022-03-03 20:49:20 +01:00
Janne Heß
3617ecb67f
nixos/switch-to-configuration: Fix backslashes in unit names
systemd needs this so special characters (like the ones in wireguard
units that appear because they are part of base64) can be escaped using
the \x syntax.

Root of the issue is that `glob()` handles the backslash internally
which is obviously not what we want here.

Also add a test case and fix some perlcritic issues in the subroutine.
2022-02-17 12:49:45 +01:00
Stig Palmquist
3d713efe41 nixos/switch-to-configuration: avoid Array::Compare dependency
Replace Array::Compare with a simple function, since Array::Compare
pulls down Module::Build which breaks cross compilation.
2022-02-15 15:37:37 +01:00
Janne Heß
08cd8ab8b6
nixos/switch-to-configuration: Don't stop swaps in dry-activate 2022-02-09 15:14:38 +01:00
Janne Heß
b9bb1de341
nixos/switch-to-configuration: Implement reload support
This is accomplished by comparing the hashes that the unit files
contain. By filtering for a special key `X-Reload-Triggers` in the
`[Unit]` section, we can differentiate between reloads and restarts.

Since activation scripts can request reloads of units as well, more
checking of this behaviour is implemented. If a unit is to be restarted,
it's never reloaded as well which would make no sense.

Also removes a useless subroutine and perl dependencies that are
nowadays handled by the propagated build inputs feature of
`perl.withPackages`.
2022-02-09 14:31:45 +01:00
Janne Heß
78db7b6529
nixos/switch-to-configuration: Allow passing parsed unit contents 2022-02-09 14:31:44 +01:00
Janne Heß
d729cc8a53
nixos/switch-to-configuration: Skip [Install] section 2022-02-09 14:31:44 +01:00
Janne Heß
b52372675d
nixos/switch-to-configuration: Clean up lower part of the script
- Fully get rid of `parseKeyValues` and use systemctl features for that
- Add some regex modifiers recommended by perlcritic
- Get rid of a postfix if
- Sort units when showing their status
- Clean the logic for showing what failed from `elif` to `next`
- Switch from `state` to `substate` for `auto-restart` because that's
  actually where the value is stored
- Show status of units with one single systemctl call and get rid of
  COLUMNS in favor of --full
- Add a test for failing units
2022-01-20 17:10:02 +01:00
Janne Heß
96d36b0c2e
nixos/switch-to-configuration: Proper unit file parser
This replaces the naive K=V unit parser with a proper INI parser from a
library and adds proper support for override files. Also adds a bunch of
comments about parsing, I hope this makes it easier to understand and
maintain in the future.

There are multiple reasons to do so, the first one is just general
correctness with is nice imo. But to get to more serious reasons (I
didn't put in all that effort for nothing) is that this is the first
step torwards more clever restart/reload handling. By using a library
like Data::Compare a future PR could replace the current way of
fingerprinting units (which is to compare store paths) by comparing the
hashes. This is more precise because units won't get restarted because
the order of the options change, comments are added, some dependency of
writeText changes, .... Also this allows us to add a feature like
`X-Reload-Triggers` so the unit can either be reloaded when these change
or restarted when everything else changes, giving module authors the
ability to have their services reloaded without having to fear that
updates are not applied because the service doesn't get restarted.
Another reason why this feature is nice is that now that the unit files
are parsed correctly (and values are just extracted from one section),
potential future rewrites can just rely on some INI library without
having to implement their own weird parser that is compatible with this
script.

This also comes with a new subroutine to handle systemd booleans because
I thought the current way of handling it was just ugly. This also allows
overriding values this script reads in an override file.

Apart from making this script more compatible with the world around it,
this also fixes two issues I saw bugging exactly 0 (zero) people. First
is that this script now supports multiple override files, also ones that
are not called override.conf and the second one is that `1` and `on` are
treated as bools by systemd but were previously not parsed as such by
switch-to-configuration.
2022-01-20 15:10:23 +01:00
Janne Heß
2cf157c781
nixos/switch-to-configuration: Rework activation script restarts
This removes `/run/nixos/activation-reload-list` (which we will need in
the future when reworking the reload logic) and makes
`/run/nixos/activation-restart-list` honor `restartIfChanged` and
`reloadIfChanged`. This way activation scripts don't have to bother with
choosing between reloading and restarting.
2022-01-17 17:57:23 +01:00
Bernardo Meurer
f40283cf62
Merge pull request #149837 from helsinki-systems/feat/redo-activation-script-restarting
nixos/switch-to-configuration: Add a massive test and do a slight refactor
2021-12-13 11:37:20 -08:00
Janne Heß
6807628791
nixos/switch-to-configuraton: Add details about sockets 2021-12-09 13:51:18 +01:00
Janne Heß
2024306048
nixos/switch-to-configuration: Restart non-services 2021-12-09 12:31:48 +01:00
Janne Heß
393c721849
nixos/switch-to-configuration: Move handleModifiedUnit into a sub 2021-12-09 11:31:59 +01:00
Janne Heß
1f41365cda
nixos/switch-to-configuration: Restart systemd when system.conf is changed 2021-12-07 14:32:19 +01:00
Janne Heß
6f1e0dc34f
nixos/switch-to-configuration: Move excludes up 2021-12-05 18:54:19 +01:00
Janne Heß
5d34545954
nixos/switch-to-configuration: Ignore scopes 2021-12-05 18:47:35 +01:00
Janne Heß
1e422e7d58
nixos/switch-to-configuration: Fix dry order
This makes the order of the dry activation messages the same as the real
actions which makes more sense than another random order.
2021-12-05 18:46:50 +01:00
Janne Heß
3693e8b093
nixos/switch-to-configuration: Clean perl code
oct() is recommended by perlcritic and the rest was unused.
2021-12-05 18:45:44 +01:00
Janne Heß
50a0f33c2a
nixos/switch-to-configuration: Remove unnecessary TODOs
The first one doesn't make any sense because the directory where the
init binary resides does not contain other tools we need like
systemd-escape.

The second one doesn't make sense either because the errors are already
ignored.
2021-12-05 18:43:42 +01:00
Michael Weiss
1cfecb636b
Revert "Merge pull request #141192 from helsinki-systems/feat/improved-socket-handling2"
This reverts commit 57961d2b838cc31fa4ce89641b6a8db544a8471a, reversing
changes made to b04f913afce4419f6b777fd64ac52315dbef3aaf.
(I.e. this reverts PR #141192.)

While well-intended, this change does unfortunately introduce very
serious regressions that are especially disruptive/noticeable on desktop
systems (e.g. users of Sway will loose their graphical session when
running "nixos-rebuild switch").

Therefore, this change has to be reverted ASAP instead of trying to fix
it in "production".
Note: An updated version should be extensively discussed, reviewed, and
tested before re-landing this change as an earlier version also had to
be reverted for the exact same issues [0].

Fix: #146727

[0]: https://github.com/NixOS/nixpkgs/pull/73871#issuecomment-559783752
2021-11-27 17:22:22 +01:00
Janne Heß
4cdbb2d891
nixos/switch-to-configuration: Fix ordering and indentation
This makes the order of operations the same in dry-activate and a "true"
activate. Also fixes the indentation I messed up and drop a useless
unlink() call (we are already unlinking that file earlier).
2021-10-17 14:35:49 +02:00
Janne Heß
ad09f7be14
nixos/switch-to-configuration: Handle stopped sockets
The previous logic failed to detect that units were socket-activated
when the socket was stopped before switch-to-configuration was run. This
commit fixes that and also starts the socket in question.
2021-10-17 14:35:48 +02:00
Janne Heß
558158b4f5
nixos/switch-to-configuration: Hide socket warnings 2021-10-17 14:35:48 +02:00
Janne Heß
047aa1a0e9
nixos/switch-to-configuration: Use early return 2021-10-17 14:35:47 +02:00
Janne Heß
4f870c7d70
nixos/switch-to-configuration: Restart timers 2021-10-17 14:35:47 +02:00
Janne Heß
adc033cd59
nixos/switch-to-configuration: Ignore path units 2021-10-17 14:35:46 +02:00
Janne Heß
de128feacc
nixos/switch-to-configuration: Ignore slice units 2021-10-17 14:35:46 +02:00
Janne Heß
b515bae5cf
nixos/switch-to-configuration: Remove some FIXMEs
The first FIXME is removed because it doesn't make sense to use
/proc/1/exe since that points to a directory that doesn't have all tools
the activation script needs (like systemd-escape).

The second one is removed because there is already no error handling
(compare with the restart logic where the return code is checked).
2021-10-17 14:35:45 +02:00
Janne Heß
cfad5e3403
nixos/switch-to-configuration: Improve socket support
This commit changes a lot more that you'd expect but it also adds a lot
of new testing code so nothing breaks in the future. The main change is
that sockets are now restarted when they change. The main reason for
the large amount of changes is the ability of activation scripts to
restart/reload units. This also works for socket-activated units now,
and honors reloadIfChanged and restartIfChanged. The two changes don't
really work without each other so they are done in the one large commit.

The test should show what works now and ensure it will continue to do so
in the future.
2021-10-17 14:35:43 +02:00
Janne Heß
744162ffb6
nixos/switch-to-configuration: Fix perlcritic warning 2021-10-17 14:33:42 +02:00
Janne Heß
f0a31f9b9f
nixos/switch-to-configuration: Ignore started scopes
They are not managed by us and it might be weird to users to see units
they didn't expect to be started.
2021-10-17 14:33:42 +02:00
Janne Heß
ec619ca369
nixos/switch-to-configuration: Remove unused variable 2021-10-17 14:33:41 +02:00
Janne Heß
e8388f8574
nixos/switch-to-configuration: Allow activation scripts to restart units
The primary use case is tools like sops-nix and agenix to restart units
when secrets change. There's probably other reasons to restart units as
well and a nice thing to have in general.
2021-09-09 13:11:52 +02:00
Janne Heß
3156730402
nixos/switch-to-configuration: Add dry activation scripts 2021-09-03 18:40:11 +02:00
Timothy DeHerrera
3cc7e2f037
Merge pull request #115927 from input-output-hk/fix-deleted-sockets-restart
nixos/switch-to-configuration: don't try restart deleted sockets
2021-08-20 08:33:42 -06:00