Files
m3ta-home/overlays/t3code-agent-identity.nix
T

120 lines
4.4 KiB
Nix
Raw Normal View History

# T3 Code agents act as the m3ta-chiron bot on every host. Baked into the
# T3 packages rather than a session or service: everything T3 spawns
# inherits it, however T3 was launched (desktop launcher, systemd, shell),
# and any host installing T3 gets it. Must come AFTER shared-nixpkgs.
#
# Push auth needs ~/.ssh/m3ta-chiron on each host, with its pubkey on the
# m3ta-chiron account of each Gitea instance. A missing key fails the push
# instead of falling back to a personal key. Other hosts keep the user's own
# ssh config.
#
# Change requests need one agenix secret per instance, owned by the T3 user.
# Without it T3 cannot recognize that instance as Gitea; the others still work.
{tokenFiles ? {}}: final: prev: let
identity = {
name = "m3ta-chiron";
email = "m3ta-chiron@agentmail.to";
};
instances = [
{
name = "m3ta";
url = "https://code.m3ta.dev";
host = "code.m3ta.dev";
port = 22;
knownHost = "code.m3ta.dev";
hostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFmmqZ4GgDEXJgj8uYrZAr/EoLIA6LJGkTIyKGQD/60a";
}
{
name = "az";
url = "https://git.az-gruppe.com";
host = "git.az-gruppe.com";
port = 2022;
knownHost = "[git.az-gruppe.com]:2022";
hostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIItSijmU5YwcJcoshtmYxpxBaVA4TPaCMk23ws7KDkAH";
}
];
tokenFile = instance: tokenFiles.${instance.name} or "/run/agenix/agent-gitea-token-${instance.name}";
giteaKnownHosts = final.writeText "gitea-known-hosts" (
final.lib.concatMapStrings (instance: "${instance.knownHost} ${instance.hostKey}\n") instances
);
# ssh expands ~ from passwd, so this does not depend on $HOME.
sshConfig = final.writeText "t3-agent-ssh-config" ''
${final.lib.concatMapStrings (instance: ''
Host ${instance.host}
User gitea
Port ${toString instance.port}
IdentityFile ~/.ssh/m3ta-chiron
IdentitiesOnly yes
UserKnownHostsFile ${giteaKnownHosts}
'')
instances}
# Conditional: IdentityFile accumulates across matching blocks, so a
# personal entry for these hosts must not be read.
Host * ${final.lib.concatMapStringsSep " " (instance: "!${instance.host}") instances}
Include ~/.ssh/config
'';
# T3 only recognizes a Gitea on a custom domain through a matching tea
# login, and tea picks the login by the host of the git remote, so a token
# only reaches its own instance. The config lives in a private temp dir per
# call: tokens stay out of the nix store and a personal ~/.config/tea is left
# alone.
agentTea = final.writeShellApplication {
name = "tea";
text = ''
config="$(${final.coreutils}/bin/mktemp -d)"
trap '${final.coreutils}/bin/rm -rf "$config"' EXIT
${final.coreutils}/bin/mkdir "$config/tea"
default=true
${final.lib.concatMapStrings (instance: ''
if [ -r ${tokenFile instance} ]; then
if [ "$default" = true ]; then
printf 'logins:\n' >"$config/tea/config.yml"
fi
printf ' - name: %s\n url: %s\n token: %s\n user: %s\n default: %s\n' \
${instance.name} ${instance.url} "$(${final.coreutils}/bin/cat ${tokenFile instance})" \
${identity.name} "$default" >>"$config/tea/config.yml"
default=false
fi
'')
instances}
XDG_CONFIG_HOME="$config" ${final.tea}/bin/tea "$@"
'';
};
wrapperArgs = final.lib.concatStringsSep " " (final.lib.mapAttrsToList (name: value: "--set ${name} ${final.lib.escapeShellArg value}") {
GIT_AUTHOR_NAME = identity.name;
GIT_AUTHOR_EMAIL = identity.email;
GIT_COMMITTER_NAME = identity.name;
GIT_COMMITTER_EMAIL = identity.email;
JJ_USER = identity.name;
JJ_EMAIL = identity.email;
# bd otherwise falls back to the user's git user.name for its audit trail.
BEADS_ACTOR = identity.name;
GIT_SSH_COMMAND = "${final.openssh}/bin/ssh -F ${sshConfig}";
});
wrapT3 = package:
final.symlinkJoin {
inherit (package) name meta passthru;
paths = [package];
nativeBuildInputs = [final.makeWrapper];
postBuild = ''
for program in $out/bin/*; do
wrapProgram "$program" ${wrapperArgs} --prefix PATH : ${agentTea}/bin
done
'';
};
in {
llm-agents =
prev.llm-agents
// {
t3code = wrapT3 prev.llm-agents.t3code;
t3code-desktop = wrapT3 prev.llm-agents.t3code-desktop;
};
}