Files
nixos-config/hosts/m3-hermes/services/t3code.nix
T
Sascha KönigandClaude Opus 5.5 09f509c37b refactor(secrets): rename agent Gitea tokens per instance, add work token for m3-kratos
hermes-gitea-token.age is renamed unchanged to agent-gitea-token-m3ta.age. agent-gitea-token-az.age carries the m3ta-chiron token for git.az-gruppe.com, for m3-kratos only. Also bumps m3ta-home to the T3 Code module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 12:27:54 +01:00

230 lines
9.4 KiB
Nix

# T3 Code server (https://t3.codes) — interactive coding-agent cockpit.
#
# Part of the three-lane agent setup (agreed 2026-10-26):
# - Lane 1, interactive cockpit: T3 Code on m3-hermes, reached over the
# NetBird mesh via direct pairing (no T3 Connect cloud relay).
# - Lane 2, autonomous workers: herdr sandbox (see herdr-sandbox.nix).
# - Lane 3, control & inbox: Hermes via Matrix (unchanged).
#
# Flow: T3 desktop/mobile app ──NetBird mesh──▶ wt0 :3773 (this server).
# The server binds 0.0.0.0, but the firewall admits the port for mesh
# peers only — same posture as ai-memory and the hermes dashboard.
#
# Agent runtime for the T3 harnesses (OpenCode / Claude Code, 2026-10-31):
# - Skills/commands/agents via agent-lib v2: a timer syncs the work repo
# (gitea m3tam3re/agent-lib — anonymously readable over HTTPS, verified,
# so no deploy token) into ~/.agents/skills + ~/.config/opencode/*. A
# path unit mirrors skills into ~/.claude/skills for Claude Code
# (port of m3ta-home profiles/sets/coding/agents/claude-skills.nix).
# - Git identity and Gitea push auth: m3ta-chiron for every harness spawned
# by T3, baked into the package (overlays/default.nix,
# t3code-agent-identity), so m3tam3re's interactive git is untouched.
# - Gitea SSH: m3tam3re's ssh config pins User gitea + key ~/.ssh/gitea
# (one-time keygen; pubkey added to the m3ta-chiron account) for
# interactive use and git+ssh:// flake inputs. Host key pinned system-wide.
# - Gitea change requests: tea with the agent-gitea-token-m3ta agenix secret,
# baked into the same package.
#
# One-time interactive steps after deploy (deliberately NOT declarative —
# subscription auth must live in each harness's own credential store and
# may not be shared with other tools per provider ToS):
# - Pair clients: `sudo -u m3tam3re t3 pair` (or take the pairing URL
# from `journalctl -u t3code` after first start) and add the
# environment in the T3 app via the printed URL/QR code.
# - Claude Code: `sudo -u m3tam3re claude auth login`
# (persists in /home/m3tam3re/.claude).
# - OpenCode: `sudo -u m3tam3re opencode auth login`
# (subscription auth persists in OpenCode's own state dir).
# - Gitea push key for the T3 lane (a private key must not live in the
# nix store, so imperative like the harness auths above):
# sudo -u m3tam3re ssh-keygen -t ed25519 -N "" \
# -C chiron-t3-m3-hermes -f /home/m3tam3re/.ssh/gitea
# Then add the pubkey to the m3ta-chiron account on code.m3ta.dev
# (m3ta-chiron → Settings → SSH / GPG Keys → "chiron-t3-m3-hermes").
# Verify: sudo -u m3tam3re ssh -T gitea@code.m3ta.dev
{
config,
pkgs,
...
}: let
# Mesh-only via interface match on wt0 (NetBird) — an IPv4 CIDR in -s
# would abort the ip6tables run of ip46tables and fail the unit.
port = config.m3ta.ports.get "t3code";
# ── Lane-1 agent runtime ─────────────────────────────────────────────
cockpitUser = "m3tam3re";
cockpitHome = "/home/${cockpitUser}";
# SSH to the private Gitea for the T3 lane. Sits in m3tam3re's ssh
# config (per-user — Hermes' own gitea access via its own ssh config
# is unaffected). URL-embedded users (gitea@code.m3ta.dev) match the
# Host block, so IdentityFile applies to git remotes and flake inputs
# alike.
t3SshConfig = pkgs.writeText "t3-gitea-ssh-config" ''
Host code.m3ta.dev
User gitea
IdentityFile ${cockpitHome}/.ssh/gitea
IdentitiesOnly yes
'';
# Mirror ~/.agents/skills → ~/.claude/skills so Claude Code sees the
# same skills OpenCode does. Per-skill symlinks: Claude's own entries
# and hand-made skills stay untouched, vanished skills get unlinked.
claudeSkillsSync = pkgs.writeShellApplication {
name = "t3-claude-skills-sync";
runtimeInputs = [pkgs.coreutils];
text = ''
src="${cockpitHome}/.agents/skills"
dst="${cockpitHome}/.claude/skills"
mkdir -p "$src" "$dst"
# Drop links whose target is gone.
for link in "$dst"/*; do
[ -L "$link" ] || continue
case "$(readlink "$link")" in
"$src"/*) [ -e "$link" ] || rm -f "$link" ;;
esac
done
# Link every skill folder; never replace a real dir or foreign link.
for skill in "$src"/*/; do
skill="''${skill%/}"
[ -f "$skill/SKILL.md" ] || continue
name="$(basename "$skill")"
target="$dst/$name"
if [ -L "$target" ] || [ ! -e "$target" ]; then
ln -sfn "$skill" "$target"
fi
done
'';
};
in {
# CLI on the system for interactive admin (t3 pair, t3 service status)
# plus the harnesses T3 Code orchestrates. Login shells need them for
# the one-time auth flows documented in the header. agent-lib enables
# interactive syncs by the cockpit user (`agent-lib sync` / `status`).
environment.systemPackages = [
pkgs.llm-agents.t3code
pkgs.llm-agents.opencode2
pkgs.llm-agents.claude-code
pkgs.llm-agents.beads
pkgs.agent-lib
];
# ── T3 Code server (system service, cockpit user) ────────────────────
systemd.services.t3code = {
description = "T3 Code server (interactive coding-agent cockpit)";
after = ["network-online.target"];
wants = ["network-online.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
User = "m3tam3re";
Group = "users";
WorkingDirectory = "/home/m3tam3re";
ExecStart = "${pkgs.llm-agents.t3code}/bin/t3 serve --host 0.0.0.0";
Restart = "on-failure";
RestartSec = "5";
};
environment = {
T3CODE_PORT = toString port;
# Deterministic service context: HOME explicit so tools resolve ~ in
# every process this service spawns, however deep t3's pty/sandbox
# chain resets the environment.
HOME = cockpitHome;
};
# Harness CLIs, git and the issue tracker for the agents T3 Code spawns.
path = with pkgs; [
git
openssh
llm-agents.opencode2
llm-agents.claude-code
llm-agents.beads
];
};
# ── Gitea (code.m3ta.dev) for the T3 lane ────────────────────────────
# Host key pinned system-wide: non-interactive SSH (git clone, nix
# flake git+ssh inputs) works for every user without a first-connect
# prompt. Read-only fact, safe to share across users.
environment.etc."ssh/ssh_known_hosts".text = ''
code.m3ta.dev ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFmmqZ4GgDEXJgj8uYrZAr/EoLIA6LJGkTIyKGQD/60a
'';
# Per-user SSH config (User gitea + key, see t3SshConfig). NOTE: L+
# replaces a pre-existing real file — none existed at PR time. If
# m3tam3re ever grows his own ssh config here, fold the Host block in
# by hand and drop this rule.
systemd.tmpfiles.rules = [
"d ${cockpitHome}/.ssh 0700 ${cockpitUser} users -"
"L+ ${cockpitHome}/.ssh/config - - - - ${t3SshConfig}"
# The fleet-wide key path the T3 package expects (see t3code-agent-identity).
"L+ ${cockpitHome}/.ssh/m3ta-chiron - - - - ${cockpitHome}/.ssh/gitea"
];
# ── agent-lib v2 client: skills/commands/agents for the harnesses ────
# Fleet rollout pattern (agent-lib docs/fleet-handoff.md): one
# admin-protected config at /etc/agent-lib/config.json, root-owned and
# world-readable (mode 0644 by default). The work repo is anonymously
# readable over HTTPS (verified 2026-10-31) — no token in the config.
environment.etc."agent-lib/config.json".text =
builtins.toJSON {repo_url = "https://code.m3ta.dev/m3tam3re/agent-prod.git";};
# Sync runs in the cockpit user's context (deployment targets resolve
# against $HOME) and mirrors skills into Claude Code right after.
systemd.services.agent-lib-sync = {
description = "agent-lib sync — deploy work repo into T3 agent runtime paths";
after = ["network-online.target"];
wants = ["network-online.target"];
serviceConfig = {
Type = "oneshot";
User = cockpitUser;
Group = "users";
ExecStart = "${pkgs.agent-lib}/bin/agent-lib sync";
ExecStartPost = "${claudeSkillsSync}/bin/t3-claude-skills-sync";
};
environment.HOME = cockpitHome;
};
systemd.timers.agent-lib-sync = {
description = "agent-lib sync every 2h (skills for T3 Code agents)";
wantedBy = ["timers.target"];
timerConfig = {
OnBootSec = "5min";
OnUnitActiveSec = "2h";
Persistent = true;
};
};
# Catch manual `agent-lib sync` runs by the cockpit user as well.
systemd.paths.t3-claude-skills-sync = {
description = "Watch ~/.agents/skills for changes (mirror to Claude Code)";
wantedBy = ["multi-user.target"];
pathConfig.PathChanged = "${cockpitHome}/.agents/skills";
};
systemd.services.t3-claude-skills-sync = {
description = "Mirror ~/.agents/skills into ~/.claude/skills (Claude Code)";
serviceConfig = {
Type = "oneshot";
User = cockpitUser;
Group = "users";
ExecStart = "${claudeSkillsSync}/bin/t3-claude-skills-sync";
};
environment.HOME = cockpitHome;
};
networking.firewall = {
extraCommands = ''
# Allow t3code only from Netbird mesh VPN
ip46tables -A nixos-fw -p tcp --dport ${toString port} -i wt0 -j nixos-fw-accept
'';
extraStopCommands = ''
ip46tables -D nixos-fw -p tcp --dport ${toString port} -i wt0 -j nixos-fw-accept 2>/dev/null || true
'';
};
}