feat: skm, pin agent skills from git repos via Nix

CLI (add, remove, list, outdated, update, import) over a skills.json manifest and a skills.lock.json pinned with nix flake prefetch, plus a home-manager module that links each locked skill via builtins.fetchTree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
johannesandClaude Opus 5.5 committed 2026-10-23 14:04:50 +02:00
commit 65a96ada8e
26 files changed
+2843

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
// Package fetch resolves flake refs to pinned store paths.
package fetch
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"os/exec"
"strings"
)
// Result is a source fetched into the Nix store.
type Result struct {
StorePath string
// Locked holds the attrs to pass to builtins.fetchTree, including narHash.
Locked map[string]any
}
// Rev returns the locked commit.
func (r Result) Rev() string {
rev, _ := r.Locked["rev"].(string)
return rev
}
type Fetcher interface {
Prefetch(ctx context.Context, flakeRef string) (Result, error)
}
// WithParam adds or replaces a query parameter on a flake ref, e.g. ref or rev.
func WithParam(flakeRef, key, value string) string {
return editQuery(flakeRef, func(q url.Values) { q.Set(key, value) })
}
// AtRev pins a flake ref to a commit. Shorthand refs (github:, gitlab:, sourcehut:)
// reject ref and rev together, so their ref is dropped; git refs keep it because
// the rev must be reachable from it.
func AtRev(flakeRef, rev string) string {
base, _, _ := strings.Cut(flakeRef, "?")
shorthand := !strings.Contains(base, "://")
return editQuery(flakeRef, func(q url.Values) {
if shorthand {
q.Del("ref")
}
q.Set("rev", rev)
})
}
func editQuery(flakeRef string, edit func(url.Values)) string {
base, query, _ := strings.Cut(flakeRef, "?")
q, err := url.ParseQuery(query)
if err != nil {
q = url.Values{}
}
edit(q)
if len(q) == 0 {
return base
}
return base + "?" + q.Encode()
}
// Nix fetches with `nix flake prefetch`.
type Nix struct {
// Bin is the nix executable; empty means "nix" from PATH.
Bin string
}
func (n Nix) Prefetch(ctx context.Context, flakeRef string) (Result, error) {
bin := n.Bin
if bin == "" {
bin = "nix"
}
cmd := exec.CommandContext(ctx, bin,
"--extra-experimental-features", "nix-command flakes",
"flake", "prefetch", "--json", flakeRef)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return Result{}, fmt.Errorf("nix flake prefetch %s: %w: %s", flakeRef, err, strings.TrimSpace(stderr.String()))
}
var raw struct {
Hash string `json:"hash"`
Locked map[string]any `json:"locked"`
StorePath string `json:"storePath"`
}
if err := json.Unmarshal(out, &raw); err != nil {
return Result{}, fmt.Errorf("parse prefetch output for %s: %w", flakeRef, err)
}
if raw.Hash == "" || raw.StorePath == "" || raw.Locked == nil {
return Result{}, fmt.Errorf("incomplete prefetch output for %s: %s", flakeRef, out)
}
// prefetch reports the hash beside locked, but fetchTree needs it inside.
raw.Locked["narHash"] = raw.Hash
return Result{StorePath: raw.StorePath, Locked: raw.Locked}, nil
}
+84
View File
@@ -0,0 +1,84 @@
package fetch
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestWithParam(t *testing.T) {
cases := []struct{ in, key, val, want string }{
{"github:o/r", "ref", "main", "github:o/r?ref=main"},
{"git+ssh://git@h/o/r?ref=main", "rev", "abc", "git+ssh://git@h/o/r?ref=main&rev=abc"},
{"github:o/r?ref=old", "ref", "v2", "github:o/r?ref=v2"},
}
for _, c := range cases {
if got := WithParam(c.in, c.key, c.val); got != c.want {
t.Errorf("WithParam(%q, %q, %q) = %q, want %q", c.in, c.key, c.val, got, c.want)
}
}
}
func TestAtRev(t *testing.T) {
cases := map[string]string{
"github:o/r?ref=main": "github:o/r?rev=abc",
"git+ssh://h/o/r?ref=main": "git+ssh://h/o/r?ref=main&rev=abc",
"git+file:///tmp/r": "git+file:///tmp/r?rev=abc",
}
for in, want := range cases {
if got := AtRev(in, "abc"); got != want {
t.Errorf("AtRev(%q) = %q, want %q", in, got, want)
}
}
}
// TestNixPrefetch runs the real nix against a local git repo. No network.
func TestNixPrefetch(t *testing.T) {
// Not on PATH inside the build sandbox, where nix can't run anyway.
if _, err := exec.LookPath("nix"); err != nil {
t.Skip("nix not on PATH")
}
repo := t.TempDir()
write(t, filepath.Join(repo, "tdd", "SKILL.md"), "# tdd\n")
git(t, repo, "init", "-q", "-b", "main")
git(t, repo, "add", ".")
git(t, repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", "init")
head := strings.TrimSpace(git(t, repo, "rev-parse", "HEAD"))
res, err := Nix{}.Prefetch(context.Background(), WithParam("git+file://"+repo, "ref", "main"))
if err != nil {
t.Fatal(err)
}
if res.Rev() != head {
t.Errorf("rev = %q, want %q", res.Rev(), head)
}
if h, _ := res.Locked["narHash"].(string); !strings.HasPrefix(h, "sha256-") {
t.Errorf("narHash = %q, want sha256-…", h)
}
if _, err := os.Stat(filepath.Join(res.StorePath, "tdd", "SKILL.md")); err != nil {
t.Errorf("skill missing from store path: %v", err)
}
}
func write(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func git(t *testing.T, dir string, args ...string) string {
t.Helper()
out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
return string(out)
}