diff --git a/README.md b/README.md index 23e3489..cda7995 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,15 @@ The repository contains system and user configuration as a Nix flake. Jujutsu (j nh os switch # rebuild & switch (uses ~/dotfiles) nixos-rebuild switch --flake .#kiwi # explicit host nix build .#wakuna-image # SD image for wakuna +nix build .#albedo-config # albedo config + skills for an image +nix run .#albedo-seed -- --help # install both into a home nix fmt # format (nixfmt, nufmt, biome) ``` `nh os switch` rebuilds and switches for the current host and assumes the repository at `~/dotfiles`. `nixos-rebuild switch` targets an explicit host. `nix build` produces the `wakuna` image. `nix fmt` formats the repository. +The MCP servers and skills under `modules/agents` are not only for the hosts here: `.#albedo-config` renders them into `/etc/albedo` (`extensions.json` plus the shared skills) and `.#albedo-seed` installs that tree into a home, merging into what albedo already wrote. An install without home-manager — the albedo image built in the infra repository — carries both and runs the seed before the daemon starts. + ## Repository structure ``` diff --git a/flake-parts.nix b/flake-parts.nix index 26bfdb6..4343bb9 100644 --- a/flake-parts.nix +++ b/flake-parts.nix @@ -13,22 +13,44 @@ imports = [ ./systems/default.nix ]; - perSystem = { pkgs, lib, ... }: { - packages = lib.packagesFromDirectoryRecursive { - inherit (pkgs) callPackage; - directory = ./pkgs; + perSystem = + { + pkgs, + lib, + self', + ... + }: + let + # albedo's config and skills for an install home-manager does not manage: + # the image the infra repo builds carries both + # (modules/agents/albedo-config.nix). + albedoInstall = import ./modules/agents/albedo-config.nix { + inherit lib pkgs self'; + # The image this feeds has no browser for the DevTools server to drive. + without = [ "chrome-devtools" ]; + }; + in + { + packages = + lib.packagesFromDirectoryRecursive { + inherit (pkgs) callPackage; + directory = ./pkgs; + } + // { + albedo-config = albedoInstall.config; + albedo-seed = albedoInstall.seed; + }; + formatter = pkgs.treefmt; + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + sops + treefmt + nixfmt + nufmt + oxfmt + ]; + }; }; - formatter = pkgs.treefmt; - devShells.default = pkgs.mkShell { - packages = with pkgs; [ - sops - treefmt - nixfmt - nufmt - oxfmt - ]; - }; - }; flake = { homeModules = { diff --git a/modules/agents/agent-skills.nix b/modules/agents/agent-skills.nix new file mode 100644 index 0000000..877a4a8 --- /dev/null +++ b/modules/agents/agent-skills.nix @@ -0,0 +1,19 @@ +# The agent skills every agent shares, as one directory. OpenCode and albedo +# both read the portable ~/.agents/skills, so it is built once here and linked +# in by whichever install delivers it: home-manager (./skills.nix) or an image +# (./albedo-config.nix). +{ pkgs }: +# symlinkJoin merges directory contents, so each skill must sit in its own +# subdirectory; a SKILL.md at the root shadows every other skill. +pkgs.symlinkJoin { + name = "agent-skills"; + paths = [ + ./skills + (pkgs.linkFarm "tuicr-skill" [ + { + name = "tuicr/SKILL.md"; + path = ../dev/tools/tuicr/SKILL.md; + } + ]) + ]; +} diff --git a/modules/agents/albedo-config.nix b/modules/agents/albedo-config.nix new file mode 100644 index 0000000..c83c306 --- /dev/null +++ b/modules/agents/albedo-config.nix @@ -0,0 +1,50 @@ +# What albedo needs where home-manager does not run: the MCP servers every +# agent shares rendered into its extensions.json, the shared skills, and the +# script that installs both into a home. The flake exposes the pair as +# .#albedo-config and .#albedo-seed; the image built in the infra repo carries +# them and seeds its home volume before the daemon starts. +{ + lib, + pkgs, + self', + # Servers the target cannot run at all, left out of its config rather than + # carried switched off: the chrome-devtools MCP drives a browser, so an image + # without one has no use for its 284 MiB of dependencies. + without ? [ ], +}: +let + # The same declarations, and the same defaults, the desktops evaluate + # through home-manager: the module needs neither, so it evaluates here too. + cfg = + (lib.evalModules { + modules = [ ./mcp-servers.nix ]; + specialArgs = { inherit self'; }; + }).config.dev.agents; + + render = import ./lib.nix { inherit lib; }; + + skills = import ./agent-skills.nix { inherit pkgs; }; + + fragment = pkgs.writeText "albedo-extensions.json" ( + builtins.toJSON { + servers = lib.mapAttrs (_: render.toAlbedo) (removeAttrs cfg.mcpServers without); + enabled.mcp = true; + } + ); + + # etc/albedo, so `contents = [ albedo-config ]` in an image lands it at + # /etc/albedo without renaming anything. + config = pkgs.runCommandLocal "albedo-config" { } '' + mkdir -p $out/etc/albedo + cp ${fragment} $out/etc/albedo/extensions.json + ln -s ${skills} $out/etc/albedo/skills + ''; +in +{ + inherit config; + seed = import ./albedo-seed.nix { + inherit lib pkgs; + fragment = "${config}/etc/albedo/extensions.json"; + skills = "${config}/etc/albedo/skills"; + }; +} diff --git a/modules/agents/albedo-merge.jq b/modules/agents/albedo-merge.jq new file mode 100644 index 0000000..09e1bc6 --- /dev/null +++ b/modules/agents/albedo-merge.jq @@ -0,0 +1,22 @@ +# Merge the extensions.json fragment this flake builds into the extensions.json +# albedo owns. A server the fragment carries is added or kept current, one it +# carried last time but no longer does was deleted in the flake and goes too, +# and every other server, toggle and setting stays as albedo wrote it. +# +# Inputs: --slurpfile f , --argjson before . +def resolve: + walk(if type == "string" and startswith("@env:") + then (.[5:] as $name | (env[$name] // .)) + else . end); +def unresolved: [.. | strings | select(startswith("@env:"))] | length > 0; + +($f[0] | resolve) as $frag +| ($frag.servers | with_entries(select(.value | unresolved | not))) as $servers +| ($servers | keys) as $owned +| ($before - $owned) as $gone +| .mcp = (.mcp // {}) +| .mcp.servers = ( + ((.mcp.servers // {}) | with_entries(select(.key as $name | ($gone | index($name)) | not))) + * $servers + ) +| .enabled = ((.enabled // {}) * ($frag.enabled // {})) diff --git a/modules/agents/albedo-seed.nix b/modules/agents/albedo-seed.nix new file mode 100644 index 0000000..b6e8c80 --- /dev/null +++ b/modules/agents/albedo-seed.nix @@ -0,0 +1,134 @@ +# Installs the albedo configuration this flake builds into a home: the MCP +# servers, the shared skills, and the Exa key when the environment carries one. +# Home-manager runs it on activation; an image with no home-manager runs it +# before the daemon starts (./albedo-config.nix), so both install the same +# things the same way. +{ + lib, + pkgs, + # The fragment to install unless the caller passes --fragment, and the skills + # to link unless the caller passes --skills. Both are store paths baked into + # the script, so it carries what it installs. + fragment, + skills ? null, +}: +pkgs.writeShellApplication { + name = "albedo-seed"; + runtimeInputs = [ + pkgs.coreutils + pkgs.jq + ]; + text = '' + fragment=${lib.escapeShellArg "${fragment}"} + skills_root=${lib.escapeShellArg (if skills == null then "" else "${skills}")} + home="''${HOME:-/root}" + dry_run="" + + usage() { + printf '%s\n\n' 'Install the albedo configuration built with this script into a home.' + printf '%s\n' 'albedo-seed [--home DIR] [--fragment FILE] [--skills DIR] [--dry-run]' + printf '\n%s\n' \ + ' --home DIR home to write into (default: the HOME variable, then /root)' \ + ' --fragment FILE extensions.json fragment to merge (default: the one built with this script)' \ + ' --skills DIR skills to link into ~/.agents/skills (default: the ones built with this script; empty for none)' \ + ' --dry-run print each command instead of running it' + } + + while [ $# -gt 0 ]; do + case "$1" in + --home) home="$2"; shift 2 ;; + --fragment) fragment="$2"; shift 2 ;; + --skills) skills_root="$2"; shift 2 ;; + --dry-run) dry_run=1; shift ;; + -h | --help) usage; exit 0 ;; + *) + printf 'albedo-seed: unknown argument: %s\n' "$1" >&2 + usage >&2 + exit 2 + ;; + esac + done + + run() { + if [ -n "$dry_run" ]; then + printf '+ %s\n' "$*" + else + "$@" + fi + } + + work="$(mktemp -d)" + trap 'rm -rf "$work"' EXIT + + target="$home/.albedo/extensions.json" + state="$home/.albedo/nix-mcp.json" + creds="$home/.albedo/creds.json" + + run install -d -m 700 "$home/.albedo" + + # A server the fragment carried last time but does not carry now was + # deleted in the flake, so the entry it left behind goes too. + before='[]' + if [ -r "$state" ]; then + before="$(jq -c '.servers // []' "$state" 2>/dev/null || printf '[]')" + fi + + if [ -r "$target" ]; then + cp "$target" "$work/extensions.json" + else + printf '{}\n' > "$work/extensions.json" + fi + + jq --slurpfile f "$fragment" --argjson before "$before" \ + -f ${./albedo-merge.jq} "$work/extensions.json" > "$work/merged.json" + chmod 600 "$work/merged.json" + # A server whose @env: reference does not resolve here is left out, so the + # summary comes from what the merge produced, not from the fragment. + installed="$(jq -r '.mcp.servers | keys | join(", ")' "$work/merged.json")" + run mv -f "$work/merged.json" "$target" + + jq -c -n --slurpfile f "$fragment" '{ servers: ($f[0].servers | keys) }' > "$work/nix-mcp.json" + chmod 600 "$work/nix-mcp.json" + run mv -f "$work/nix-mcp.json" "$state" + + # Exa reads its key from creds.json rather than from the environment, so + # one in this environment is copied in. Only a changed key rewrites the + # file: every other section belongs to albedo, and rewriting an identical + # one would race its own writes. + if [ -n "''${EXA_API_KEY:-}" ]; then + saved="$(jq -r '.exa.apiKey // ""' "$creds" 2>/dev/null || true)" + if [ "$saved" != "$EXA_API_KEY" ]; then + if [ -r "$creds" ]; then + cp "$creds" "$work/creds.json" + else + printf '{}\n' > "$work/creds.json" + fi + jq --arg key "$EXA_API_KEY" '.exa.apiKey = $key' "$work/creds.json" > "$work/creds-next.json" + chmod 600 "$work/creds-next.json" + run mv -f "$work/creds-next.json" "$creds" + fi + fi + + # One symlink per skill, so a skill added by hand stays and a skill that + # moved in the flake lands on the next run. A symlinked skills root was put + # there by home-manager and owns the directory. + if [ -n "$skills_root" ] && [ -d "$skills_root" ]; then + if [ -L "$home/.agents/skills" ]; then + printf 'albedo-seed: %s is a symlink, leaving the skills to it\n' "$home/.agents/skills" + else + run install -d -m 755 "$home/.agents/skills" + for skill in "$skills_root"/*; do + [ -e "$skill" ] || continue + name="$(basename "$skill")" + if [ -e "$home/.agents/skills/$name" ] && [ ! -L "$home/.agents/skills/$name" ]; then + printf 'albedo-seed: %s exists and is not a link, leaving it\n' "$home/.agents/skills/$name" >&2 + continue + fi + run ln -sfnT "$skill" "$home/.agents/skills/$name" + done + fi + fi + + printf 'albedo-seed: %s -> %s\n' "$installed" "$target" + ''; +} diff --git a/modules/agents/albedo.nix b/modules/agents/albedo.nix index a936325..c0cb7e7 100644 --- a/modules/agents/albedo.nix +++ b/modules/agents/albedo.nix @@ -1,6 +1,7 @@ -# Albedo's side of the shared agent config: the MCP servers, written into -# $ALBEDO_HOME/extensions.json, and a wrapper that puts the agent secrets in -# the daemon's environment, which is where albedo resolves `{ "env": ... }`. +# Albedo's side of the shared agent config: the MCP servers, merged into +# $ALBEDO_HOME/extensions.json on activation, and a wrapper that puts the agent +# secrets in the daemon's environment, which is where albedo resolves +# `{ "env": ... }`. { config, lib, @@ -30,12 +31,6 @@ let ''; }; - # Server names this repo owns, so a server deleted here is deleted from - # albedo's config too, while servers added from albedo's own UI stay. - state = "$HOME/.albedo/nix-mcp.json"; - target = "$HOME/.albedo/extensions.json"; - creds = "$HOME/.albedo/creds.json"; - fragment = pkgs.writeText "albedo-mcp.json" ( builtins.toJSON { servers = lib.mapAttrs (_: render.toAlbedo) cfg.mcpServers; @@ -45,27 +40,12 @@ let render = import ./lib.nix { inherit lib; }; - # Deep merge, nix wins per field: a server declared here is added, its - # fields are kept current, and every other server, toggle and setting in - # extensions.json is left alone. - merge = '' - def resolve: - walk(if type == "string" and startswith("@env:") - then (.[5:] as $name | (env[$name] // .)) - else . end); - def unresolved: [.. | strings | select(startswith("@env:"))] | length > 0; - - ($f[0] | resolve) as $frag - | ($frag.servers | with_entries(select(.value | unresolved | not))) as $servers - | ($servers | keys) as $owned - | ($before - $owned) as $gone - | .mcp = (.mcp // {}) - | .mcp.servers = ( - ((.mcp.servers // {}) | with_entries(select(.key as $name | ($gone | index($name)) | not))) - * $servers - ) - | .enabled = ((.enabled // {}) * ($frag.enabled // {})) - ''; + # The install itself is shared with the image the infra repo builds, so a + # merged extensions.json looks the same on a desktop and in a container. + seed = import ./albedo-seed.nix { + inherit lib pkgs; + inherit fragment; + }; in { options.dev.agents.albedo = { @@ -89,60 +69,17 @@ in home.activation.albedoMcp = lib.hm.dag.entryAfter ([ "writeBoundary" ] ++ lib.optional (cfg.envFile != "/dev/null") "sops-nix") '' - # A subshell, so the tools this needs stay out of every other - # activation entry's PATH. + # A subshell, so sourcing the secrets stays out of every other + # activation entry's environment. ( - export PATH="${ - lib.makeBinPath [ - pkgs.coreutils - pkgs.jq - ] - }:$PATH" - - # Secrets first: the fragment carries @env:NAME@ for URLs that are - # themselves secret, and jq resolves them from this environment. + # Secrets first: the fragment carries @env:NAME for URLs that are + # themselves secret, and the seed resolves them from this + # environment. It also copies EXA_API_KEY into creds.json, which is + # the only place Exa looks for it. # shellcheck disable=SC1091 if [ -f "${cfg.envFile}" ]; then set -a; . "${cfg.envFile}"; set +a; fi - $VERBOSE_ARG mkdir -p "$HOME/.albedo" - work="$(mktemp -d)" - trap 'rm -rf "$work"' EXIT - - before='[]' - if [ -r "${state}" ]; then - before="$(jq -c '.servers // []' "${state}" 2>/dev/null || echo '[]')" - fi - - if [ -r "${target}" ]; then - cp "${target}" "$work/extensions.json" - else - echo '{}' > "$work/extensions.json" - fi - - jq --slurpfile f "${fragment}" --argjson before "$before" '${merge}' \ - "$work/extensions.json" > "$work/merged.json" - chmod 600 "$work/merged.json" - $DRY_RUN_CMD mv -f "$work/merged.json" "${target}" - - jq -c -n --slurpfile f "${fragment}" '{ servers: ($f[0].servers | keys) }' > "$work/nix-mcp.json" - chmod 600 "$work/nix-mcp.json" - $DRY_RUN_CMD mv -f "$work/nix-mcp.json" "${state}" - - # Exa's search provider reads its key from creds.json, not from the - # environment the wrapper exports, so the key is copied in. Only a - # changed key rewrites the file: every other section belongs to - # albedo, and rewriting an identical one would race its own writes. - saved="$(jq -r '.exa.apiKey // ""' "${creds}" 2>/dev/null || true)" - if [ -n "''${EXA_API_KEY:-}" ] && [ "$saved" != "$EXA_API_KEY" ]; then - if [ -r "${creds}" ]; then - cp "${creds}" "$work/creds.json" - else - echo '{}' > "$work/creds.json" - fi - jq --arg key "$EXA_API_KEY" '.exa.apiKey = $key' "$work/creds.json" > "$work/creds-exa.json" - chmod 600 "$work/creds-exa.json" - $DRY_RUN_CMD mv -f "$work/creds-exa.json" "${creds}" - fi + ${lib.getExe seed} ''${DRY_RUN_CMD:+--dry-run} ) ''; }; diff --git a/modules/agents/default.nix b/modules/agents/default.nix index 4600847..2a454be 100644 --- a/modules/agents/default.nix +++ b/modules/agents/default.nix @@ -2,15 +2,11 @@ # servers each one is given, the skills they all read, and one module per agent # rendering that shared set into the agent's own format. Agent-specific settings # stay in that agent's own module. -{ config, lib, ... }: { +{ ... }: { imports = [ ./albedo.nix ./mcp.nix ./opencode.nix ./skills.nix ]; - - options.dev.agents.enable = lib.mkEnableOption "shared coding-agent configuration"; - - config.dev.agents.enable = lib.mkDefault true; } diff --git a/modules/agents/mcp-servers.nix b/modules/agents/mcp-servers.nix new file mode 100644 index 0000000..8a33bf3 --- /dev/null +++ b/modules/agents/mcp-servers.nix @@ -0,0 +1,210 @@ +# The MCP servers every agent on the host is given, in one transport-neutral +# shape, and nothing about the way an install delivers them. ./mcp.nix adds the +# home-manager side (sops), and the flake renders this same set into albedo's +# own config for a machine home-manager never touches (./albedo-config.nix). +# +# A tag module adds a host's own servers with +# `dev.agents.mcpServers. = { ... };`. +{ + config, + lib, + self', + ... +}: +let + cfg = config.dev.agents; + + # A header or environment value is a literal, or a reference to a variable + # the agent resolves when it connects. + source = lib.types.either lib.types.str ( + lib.types.submodule { + options.env = lib.mkOption { + type = lib.types.str; + description = "Name of the environment variable holding the value."; + }; + } + ); + + server = lib.types.submodule { + options = { + type = lib.mkOption { + type = lib.types.enum [ + "http" + "stdio" + ]; + description = "An HTTP endpoint, or a command spoken to over stdio."; + }; + url = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = "Endpoint of an HTTP server."; + }; + urlEnv = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + Environment variable holding the whole endpoint, for a URL that is + itself a secret. Albedo cannot reference a variable in `url`, so its + config carries the value substituted at activation. + ''; + }; + bearerEnv = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = "Environment variable holding a token sent as `Authorization: Bearer $VAR`."; + }; + headers = lib.mkOption { + type = lib.types.attrsOf source; + default = { }; + description = "Extra headers of an HTTP server."; + }; + oauth = lib.mkOption { + type = lib.types.nullOr (lib.types.either lib.types.bool lib.types.attrs); + default = null; + description = '' + OpenCode's OAuth settings; `false` stops it probing for one. Albedo + has no equivalent and starts its own sign-in whenever a server asks + for one. + ''; + }; + command = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = "Command and arguments of a stdio server."; + }; + env = lib.mkOption { + type = lib.types.attrsOf source; + default = { }; + description = "Environment of a stdio server."; + }; + cwd = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = "Working directory of a stdio server."; + }; + enabled = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Off keeps the definition without connecting to it."; + }; + timeoutMs = lib.mkOption { + type = lib.types.nullOr lib.types.int; + default = null; + description = "Per-request timeout in milliseconds."; + }; + startupTimeoutMs = lib.mkOption { + type = lib.types.nullOr lib.types.int; + default = null; + description = "How long a session waits for the server to start. Albedo only."; + }; + }; + }; +in +{ + options.dev.agents = { + enable = lib.mkEnableOption "shared coding-agent configuration"; + + secretServers = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Include the servers that need a secret from the environment. A host + that holds the secrets itself (home-manager decrypts them with sops, a + container gets them from its deployment) says so here, so the servers + without one stay declared and switched off. + ''; + }; + + mcpBrowser = lib.mkOption { + type = lib.types.nullOr lib.types.package; + default = null; + description = '' + Browser the chrome-devtools MCP drives, passed as --executablePath. + Null leaves the flag off and lets the server look for a Chrome itself. + Desktop hosts set this to helium (modules/desktop/apps/browser.nix); + override it per host to use a different browser. + ''; + }; + + attachBrowser = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + DevToolsActivePort file of a browser that is already running, as a path + this machine can read. When set, the chrome-devtools MCP attaches to + that browser over CDP instead of launching one, so the agent sees the + tabs and logged-in sessions of the user's own profile. + + The file holds two lines: the CDP port, then the + /devtools/browser/ socket path. Chromium serves no + /json/version on that implicit endpoint, so the server needs + --wsEndpoint rather than --browserUrl, and it must read the file at + connect time because the uuid changes on every browser start. + ''; + }; + + mcpServers = lib.mkOption { + type = lib.types.attrsOf server; + default = { }; + description = '' + MCP servers every agent on this host is given. Declared here or in a + tag module; rendered into each agent's own config by ./lib.nix. + ''; + }; + }; + + config = { + dev.agents.enable = lib.mkDefault true; + + dev.agents.mcpServers = lib.mkIf cfg.enable ( + { + # outline doesn't need secrets, always available + outline = { + type = "http"; + url = "https://outline.dolly-ruffe.ts.net/mcp"; + }; + + # Off unless a host points it at a running browser: the fallback + # branch launches one, which is a blank profile and no sessions. + chrome-devtools = { + type = "stdio"; + enabled = cfg.attachBrowser != null; + command = + if cfg.attachBrowser != null then + [ + "bash" + "-lc" + ( + "f=${lib.escapeShellArg cfg.attachBrowser}; " + + "exec ${lib.getExe self'.packages.chrome-devtools-mcp} " + + "--wsEndpoint \"ws://127.0.0.1:$(sed -n 1p \"$f\")$(sed -n 2p \"$f\")\"" + ) + ] + else + [ (lib.getExe self'.packages.chrome-devtools-mcp) ] + ++ lib.optionals (cfg.mcpBrowser != null) [ + "--executablePath" + (lib.getExe cfg.mcpBrowser) + ]; + }; + } + // lib.optionalAttrs cfg.secretServers { + # These need a secret from the environment, so they only appear where + # the host has one to give. + github = { + type = "http"; + url = "https://api.githubcopilot.com/mcp/"; + bearerEnv = "GITHUB_TOKEN"; + }; + + kagi = { + type = "http"; + url = "https://mcp.kagi.com/mcp"; + bearerEnv = "KAGI_API_KEY"; + oauth = false; + enabled = false; + }; + } + ); + }; +} diff --git a/modules/agents/mcp.nix b/modules/agents/mcp.nix index 7d91b65..4f82556 100644 --- a/modules/agents/mcp.nix +++ b/modules/agents/mcp.nix @@ -1,115 +1,33 @@ -# The MCP servers every agent on this host is given, declared once in a -# transport-neutral shape and rendered per agent (see ./lib.nix). A tag module -# adds a host's own servers with `dev.agents.mcpServers. = { ... };`. +# The home-manager side of the shared MCP servers (./mcp-servers.nix): the +# encrypted secrets they read, and the hosts that have them. The servers +# themselves live in the module this one imports, so the flake can evaluate +# them without home-manager for an image that home-manager never configures. { config, inputs, lib, - self', ... }: let cfg = config.dev.agents; - - source = lib.types.either lib.types.str ( - lib.types.submodule { - options.env = lib.mkOption { - type = lib.types.str; - description = "Name of the environment variable holding the value."; - }; - } - ); - - server = lib.types.submodule { - options = { - type = lib.mkOption { - type = lib.types.enum [ - "http" - "stdio" - ]; - description = "An HTTP endpoint, or a command spoken to over stdio."; - }; - url = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = "Endpoint of an HTTP server."; - }; - urlEnv = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = '' - Environment variable holding the whole endpoint, for a URL that is - itself a secret. Albedo cannot reference a variable in `url`, so its - config carries the value substituted at activation. - ''; - }; - bearerEnv = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = "Environment variable holding a token sent as `Authorization: Bearer $VAR`."; - }; - headers = lib.mkOption { - type = lib.types.attrsOf source; - default = { }; - description = "Extra headers of an HTTP server."; - }; - oauth = lib.mkOption { - type = lib.types.nullOr (lib.types.either lib.types.bool lib.types.attrs); - default = null; - description = '' - OpenCode's OAuth settings; `false` stops it probing for one. Albedo - has no equivalent and starts its own sign-in whenever a server asks - for one. - ''; - }; - command = lib.mkOption { - type = lib.types.listOf lib.types.str; - default = [ ]; - description = "Command and arguments of a stdio server."; - }; - env = lib.mkOption { - type = lib.types.attrsOf source; - default = { }; - description = "Environment of a stdio server."; - }; - cwd = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = "Working directory of a stdio server."; - }; - enabled = lib.mkOption { - type = lib.types.bool; - default = true; - description = "Off keeps the definition without connecting to it."; - }; - timeoutMs = lib.mkOption { - type = lib.types.nullOr lib.types.int; - default = null; - description = "Per-request timeout in milliseconds."; - }; - startupTimeoutMs = lib.mkOption { - type = lib.types.nullOr lib.types.int; - default = null; - description = "How long a session waits for the server to start. Albedo only."; - }; - }; - }; in { - # This module owns the encrypted agents' secrets, so it brings in the - # home-manager side of sops itself instead of leaving it to whichever agent - # module happens to be imported. - imports = [ inputs.sops-nix.homeManagerModules.sops ]; + imports = [ + ./mcp-servers.nix + inputs.sops-nix.homeManagerModules.sops + ]; options.dev.agents = { - sops.enable = lib.mkOption { - type = lib.types.bool; - default = true; - description = '' - This host has a key in .sops.yaml, so the agent secrets decrypt. Off - drops every secret-bearing MCP server and leaves the agents running - without them. - ''; + sops = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + This host has a key in .sops.yaml, so the agent secrets decrypt. Off + drops every secret-bearing MCP server and leaves the agents running + without them. + ''; + }; }; envFile = lib.mkOption { type = lib.types.path; @@ -117,96 +35,16 @@ in default = lib.attrByPath [ "agents/env" "path" ] "/dev/null" config.sops.secrets; description = "Decrypted dotenv file holding the agents' secrets."; }; - mcpBrowser = lib.mkOption { - type = lib.types.nullOr lib.types.package; - default = null; - description = '' - Browser the chrome-devtools MCP drives, passed as --executablePath. - Null leaves the flag off and lets the server look for a Chrome itself. - Desktop hosts set this to helium (modules/desktop/apps/browser.nix); - override it per host to use a different browser. - ''; - }; - attachBrowser = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = '' - DevToolsActivePort file of a browser that is already running, as a path - this machine can read. When set, the chrome-devtools MCP attaches to - that browser over CDP instead of launching one, so the agent sees the - tabs and logged-in sessions of the user's own profile. - - The file holds two lines: the CDP port, then the - /devtools/browser/ socket path. Chromium serves no - /json/version on that implicit endpoint, so the server needs - --wsEndpoint rather than --browserUrl, and it must read the file at - connect time because the uuid changes on every browser start. - ''; - }; - mcpServers = lib.mkOption { - type = lib.types.attrsOf server; - default = { }; - description = '' - MCP servers every agent on this host is given. Declared here or in a - tag module; rendered into each agent's own config by ./lib.nix. - ''; - }; }; config = { + # A host whose secrets decrypt is a host whose secret-bearing servers have + # something to send; ./mcp-servers.nix only knows that second fact. + dev.agents.secretServers = lib.mkDefault cfg.sops.enable; + sops.secrets."agents/env" = lib.mkIf cfg.sops.enable { sopsFile = ../../secrets/agents.env; format = "dotenv"; }; - - dev.agents.mcpServers = lib.mkIf cfg.enable ( - { - # outline doesn't need secrets, always available - outline = { - type = "http"; - url = "https://outline.dolly-ruffe.ts.net/mcp"; - }; - - # Off unless a host points it at a running browser: the fallback - # branch launches one, which is a blank profile and no sessions. - chrome-devtools = { - type = "stdio"; - enabled = cfg.attachBrowser != null; - command = - if cfg.attachBrowser != null then - [ - "bash" - "-lc" - ( - "f=${lib.escapeShellArg cfg.attachBrowser}; " - + "exec ${lib.getExe self'.packages.chrome-devtools-mcp} " - + "--wsEndpoint \"ws://127.0.0.1:$(sed -n 1p \"$f\")$(sed -n 2p \"$f\")\"" - ) - ] - else - [ (lib.getExe self'.packages.chrome-devtools-mcp) ] - ++ lib.optionals (cfg.mcpBrowser != null) [ - "--executablePath" - (lib.getExe cfg.mcpBrowser) - ]; - }; - } - // lib.optionalAttrs cfg.sops.enable { - # These need secrets from sops, only configured when sops is enabled - github = { - type = "http"; - url = "https://api.githubcopilot.com/mcp/"; - bearerEnv = "GITHUB_TOKEN"; - }; - - kagi = { - type = "http"; - url = "https://mcp.kagi.com/mcp"; - bearerEnv = "KAGI_API_KEY"; - oauth = false; - enabled = false; - }; - } - ); }; } diff --git a/modules/agents/skills.nix b/modules/agents/skills.nix index 24da621..4f5bc7b 100644 --- a/modules/agents/skills.nix +++ b/modules/agents/skills.nix @@ -6,26 +6,10 @@ pkgs, ... }: -let - # symlinkJoin merges directory contents, so each skill must sit in its own - # subdirectory; a SKILL.md at the root shadows every other skill. - agentSkills = pkgs.symlinkJoin { - name = "agent-skills"; - paths = [ - ./skills - (pkgs.linkFarm "tuicr-skill" [ - { - name = "tuicr/SKILL.md"; - path = ../dev/tools/tuicr/SKILL.md; - } - ]) - ]; - }; -in { config = lib.mkIf config.dev.agents.enable { home.file.".agents/skills" = { - source = agentSkills; + source = import ./agent-skills.nix { inherit pkgs; }; recursive = true; }; };