From 69691f96d1688b2f8f50f9a63da7cc7638b65214 Mon Sep 17 00:00:00 2001 From: oqyude Date: Sun, 25 Oct 2026 02:09:40 +0300 Subject: [PATCH] fix(systemd): move the storage guard into [Unit] (T4/T52) mkStorageGuard returned a serviceConfig fragment, so ConditionPathIsMountPoint landed in [Service] where systemd ignores it - the guard was dead on 13 units. - helper now returns a unitConfig fragment ([Unit]); RequiresMountsFor still intentionally absent - consumers re-pointed at the real unit names: samba-smbd, phpfpm-nextcloud, immich-server, podman-3xui_app, podman-taperotation-{backend,frontend}; the old serviceConfig keys silently rendered three empty phantom units - vds excluded: otreca has no /home/oqyude/External and a live condition would skip podman-3xui_app and kill the 3x-ui panel at pubray1.zeroq.su Verified: sapphira and otreca toplevel builds exit 0; all 13 units carry the condition under [Unit] (line 3), zero occurrences under [Service]. --- lib/xlib/helpers.nix | 62 +++++++++++++++++++--------- modules/containers/3x-ui.nix | 6 ++- modules/containers/tape-rotation.nix | 12 +++++- modules/server/calibre-web.nix | 4 +- modules/server/gitea.nix | 4 +- modules/server/homebox.nix | 4 +- modules/server/immich.nix | 8 +++- modules/server/navidrome.nix | 4 +- modules/server/nextcloud.nix | 9 +++- modules/server/postgresql.nix | 4 +- modules/server/samba.nix | 9 +++- modules/server/syncthing.nix | 4 +- modules/server/uptime-kuma.nix | 4 +- 13 files changed, 102 insertions(+), 32 deletions(-) diff --git a/lib/xlib/helpers.nix b/lib/xlib/helpers.nix index e02fa98..0e8f5d5 100644 --- a/lib/xlib/helpers.nix +++ b/lib/xlib/helpers.nix @@ -159,12 +159,12 @@ in mkSymlinks ; - # Storage guard. Returns a systemd serviceConfig fragment that prevents - # a service from starting when the external storage filesystem - # (`xlib.dirs.server-home` = `/home/$user/External`) is not actually - # mounted. Without this guard, services whose `stateDir` / `dataDir` / - # bind mount source is a subdir of `/mnt/services` would happily start - # on an empty bind mount and create a fresh empty database — silent + # Storage guard. Returns a systemd **unitConfig** fragment (the `[Unit]` + # section) that prevents a service from starting when the external storage + # filesystem (`xlib.dirs.server-home` = `/home/$user/External`) is not + # actually mounted. Without this guard, services whose `stateDir` / + # `dataDir` / bind mount source is a subdir of `/mnt/services` would happily + # start on an empty bind mount and create a fresh empty database — silent # data loss. See T4 (B1) in `.agent/tasks/manifest.json` and R1.2 in # `.agent/rules/project-rules.md`. # @@ -173,18 +173,42 @@ in # paths always reports "yes" (st_dev matches) — useless. We anchor on # `server-home` (the real mount) instead. # + # (a) Why `unitConfig` and NOT `serviceConfig`: `ConditionPathIsMountPoint` + # is a `[Unit]` directive. Injected through `serviceConfig` it is + # rendered into `[Service]`, where systemd does not read it — the guard + # is silently dead (T4: 13 units / 12 services on sapphira). + # `unitConfig` is the only injection point that lands in `[Unit]`. + # + # (b) Why `RequiresMountsFor` is deliberately absent: it would make systemd + # auto-mount the filesystem when the unit is started, which defeats the + # guard — the unit would start because systemd mounted the disk, not + # because the disk was already there (tested and confirmed: v8 test on + # sapphira, 2026-10-10). Ordering belongs into `After=` on the .mount + # unit in the consumer's `systemd.services` block, not into a + # guard-creating dependency here. + # + # (c) Why the `vds` device type is excluded: a VDS has no External disk at + # all. On otreca `/home/oqyude/External` does not even exist + # (`ls` → "No such file or directory", `findmnt` → not a mount point; + # verified 2026-10-25) and every service keeps its data on the root + # filesystem (`/mnt/services` is a plain directory there, not a bind + # mount of the External disk as on sapphira). A *live* condition on such + # a host is not just useless, it is destructive: systemd would skip the + # unit, and the 3x-ui panel (`podman-3xui_app`, the only guarded unit + # reachable from `modules/vds`) would never start — killing the panel + # at `pubray1.zeroq.su`. Hence: guard only where the disk exists. + # # Usage in a service module: - # systemd.services..serviceConfig = xlib.helpers.mkStorageGuard xlib; - # or merge with an existing serviceConfig: - # serviceConfig = xlib.helpers.mkStorageGuard xlib // { ...other fields... }; - mkStorageGuard = xlib: { - # Guard via ConditionPathIsMountPoint ONLY. We intentionally do - # NOT add RequiresMountsFor: that creates a dependency that causes - # systemd to auto-remount the filesystem when starting the unit, - # which defeats the guard (tested and confirmed: v8 test on - # sapphira, 2026-10-10). Ordering should be expressed via After= - # on the .mount unit in the consumer's systemd.services block, - # not via a guard-creating dependency here. - ConditionPathIsMountPoint = [ xlib.dirs.server-home ]; - }; + # systemd.services..unitConfig = xlib.helpers.mkStorageGuard xlib; + # On a `vds` host this evaluates to `{}` — an empty `unitConfig`, which is + # a valid value (`unitConfig` is `attrsOf`) and simply adds no condition. + # The unit name must be the REAL unit of the service (e.g. `samba-smbd`, + # `phpfpm-nextcloud`, `immich-server`) — see ADR-0004: a wrong key renders a + # phantom unit with no ExecStart, and systemd refuses to start it. + mkStorageGuard = xlib: + lib.optionalAttrs (xlib.device.type != "vds") { + # Guard via ConditionPathIsMountPoint ONLY — see (b) above for why + # RequiresMountsFor must NOT be added. + ConditionPathIsMountPoint = [ xlib.dirs.server-home ]; + }; } diff --git a/modules/containers/3x-ui.nix b/modules/containers/3x-ui.nix index 3b97b0b..b6cd68a 100644 --- a/modules/containers/3x-ui.nix +++ b/modules/containers/3x-ui.nix @@ -84,7 +84,11 @@ in systemd = { services = { "podman-3xui_app" = { - serviceConfig = xlib.helpers.mkStorageGuard xlib // { + # Guard goes to `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` + # is a unit-level directive and is ignored by systemd in `[Service]` + # (T4/R1.2). + unitConfig = xlib.helpers.mkStorageGuard xlib; + serviceConfig = { Restart = lib.mkOverride 90 "always"; }; partOf = [ "podman-compose-3x-ui-root.target" ]; diff --git a/modules/containers/tape-rotation.nix b/modules/containers/tape-rotation.nix index df98f58..2cc0aec 100644 --- a/modules/containers/tape-rotation.nix +++ b/modules/containers/tape-rotation.nix @@ -102,7 +102,11 @@ in systemd = { services = { "podman-taperotation-backend" = { - serviceConfig = xlib.helpers.mkStorageGuard xlib // { + # Guard goes to `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` + # is a unit-level directive and is ignored by systemd in `[Service]` + # (T4/R1.2). + unitConfig = xlib.helpers.mkStorageGuard xlib; + serviceConfig = { Restart = lib.mkOverride 90 "always"; }; after = [ "podman-network-taperotation_default.service" ]; @@ -111,7 +115,11 @@ in wantedBy = [ "podman-compose-tape-rotation-root.target" ]; }; "podman-taperotation-frontend" = { - serviceConfig = xlib.helpers.mkStorageGuard xlib // { + # Guard goes to `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` + # is a unit-level directive and is ignored by systemd in `[Service]` + # (T4/R1.2). + unitConfig = xlib.helpers.mkStorageGuard xlib; + serviceConfig = { Restart = lib.mkOverride 90 "always"; }; after = [ diff --git a/modules/server/calibre-web.nix b/modules/server/calibre-web.nix index 78d1732..b0ac598 100644 --- a/modules/server/calibre-web.nix +++ b/modules/server/calibre-web.nix @@ -64,7 +64,9 @@ in "Z" ]; }; - services.calibre-web.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services."calibre-web".unitConfig = xlib.helpers.mkStorageGuard xlib; }; fileSystems = xlib.helpers.mkBindMount { diff --git a/modules/server/gitea.nix b/modules/server/gitea.nix index af54869..cbd1e32 100644 --- a/modules/server/gitea.nix +++ b/modules/server/gitea.nix @@ -35,6 +35,8 @@ user = "gitea"; group = "gitea"; }; - services.gitea.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services.gitea.unitConfig = xlib.helpers.mkStorageGuard xlib; }; } diff --git a/modules/server/homebox.nix b/modules/server/homebox.nix index 82c7d42..93b8da7 100644 --- a/modules/server/homebox.nix +++ b/modules/server/homebox.nix @@ -30,6 +30,8 @@ in }; systemd = storage.systemd // { - services.homebox.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services.homebox.unitConfig = xlib.helpers.mkStorageGuard xlib; }; } diff --git a/modules/server/immich.nix b/modules/server/immich.nix index 7e220c1..ddeed82 100644 --- a/modules/server/immich.nix +++ b/modules/server/immich.nix @@ -23,7 +23,13 @@ tmpfiles.rules = [ (xlib.helpers.mkTmpfile "z" config.services.immich.mediaLocation "0755" "immich" "immich") ]; - services.immich.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # nixpkgs names the immich backend unit `immich-server`; there is NO unit + # called `immich`. `systemd.services.immich` would render a phantom unit + # with no ExecStart → systemd refuses to start it (ADR-0004). + # + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services."immich-server".unitConfig = xlib.helpers.mkStorageGuard xlib; }; users.users.immich.extraGroups = [ diff --git a/modules/server/navidrome.nix b/modules/server/navidrome.nix index ab90758..19058bb 100644 --- a/modules/server/navidrome.nix +++ b/modules/server/navidrome.nix @@ -30,6 +30,8 @@ in where = pointDir; }) ]; - services.navidrome.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services.navidrome.unitConfig = xlib.helpers.mkStorageGuard xlib; }; } diff --git a/modules/server/nextcloud.nix b/modules/server/nextcloud.nix index 114531f..7200c56 100644 --- a/modules/server/nextcloud.nix +++ b/modules/server/nextcloud.nix @@ -211,7 +211,14 @@ in tmpfiles.rules = [ (xlib.helpers.mkTmpfile "z" config.services.nextcloud.home "0750" "nextcloud" "nextcloud") ]; - services.nextcloud.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # nixpkgs serves nextcloud through the PHP-FPM unit `phpfpm-nextcloud`; + # there is NO unit called `nextcloud`. `systemd.services.nextcloud` would + # render a phantom unit with no ExecStart → systemd refuses to start it + # (ADR-0004). + # + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services."phpfpm-nextcloud".unitConfig = xlib.helpers.mkStorageGuard xlib; }; environment.systemPackages = [ diff --git a/modules/server/postgresql.nix b/modules/server/postgresql.nix index 1f0d4d4..c134237 100644 --- a/modules/server/postgresql.nix +++ b/modules/server/postgresql.nix @@ -24,6 +24,8 @@ in }; systemd = storage.systemd // { - services.postgresql.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services.postgresql.unitConfig = xlib.helpers.mkStorageGuard xlib; }; } diff --git a/modules/server/samba.nix b/modules/server/samba.nix index 8a3e956..8010228 100644 --- a/modules/server/samba.nix +++ b/modules/server/samba.nix @@ -73,6 +73,13 @@ in }; systemd = storage.systemd // { - services.samba.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # The samba file server is the unit `samba-smbd` (nixpkgs names it that + # way; there is NO unit called `samba`). `systemd.services.samba` would + # render a phantom unit with no ExecStart → systemd refuses to start it + # (ADR-0004). + # + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services."samba-smbd".unitConfig = xlib.helpers.mkStorageGuard xlib; }; } diff --git a/modules/server/syncthing.nix b/modules/server/syncthing.nix index 08bd682..f6e9db1 100644 --- a/modules/server/syncthing.nix +++ b/modules/server/syncthing.nix @@ -15,5 +15,7 @@ user = "${xlib.device.username}"; }; - systemd.services.syncthing.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + systemd.services.syncthing.unitConfig = xlib.helpers.mkStorageGuard xlib; } diff --git a/modules/server/uptime-kuma.nix b/modules/server/uptime-kuma.nix index 3a12c9f..8f9fd01 100644 --- a/modules/server/uptime-kuma.nix +++ b/modules/server/uptime-kuma.nix @@ -23,7 +23,9 @@ in tmpfiles.rules = [ (xlib.helpers.mkTmpfile "z" sourceDir "0755" "nobody" "nogroup") ]; - services.uptime-kuma.serviceConfig = xlib.helpers.mkStorageGuard xlib; + # `unitConfig` → `[Unit]`: `ConditionPathIsMountPoint` is a unit-level + # directive and is ignored by systemd in `[Service]` (T4/R1.2). + services."uptime-kuma".unitConfig = xlib.helpers.mkStorageGuard xlib; }; fileSystems = xlib.helpers.mkBindMount {