Skip to content

Settings for all crates: crateConfig

The crateConfig argument of the generated Cargo.nix adds buildRustCrate arguments to every crate that it builds, your own crates as well as all dependencies, build dependencies and proc-macros. Use it for settings that buildRustCrate only accepts per crate, e.g. extra rustc options or the number of codegen units:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
crateConfig = {
# Debug info in release builds.
extraRustcOpts = [ "-C" "debuginfo=1" ];
codegenUnits = 16;
};
};
in cargoNix.rootCrate.build

These lists in crateConfig are appended to the ones that crate2nix passes to buildRustCrate (e.g. the -C target-feature=... option for the targetFeatures argument):

  • extraRustcOpts
  • extraRustcOptsForBuildRs
  • extraLinkFlags
  • buildInputs
  • nativeBuildInputs
  • propagatedBuildInputs

Any other value, including other lists such as features or crateBin, replaces the one crate2nix passes, like with //.

crateConfig applies to all crates, including build scripts’ dependencies and proc-macros (and their dependencies), which are built for and run on the build platform. Options specific to the target CPU, such as -C target-cpu=native or -C target-feature=..., therefore also reach them, like the targetFeatures argument does. That is harmless when building for the machine you build on, but not when cross-compiling.

crateConfig can also be a function. It gets the buildRustCrate arguments of a crate, e.g. crateName, version, edition and features, and returns the attributes to add for it:

pkgs.callPackage ./Cargo.nix {
crateConfig = crate: {
extraRustcOpts = [ "-C" "debuginfo=1" ]
++ lib.optionals (crate.crateName == "my_app") [ "--cfg" "my_app_extra" ];
};
}

lib/build-from-json.nix, which builds the output of crate2nix generate --format json, takes a crateConfig argument, too, an attribute set or a function, merged in the same way. It does not read a crate-config.nix. The function form gets the buildRustCrate arguments that JSON mode passes, which lack some that Cargo.nix passes, e.g. release and extraRustcOpts: use crate.release or true and crate.extraRustcOpts or [ ] in a function for both modes.

If you do not pass crateConfig, the generated Cargo.nix uses the contents of a crate-config.nix file next to it, if there is one. If that file contains a function, it is called with pkgs.callPackage, so it can ask for packages and return either an attribute set or a per-crate function:

crate-config.nix
{ lib }:
crate: {
extraRustcOpts = [ "-C" "debuginfo=1" ]
++ lib.optionals (crate.crateName == "my_app") [ "--cfg" "my_app_extra" ];
}

A file with a plain attribute set works, too. A function whose argument is not a set of packages, like crate: { ... }, does not: pkgs.callPackage would call it with an empty set instead of the crate. Wrap it like in the example above ({ }: crate: { ... }).

With generatedCargoNix from tools.nix, the Cargo.nix is generated into the root of src, so put crate-config.nix there (next to Cargo.toml, unless you pass a cargoToml in a subdirectory).

cargo-hakari adds a “workspace-hack” crate to a workspace. It has no code of its own, only dependencies on many crates of the workspace’s dependency graph, with the features that any member enables, and all members depend on it. For cargo, that makes every member build its dependencies with the same features.

crate2nix has already resolved the features of every crate when it calls buildRustCrate, taking the workspace-hack’s dependencies into account. The workspace-hack’s own dependencies add nothing to that, but through them every member depends on all crates that the workspace-hack depends on, also those that the member does not use, so building one member builds all of them. Drop them with crateConfig:

pkgs.callPackage ./Cargo.nix {
crateConfig = crate: lib.optionalAttrs (crate.crateName == "workspace-hack") {
dependencies = [ ];
buildDependencies = [ ];
};
}

Use the name of your workspace-hack crate. The features of the other crates stay the ones resolved with the workspace-hack’s dependencies.

To build each dependency only once for all members, also consider featureUnification = "workspace".

Crate overrides and crateConfig both change the arguments of buildRustCrate, but:

  • Crate overrides apply to the crates whose names you list. crateConfig applies to all crates; use a function to pick crates.
  • The attributes a crate override returns replace the crate’s attributes, so an override that sets extraRustcOpts has to append to attrs.extraRustcOpts itself. crateConfig appends flags and inputs.
  • Crate overrides are applied by buildRustCrate, after crateConfig: they see, and can still change, the attributes that crateConfig set.
  • crateConfig is an argument of Cargo.nix. Crate overrides are configured through defaultCrateOverrides, either as the Cargo.nix argument or via buildRustCrateForPkgs.

crate2nix resolves dependencies and features before it calls buildRustCrate, so crateConfig cannot change the features of crates. Setting dependencies or features replaces what is passed to that one crate’s buildRustCrate; all other crates keep the features resolved for them. Cargo profiles and .cargo/config.toml settings such as rustflags are not read, but you can translate them into crateConfig.

The function form gets the same arguments for crates built for the build platform as for those built for the host platform, so it cannot give them different settings yet.