From be37a3492d256910e7f9d5a6d8354f37aadd2c07 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Fri, 22 Aug 2025 22:13:07 +0100 Subject: [PATCH] claude-code: added 'commandsDir' option to specify commands from filesystem --- modules/programs/claude-code.nix | 19 +++++++++++++++++++ .../programs/claude-code/assertion.nix | 7 +++++++ .../programs/claude-code/commands-dir.nix | 14 ++++++++++++++ .../claude-code/commands/test-command.md | 6 ++++++ .../modules/programs/claude-code/default.nix | 1 + 5 files changed, 47 insertions(+) create mode 100644 tests/modules/programs/claude-code/commands-dir.nix create mode 100644 tests/modules/programs/claude-code/commands/test-command.md diff --git a/modules/programs/claude-code.nix b/modules/programs/claude-code.nix index 11d0ebe4..4376c0a9 100644 --- a/modules/programs/claude-code.nix +++ b/modules/programs/claude-code.nix @@ -196,6 +196,16 @@ in example = lib.literalExpression "./agents"; }; + commandsDir = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = '' + Path to a directory containing command files for Claude Code. + Command files from this directory will be symlinked to .claude/commands/. + ''; + example = lib.literalExpression "./commands"; + }; + mcpServers = lib.mkOption { type = lib.types.attrsOf jsonFormat.type; default = { }; @@ -251,6 +261,10 @@ in assertion = !(cfg.agents != { } && cfg.agentsDir != null); message = "Cannot specify both `programs.claude-code.agents` and `programs.claude-code.agentsDir`"; } + { + assertion = !(cfg.commands != { } && cfg.commandsDir != null); + message = "Cannot specify both `programs.claude-code.commands` and `programs.claude-code.commandsDir`"; + } ]; programs.claude-code.finalPackage = @@ -300,6 +314,11 @@ in source = cfg.agentsDir; recursive = true; }; + + ".claude/commands" = lib.mkIf (cfg.commandsDir != null) { + source = cfg.commandsDir; + recursive = true; + }; } // lib.mapAttrs' ( name: content: diff --git a/tests/modules/programs/claude-code/assertion.nix b/tests/modules/programs/claude-code/assertion.nix index 6736d394..c5b3fff7 100644 --- a/tests/modules/programs/claude-code/assertion.nix +++ b/tests/modules/programs/claude-code/assertion.nix @@ -26,11 +26,18 @@ test-agent = "test content"; }; agentsDir = ./agents; + + # assert fail: cannot set commands and commandsDir at the same time. + commands = { + test-command = "test content"; + }; + commandsDir = ./commands; }; test.asserts.assertions.expected = [ "`programs.claude-code.package` cannot be null when `mcpServers` is configured" "Cannot specify both `programs.claude-code.memory.text` and `programs.claude-code.memory.source`" "Cannot specify both `programs.claude-code.agents` and `programs.claude-code.agentsDir`" + "Cannot specify both `programs.claude-code.commands` and `programs.claude-code.commandsDir`" ]; } diff --git a/tests/modules/programs/claude-code/commands-dir.nix b/tests/modules/programs/claude-code/commands-dir.nix new file mode 100644 index 00000000..d988248e --- /dev/null +++ b/tests/modules/programs/claude-code/commands-dir.nix @@ -0,0 +1,14 @@ +{ + programs.claude-code = { + enable = true; + commandsDir = ./commands; + }; + + nmt.script = '' + assertFileExists home-files/.claude/commands/test-command.md + assertLinkExists home-files/.claude/commands/test-command.md + assertFileContent \ + home-files/.claude/commands/test-command.md \ + ${./commands/test-command.md} + ''; +} diff --git a/tests/modules/programs/claude-code/commands/test-command.md b/tests/modules/programs/claude-code/commands/test-command.md new file mode 100644 index 00000000..9a4649c1 --- /dev/null +++ b/tests/modules/programs/claude-code/commands/test-command.md @@ -0,0 +1,6 @@ +--- +allowed-tools: Bash(echo:*) +description: Test command for commandsDir functionality +--- + +This is a test command to verify that commandsDir works correctly. \ No newline at end of file diff --git a/tests/modules/programs/claude-code/default.nix b/tests/modules/programs/claude-code/default.nix index 138c3633..219fb36e 100644 --- a/tests/modules/programs/claude-code/default.nix +++ b/tests/modules/programs/claude-code/default.nix @@ -6,4 +6,5 @@ claude-code-memory-management = ./memory-management.nix; claude-code-memory-from-source = ./memory-from-source.nix; claude-code-agents-dir = ./agents-dir.nix; + claude-code-commands-dir = ./commands-dir.nix; }