Skip to content

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.

Terminal window
crate2nix generate --build-std core,alloc

This 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";
};
in
cargoNix.rootCrate.build

If 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.

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.

  • 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 --extern flags for the built core, alloc, … (with noprelude, 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-std is unstable, so a nightly toolchain is needed, or RUSTC_BOOTSTRAP=1 with a stable one. crate2nix sets RUSTC_BOOTSTRAP=1 for the standard library crates themselves.
  • The rust-src must belong to the same compiler version. crate2nix compares the version in rustSrc with rustc --version and 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 with panic_abort.
  • std for targets without an upstream std port isn’t possible, as with cargo.
  • Building std (not only core/alloc) needs a C compiler and libc for the target from the cross stdenv.
  • Generation time vs evaluation time: resolving library/Cargo.lock at generation time ties Cargo.nix to one toolchain version. Alternative: a separate, toolchain-specific Std.nix that crate2nix generates from rust-src and that Cargo.nix imports.
  • 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 buildStd per crate (crate override) useful, e.g. to build only one firmware binary with a custom core?