Skip to content

Development inputs

Crates with native dependencies need the same libraries and tools in your development shell (for cargo build, rust-analyzer, …) as in the Nix build. Usually you list them twice: in crate overrides for crate2nix and in mkShell for cargo. devInputs collects them from the crate graph instead.

devInputs is an attribute next to build, so which crates it covers is clear from where you take it:

Attribute Collects the inputs of
cargoNix.rootCrate.devInputs the root crate and its dependencies
cargoNix.workspaceMembers.<name>.devInputs one member and its dependencies
cargoNix.allWorkspaceMembers.devInputs all workspace members and their dependencies

It is a pkgs.mkShell derivation without any tools of its own, meant as an input of your shell, where you add the tools you want:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
my_server = attrs: { buildInputs = [ pkgs.postgresql ]; };
};
};
in
{
devShells.default = pkgs.mkShell {
inputsFrom = [ cargoNix.allWorkspaceMembers.devInputs ];
packages = [ pkgs.rust-analyzer pkgs.cargo-watch ];
};
}

For every crate in the dependency graph of all workspace members (including build dependencies and, as tests are built in a dev shell, dev dependencies), with the features from rootFeatures and the overrides from defaultCrateOverrides:

  • buildInputs and nativeBuildInputs, e.g. openssl, pkg-config, protobuf from pkgs.defaultCrateOverrides,
  • the toolchain that buildRustCrateForPkgs uses (rustc, cargo, the C compiler),
  • the environment variables that crate overrides set, taken the same way as buildRustCrate turns override attributes into the environment of the crate’s build: env.* attributes and the other attributes that end up as environment variables (OPENSSL_NO_VENDOR, PROTOC, LIBCLANG_PATH, …). Build steps of the overrides, like preBuild or patches, are ignored.
  • no other tools: rust-analyzer, clippy, rustfmt and friends are up to you.

With featureUnification = "workspace", crates are collected with the unified features, so the shell matches what cargo build --workspace needs.

  • If two crates set the same environment variable to different values, evaluating devInputs fails with an error naming both crates. Set the variable explicitly in your own mkShell to resolve it.
  • Inputs are deduplicated. Their order follows the dependency order of the crates.
  • Cross-compilation: nativeBuildInputs are for the build platform and buildInputs for the host platform, as in the crate builds, so pkgsCross.<target>.callPackage ./Cargo.nix { } gives inputs for cross development (with cargo, you still need --target).
  • Evaluating devInputs evaluates the overrides of all crates, but builds nothing except the inputs themselves.
  • JSON mode: lib/build-from-json.nix provides the same attributes.
  • devInputs instead of devShell, since it contains no tools: they are left to the user.
  • It is an attribute of rootCrate, workspaceMembers.<name> and allWorkspaceMembers, so the selection of crates is clear from the context.
  • Environment variables are taken the way crate overrides set them.
  • Conflicting environment variables fail the evaluation.