diff --git a/modules/programs/claude-code.nix b/modules/programs/claude-code.nix index 4376c0a91..265fc97b6 100644 --- a/modules/programs/claude-code.nix +++ b/modules/programs/claude-code.nix @@ -156,6 +156,26 @@ in }; }; + hooks = lib.mkOption { + type = lib.types.attrsOf lib.types.lines; + default = { }; + description = '' + Custom hooks for Claude Code. + The attribute name becomes the hook filename, and the value is the hook script content. + Hooks are stored in .claude/hooks/ directory. + ''; + example = { + pre-edit = '' + #!/usr/bin/env bash + echo "About to edit file: $1" + ''; + post-commit = '' + #!/usr/bin/env bash + echo "Committed with message: $1" + ''; + }; + }; + memory = { text = lib.mkOption { type = lib.types.nullOr lib.types.lines; @@ -206,6 +226,16 @@ in example = lib.literalExpression "./commands"; }; + hooksDir = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = '' + Path to a directory containing hook files for Claude Code. + Hook files from this directory will be symlinked to .claude/hooks/. + ''; + example = lib.literalExpression "./hooks"; + }; + mcpServers = lib.mkOption { type = lib.types.attrsOf jsonFormat.type; default = { }; @@ -265,6 +295,10 @@ in assertion = !(cfg.commands != { } && cfg.commandsDir != null); message = "Cannot specify both `programs.claude-code.commands` and `programs.claude-code.commandsDir`"; } + { + assertion = !(cfg.hooks != { } && cfg.hooksDir != null); + message = "Cannot specify both `programs.claude-code.hooks` and `programs.claude-code.hooksDir`"; + } ]; programs.claude-code.finalPackage = @@ -319,6 +353,11 @@ in source = cfg.commandsDir; recursive = true; }; + + ".claude/hooks" = lib.mkIf (cfg.hooksDir != null) { + source = cfg.hooksDir; + recursive = true; + }; } // lib.mapAttrs' ( name: content: @@ -331,7 +370,13 @@ in lib.nameValuePair ".claude/commands/${name}.md" { text = content; } - ) cfg.commands; + ) cfg.commands + // lib.mapAttrs' ( + name: content: + lib.nameValuePair ".claude/hooks/${name}" { + text = content; + } + ) cfg.hooks; }; }; } diff --git a/tests/modules/programs/claude-code/assertion.nix b/tests/modules/programs/claude-code/assertion.nix index c5b3fff7f..73f936904 100644 --- a/tests/modules/programs/claude-code/assertion.nix +++ b/tests/modules/programs/claude-code/assertion.nix @@ -32,6 +32,12 @@ test-command = "test content"; }; commandsDir = ./commands; + + # assert fail: cannot set hooks and hooksDir at the same time. + hooks = { + test-hook = "test content"; + }; + hooksDir = ./hooks; }; test.asserts.assertions.expected = [ @@ -39,5 +45,6 @@ "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`" + "Cannot specify both `programs.claude-code.hooks` and `programs.claude-code.hooksDir`" ]; } diff --git a/tests/modules/programs/claude-code/default.nix b/tests/modules/programs/claude-code/default.nix index 219fb36e5..f7bf30fb1 100644 --- a/tests/modules/programs/claude-code/default.nix +++ b/tests/modules/programs/claude-code/default.nix @@ -7,4 +7,5 @@ claude-code-memory-from-source = ./memory-from-source.nix; claude-code-agents-dir = ./agents-dir.nix; claude-code-commands-dir = ./commands-dir.nix; + claude-code-hooks-dir = ./hooks-dir.nix; } diff --git a/tests/modules/programs/claude-code/full-config.nix b/tests/modules/programs/claude-code/full-config.nix index 69dd8e136..8841865c1 100644 --- a/tests/modules/programs/claude-code/full-config.nix +++ b/tests/modules/programs/claude-code/full-config.nix @@ -104,6 +104,17 @@ Focus on user-friendly explanations and examples. ''; }; + + hooks = { + pre-edit = '' + #!/usr/bin/env bash + echo "About to edit file: $1" + ''; + post-commit = '' + #!/usr/bin/env bash + echo "Committed with message: $1" + ''; + }; }; nmt.script = '' @@ -122,5 +133,11 @@ assertFileExists home-files/.claude/commands/commit.md assertFileContent home-files/.claude/commands/commit.md ${./expected-commit} + + assertFileExists home-files/.claude/hooks/pre-edit + assertFileRegex home-files/.claude/hooks/pre-edit "About to edit file" + + assertFileExists home-files/.claude/hooks/post-commit + assertFileRegex home-files/.claude/hooks/post-commit "Committed with message" ''; } diff --git a/tests/modules/programs/claude-code/hooks-dir.nix b/tests/modules/programs/claude-code/hooks-dir.nix new file mode 100644 index 000000000..3d6446092 --- /dev/null +++ b/tests/modules/programs/claude-code/hooks-dir.nix @@ -0,0 +1,14 @@ +{ + programs.claude-code = { + enable = true; + hooksDir = ./hooks; + }; + + nmt.script = '' + assertFileExists home-files/.claude/hooks/test-hook + assertLinkExists home-files/.claude/hooks/test-hook + assertFileContent \ + home-files/.claude/hooks/test-hook \ + ${./hooks/test-hook} + ''; +} diff --git a/tests/modules/programs/claude-code/hooks/test-hook b/tests/modules/programs/claude-code/hooks/test-hook new file mode 100644 index 000000000..e001278f7 --- /dev/null +++ b/tests/modules/programs/claude-code/hooks/test-hook @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +# Test hook for claude-code +echo "Test hook executed with: $@" \ No newline at end of file