# 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 # `/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 /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 /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 ''; }; }