This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
crate2nix generates Nix build files (Cargo.nix) for Rust/Cargo projects,
enabling crate-by-crate hermetic builds with Nix. It reads
Cargo.toml/Cargo.lock, resolves dependencies, prefetches hashes, and
renders Nix derivations via Tera templates.
The project uses a Nix flake with direnv integration. Enter the dev shell
via direnv allow or nix develop. All shell scripts (e.g., cargo.sh,
run_tests.sh) auto-enter the pure nix-shell if not already inside.
nix build # Build crate2nix via Nix
./cargo.sh build # Build with cargo (inside nix-shell)./run_tests.sh # Full test suite
./run_tests.sh --no-cargo-build # Skip cargo build/test steps (Nix-only)
./cargo.sh test # Rust unit tests only
./cargo.sh test test_name # Single Rust test
./nix-test.sh ./crate2nix/templates/nix/crate2nix/tests/default.nix
nix flake check # Nix integration tests./cargo.sh clippy # Rust linting
./cargo.sh fmt # Rust formatting
./nixpkgs-fmt.sh \
./{,nix/}{,*/}*.nix \
./crate2nix/templates/nix/crate2nix/{*.nix,tests/*.nix} \
./sample_projects/*/[[:lower:]]*.nix # Nix formatting./regenerate_cargo_nix.sh # Regenerate all Cargo.nix filesnix build .#docs # Build static docs site
cd docs && npm run dev # Local dev server
cd docs && npm run build # Build via npm directlyThe CLI entry point is main.rs using structopt. The main command is
generate.
Core pipeline in lib.rs via BuildInfo::for_config():
- metadata.rs - Calls
cargo metadata, merges results from multiple Cargo.toml files intoIndexedMetadata - resolve.rs - Resolves each package into
CrateDerivationwith source type (CratesIo,Git,Registry, etc.), dependencies (normal/build/dev), platform conditions, and features - lock.rs - Parses
Cargo.lockto extract checksums, avoiding unnecessary prefetches - prefetch.rs - Prefetches SHA256 hashes via
nix-prefetch-url/nix-prefetch-git, caches incrate-hashes.jsonandregistry-hashes.json - render.rs - Renders output using Tera templates
- config.rs - Reads optional
crate2nix.jsonfor out-of-tree sources and configuration - sources.rs - Manages source fetching for crates.io, git, and alternative registries
Cargo.nix.tera- Main output template; generates a Nix file that providesrootCrate,workspaceMembers, and per-crate derivationsnix/- Template includes for the build infrastructure (crate building, feature resolution, etc.)
tools.nix- Public Nix API; providesgeneratedCargoNixhelper for generating Cargo.nix in Nix buildstests.nix- Integration test harness; definesbuildTestfunction and runs allsample_projects/default.nix- Package derivation for crate2nix itself
30+ test projects covering various scenarios: binary/library crates,
features, git dependencies, workspaces, cross-compilation, cdylib,
codegen, etc. Each has a pregenerated Cargo.nix that gets regenerated
by regenerate_cargo_nix.sh.
- Rust edition 2021,
#![forbid(unsafe_code)],#![deny(missing_docs)]in lib.rs - Rust formatting: edition 2018 style with reordered imports
(
rustfmt.toml) - Nix formatting:
nixpkgs-fmt - License: Apache-2.0 (Rust crate), dual Apache-2.0/MIT (repo)
- Version tags use bare numbers (e.g.,
0.14.2, notv0.14.2) - The
Cargo.nixfiles are checked into git;run_tests.shverifies no uncommitted changes after regeneration