# 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 `). 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]; }; }