agentleFS
gurinderugurinderu/craft/skills/nix-packaging/SKILL.md

nix-packaging

Skill1 starsChanged 41 days agoMITView on GitHub

Nix derivations — stdenv.mkDerivation and the phase model, fixed-output hashes for src fetchers, language-builder dependency hashes (cargoHash, vendorHash, npmDepsHash), the lib.fakeHash → copy-real-hash workflow, and meta attributes. Use when writing, debugging, or reviewing a derivation, packaging a Rust/Go/Python/Node project, or fixing a hash mismatch. Triggers: mkDerivation, buildRustPackage, buildGoModule, vendorHash, cargoHash, fetchFromGitHub.

  • Installs packages

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 0 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every fetch* function is a fixed-output derivation: it may access the network, but the result must match the declared hash — any mismatch …

    line 69
  2. Very specific:

    Use hash (SRI, sha256-<base64>) on new code — it is the current preferred form.

    line 88
  3. Very specific:

    Run nix build (or nix-build).

    line 108
  4. Very specific:

    Run nix build again — it succeeds.

    line 115
  5. Very specific:

    Every derivation exposed as a package must carry a meta block.

    line 135
  6. Very specific:

    Do not run cargo fetch, npm install, pip install, or go mod download inside buildPhase or installPhase without pre-vendoring.

    line 155

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Nix Packaging

A derivation is the fundamental build unit in Nix — a description of how to produce a store path from inputs. stdenv.mkDerivation is the generic builder; language ecosystems add buildRustPackage, buildGoModule, etc. on top of it. The rules are the same everywhere: every input must be pinned with a hash, and the build sandbox must never reach the network.

stdenv.mkDerivation and the phase model

The minimal shape:

{ lib, stdenv, fetchFromGitHub }:

stdenv.mkDerivation {
  pname   = "mytool";
  version = "1.2.3";

  src = fetchFromGitHub {
    owner  = "example";
    repo   = "mytool";
    rev    = "v1.2.3";
    hash   = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
  };

  # build inputs available at compile time
  nativeBuildInputs = [ ];
  # runtime dependencies linked into the output
  buildInputs = [ ];

  meta = with lib; {
    description = "A short, imperative sentence describing the tool";
    license     = licenses.mit;
    mainProgram = "mytool";   # the binary nix run exposes
    maintainers = [ ];
  };
}

Nix runs the build through a sequence of phases. The defaults handle the most common patterns; override only what you need:

PhaseDefault actionOverride hook
unpackPhasetar xf $src (or cp for single files)unpackPhase = "...";
patchPhaseApply patches = [ ] listpatchPhase = "...";
configurePhase./configure --prefix=$outconfigurePhase = "...";
buildPhasemakebuildPhase = "...";
checkPhasemake check (skipped unless doCheck = true)checkPhase = "...";
installPhasemake installinstallPhase = "...";
fixupPhasepatchelf, strip, wrap executablesrarely overridden

Override a phase with a Bash string. Inside the string, $out is the derivation's store path:

installPhase = ''
  install -Dm755 result/mytool $out/bin/mytool
'';

Fixed-output hashes — src fetchers

Every fetch* function is a fixed-output derivation: it may access the network, but the result must match the declared hash — any mismatch aborts the build. This makes fetches reproducible.

Common fetchers and their hash field:

# GitHub tarball
src = fetchFromGitHub {
  owner = "org"; repo = "proj"; rev = "v1.0.0";
  hash = "sha256-…";   # SRI format, preferred
};

# Generic URL
src = fetchzip {
  url    = "https://example.com/proj-1.0.0.tar.gz";
  sha256 = "0000000000000000000000000000000000000000000000000000";   # base32 ok too
};

Use hash (SRI, sha256-<base64>) on new code — it is the current preferred form. The older sha256 (base32 / hex) works but is being phased out.

Language-builder dependency hashes

Language builders need a second hash for vendored dependencies. This hash covers the full dependency tree fetched separately from the source, so it is also a fixed-output derivation.

BuilderDep-hash fieldWhat it covers
buildRustPackagecargoHash (current) or vendorHash (alias)Cargo.lock dependency tree
buildGoModulevendorHashGo module graph (go.sum)
buildPythonPackageno single field; use fetchPypi for src or poetry2nix / mach-nix—
buildNpmPackagenpmDepsHashpackage-lock.json dependency tree

The iterative lib.fakeHash → copy-real-hash flow

You never know the hash before the first build. The workflow:

  1. Set the hash field to lib.fakeHash (a known-wrong but syntactically valid hash placeholder).
  2. Run nix build (or nix-build). The build fails with an error like:
    error: hash mismatch in fixed-output derivation '/nix/store/…':
      specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
      got:       sha256-<actual base64 hash>
  3. Copy the got: value back into the expression, replacing lib.fakeHash.
  4. Run nix build again — it succeeds.

Repeat for each hash field independently (src hash, then dep hash).

# Step 1 — placeholder
cargoHash = lib.fakeHash;

# Step 3 — paste the "got:" value
cargoHash = "sha256-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=";

lib.fakeHash is defined in <nixpkgs/lib> — import lib from pkgs or the nixpkgs argument:

{ lib, pkgs, buildRustPackage, ... }:

meta — required in review

Every derivation exposed as a package must carry a meta block. The three fields checked in review:

meta = with lib; {
  description = "Serve HTTP static files from a local directory";   # imperative, no trailing dot
  license     = licenses.mit;        # from lib.licenses — use the attribute, not a string
  mainProgram = "serve";             # enables `nix run` without specifying the binary name
  maintainers = with maintainers; [ ]; # optional but conventional
  homepage    = "https://github.com/org/serve";                     # optional, good practice
};

Common lib.licenses values: mit, asl20, gpl3Only, gpl3Plus, bsd2, bsd3, mpl20, unfree.

Never fetch impurely at build time

The Nix sandbox blocks network access during a normal derivation build. This means:

  • Do not run cargo fetch, npm install, pip install, or go mod download inside buildPhase or installPhase without pre-vendoring.
  • Language builders handle this by fetching dependencies as a separate fixed-output derivation (keyed by the dep hash) before the main build runs. The main build then works offline against the pre-fetched vendor directory.
  • A bare runCommand or mkDerivation has no such mechanism — it gets no network. If you need deps, use the appropriate language builder.

Impure fetches break on every nix build run on a machine that hasn't cached the result, and they make the build non-reproducible. They are a blocking finding in nix-review.

Details → builders.md

Minimal correct derivations for each builder with all required hash fields: builders.md

Boundaries

  • Wiring the derivation into a flake's packages output, pinning nixpkgs, follows → nix-flakes.
  • Review rubric for packaging quality, hash hygiene, meta completeness → nix-review (consumes this skill's rules).
  • Dev shells, mkShell, nix develop patterns → nix-dev-env.
  • NixOS module options, nixosConfigurations → nixos.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.