1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Maximilian Bosch <maximilian@mbosch.me>
3Date: Fri, 1 Sep 2023 09:57:02 +0200
4Subject: [PATCH] systemctl-edit: suggest `systemdctl edit --runtime` on system
5 scope
6
7This 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
13This is because `/etc/systemd/system` is a symlink into the store. In
14fact, I'd consider this a feature rather than a bug since this ensures I
15don't introduce state imperatively.
16
17However, people wrongly assume that it's not possible to edit units
18ad-hoc and re-deploy their system for quick&dirty debugging where this
19would be absolutely fine (and doable with `--runtime` which adds a
20transient and non-persistent unit override in `/run`).
21
22To make sure that people learn about it quicker, this patch
23throws an error which suggests using `--runtime` when running
24`systemctl edit` on the system scope.
25
26For the user scope this isn't needed because user-level unit overrides
27are written into `$XDG_CONFIG_HOME/systemd/user`.
28---
29 src/systemctl/systemctl-edit.c | 3 +++
30 1 file changed, 3 insertions(+)
31
32diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c
33index c42a31153d..154dbf0402 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() && !arg_stdin)
44 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units interactively if not on a tty.");
45