Files
m3ta-home/profiles/sets/coding/agents/ai-memory.nix
T
m3ta-chironandClaude Fable 5.1 c9f68a4361 feat(coding/agents): hand the shared MCP catalogue to Claude Code
- Move programs.mcp.servers out of agents.nix into mcp.nix so hosts that
  do not load the whole coding set (T3 Code servers) can import it alone
- Add claude-code.nix: programs.claude-code with enableMcpIntegration,
  replacing the bare llm-agents.claude-code package (would collide on
  bin/claude). home-manager delivers the catalogue as a generated plugin,
  ~/.claude/settings.json and ~/.claude.json stay writable
- ai-memory: register the session-aware Claude bridge via
  programs.claude-code.mcpServers instead of `ai-memory install-mcp`;
  activation only removes the stale user-scope entry now
- Adjust ai-memory.test.sh accordingly

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-10 20:15:24 +01:00

157 lines
6.2 KiB
Nix

# ai-memory client (https://aimemory.sh) — shared agent memory.
#
# The server runs on m3-hermes (nixos-config) and is reached at
# memory.m3ta.dev; no server runs on clients. Hybrid setup:
# - Nix owns the package, the client config (server URL + bearer token
# file), the MCP entries (programs.mcp for OpenCode, and a session-aware
# override in programs.claude-code.mcpServers for Claude Code, both
# delivered through the agents' enableMcpIntegration) and the scope
# markers (~/.ai-memory.toml plus one marker per workspace directory).
# - Activation runs ai-memory's own installers for the lifecycle hooks and
# the skills, so generated files always match the installed ai-memory
# version.
#
# The bearer token (an `aim_` API key, one per host) is an agenix secret the
# host repo provides at `tokenFile`. It is never copied into a generated
# file: the MCP bridge reads `[auth].bearer_token_file` from the client
# config, while the native hooks and the OpenCode plugin read
# `<data_dir>/auth-token`, which activation symlinks to the secret. Without
# the token activation skips the installers instead of writing hooks that
# cannot authenticate.
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.coding.aiMemory;
package = pkgs.llm-agents.ai-memory;
exe = "${package}/bin/ai-memory";
dataDir = "${config.home.homeDirectory}/.local/share/ai-memory";
tomlList = xs: "[${concatMapStringsSep ", " (x: ''"${x}"'') xs}]";
# Markers are not merged with their ancestors, so every marker repeats
# the capture exclusions.
marker = workspace: ''
workspace = "${workspace}"
[capture]
ignore_paths = ${tomlList cfg.ignorePaths}
'';
in {
options.coding.aiMemory = {
enable = mkEnableOption "the ai-memory client (hooks, MCP and skills for coding agents)";
serverUrl = mkOption {
type = types.str;
default = "https://memory.m3ta.dev";
description = "Base URL of the ai-memory server; MCP is served at <serverUrl>/mcp.";
};
tokenFile = mkOption {
type = types.str;
default = "/run/agenix/ai-memory-token";
description = "File holding this host's `aim_` API key, readable by the user.";
};
agents = mkOption {
type = types.listOf (types.enum ["claude-code" "opencode2"]);
default = ["claude-code" "opencode2"];
description = "Agents whose lifecycle hooks are installed.";
};
workspace = mkOption {
type = types.str;
default = "private";
description = ''
ai-memory workspace for every directory below $HOME that has no
marker of its own (and no entry in `workspaceDirs`). Projects are
still derived per repository (git remote), only the workspace is
fixed here. Work hosts set this to the work workspace.
'';
};
workspaceDirs = mkOption {
type = types.attrsOf types.str;
default = {};
example = {"p/AZ" = "az";};
description = ''
Home-relative directories whose subtree belongs to another
workspace. Each gets its own `.ai-memory.toml` marker.
'';
};
ignorePaths = mkOption {
type = types.listOf types.str;
default = ["/run/agenix/**" "~/.config/sops/**" "~/.ssh/**" "**/.env*"];
description = "Paths kept out of capture via the markers' [capture] ignore_paths.";
};
};
config = mkIf cfg.enable {
# The llm-agents build wraps the binary with git on PATH (hooks need it).
home.packages = [package];
home.sessionVariables.AI_MEMORY_SERVER_URL = cfg.serverUrl;
# OpenCode2 picks this up via programs.opencode.enableMcpIntegration.
programs.mcp.servers.ai-memory = {
command = exe;
args = ["mcp-bridge" "--no-session-id" "--server-url" "${cfg.serverUrl}/mcp"];
};
# Claude Code gets the same server name through
# programs.claude-code.enableMcpIntegration, but session-aware: the stdio
# bridge forwards the lifecycle-hook session id and reads the token file
# itself. (A plain HTTP entry carries no credentials, and Claude Code then
# tries OAuth client registration, which ai-memory does not offer.)
# Claude Code's own mcpServers take precedence over the shared entry, so
# this is one server, not two.
programs.claude-code.mcpServers.ai-memory = mkIf (elem "claude-code" cfg.agents) {
command = exe;
args = ["mcp-bridge" "--server-url" "${cfg.serverUrl}/mcp"];
};
home.file =
{
# Client config: `ai-memory hook`, `mcp-bridge` and the installers
# read server_url and the token from here, so no generated file
# carries the token.
".local/share/ai-memory/config.toml".text = ''
server_url = "${cfg.serverUrl}"
[auth]
bearer_token_file = "${cfg.tokenFile}"
'';
# Scope marker: $HOME governs everything without a closer marker.
".ai-memory.toml".text = marker cfg.workspace;
}
# One marker per directory that belongs to another workspace.
// mapAttrs' (dir: ws: nameValuePair "${dir}/.ai-memory.toml" {text = marker ws;}) cfg.workspaceDirs;
home.activation.aiMemoryInstall = lib.hm.dag.entryAfter ["writeBoundary" "claudeSkillsSync"] ''
if [ ! -r ${escapeShellArg cfg.tokenFile} ]; then
echo "ai-memory: token file ${cfg.tokenFile} missing, skipping" >&2
else
# Hooks and the OpenCode plugin read <data_dir>/auth-token; point it at the secret.
run mkdir -p ${escapeShellArg dataDir}
run ln -sfn ${escapeShellArg cfg.tokenFile} ${escapeShellArg "${dataDir}/auth-token"}
${concatMapStringsSep "\n" (agent: ''
run ${exe} install-hooks --agent ${agent} --apply --server-url ${escapeShellArg cfg.serverUrl}
'')
cfg.agents}
${optionalString (elem "claude-code" cfg.agents) ''
# The MCP entry now comes from programs.claude-code (see above);
# drop the user-scope entry earlier versions wrote into ~/.claude.json.
export PATH="${pkgs.llm-agents.claude-code}/bin:$PATH"
run claude mcp remove ai-memory -s user >/dev/null 2>&1 || true
''}
# Into ~/.agents/skills; claude-skills-sync mirrors them to Claude.
run ${exe} install-skills --scope global --agent agents
fi
'';
};
}