# 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 OpenCode MCP entry (via programs.mcp, since opencode.json # is a home-manager symlink) and the scope markers (~/.ai-memory.toml # plus one marker per workspace directory). # - Activation runs ai-memory's own installers for the lifecycle hooks, # the session-aware Claude Code MCP entry 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: hooks and the MCP bridge read `[auth].bearer_token_file` from the # client config, and the OpenCode plugin reads `/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"]; }; 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 # The OpenCode plugin reads /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) '' export PATH="${pkgs.llm-agents.claude-code}/bin:$PATH" run claude mcp remove ai-memory -s user >/dev/null 2>&1 || true # --session-aware: stdio bridge that 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. run ${exe} install-mcp --client claude-code --apply --session-aware \ --server-url ${escapeShellArg "${cfg.serverUrl}/mcp"} ''} # Into ~/.agents/skills; claude-skills-sync mirrors them to Claude. run ${exe} install-skills --scope global --agent agents fi ''; }; }