Merge pull request #252766 from Ma27/systemctl-edit-runtime-suggest

+57
+11
nixos/tests/systemd.nix
··· 76 # wait for user services 77 machine.wait_for_unit("default.target", "alice") 78 79 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049 80 with subtest("systemd reads timezone database in /etc/zoneinfo"): 81 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
··· 76 # wait for user services 77 machine.wait_for_unit("default.target", "alice") 78 79 + with subtest("systemctl edit suggests --runtime"): 80 + # --runtime is suggested when using `systemctl edit` 81 + ret, out = machine.execute("systemctl edit testservice1.service 2>&1") 82 + assert ret == 1 83 + assert out.rstrip("\n") == "The unit-directory '/etc/systemd/system' is read-only on NixOS, so it's not possible to edit system-units directly. Use 'systemctl edit --runtime' instead." 84 + # editing w/o `--runtime` is possible for user-services, however 85 + # it's not possible because we're not in a tty when grepping 86 + # (i.e. hacky way to ensure that the error from above doesn't appear here). 87 + _, out = machine.execute("systemctl --user edit testservice2.service 2>&1") 88 + assert out.rstrip("\n") == "Cannot edit units if not on a tty." 89 + 90 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049 91 with subtest("systemd reads timezone database in /etc/zoneinfo"): 92 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
+45
pkgs/os-specific/linux/systemd/0019-systemctl-edit-suggest-systemdctl-edit-runtime-on-sy.patch
···
··· 1 + From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 2 + From: Maximilian Bosch <maximilian@mbosch.me> 3 + Date: Fri, 1 Sep 2023 09:57:02 +0200 4 + Subject: [PATCH] systemctl-edit: suggest `systemdctl edit --runtime` on system 5 + scope 6 + 7 + This is a NixOS-specific change. When trying to modify a unit with 8 + `systemctl edit` on NixOS, it'll fail with "Read-only file system": 9 + 10 + $ systemctl edit libvirtd 11 + Failed to open "/etc/systemd/system/libvirtd.service.d/.#override.conffa9825a0c9a249eb": Read-only file system 12 + 13 + This is because `/etc/systemd/system` is a symlink into the store. In 14 + fact, I'd consider this a feature rather than a bug since this ensures I 15 + don't introduce state imperatively. 16 + 17 + However, people wrongly assume that it's not possible to edit units 18 + ad-hoc and re-deploy their system for quick&dirty debugging where this 19 + would be absolutely fine (and doable with `--runtime` which adds a 20 + transient and non-persistent unit override in `/run`). 21 + 22 + To make sure that people learn about it quicker, this patch 23 + throws an error which suggests using `--runtime` when running 24 + `systemctl edit` on the system scope. 25 + 26 + For the user scope this isn't needed because user-level unit overrides 27 + are written into `$XDG_CONFIG_HOME/systemd/user`. 28 + --- 29 + src/systemctl/systemctl-edit.c | 3 +++ 30 + 1 file changed, 3 insertions(+) 31 + 32 + diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c 33 + index e3f25d52d5..81c9c6f6b7 100644 34 + --- a/src/systemctl/systemctl-edit.c 35 + +++ b/src/systemctl/systemctl-edit.c 36 + @@ -323,6 +323,9 @@ int verb_edit(int argc, char *argv[], void *userdata) { 37 + sd_bus *bus; 38 + int r; 39 + 40 + + if (!arg_runtime && arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) 41 + + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The unit-directory '/etc/systemd/system' is read-only on NixOS, so it's not possible to edit system-units directly. Use 'systemctl edit --runtime' instead."); 42 + + 43 + if (!on_tty()) 44 + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty."); 45 +
+1
pkgs/os-specific/linux/systemd/default.nix
··· 201 ./0016-inherit-systemd-environment-when-calling-generators.patch 202 ./0017-core-don-t-taint-on-unmerged-usr.patch 203 ./0018-tpm2_context_init-fix-driver-name-checking.patch 204 ] ++ lib.optional stdenv.hostPlatform.isMusl ( 205 let 206 oe-core = fetchzip {
··· 201 ./0016-inherit-systemd-environment-when-calling-generators.patch 202 ./0017-core-don-t-taint-on-unmerged-usr.patch 203 ./0018-tpm2_context_init-fix-driver-name-checking.patch 204 + ./0019-systemctl-edit-suggest-systemdctl-edit-runtime-on-sy.patch 205 ] ++ lib.optional stdenv.hostPlatform.isMusl ( 206 let 207 oe-core = fetchzip {