From ebd6ad4b2c08b56b5661b5083c59dc5eb270439b Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 22:29:02 -0600 Subject: [PATCH 1/2] feat(pm-projector): proxy socket activation through systemd --- nixos/services/pm-stack/default.nix | 40 +++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/nixos/services/pm-stack/default.nix b/nixos/services/pm-stack/default.nix index 2a3610c61..425570766 100644 --- a/nixos/services/pm-stack/default.nix +++ b/nixos/services/pm-stack/default.nix @@ -198,6 +198,12 @@ in description = "Port the pm-projector listens on (loopback only)."; }; + projectorBackendPort = lib.mkOption { + type = lib.types.port; + default = 17801; + description = "Private loopback port for pm-projector behind its socket-activated proxy."; + }; + aggregatorPort = lib.mkOption { type = lib.types.port; default = 4870; @@ -231,7 +237,10 @@ in # state dir as cwd also keeps the code's cwd-relative # fallback (file:data/projector.db) out of any checkout. WorkingDirectory = projState; - ExecStart = "${pmProjector}/bin/pm-projector"; + # Bun 1.4.4 cannot adopt a systemd-passed listener. Keep the + # public port held by pm-projector.socket and proxy accepted + # connections to this private backend instead (#1822). + ExecStart = "${pkgs.coreutils}/bin/env PORT=${toString cfg.projectorBackendPort} ${pmProjector}/bin/pm-projector"; EnvironmentFile = "${projState}/.env"; Environment = [ "PM_AGG_FRESHNESS_URL=http://127.0.0.1:4871/publish" ]; Restart = "on-failure"; @@ -683,14 +692,14 @@ in }; }; - # Socket activation for the PM HTTP services (#1822, slice 1 of - # task 15848): the socket units are DEFINED here but deliberately - # NOT enabled (no Install.WantedBy) — today each service still - # binds its own ports, and enabling a socket for a port the - # service also binds would race it. Slice 2 teaches each service - # to accept the systemd-passed fd (LISTEN_FDS); slice 3 enables - # these sockets (Install.WantedBy = [ "sockets.target" ]) and - # drops each service's own bind in the same switch. Listen + # Socket activation for the PM HTTP services (#1822, task 15848): + # the socket units are DEFINED here but deliberately NOT enabled + # (no Install.WantedBy). pm-projector's socket now targets a + # socket-proxyd service because Bun 1.4.4 cannot consume LISTEN_FDS; + # its app listens on a private backend port. The other sockets stay + # inactive until their services have a working listener adapter. + # A later live-switch slice enables sockets and removes the public + # binds together. Listen # addresses mirror the services' live binds, verified on the # laptop 2026-11-14: projector 127.0.0.1:7801; aggregator # 127.0.0.1:4870 + tailnet:4870 + 127.0.0.1:4871 (the freshness @@ -701,7 +710,7 @@ in Unit.Description = "PM projector listener (#1822; inactive until slice 3)"; Socket = { ListenStream = "127.0.0.1:${toString cfg.projectorPort}"; - Service = "pm-projector.service"; + Service = "pm-projector-proxy.service"; }; }; @@ -730,6 +739,17 @@ in }; }; }; + + systemd.user.services.pm-projector-proxy = { + Unit = { + Description = "PM projector socket proxy (#1822)"; + Requires = [ "pm-projector.service" ]; + After = [ "pm-projector.service" ]; + }; + Service = { + ExecStart = "${config.systemd.package}/lib/systemd/systemd-socket-proxyd 127.0.0.1:${toString cfg.projectorBackendPort}"; + }; + }; }; }; } From f3e99c2413b1214cf613c06958b1c5c172005830 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 22:52:07 -0600 Subject: [PATCH 2/2] fix(pm-projector): coordinate socket and backend ports --- nixos/services/pm-stack/default.nix | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/nixos/services/pm-stack/default.nix b/nixos/services/pm-stack/default.nix index 425570766..989b08de3 100644 --- a/nixos/services/pm-stack/default.nix +++ b/nixos/services/pm-stack/default.nix @@ -204,6 +204,8 @@ in description = "Private loopback port for pm-projector behind its socket-activated proxy."; }; + projectorSocketActivation = lib.mkEnableOption "the pm-projector socket-proxy listener"; + aggregatorPort = lib.mkOption { type = lib.types.port; default = 4870; @@ -237,10 +239,12 @@ in # state dir as cwd also keeps the code's cwd-relative # fallback (file:data/projector.db) out of any checkout. WorkingDirectory = projState; - # Bun 1.4.4 cannot adopt a systemd-passed listener. Keep the - # public port held by pm-projector.socket and proxy accepted - # connections to this private backend instead (#1822). - ExecStart = "${pkgs.coreutils}/bin/env PORT=${toString cfg.projectorBackendPort} ${pmProjector}/bin/pm-projector"; + # Bun 1.4.4 cannot adopt a systemd-passed listener. Preserve + # the direct public bind until the socket is explicitly + # enabled; the socket and backend port switch together (#1822). + ExecStart = "${pkgs.coreutils}/bin/env PORT=${ + toString (if cfg.projectorSocketActivation then cfg.projectorBackendPort else cfg.projectorPort) + } ${pmProjector}/bin/pm-projector"; EnvironmentFile = "${projState}/.env"; Environment = [ "PM_AGG_FRESHNESS_URL=http://127.0.0.1:4871/publish" ]; Restart = "on-failure"; @@ -693,13 +697,13 @@ in }; # Socket activation for the PM HTTP services (#1822, task 15848): - # the socket units are DEFINED here but deliberately NOT enabled - # (no Install.WantedBy). pm-projector's socket now targets a - # socket-proxyd service because Bun 1.4.4 cannot consume LISTEN_FDS; - # its app listens on a private backend port. The other sockets stay + # the socket units are DEFINED here but deliberately NOT enabled by + # default. pm-projector's socket is enabled only by + # projectorSocketActivation, which also switches its app to the + # private backend port; this socket targets socket-proxyd because + # Bun 1.4.4 cannot consume LISTEN_FDS. The other sockets stay # inactive until their services have a working listener adapter. - # A later live-switch slice enables sockets and removes the public - # binds together. Listen + # Listen # addresses mirror the services' live binds, verified on the # laptop 2026-11-14: projector 127.0.0.1:7801; aggregator # 127.0.0.1:4870 + tailnet:4870 + 127.0.0.1:4871 (the freshness @@ -712,6 +716,7 @@ in ListenStream = "127.0.0.1:${toString cfg.projectorPort}"; Service = "pm-projector-proxy.service"; }; + Install.WantedBy = lib.optionals cfg.projectorSocketActivation [ "sockets.target" ]; }; pm-aggregator = {