Generating Cargo.nix via IFD
- ✅ No need to install
crate2nix. - ✅ Auto-generates nix from your
Cargo.lockfile. - ⚠️ Uses the import from derivation feature from Nix which may lead to disabling build parallelism.
There are two commands in the tools.nix file:
generatedCargoNixwill generate a folder containing adefault.nix, to be used as aCargo.nixfile. The argument it takes are:name: required. The name of the project (need to be a valid nix name identifier, so no space are allowed, but dash are.)src: required. The folder that contain the root of the Rust project.cargoToml: optional. The name and path relative to thesrcroot of theCargo.tomlto use. Default toCargo.toml.additionalCargoNixArgs: optional, additional argument forcrate2nix, in a list
appliedCargoNixtake the same argument thatgeneratedCargoNix, but also call the generated file with thepkgsprovided when callingtools.nix
In a flake with flake-parts:
{ # ...
inputs = { flake-parts = { url = "github:hercules-ci/flake-parts"; inputs.nixpkgs-lib.follows = "nixpkgs"; }; crate2nix.url = "github:nix-community/crate2nix"; # ... };
outputs = inputs @ { self , nixpkgs , flake-parts , rust-overlay , crate2nix , ... }: flake-parts.lib.mkFlake { inherit inputs; } { systems = [ "x86_64-linux" "aarch64-linux" "x86_64-linux" "aarch64-darwin" ];
perSystem = { system, pkgs, lib, inputs', ... }: let cargoNix = inputs.crate2nix.tools.${system}.appliedCargoNix { name = "rustnix"; src = ./.; }; in rec { checks = { rustnix = cargoNix.rootCrate.build.override { runTests = true; }; };
packages = { rustnix = cargoNix.rootCrate.build; default = packages.rustnix; }; }; };}Without flakes, crate2nix needs to point to the root of the crate2nix sources:
let crate2nix = ...; crate2nix-tools = pkgs.callPackage "${crate2nix}/tools.nix" {}; generated = crate2nix-tools.generatedCargoNix { name = "test-rust-project"; src = ./.; }; called = pkgs.callPackage "${generated}/default.nix" {};in called.rootCrate.build Learn How it works.