$ show nix-community/naersk
Naersk is a Nix library that automates building Rust projects by parsing `Cargo.lock` and creating Nix derivations for dependencies and the final binary. It leverages Nix's sandboxing and caching to achieve reproducible and incremental builds. The library exposes a single entry point, `buildPackage`, which accepts a source directory (or attribute set) and returns a derivation. Internally, it implements a two-phase build: first compiling all dependencies into a cached derivation, then compiling the user's crate with those pre-built dependencies. This architecture optimizes rebuild times when only the application code changes. Key capabilities include support for git dependencies, workspace crates, custom `rustc`/`cargo` toolchains, cross-compilation (via `CARGO_BUILD_TARGET`), and automatic crate-specific overrides (e.g., adding `openssl` and `pkg-config` for the `openssl-sys` crate). The codebase is implemented entirely in pure Nix, showcasing advanced Nix patterns like recursive toml serialization, generic closure traversal, and dynamic source stub generation. The project is actively maintained, has a comprehensive test suite (fast and slow), and is widely used in the Nix ecosystem.
The LLM identified 2 files worth highlighting for reuse:
A pure Nix function that serializes Nix attribute sets into TOML format. It recursively handles strings, booleans, integers, lists, nested sets, and list-of-attrs (for TOML arrays of tables). The implementation uses string matching and type inference to correctly format keys and values, including quoting and escaping. This component is self-contained and could be extracted as a standalone library for any Nix project needing TOML output, demonstrating advanced Nix string operations and recursion.
A collection of utility functions central to Naersk's core logic: `mkVersions` extracts crate checksums from `Cargo.lock`; `findGitDependencies` parses git dependency entries (branches, tags, revisions); `dummySrc` generates a minimal source tree to satisfy Cargo's build scripts; and helper functions for dependency graph traversal. These functions are well-abstracted and could be reused in other Nix-based tooling for analyzing Rust project dependencies. The `findGitDependencies` function in particular handles edge cases like URL-encoded branches (v4 lock format) and symlink resolution.