Nix perf

From juspay/skills · View interactive version

Nix perf is an AI agent skill from the GitHub repository juspay/skills. Use this when a flake's `nix develop` / `direnv allow` / `nix flake archive` is slow on a fresh checkout (the "first time takes 10 minutes" complaint). Diagnoses where the time actually lives and how to shrink the flake.lock input graph without changing build outputs.

How to use this skill

Skill file

Diagnosing slow flake first-fetch

Triage which operation is actually slow

Before optimizing anything, pin down which command the user runs and time each on a fresh state (cleared eval cache + cold /nix/store for an honest measurement):

| Command | What it does | Bottleneck |

|---|---|---|

| nix develop --command true | Lazy: fetches only inputs whose outputs the devShell eval touches | Eval + minimal substitution |

| nix print-dev-env | Same laziness as above; what plain nix-direnv use flake invokes | Same as above |

| nix flake archive --no-write-lock-file | Eager: fetches every node in flake.lock into the store | Total lock-graph size |

| nix build .#default | Eval + realize closure | Eval + builds not in cache |

The "direnv takes 10 minutes" complaint usually maps to nix flake archive (nix-direnv with input pre-caching enabled, or wrappers that warm the store for offline use), not to nix print-dev-env. Confirm which one is slow before you start optimizing — lazy eval is invariant to lockfile bloat; eager archive isn't.

Inspect lockfile shape

Use nix run nixpkgs#jq on flake.lock to extract:

Reduce the graph with `follows`

For each transitive duplicate that has a sibling at the root, add inputs.X.inputs.Y.follows = "Y". You can chain: inputs.A.inputs.B.inputs.C.follows = "C". The rules:

After each change run nix flake lock and re-measure both node count and unique-narHash count. Commit only changes that move the unique-narHash number.

Measurement methodology

For cold-fetch numbers, use a fresh VM. On juspay infrastructure: pu create --name X, ship the repo snapshot with git archive <ref> | ssh X 'tar -x -C /tmp/...', time the operation, then pu destroy X and recreate for the next sample so /nix/store starts empty. Don't try to simulate cold by deleting paths from a live /nix/store — the nix-daemon protects flake-input paths as live roots and --ignore-liveness doesn't always evict them.

For eval-only numbers (no fetch), keep /nix/store warm and clear ~/.cache/nix/eval-cache-v* between runs. Median of 3–5 runs.

Always record both numbers separately. Eval improvements and fetch improvements look very different and conflating them produces misleading PR descriptions.

Diminishing returns

Stop optimizing when the remaining lockfile duplicates are at distinct revisions (different repos pinning different nixpkgs revs, for instance). Further consolidation requires bumping inputs, which is a separate decision from input-graph hygiene.

Companion docs

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.