Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config/cursor/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,28 @@
home.activation.cursorHooks = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
$DRY_RUN_CMD ${pkgs.bash}/bin/bash "${./activate.sh}" "${./hooks.json}"
'';

home.file.".cursor/hooks/with-env.sh" = {
source = ./hooks/with-env.sh;
executable = true;
force = true;
};

home.file.".cursor/hooks/security.sh" = {
source = ./hooks/security.sh;
executable = true;
force = true;
};

home.file.".cursor/hooks/notify.sh" = {
source = ./hooks/notify.sh;
executable = true;
force = true;
};

home.file.".cursor/hooks/pushover.sh" = {
source = ./hooks/pushover.sh;
executable = true;
force = true;
};
}
42 changes: 38 additions & 4 deletions config/cursor/hooks.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
{
"version": 1,
"hooks": {
"beforeShellExecution": [
{
"command": "$HOME/.cursor/hooks/security.sh",
"timeout": 5
},
{
"command": "$HOME/.cursor/hooks/with-env.sh $HOME/dotfiles/config/shared/hooks/block-git-push.sh",
"timeout": 5
},
{
"command": "$HOME/.cursor/hooks/with-env.sh $HOME/dotfiles/config/shared/hooks/block-gh-settings.sh",
"timeout": 5
},
{
"command": "$HOME/.cursor/hooks/notify.sh",
"timeout": 3
},
{
"command": "$HOME/.cursor/hooks/pushover.sh",
"timeout": 6
}
],
"afterFileEdit": [
{
"command": "git-ai checkpoint cursor --hook-input stdin"
"command": "$HOME/.cargo/bin/git-ai checkpoint cursor --hook-input stdin",
"timeout": 15
}
],
"beforeSubmitPrompt": [
{
"command": "git-ai checkpoint cursor --hook-input stdin"
"command": "$HOME/.cargo/bin/git-ai checkpoint cursor --hook-input stdin",
"timeout": 15
}
],
"stop": [
Comment thread
shunkakinoki marked this conversation as resolved.
Resolved
{
"command": "$HOME/.cursor/hooks/notify.sh",
"timeout": 3
},
{
"command": "$HOME/.cursor/hooks/pushover.sh",
"timeout": 6
}
]
},
"version": 1
}
}
61 changes: 61 additions & 0 deletions config/cursor/hooks/notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

# Cursor local notification hook
# Sends native OS notifications via the shared notify-local script.
# Skips local popups when Pushover is configured (pushover.sh handles those).

export PATH="$HOME/.cargo/bin:/etc/profiles/per-user/shunkakinoki/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"

command -v jq >/dev/null 2>&1 || exit 0

if [[ -z ${PUSHOVER_API_TOKEN:-} ]] || [[ -z ${PUSHOVER_USER_KEY:-} ]]; then
if [[ -f "$HOME/dotfiles/.env" ]]; then
set -a
# shellcheck source=/dev/null
source "$HOME/dotfiles/.env" 2>/dev/null
set +a
fi
fi

if [[ -n ${PUSHOVER_API_TOKEN:-} ]] && [[ -n ${PUSHOVER_USER_KEY:-} ]]; then
exit 0
fi
Comment thread
shunkakinoki marked this conversation as resolved.
Resolved

input=$(cat)

notify() {
local message="$1"
local sound="${2:-Sonar}"
local notifier

[[ -z $message ]] && return

notifier="$(command -v notify-local 2>/dev/null || true)"
if [[ -z $notifier ]] && [[ -x "$HOME/.local/scripts/notify-local" ]]; then
notifier="$HOME/.local/scripts/notify-local"
fi

[[ -n $notifier ]] && "$notifier" "Cursor" "$message" "$sound"
}

event=$(echo "$input" | jq -r '.hook_event_name // empty' 2>/dev/null)

case "$event" in
stop)
status=$(echo "$input" | jq -r '.status // "completed"' 2>/dev/null)
case "$status" in
completed) notify "Work completed" ;;
aborted) notify "Work aborted" "Basso" ;;
error) notify "Work errored" "Basso" ;;
esac
;;
beforeShellExecution)
command=$(echo "$input" | jq -r '.command // empty' 2>/dev/null)
if echo "$command" | grep -qiE 'rm -rf|drop database|truncate|DELETE FROM|format'; then
risky_msg=$(echo "$command" | head -c 100)
notify "Risky: ${risky_msg}" "Basso"
fi
;;
esac

