Skip to content

crate2nix's buildRustCrate

A Cargo.nix builds each crate with a buildRustCrate function. By default, that is nixpkgs’ buildRustCrate, pkgs.buildRustCrate.

crate2nix also ships its own variant of it. Upcoming crate2nix features that need changes to buildRustCrate (e.g. building examples and benches, or doctests) will only be available with crate2nix’s buildRustCrate, so that they don’t have to wait for a nixpkgs release.

How it relates to nixpkgs’ buildRustCrate

Section titled “How it relates to nixpkgs’ buildRustCrate”

crate2nix’s buildRustCrate is a copy of nixpkgs’ buildRustCrate from a pinned nixpkgs revision, plus a small series of patches. The patches are meant to be proposed back to nixpkgs. Right now, there are no patches, so it builds exactly the same derivations as pkgs.buildRustCrate of that nixpkgs revision. With another nixpkgs version, its derivations can differ from that version’s pkgs.buildRustCrate. It needs a recent nixpkgs; it is tested with the nixpkgs of crate2nix’s flake.lock.

It is a drop-in replacement: it accepts exactly the arguments of pkgs.buildRustCrate with the same behaviour, and supports .override with the same arguments (e.g. rustc, cargo, defaultCrateOverrides). It uses the toolchain (rustc, cargo) and defaultCrateOverrides of the pkgs that it is called with, like pkgs.buildRustCrate.

The rule is:

Everything works with both nixpkgs’ and crate2nix’s buildRustCrate. Features that need crate2nix’s buildRustCrate fail with a clear error under pkgs.buildRustCrate.

The vendored code, its upstream revision and the patches are in nix/build-rust-crate.

A generated Cargo.nix (from crate2nix generate or generatedCargoNix) and appliedCargoNix use pkgs.buildRustCrate by default. Opt in to crate2nix’s buildRustCrate as shown below.

appliedCargoNix will switch to crate2nix’s buildRustCrate by default once it has its first own change (e.g. doctests). The generated Cargo.nix keeps defaulting to pkgs.buildRustCrate.

Pass crate2nix’s buildRustCrateForPkgs to the Cargo.nix. It is available as

  • crate2nix.tools.${system}.buildRustCrateForPkgs or crate2nix.lib.buildRustCrateForPkgs from the crate2nix flake,
  • buildRustCrateForPkgs in tools.nix.

In a flake:

# `crate2nix` is the crate2nix flake input.
cargoNix = import ./Cargo.nix {
inherit pkgs;
buildRustCrateForPkgs = crate2nix.lib.buildRustCrateForPkgs;
};

Without flakes, with crate2nix pointing to the root of the crate2nix sources:

let
crate2nixTools = pkgs.callPackage "${crate2nix}/tools.nix" { };
cargoNix = import ./Cargo.nix {
inherit pkgs;
inherit (crate2nixTools) buildRustCrateForPkgs;
};
in
cargoNix.rootCrate.build

buildRustCrateForPkgs pkgs returns the buildRustCrate function for pkgs, so you can customize it like pkgs.buildRustCrate, e.g. with a different toolchain or crate overrides:

cargoNix = import ./Cargo.nix {
inherit pkgs;
buildRustCrateForPkgs = pkgs:
(crate2nix.lib.buildRustCrateForPkgs pkgs).override {
rustc = pkgs.rust-bin.stable.latest.default;
cargo = pkgs.rust-bin.stable.latest.default;
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
funky-things = attrs: { buildInputs = [ pkgs.openssl ]; };
};
};
};

Pass buildRustCrateForPkgs to appliedCargoNix:

cargoNix = crate2nix.tools.${system}.appliedCargoNix {
name = "my-crate";
src = ./.;
buildRustCrateForPkgs = crate2nix.lib.buildRustCrateForPkgs;
};

crate2nix’s buildRustCrate function, and the derivations that it builds, carry a crate2nix attribute, e.g.

{ builder = "crate2nix"; upstreamRev = "<nixpkgs revision>"; features = [ ]; }

features lists the crate2nix-specific features that it supports. The helpers isCrate2nixBuildRustCrate, supportsFeature and requireFeature in tools.nix’s buildRustCrateLib check for them.

The attribute on the function is kept by .override, but not if you wrap the function in your own function. On a derivation, a crate override that sets passthru replaces it.