Building the standard library (build-std)
For targets without a prebuilt standard library (bare-metal targets, custom
target specs) or to compile core/std with your own flags, cargo has the
unstable -Z build-std option. crate2nix supports the same: it builds the
standard library crates from the toolchain’s rust-src component as ordinary
crate derivations and links your crates against them.
crate2nix generate --build-std core,allocThis adds the standard library crates and their dependencies (from
library/Cargo.lock in rust-src) to Cargo.nix. Then build for the target:
let pkgs = import nixpkgs { localSystem = "x86_64-linux"; overlays = [ rust-overlay.overlays.default ]; crossSystem = { config = "riscv32imc-unknown-none-elf"; rust.rustcTarget = "riscv32imc-unknown-none-elf"; }; }; toolchain = pkgs.pkgsBuildHost.rust-bin.nightly.latest.default.override { extensions = [ "rust-src" ]; }; cargoNix = pkgs.callPackage ./Cargo.nix { buildRustCrateForPkgs = pkgs: pkgs.buildRustCrate.override { rustc = toolchain; cargo = toolchain; }; buildStd = [ "core" "alloc" ]; rustSrc = "${toolchain}/lib/rustlib/src/rust"; };incargoNix.rootCrate.buildIf your .cargo/config.toml has
[unstable]build-std = ["core", "alloc"]build-std-features = ["compiler-builtins-mem"]crate2nix generate picks it up without --build-std, see
.cargo/config.toml.
Arguments
Section titled “Arguments”Cargo.nix argument |
Default | Meaning |
|---|---|---|
buildStd |
from generation, usually [ ] |
Standard library crates to build, like -Z build-std=…. [ ] uses the toolchain’s prebuilt library. |
buildStdFeatures |
from generation | Features of the standard library, like -Z build-std-features=…. |
rustSrc |
rust-src of the toolchain |
Path to the Rust sources (the directory that contains library/). |
buildStd can only select crates that were included at generation time
(--build-std): crate2nix generate has to resolve their dependencies and
prefetch their crates.io dependencies.
Semantics
Section titled “Semantics”- The standard library crates are built once per target and configuration and shared by all crates, like any other dependency.
- Crates built for the host platform are compiled with
--externflags for the builtcore,alloc, … (withnoprelude, as cargo does) and-Z unstable-options, so they don’t use the toolchain’s prebuilt library. - Build scripts and proc-macros run on the build platform and use the toolchain’s prebuilt standard library, as with cargo.
-Z build-stdis unstable, so a nightly toolchain is needed, orRUSTC_BOOTSTRAP=1with a stable one. crate2nix setsRUSTC_BOOTSTRAP=1for the standard library crates themselves.- The
rust-srcmust belong to the same compiler version. crate2nix compares the version inrustSrcwithrustc --versionand fails early if they differ. - Profiles apply to the standard
library crates, too (as dependencies, so
package."*"settings apply). panic = "abort"builds the standard library withpanic_abort.
Limitations
Section titled “Limitations”stdfor targets without an upstreamstdport isn’t possible, as with cargo.- Building
std(not onlycore/alloc) needs a C compiler and libc for the target from the cross stdenv.
Open questions / alternatives
Section titled “Open questions / alternatives”- Generation time vs evaluation time: resolving
library/Cargo.lockat generation time tiesCargo.nixto one toolchain version. Alternative: a separate, toolchain-specificStd.nixthat crate2nix generates fromrust-srcand thatCargo.niximports. - Should crate2nix offer a ready-made way to get a nightly toolchain with
rust-src(e.g. via rust-overlay), or leave that to the user? - Is a
buildStdper crate (crate override) useful, e.g. to build only one firmware binary with a customcore?