From dc57c37d192dea5a4f1eaf2a0bc23e0f5d5bba4d Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Sun, 15 Nov 2026 15:35:02 -0600 Subject: [PATCH] =?UTF-8?q?nixos:=20sandbox-link-tunnel=20service=20?= =?UTF-8?q?=E2=80=94=20Cloudflare=20connector=20for=20the=20sandbox<->lapt?= =?UTF-8?q?op=20control=20path=20(code#10356)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirrors the lab-tunnel module: home-manager user service running cloudflared (http2, metrics 127.0.0.1:20244, --token-file ~/.cloudflared-sandbox-link/tunnel.token) plus the /ready watchdog service+timer. Enabled for host amd. Ingress stays Cloudflare-managed: sandbox-link.security.cab -> the laptop-agent daemon (Access service-token gated), relay.security.cab -> the iroh relay. Activates at the next switch; an interim user unit of the same name already runs the connector and is shadowed by this unit when it lands. --- nixos/hosts/default.nix | 8 ++ .../services/sandbox-link-tunnel/default.nix | 132 ++++++++++++++++++ .../sandbox-link-tunnel-watchdog.sh | 31 ++++ 3 files changed, 171 insertions(+) create mode 100644 nixos/services/sandbox-link-tunnel/default.nix create mode 100644 nixos/services/sandbox-link-tunnel/sandbox-link-tunnel-watchdog.sh diff --git a/nixos/hosts/default.nix b/nixos/hosts/default.nix index f0f0694277..4d152ad986 100644 --- a/nixos/hosts/default.nix +++ b/nixos/hosts/default.nix @@ -25,6 +25,7 @@ in "services/id-mail" "services/pm-stack" "services/lab-tunnel" + "services/sandbox-link-tunnel" "services/pm-apps" "services/power-guard" "services/openai-cred-watchdog" @@ -71,6 +72,13 @@ in # edge status service for lab.security.cab. services.lab-tunnel.enable = true; } + { + # Sandbox link Cloudflare Tunnel (code#10356): connector for + # sandbox-link.security.cab — the sandbox -> laptop-agent + # control path rides it as primary transport; Tailscale is + # the client's fallback. + services.sandbox-link-tunnel.enable = true; + } { # PM app servers (code#1752): Kaneo + PostgreSQL, Grist, and # Pad as oci-containers, ready-for-agent as a user service — diff --git a/nixos/services/sandbox-link-tunnel/default.nix b/nixos/services/sandbox-link-tunnel/default.nix new file mode 100644 index 0000000000..44282a6df4 --- /dev/null +++ b/nixos/services/sandbox-link-tunnel/default.nix @@ -0,0 +1,132 @@ +# Sandbox link Cloudflare Tunnel (code#10356). Drew's transport +# ruling, 2026-11-15: "stop using tailscale. point the sandbox at +# cloudflare point laptop at cloudflare tunnel." The sandbox <-> +# laptop fleet control path (laptop-agent) rides this tunnel as its +# primary transport; the Tailscale session proxy remains the client's +# automatic fallback, not the primary. +# +# A third, separate remotely-managed tunnel named "sandbox-link" +# (722f72ba-b27a-42a8-b06a-9b72ed2f8e8c), distinct from the pm stack's +# "amd-6-pm-stack" and the lab's "lab-cluster" tunnels, so its +# ingress, credentials, and connector lifecycle never entangle with +# theirs. The connector pattern is lab-tunnel's, verbatim in shape: +# home-manager user service, cloudflared from this flake's nixpkgs, +# token from an operator-rendered file, --protocol http2 (the QUIC +# default kept dropping on this host's network path — #5647). +# +# Ingress is managed in Cloudflare (config source: cloudflare), not +# here, exactly like the other tunnels: +# sandbox-link.security.cab -> http://100.99.113.103:4799 +# (the laptop-agent daemon, which binds the tailnet IP only; the +# connector dials it locally. The hostname is behind Cloudflare +# Access with a service-token policy — the sandbox client sends +# the Access headers plus the daemon's own X-Agent-Token.) +# relay.security.cab -> http://localhost:3340 +# (the stock iroh relay from the code#10391 probes; a policy +# choice, kept on this tunnel's config — do not drop the row +# when editing ingress.) +# (catch-all) -> http_status:404 +# DNS: CNAME sandbox-link.security.cab -> +# 722f72ba-...cfargotunnel.com (proxied) in the security.cab zone. +# +# Runtime state + the secret stay on the machine, never in this repo: +# ~/.cloudflared-sandbox-link/tunnel.token (mode 600) carries the raw +# tunnel token, rendered by the operator from BWS +# (cloudflare.tunnel.sandbox-link-token) and read by cloudflared via +# --token-file (#8933: the token never enters the connector's argv). +# +# Until the switch that first evaluates this module, an interim user +# unit of the same name in ~/.local/share/systemd/user/ runs the +# connector; this module's unit shadows it by systemd unit search +# order the moment it lands, and the interim file can then be +# removed. +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.services.sandbox-link-tunnel; + cloudflared = "${pkgs.cloudflared}/bin/cloudflared"; + + # The port cloudflared serves its metrics + /ready endpoint on + # (loopback only; pm-tunnel uses 20242, lab-tunnel 20243). The + # watchdog below reads /ready from it, mirroring pm-tunnel's #6992 + # recovery path (#8933). + tunnelMetricsPort = 20244; + + tunnelWatchdog = pkgs.writeShellScript "sandbox-link-tunnel-watchdog" ( + lib.replaceStrings + [ "@curl@" "@systemctl@" "@readyUrl@" ] + [ + "${pkgs.curl}/bin/curl" + "/run/current-system/sw/bin/systemctl" + "http://127.0.0.1:${toString tunnelMetricsPort}/ready" + ] + (builtins.readFile ./sandbox-link-tunnel-watchdog.sh) + ); + home = "/home/${cfg.user}"; + stateDir = "${home}/.cloudflared-sandbox-link"; +in +{ + options.services.sandbox-link-tunnel = { + enable = lib.mkEnableOption "the sandbox link Cloudflare Tunnel connector as a NixOS-managed user service"; + + user = lib.mkOption { + type = lib.types.str; + default = "user"; + description = "The user whose home-manager session runs the sandbox link tunnel services."; + }; + }; + + config = lib.mkIf cfg.enable { + home-manager.users.${cfg.user} = { + systemd.user.services = { + sandbox-link-tunnel = { + Unit = { + Description = "Cloudflare Tunnel connector for the sandbox link — sandbox-link.security.cab ingress to the laptop-agent daemon (NixOS)"; + After = [ "network-online.target" ]; + Wants = [ "network-online.target" ]; + }; + Service = { + Type = "simple"; + # tunnel.token is operator-rendered from BWS + # (cloudflare.tunnel.sandbox-link-token) as a raw-token + # file; cloudflared reads it via --token-file so the + # token stays out of the process argv (#8933). + # --protocol http2 (#5647): the default QUIC transport + # kept dropping on this host's network path. + ExecStart = "${cloudflared} tunnel --no-autoupdate --protocol http2 --metrics 127.0.0.1:${toString tunnelMetricsPort} run --token-file ${stateDir}/tunnel.token"; + Restart = "on-failure"; + RestartSec = 5; + }; + Install.WantedBy = [ "default.target" ]; + }; + + sandbox-link-tunnel-watchdog = { + Unit = { + Description = "sandbox-link-tunnel watchdog — restart sandbox-link-tunnel when its edge connections stay down (#8933, mirroring pm-tunnel #6992)"; + After = [ "sandbox-link-tunnel.service" ]; + }; + Service = { + Type = "oneshot"; + ExecStart = "${tunnelWatchdog}"; + }; + }; + }; + + systemd.user.timers = { + sandbox-link-tunnel-watchdog = { + Unit.Description = "Check sandbox-link-tunnel edge readiness every 2 minutes (#8933)"; + Timer = { + OnBootSec = "2min"; + OnUnitActiveSec = "2min"; + Unit = "sandbox-link-tunnel-watchdog.service"; + }; + Install.WantedBy = [ "timers.target" ]; + }; + }; + }; + }; +} diff --git a/nixos/services/sandbox-link-tunnel/sandbox-link-tunnel-watchdog.sh b/nixos/services/sandbox-link-tunnel/sandbox-link-tunnel-watchdog.sh new file mode 100644 index 0000000000..d81b38f3b7 --- /dev/null +++ b/nixos/services/sandbox-link-tunnel/sandbox-link-tunnel-watchdog.sh @@ -0,0 +1,31 @@ +# sandbox-link-tunnel watchdog: restart the connector when its edge +# connections stay down (#8933, mirroring pm-tunnel's #6992 watchdog +# and lab-tunnel's identical script). +# +# cloudflared can stay process-alive with no live edge connections; +# its own /ready endpoint is the truth (200 = at least one live edge +# connection). Three consecutive failures (the timer runs every 2 +# minutes) restart sandbox-link-tunnel; any success resets the +# counter. +# +# Placeholders @curl@, @systemctl@ and @readyUrl@ are substituted by +# the NixOS module (nixos/services/sandbox-link-tunnel/default.nix, +# via lib.replaceStrings on this file) — keep them intact. +stateDir="$HOME/.local/state/sandbox-link-tunnel-watchdog" +mkdir -p "$stateDir" +countFile="$stateDir/consecutive-failures" +count=0 +if [ -f "$countFile" ]; then + count=$(cat "$countFile") +fi +if @curl@ -fsS -m 10 "@readyUrl@" >/dev/null 2>&1; then + echo 0 >"$countFile" + exit 0 +fi +count=$((count + 1)) +echo "$count" >"$countFile" +echo "sandbox-link-tunnel-watchdog: /ready failed ($count consecutive)" >&2 +if [ "$count" -ge 3 ]; then + echo 0 >"$countFile" + exec @systemctl@ --user restart sandbox-link-tunnel.service +fi