fedora-csb-system-manager
1#!/usr/bin/env bash
2#
3# setup-hooks.sh - Configure Claude Code hooks to use Nix-built binaries
4#
5# This script updates ~/.claude/settings.json to reference the correct
6# hook binaries from the Nix store (via PATH).
7#
8# Usage:
9# ./setup-hooks.sh # Update settings.json
10# ./setup-hooks.sh --dry-run # Show what would be changed
11
12set -euo pipefail
13
14SETTINGS_FILE="${HOME}/.claude/settings.json"
15SETTINGS_BACKUP="${HOME}/.claude/settings.json.backup"
16
17# Determine if this is a dry run
18DRY_RUN=false
19if [[ "${1:-}" == "--dry-run" ]]; then
20 DRY_RUN=true
21fi
22
23# Check if settings.json exists
24if [[ ! -f "$SETTINGS_FILE" ]]; then
25 echo "Error: $SETTINGS_FILE does not exist"
26 echo "Please create it first or run Claude Code to initialize it"
27 exit 1
28fi
29
30# Backup existing settings
31if [[ "$DRY_RUN" == false ]]; then
32 cp "$SETTINGS_FILE" "$SETTINGS_BACKUP"
33 echo "Backed up existing settings to: $SETTINGS_BACKUP"
34fi
35
36# Read current settings
37CURRENT_SETTINGS=$(cat "$SETTINGS_FILE")
38
39# Build new hooks configuration
40# Note: The binaries should be in PATH after installing via Nix
41NEW_HOOKS=$(cat <<'EOF'
42{
43 "hooks": [
44 {
45 "type": "command",
46 "command": "claude-hooks-initialize-session"
47 }
48 ]
49}
50EOF
51)
52
53POST_TOOL_HOOKS=$(cat <<'EOF'
54{
55 "hooks": [
56 {
57 "type": "command",
58 "command": "claude-hooks-capture-tool-output"
59 }
60 ]
61}
62EOF
63)
64
65SESSION_END_HOOKS=$(cat <<'EOF'
66{
67 "hooks": [
68 {
69 "type": "command",
70 "command": "claude-hooks-save-session"
71 }
72 ]
73}
74EOF
75)
76
77# Use jq to update the settings.json with proper JSON merging
78UPDATED_SETTINGS=$(echo "$CURRENT_SETTINGS" | jq --argjson sessionStart "$NEW_HOOKS" \
79 --argjson postTool "$POST_TOOL_HOOKS" \
80 --argjson sessionEnd "$SESSION_END_HOOKS" \
81 '.hooks.SessionStart = [$sessionStart] | .hooks.PostToolUse = [$postTool] | .hooks.SessionEnd = [$sessionEnd]')
82
83if [[ "$DRY_RUN" == true ]]; then
84 echo "=== DRY RUN - No changes made ==="
85 echo ""
86 echo "Would update $SETTINGS_FILE with:"
87 echo "$UPDATED_SETTINGS" | jq '.'
88 echo ""
89 echo "Run without --dry-run to apply these changes"
90else
91 echo "$UPDATED_SETTINGS" | jq '.' > "$SETTINGS_FILE"
92 echo "Successfully updated $SETTINGS_FILE"
93 echo ""
94 echo "Hook binaries configured:"
95 echo " - SessionStart: claude-hooks-initialize-session"
96 echo " - PostToolUse: claude-hooks-capture-tool-output"
97 echo " - SessionEnd: claude-hooks-save-session"
98 echo ""
99 echo "Make sure claude-hooks is installed:"
100 echo " nix profile install .#claude-hooks"
101 echo ""
102 echo "Or add to your home-manager configuration:"
103 echo " home.packages = [ pkgs.claude-hooks ];"
104fi