Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nixos/hosts/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 —
Expand Down
132 changes: 132 additions & 0 deletions nixos/services/sandbox-link-tunnel/default.nix
Original file line number Diff line number Diff line change
@@ -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" ];
};
};
};
};
}
31 changes: 31 additions & 0 deletions nixos/services/sandbox-link-tunnel/sandbox-link-tunnel-watchdog.sh
Original file line number Diff line number Diff line change
@@ -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