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 ]; };}What’s in it
Section titled “What’s in it”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:
buildInputsandnativeBuildInputs, e.g.openssl,pkg-config,protobuffrompkgs.defaultCrateOverrides,- the toolchain that
buildRustCrateForPkgsuses (rustc,cargo, the C compiler), - the environment variables that crate overrides set, taken the same way as
buildRustCrateturns 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, likepreBuildorpatches, 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.
Semantics
Section titled “Semantics”- If two crates set the same environment variable to different values,
evaluating
devInputsfails with an error naming both crates. Set the variable explicitly in your ownmkShellto resolve it. - Inputs are deduplicated. Their order follows the dependency order of the crates.
- Cross-compilation:
nativeBuildInputsare for the build platform andbuildInputsfor the host platform, as in the crate builds, sopkgsCross.<target>.callPackage ./Cargo.nix { }gives inputs for cross development (with cargo, you still need--target). - Evaluating
devInputsevaluates the overrides of all crates, but builds nothing except the inputs themselves. - JSON mode:
lib/build-from-json.nixprovides the same attributes.
Decisions
Section titled “Decisions”devInputsinstead ofdevShell, since it contains no tools: they are left to the user.- It is an attribute of
rootCrate,workspaceMembers.<name>andallWorkspaceMembers, 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.