{ config, lib, pkgs, ... }: # Claude Code's long-term memory in luna's Honcho, through mars' gate # (hosts/mars/honcho-gate.nix). The plugin itself (plastic-labs/claude-honcho) # is installed by hand with /plugin; this wires its config and adds luna as an # observer of every Claude session, which the plugin never does: without it # the deriver writes only darman's self-view, and Hermes (directional) reads # luna's view of darman, so luna would learn nothing from these sessions. let baseUrl = "http://mars.orbit.sol:8099"; workspace = "luna"; tokenFile = config.sops.secrets.honcho_gate_token.path; # Get-or-create, so it doesn't race the plugin's own SessionStart; the # plugin's later get-or-create still adds its repo/host scopes. lunaObserver = pkgs.writeShellApplication { name = "honcho-luna-observer"; runtimeInputs = [ pkgs.curl pkgs.jq pkgs.coreutils ]; text = '' log="$HOME/.honcho/luna-observer.log" sid=$(jq -r '.session_id // empty') [ -n "$sid" ] || exit 0 # Same mapping as the plugin's getSessionName. name=$(printf %s "$sid" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9_-' '-') # Header via stdin: argv is world-readable in /proc. if ! printf 'header = "Authorization: Bearer %s"\n' "$(cat ${tokenFile})" | curl -fsS --max-time 8 -K - -X POST \ -H 'Content-Type: application/json' \ -d '{"luna":{"observe_me":false,"observe_others":true}}' \ "${baseUrl}/v3/workspaces/${workspace}/sessions/$name/peers" >/dev/null 2>>"$log"; then echo "$(date -Is) $name: adding luna failed" >>"$log" fi exit 0 ''; }; in { # Same value in secrets/mars.yaml (the gate). sops.secrets.honcho_gate_token.owner = "darman"; home-manager.users.darman = { lib, ... }: { # Both files are also written by Claude Code and the plugin, so only these # keys are asserted; everything else in them stays. home.activation.claudeHoncho = lib.hm.dag.entryAfter [ "writeBoundary" ] '' merge() { # file, jq filter, args… f=$1; shift [ -s "$f" ] || echo '{}' > "$f" ${pkgs.jq}/bin/jq "$@" "$f" > "$f.new" chmod 600 "$f.new" mv -f "$f.new" "$f" } ( umask 077 mkdir -p "$HOME/.honcho" "$HOME/.claude" # A missing secret must not fail the whole switch. The block needs its # own schemaVersion: without it the plugin "migrates" it and drops # baseUrl, falling back to Plastic Labs' cloud with our token. [ -r ${tokenFile} ] && merge "$HOME/.honcho/config.json" \ --rawfile key ${tokenFile} \ '.schemaVersion = 2 | .hosts.claude_code = ((.hosts.claude_code // {}) + { schemaVersion: 2, baseUrl: "${baseUrl}", apiKey: ($key | rtrimstr("\n")), workspace: "${workspace}", peerName: "darman", aiPeer: "claude" })' # Env beats the file, so no plugin rewrite of it can reroute to the cloud. merge "$HOME/.claude/settings.json" \ --arg cmd ${lunaObserver}/bin/honcho-luna-observer \ '.env = ((.env // {}) + { HONCHO_BASE_URL: "${baseUrl}", HONCHO_WORKSPACE: "${workspace}", HONCHO_PEER_NAME: "darman", HONCHO_AI_PEER: "claude" }) | .hooks.SessionStart = ([(.hooks.SessionStart // [])[] | select([.hooks[]?.command] | any(contains("honcho-luna-observer")) | not)] + [{ hooks: [{ type: "command", command: $cmd, timeout: 10 }] }])' ) ''; }; }