feat(t3code): add NixOS module bound to the NetBird interface
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d4a39dc74
commit
f683e8bf43
4 files changed
+226
-2
No files matched your search
@@ -50,6 +50,10 @@
|
|||||||
selfPath = self.outPath;
|
selfPath = self.outPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# NixOS modules — import in nixos-config via:
|
||||||
|
# imports = [ inputs.m3ta-home.nixosModules.t3code ];
|
||||||
|
nixosModules.t3code = ./modules/nixos/t3code.nix;
|
||||||
|
|
||||||
# Library functions — use in nixos-config via:
|
# Library functions — use in nixos-config via:
|
||||||
# m3ta-lib = inputs.m3ta-home.lib;
|
# m3ta-lib = inputs.m3ta-home.lib;
|
||||||
# imports = [ (m3ta-lib.mkHome { ... }) ];
|
# imports = [ (m3ta-lib.mkHome { ... }) ];
|
||||||
@@ -58,7 +62,11 @@
|
|||||||
selfPath = self.outPath;
|
selfPath = self.outPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
# Overlays (re-exports from m3ta-nixpkgs)
|
# Overlays (re-exports from m3ta-nixpkgs, plus our own)
|
||||||
overlays = inputs.m3ta-nixpkgs.overlays;
|
overlays =
|
||||||
|
inputs.m3ta-nixpkgs.overlays
|
||||||
|
// {
|
||||||
|
t3code-agent-identity = import ./overlays/t3code-agent-identity.nix {};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Start T3 Code on the interface's current IPv4 address. The address changes
|
||||||
|
# with the NetBird profile, so it cannot be a Nix value.
|
||||||
|
# Usage: t3code-serve.sh <interface> <t3 binary>
|
||||||
|
interface="$1"
|
||||||
|
t3="$2"
|
||||||
|
timeout="${T3CODE_ADDR_TIMEOUT:-60}"
|
||||||
|
|
||||||
|
address=""
|
||||||
|
for ((waited = 0; waited < timeout; waited++)); do
|
||||||
|
line="$(ip -4 -o addr show dev "$interface" 2>/dev/null || true)"
|
||||||
|
if [[ "$line" =~ inet[[:space:]]+([0-9.]+)/ ]]; then
|
||||||
|
address="${BASH_REMATCH[1]}"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ -z "$address" ]]; then
|
||||||
|
echo "no IPv4 address on $interface after ${timeout}s" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "$t3" serve --host "$address"
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
# T3 Code server, reachable from the active NetBird network only.
|
||||||
|
#
|
||||||
|
# The service follows the mesh interface: it stops when NetBird removes the
|
||||||
|
# interface and starts again with the new address when a profile comes up
|
||||||
|
# (`netbird up --profile <name>`). It owns ~/.t3, so a desktop app on the same
|
||||||
|
# host must connect as a client with its local environment off.
|
||||||
|
#
|
||||||
|
# Wanted by the device rather than multi-user.target, so a host without NetBird
|
||||||
|
# connection does not wait for it at boot. A first deploy with the interface
|
||||||
|
# already up needs one `systemctl start t3code`.
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
utils,
|
||||||
|
...
|
||||||
|
}: let
|
||||||
|
cfg = config.m3ta.t3code;
|
||||||
|
device = "${utils.escapeSystemdPath "/sys/subsystem/net/devices/${cfg.interface}"}.device";
|
||||||
|
home = config.users.users.${cfg.user}.home;
|
||||||
|
|
||||||
|
serve = pkgs.writeShellApplication {
|
||||||
|
name = "t3code-serve";
|
||||||
|
runtimeInputs = [pkgs.iproute2 pkgs.coreutils];
|
||||||
|
text = builtins.readFile ./t3code-serve.sh;
|
||||||
|
};
|
||||||
|
in {
|
||||||
|
options.m3ta.t3code = {
|
||||||
|
enable = lib.mkEnableOption "the T3 Code server on the NetBird interface";
|
||||||
|
|
||||||
|
user = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
description = "User running the server; the harnesses come from this user's profile.";
|
||||||
|
example = "m3tam3re";
|
||||||
|
};
|
||||||
|
|
||||||
|
port = lib.mkOption {
|
||||||
|
type = lib.types.port;
|
||||||
|
default = 3773;
|
||||||
|
description = "Port for the HTTP/WebSocket server.";
|
||||||
|
};
|
||||||
|
|
||||||
|
interface = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "wt0";
|
||||||
|
description = "NetBird interface to bind to and open the port on.";
|
||||||
|
};
|
||||||
|
|
||||||
|
package = lib.mkOption {
|
||||||
|
type = lib.types.package;
|
||||||
|
default = pkgs.llm-agents.t3code;
|
||||||
|
defaultText = lib.literalExpression "pkgs.llm-agents.t3code";
|
||||||
|
description = "T3 Code package.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf cfg.enable {
|
||||||
|
assertions = [
|
||||||
|
{
|
||||||
|
assertion = config.services.netbird.enable;
|
||||||
|
message = "m3ta.t3code requires services.netbird.enable";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
systemd.services.t3code = {
|
||||||
|
description = "T3 Code server";
|
||||||
|
after = [device "netbird.service"];
|
||||||
|
bindsTo = [device];
|
||||||
|
wantedBy = [device];
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
User = cfg.user;
|
||||||
|
Group = "users";
|
||||||
|
WorkingDirectory = home;
|
||||||
|
ExecStart = "${serve}/bin/t3code-serve ${cfg.interface} ${cfg.package}/bin/t3";
|
||||||
|
Restart = "on-failure";
|
||||||
|
RestartSec = "5";
|
||||||
|
};
|
||||||
|
|
||||||
|
environment = {
|
||||||
|
T3CODE_PORT = toString cfg.port;
|
||||||
|
HOME = home;
|
||||||
|
};
|
||||||
|
|
||||||
|
# The harnesses come from the user's profile, not from the service.
|
||||||
|
path = [
|
||||||
|
"/etc/profiles/per-user/${cfg.user}"
|
||||||
|
"/run/current-system/sw"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
networking.firewall.interfaces.${cfg.interface}.allowedTCPPorts = [cfg.port];
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Eval and start-script checks for nixosModules.t3code.
|
||||||
|
# Usage: t3code.test.sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
here="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
repo="$(cd "$here/../.." && pwd)"
|
||||||
|
fail=0
|
||||||
|
|
||||||
|
check() { # name, got, want
|
||||||
|
if [[ "$2" == "$3" ]]; then echo "ok $1"; else echo "FAIL $1: got '$2', want '$3'"; fail=1; fi
|
||||||
|
}
|
||||||
|
contains() { # name, haystack, needle
|
||||||
|
if [[ "$2" == *"$3"* ]]; then echo "ok $1"; else echo "FAIL $1: missing '$3' in '$2'"; fail=1; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# ev <module-attrs> <expression over `config`>; prints JSON, or the error on stderr.
|
||||||
|
ev() {
|
||||||
|
nix eval --impure --json --expr "
|
||||||
|
let
|
||||||
|
flake = builtins.getFlake \"path:$repo\";
|
||||||
|
system = flake.inputs.nixpkgs.lib.nixosSystem {
|
||||||
|
modules = [
|
||||||
|
flake.nixosModules.t3code
|
||||||
|
({pkgs, ...}: {
|
||||||
|
nixpkgs.hostPlatform = \"x86_64-linux\";
|
||||||
|
nixpkgs.overlays = [(final: prev: {llm-agents.t3code = final.hello;})];
|
||||||
|
boot.loader.grub.enable = false;
|
||||||
|
fileSystems.\"/\" = {device = \"none\"; fsType = \"tmpfs\";};
|
||||||
|
system.stateVersion = \"25.05\";
|
||||||
|
users.users.alice = {isNormalUser = true; home = \"/home/alice\";};
|
||||||
|
})
|
||||||
|
($1)
|
||||||
|
];
|
||||||
|
};
|
||||||
|
config = system.config;
|
||||||
|
in $2"
|
||||||
|
}
|
||||||
|
|
||||||
|
on='{services.netbird.enable = true; m3ta.t3code = {enable = true; user = "alice";};}'
|
||||||
|
dev="sys-subsystem-net-devices-wt0.device"
|
||||||
|
|
||||||
|
check "bindsTo" "$(ev "$on" 'config.systemd.services.t3code.bindsTo')" "[\"$dev\"]"
|
||||||
|
check "wantedBy" "$(ev "$on" 'config.systemd.services.t3code.wantedBy')" "[\"$dev\"]"
|
||||||
|
contains "after device" "$(ev "$on" 'config.systemd.services.t3code.after')" "$dev"
|
||||||
|
contains "after netbird" "$(ev "$on" 'config.systemd.services.t3code.after')" "netbird.service"
|
||||||
|
check "user" "$(ev "$on" 'config.systemd.services.t3code.serviceConfig.User')" '"alice"'
|
||||||
|
check "port env" "$(ev "$on" 'config.systemd.services.t3code.environment.T3CODE_PORT')" '"3773"'
|
||||||
|
check "home env" "$(ev "$on" 'config.systemd.services.t3code.environment.HOME')" '"/home/alice"'
|
||||||
|
check "firewall" "$(ev "$on" 'config.networking.firewall.interfaces.wt0.allowedTCPPorts')" '[3773]'
|
||||||
|
contains "path has user profile" \
|
||||||
|
"$(ev "$on" 'map toString config.systemd.services.t3code.path')" "/etc/profiles/per-user/alice"
|
||||||
|
|
||||||
|
custom='{services.netbird.enable = true; m3ta.t3code = {enable = true; user = "alice"; port = 4000; interface = "nb-x";};}'
|
||||||
|
check "custom port" "$(ev "$custom" 'config.networking.firewall.interfaces."nb-x".allowedTCPPorts')" '[4000]'
|
||||||
|
contains "custom interface" "$(ev "$custom" 'config.systemd.services.t3code.bindsTo')" 'nb\\x2dx.device'
|
||||||
|
|
||||||
|
check "disabled has no service" \
|
||||||
|
"$(ev '{services.netbird.enable = true;}' 'config.systemd.services ? t3code')" "false"
|
||||||
|
|
||||||
|
err="$(ev '{m3ta.t3code = {enable = true; user = "alice";};}' 'config.system.build.toplevel.drvPath' 2>&1 || true)"
|
||||||
|
contains "netbird assertion" "$err" "m3ta.t3code requires services.netbird.enable"
|
||||||
|
|
||||||
|
err="$(ev '{services.netbird.enable = true; m3ta.t3code.enable = true;}' 'config.systemd.services.t3code.serviceConfig.User' 2>&1 || true)"
|
||||||
|
contains "user is required" "$err" "m3ta.t3code.user"
|
||||||
|
|
||||||
|
# ── Start script, with stubs for ip and t3 ──
|
||||||
|
stubs="$(mktemp -d)"
|
||||||
|
trap 'rm -rf "$stubs"' EXIT
|
||||||
|
cat >"$stubs/t3" <<'EOF'
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
echo "t3 $*"
|
||||||
|
EOF
|
||||||
|
chmod +x "$stubs/t3"
|
||||||
|
|
||||||
|
run() { # ip stub body
|
||||||
|
cat >"$stubs/ip" <<EOF
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
$1
|
||||||
|
EOF
|
||||||
|
chmod +x "$stubs/ip"
|
||||||
|
PATH="$stubs:$PATH" T3CODE_ADDR_TIMEOUT=1 bash "$here/t3code-serve.sh" wt0 "$stubs/t3" 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
check "binds the interface address" \
|
||||||
|
"$(run 'echo "5: wt0 inet 100.1.2.3/16 brd 100.1.255.255 scope global wt0"')" "t3 serve --host 100.1.2.3"
|
||||||
|
|
||||||
|
set +e
|
||||||
|
out="$(run 'exit 0')"; rc=$?
|
||||||
|
set -e
|
||||||
|
check "no address exits 1" "$rc" "1"
|
||||||
|
if [[ "$out" == *"t3 serve"* ]]; then echo "FAIL no address must not start t3: $out"; fail=1; else echo "ok no address does not start t3"; fi
|
||||||
|
|
||||||
|
set +e
|
||||||
|
out="$(run 'echo "Device does not exist" >&2; exit 1')"; rc=$?
|
||||||
|
set -e
|
||||||
|
check "missing interface exits 1" "$rc" "1"
|
||||||
|
|
||||||
|
exit "$fail"
|
||||||
Reference in new issue
Block a user