|
1 | | -{ config, lib, ... }: |
| 1 | +{ config, lib, pkgs, ... }: |
2 | 2 |
|
3 | 3 | let |
4 | 4 | cfg = config.programs.claude-skills; |
|
26 | 26 | # Private skills via git (activation script) |
27 | 27 | home.activation.syncPrivateSkills = lib.mkIf cfg.enablePrivate ( |
28 | 28 | lib.hm.dag.entryAfter [ "writeBoundary" ] '' |
29 | | - SKILLS_DIR="$HOME/.claude/skills" |
30 | | - CACHE_DIR="$HOME/.cache/claude-private-skills" |
31 | | -
|
32 | | - if [ ! -d "$CACHE_DIR" ]; then |
33 | | - git clone -b ${cfg.privateRepo.ref} ${cfg.privateRepo.url} "$CACHE_DIR" |
34 | | - else |
35 | | - git -C "$CACHE_DIR" pull --ff-only origin ${cfg.privateRepo.ref} || true |
36 | | - fi |
37 | | -
|
38 | | - # Symlink each private skill into ~/.claude/skills/ |
39 | | - for skill in "$CACHE_DIR"/*/; do |
40 | | - [ -d "$skill" ] || continue |
41 | | - skill_name=$(basename "$skill") |
42 | | - ln -sfn "$skill" "$SKILLS_DIR/$skill_name" |
43 | | - done |
| 29 | + SKILLS_DIR="$HOME/.claude/skills" |
| 30 | + CACHE_DIR="$HOME/.cache/claude-private-skills" |
| 31 | + MANAGED_LINKS_FILE="$HOME/.cache/claude-private-skills-managed-links" |
| 32 | + JQ_BIN="${pkgs.jq}/bin/jq" |
| 33 | +
|
| 34 | + mkdir -p "$SKILLS_DIR" |
| 35 | + mkdir -p "$(dirname "$MANAGED_LINKS_FILE")" |
| 36 | +
|
| 37 | + if [ ! -d "$CACHE_DIR" ]; then |
| 38 | + git clone -b ${cfg.privateRepo.ref} ${cfg.privateRepo.url} "$CACHE_DIR" |
| 39 | + else |
| 40 | + git -C "$CACHE_DIR" pull --ff-only origin ${cfg.privateRepo.ref} || true |
| 41 | + fi |
| 42 | +
|
| 43 | + # Clean only links that this activation previously managed. |
| 44 | + if [ -f "$MANAGED_LINKS_FILE" ]; then |
| 45 | + while IFS= read -r old_link; do |
| 46 | + [ -L "$old_link" ] && rm -f "$old_link" |
| 47 | + done < "$MANAGED_LINKS_FILE" |
| 48 | + fi |
| 49 | + : > "$MANAGED_LINKS_FILE" |
| 50 | +
|
| 51 | + # Discover skills recursively. A skill is any directory containing SKILL.md. |
| 52 | + while IFS= read -r skill_md; do |
| 53 | + skill_dir="$(dirname "$skill_md")" |
| 54 | + manifest="$skill_dir/skill.json" |
| 55 | + skill_name="$(basename "$skill_dir")" |
| 56 | +
|
| 57 | + enabled="true" |
| 58 | + destinations="$SKILLS_DIR" |
| 59 | +
|
| 60 | + # Optional per-skill metadata: |
| 61 | + # { |
| 62 | + # "enabled": true, |
| 63 | + # "destinations": ["~/.claude/skills", "~/work/byoc/.claude/skills"] |
| 64 | + # } |
| 65 | + if [ -f "$manifest" ]; then |
| 66 | + enabled="$("$JQ_BIN" -r '.enabled // true' "$manifest" 2>/dev/null || echo true)" |
| 67 | + if [ "$enabled" != "true" ]; then |
| 68 | + continue |
| 69 | + fi |
| 70 | +
|
| 71 | + destinations="$("$JQ_BIN" -r '(.destinations // ["~/.claude/skills"])[]' "$manifest" 2>/dev/null || echo "~/.claude/skills")" |
| 72 | + fi |
| 73 | +
|
| 74 | + while IFS= read -r destination; do |
| 75 | + [ -n "$destination" ] || continue |
| 76 | +
|
| 77 | + # Expand "~" to $HOME for destination paths. |
| 78 | + case "$destination" in |
| 79 | + "~") |
| 80 | + destination="$HOME" |
| 81 | + ;; |
| 82 | + "~/"*) |
| 83 | + destination="$HOME/$(printf '%s' "$destination" | sed 's#^~/##')" |
| 84 | + ;; |
| 85 | + esac |
| 86 | +
|
| 87 | + mkdir -p "$destination" |
| 88 | + target="$destination/$skill_name" |
| 89 | + ln -sfn "$skill_dir" "$target" |
| 90 | + echo "$target" >> "$MANAGED_LINKS_FILE" |
| 91 | + done <<EOF |
| 92 | + $destinations |
| 93 | + EOF |
| 94 | + done <<EOF |
| 95 | + $(find "$CACHE_DIR" -type f -name SKILL.md) |
| 96 | + EOF |
44 | 97 | '' |
45 | 98 | ); |
46 | 99 | }; |
|
0 commit comments