mirror of
https://github.com/nix-community/home-manager.git
synced 2026-01-12 01:59:37 +08:00
claude-code: added 'hooks' and 'hooksDir' options to specify hooks from filesystem
This commit is contained in:
@@ -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;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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`"
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
'';
|
||||
}
|
||||
|
||||
14
tests/modules/programs/claude-code/hooks-dir.nix
Normal file
14
tests/modules/programs/claude-code/hooks-dir.nix
Normal file
@@ -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}
|
||||
'';
|
||||
}
|
||||
3
tests/modules/programs/claude-code/hooks/test-hook
Normal file
3
tests/modules/programs/claude-code/hooks/test-hook
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
# Test hook for claude-code
|
||||
echo "Test hook executed with: $@"
|
||||
Reference in New Issue
Block a user