Files
nixos-config/hosts/m3-hermes/services/t3code.nix
T

190 lines
8.3 KiB
Nix
Raw Normal View History

# 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 itself comes from m3ta-home's t3code module: it binds the
# current NetBird address of wt0 and opens the port on wt0 only. This file
# adds the hermes-specific agent runtime around it.
#
# 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 (m3ta-home overlay
# 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
# ── 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 (m3ta-home module, cockpit user) ─────────────────
# The harnesses, git and beads for the agents come from the system
# profile above (the module puts /run/current-system/sw on the path).
m3ta.t3code = {
enable = true;
user = cockpitUser;
port = config.m3ta.ports.get "t3code";
};
# ── 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 =
2026-10-31 16:20:18 +01:00
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;
};
}