exit 0
41 changes: 41 additions & 0 deletions config/cursor/hooks/pushover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# Cursor Pushover notification hook
# Sends notifications to phone/watch via the shared pushover-notify script.

export PATH="$HOME/.cargo/bin:/etc/profiles/per-user/shunkakinoki/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"

command -v jq >/dev/null 2>&1 || exit 0

input=$(cat)

pushover="$(command -v pushover-notify 2>/dev/null || true)"
if [[ -z $pushover ]] && [[ -x "$HOME/.local/scripts/pushover-notify" ]]; then
pushover="$HOME/.local/scripts/pushover-notify"
fi
[[ -z $pushover ]] && exit 0

HOSTNAME=$(scutil --get ComputerName 2>/dev/null || hostname -s 2>/dev/null || echo "unknown")
Comment thread
shunkakinoki marked this conversation as resolved.
Resolved
CWD=$(echo "$input" | jq -r '.cwd // (.workspace_roots[0] // empty)' 2>/dev/null | sed "s|$HOME|~|")

event=$(echo "$input" | jq -r '.hook_event_name // empty' 2>/dev/null)

case "$event" in
stop)
status=$(echo "$input" | jq -r '.status // "completed"' 2>/dev/null)
case "$status" in
completed) "$pushover" "Cursor" "[$HOSTNAME] Work completed${CWD:+ in $CWD}" 0 ;;
aborted) "$pushover" "Cursor" "[$HOSTNAME] Work aborted${CWD:+ in $CWD}" 0 ;;
error) "$pushover" "Cursor" "[$HOSTNAME] Work errored${CWD:+ in $CWD}" 1 ;;
esac
;;
beforeShellExecution)
command=$(echo "$input" | jq -r '.command // empty' 2>/dev/null)
if echo "$command" | grep -qiE 'rm -rf|drop database|truncate|DELETE FROM|format'; then
risky_msg=$(echo "$command" | head -c 100)
"$pushover" "Cursor" "[$HOSTNAME] Risky: ${risky_msg}" 1
fi
;;
esac

exit 0
56 changes: 56 additions & 0 deletions config/cursor/hooks/security.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

# Cursor Security Hook (beforeShellExecution)
# Blocks dangerous Bash commands by checking against deny patterns.
# Mirrors codex/copilot security.sh; Cursor sends `command` at top level.
# Returns exit code 2 to block, exit code 0 to allow.

export PATH="$HOME/.cargo/bin:/etc/profiles/per-user/shunkakinoki/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"

set -euo pipefail

command -v jq >/dev/null 2>&1 || exit 0

input=$(cat)

command=$(echo "$input" | jq -r '
.command
// .tool.input.command
// .tool_input.command
// empty
' 2>/dev/null)
[[ -z $command ]] && exit 0

deny_patterns=(
"chmod -R 777"
"dd if="
"docker system prune -a"
"docker system prune -f"
"git push --force origin main"
"git push --force origin master"
"git push --force-with-lease origin main"
"git push --force-with-lease origin master"
"git push -f origin main"
"git push -f origin master"
"mkfs"
"rm -rf /*"
"rm -rf ~/*"
"sudo"
)

IFS=$'\n' read -r -d '' -a segments < <(echo "$command" | sed -E 's/[;&|]+/\n/g' && printf '\0') || true

for segment in "${segments[@]}"; do
segment=$(echo "$segment" | xargs 2>/dev/null) || continue
[[ -z $segment ]] && continue

for pattern in "${deny_patterns[@]}"; do
if [[ $segment == $pattern* ]]; then
jq -n --arg msg "Command '$segment' matches deny pattern '$pattern'" \
'{permission: "deny", user_message: $msg, agent_message: $msg}'
exit 2
Comment thread
shunkakinoki marked this conversation as resolved.
Resolved
fi
done
done

exit 0
8 changes: 8 additions & 0 deletions config/cursor/hooks/with-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# Cursor on macOS launches GUI apps with a minimal PATH that excludes
# ~/.cargo/bin, ~/.nix-profile/bin, /opt/homebrew/bin, etc.
# This wrapper exports a full PATH then execs the given command.
# Usage (in hooks.json):
# {"command": "$HOME/.cursor/hooks/with-env.sh /path/to/hook.sh"}
export PATH="$HOME/.cargo/bin:/etc/profiles/per-user/shunkakinoki/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"
exec "$@"
Loading