From 63e9984c76eea4401ab81361d6c5f250d4074e95 Mon Sep 17 00:00:00 2001 From: andrewthomaslee Date: Sun, 29 Nov 2026 08:18:33 -0600 Subject: [PATCH] Agents: homeSpec.agents.claude.autoMode for auto mode rules Auto mode reads autoMode.{environment,allow,soft_deny,hard_deny} from user settings, which home-manager owns read-only. claude.settings is merged with recursiveUpdate, which replaces lists, so typed list options let several modules add rules. Every non-empty list starts with "$defaults" so claude's built-in rules stay in force. Co-Authored-By: Claude Opus 5.5 --- flake-parts/homeModules/agents/agents.nix | 39 +++++++++++++++++++++++ tests/fixtures/nixos.nix | 8 ++++- tests/flake-module.nix | 3 ++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/flake-parts/homeModules/agents/agents.nix b/flake-parts/homeModules/agents/agents.nix index fd32e73..f225575 100644 --- a/flake-parts/homeModules/agents/agents.nix +++ b/flake-parts/homeModules/agents/agents.nix @@ -19,6 +19,9 @@ # appended after the preset (inputs.skills.lib.selections). # homeSpec.agents..enable — install pi / kimi / claude / # opencode and point the agent's skills dir at the shared tree. + # homeSpec.agents.claude.autoMode — claude's auto mode classifier + # rules (environment, allow, softDeny, hardDeny), concatenated + # across modules and led by "$defaults". # homeSpec.agents.claude.desktop — Claude Desktop next to claude # (on with it), Cowork's QEMU when the machine has KVM; the host # side (kvm group, vhost_vsock) is nixosModules.claude-desktop. @@ -276,8 +279,21 @@ }) (lib.optionalAttrs skillsRuntimeEnabled {inherit (skillsRuntime) env;}) (lib.optionalAttrs (claudeInstructions.mode == "replace") {outputStyle = claudeOutputStyle;}) + (lib.optionalAttrs (claudeAutoMode != {}) {autoMode = claudeAutoMode;}) cfg.claude.settings ]; + + # settings.json autoMode from homeSpec.agents.claude.autoMode: only + # the non-empty lists, each led by "$defaults" unless opted out. + claudeAutoMode = let + am = cfg.claude.autoMode; + lists = lib.filterAttrs (_: rules: rules != []) { + inherit (am) environment allow; + soft_deny = am.softDeny; + hard_deny = am.hardDeny; + }; + in + lib.mapAttrs (_: rules: lib.optional am.includeDefaults "$defaults" ++ rules) lists; in { imports = [ (lib.mkRemovedOptionModule ["homeSpec" "agents" "claude" "claudeMd"] "Use homeSpec.agents.instructions (or homeSpec.agents.claude.instructions) instead; its text is added to the machine context and ground rules, and mode = \"replace\" replaces the system prompt.") @@ -548,6 +564,29 @@ default = {}; description = "Extra claude-code settings.json attrs, merged over the homeSpec.agents.nixWorkflow defaults (these win key by key)."; }; + # Auto mode's classifier rules (settings.json autoMode.*). Typed + # lists, not claude.settings: settings is merged with + # recursiveUpdate, which replaces lists, so two modules could not + # each add rules there. claude reads autoMode only from user, + # flag and policy settings, never a project's .claude/. + autoMode = let + rules = section: + lib.mkOption { + type = with lib.types; listOf str; + default = []; + description = "Prose rules for auto mode's ${section}. Definitions from several modules are concatenated."; + }; + in { + includeDefaults = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Start every non-empty list with \"$defaults\", so claude's built-in rules stay in force and these rules add to them."; + }; + environment = rules "environment (the trusted context: org, repos, domains, providers)"; + allow = rules "allow section (actions that need no prompt)"; + softDeny = rules "SOFT BLOCK section (destructive or irreversible actions the user's intent can clear)"; + hardDeny = rules "HARD BLOCK section (security boundaries the user's intent never clears)"; + }; # The sandbox bakes ~/.claude.json too (claude merges either # file) so the onboarding skip survives claude rewriting its # mutable state file. diff --git a/tests/fixtures/nixos.nix b/tests/fixtures/nixos.nix index e2c367d..daaafb5 100644 --- a/tests/fixtures/nixos.nix +++ b/tests/fixtures/nixos.nix @@ -140,7 +140,13 @@ }; pi.enable = true; kimi.enable = true; - claude.enable = true; + claude = { + enable = true; + autoMode = { + environment = ["Trusted org: example"]; + softDeny = ["Deploying a machine"]; + }; + }; mcp.github.auth = "pat"; skills = { preset = "full"; diff --git a/tests/flake-module.nix b/tests/flake-module.nix index 7cd6486..8f67455 100644 --- a/tests/flake-module.nix +++ b/tests/flake-module.nix @@ -252,6 +252,9 @@ assert expect (lib.length claude.hooks.PostToolUse == 1) "no .nix format hook"; assert expect (lib.length claude.hooks.PreToolUse == 1) "no git hook guard"; assert expect (!(claude ? mcpServers)) "settings.json carries mcpServers"; + assert expect (claude.autoMode.environment == ["$defaults" "Trusted org: example"]) "autoMode.environment does not keep the built-in rules first"; + assert expect (claude.autoMode.soft_deny == ["$defaults" "Deploying a machine"]) "autoMode.softDeny not rendered as soft_deny"; + assert expect (!(claude.autoMode ? allow || claude.autoMode ? hard_deny)) "empty autoMode lists are written"; assert expect (hm.home.activation ? claudeMcpServers) "no ~/.claude.json MCP sync"; assert expect (hm.programs.opencode.settings.mcp.servers.github.type == "local") "opencode github not on the pat server"; assert expect ((json ".kimi-code/mcp.json").mcpServers ? github) "kimi has no github server";