···7676 # wait for user services
7777 machine.wait_for_unit("default.target", "alice")
78787979+ with subtest("systemctl edit suggests --runtime"):
8080+ # --runtime is suggested when using `systemctl edit`
8181+ ret, out = machine.execute("systemctl edit testservice1.service 2>&1")
8282+ assert ret == 1
8383+ 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."
8484+ # editing w/o `--runtime` is possible for user-services, however
8585+ # it's not possible because we're not in a tty when grepping
8686+ # (i.e. hacky way to ensure that the error from above doesn't appear here).
8787+ _, out = machine.execute("systemctl --user edit testservice2.service 2>&1")
8888+ assert out.rstrip("\n") == "Cannot edit units if not on a tty."
8989+7990 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049
8091 with subtest("systemd reads timezone database in /etc/zoneinfo"):
8192 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
···11+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22+From: Maximilian Bosch <maximilian@mbosch.me>
33+Date: Fri, 1 Sep 2023 09:57:02 +0200
44+Subject: [PATCH] systemctl-edit: suggest `systemdctl edit --runtime` on system
55+ scope
66+77+This is a NixOS-specific change. When trying to modify a unit with
88+`systemctl edit` on NixOS, it'll fail with "Read-only file system":
99+1010+ $ systemctl edit libvirtd
1111+ Failed to open "/etc/systemd/system/libvirtd.service.d/.#override.conffa9825a0c9a249eb": Read-only file system
1212+1313+This is because `/etc/systemd/system` is a symlink into the store. In
1414+fact, I'd consider this a feature rather than a bug since this ensures I
1515+don't introduce state imperatively.
1616+1717+However, people wrongly assume that it's not possible to edit units
1818+ad-hoc and re-deploy their system for quick&dirty debugging where this
1919+would be absolutely fine (and doable with `--runtime` which adds a
2020+transient and non-persistent unit override in `/run`).
2121+2222+To make sure that people learn about it quicker, this patch
2323+throws an error which suggests using `--runtime` when running
2424+`systemctl edit` on the system scope.
2525+2626+For the user scope this isn't needed because user-level unit overrides
2727+are written into `$XDG_CONFIG_HOME/systemd/user`.
2828+---
2929+ src/systemctl/systemctl-edit.c | 3 +++
3030+ 1 file changed, 3 insertions(+)
3131+3232+diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c
3333+index e3f25d52d5..81c9c6f6b7 100644
3434+--- a/src/systemctl/systemctl-edit.c
3535++++ b/src/systemctl/systemctl-edit.c
3636+@@ -323,6 +323,9 @@ int verb_edit(int argc, char *argv[], void *userdata) {
3737+ sd_bus *bus;
3838+ int r;
3939+4040++ if (!arg_runtime && arg_runtime_scope == RUNTIME_SCOPE_SYSTEM)
4141++ 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.");
4242++
4343+ if (!on_tty())
4444+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty.");
4545+