Skip to content

crate2nix builds your cargo-based rust project crate-by-crate with nix

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

nix-community/crate2nix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,430 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

crate2nix

tests-nix-linux tests-nix-macos Crate

crate2nix generates Nix build files for Rust/Cargo projects, building each crate individually for precise, incremental rebuilds.

  • Incremental CI builds -- only rebuild the crates that actually changed.
  • Full Nix integration -- remote builds, binary caches, Docker images, NixOS modules.
  • Local dev unchanged -- keep using cargo and rust-analyzer as usual.

Quick start

Without installing

nix run nixpkgs#crate2nix -- generate
nix build -f Cargo.nix rootCrate.build

With a flake template

nix flake init --template github:nix-community/crate2nix

Installing

# From nixpkgs
nix profile install nixpkgs#crate2nix

# Latest development version
nix profile install github:nix-community/crate2nix

Then, inside your project:

crate2nix generate        # creates Cargo.nix
nix build -f Cargo.nix rootCrate.build

How it works

crate2nix reads Cargo.toml and Cargo.lock, resolves the full dependency tree via cargo metadata, prefetches source hashes, and renders a Cargo.nix file through Tera templates. The generated file contains one Nix derivation per crate, so Nix rebuilds only what changed.

Two generation strategies are supported:

Strategy Pros Cons
Manual (crate2nix generate) No IFD, full build parallelism Must regenerate when deps change
Auto (Import From Derivation) Always in sync with Cargo.lock May reduce parallelism

Nix API

tools.nix exposes helpers for use in your own Nix expressions:

let
  crate2nix = builtins.fetchTarball "https://github.com/nix-community/crate2nix/tarball/master";
  tools = import "${crate2nix}/tools.nix" { inherit pkgs; };

  generated = tools.generatedCargoNix {
    name = "my-project";
    src = ./.;
  };

  project = pkgs.callPackage "${generated}/default.nix" {};
in
  project.rootCrate.build

Or the shorthand appliedCargoNix which combines generation and import.

Documentation

Full documentation is at https://nix-community.github.io/crate2nix/, covering:

Contributing

Contributions are welcome! See the contributing guide for details.

License

Apache-2.0

About

crate2nix builds your cargo-based rust project crate-by-crate with nix

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors 58