.cargo/config.toml
Cargo reads configuration from .cargo/config.toml files, e.g. rustflags
for a target or environment variables for the build. crate2nix can apply the
settings that affect how crates are built. It doesn’t do so by default, since
that would change the builds of existing projects, e.g. with rustflags meant
only for local development such as -C target-cpu=native. Turn it on with
dotCargoConfig = true:
[build]rustflags = ["--cfg", "tokio_unstable"]
[target.'cfg(target_os = "linux")']rustflags = ["-C", "target-cpu=x86-64-v3"]
[env]OPENSSL_NO_VENDOR = "1"pkgs.callPackage ./Cargo.nix { dotCargoConfig = true;}dotCargoConfig |
Effect |
|---|---|
null (default) |
not applied; if a .cargo/config.toml with supported settings exists, a warning says so |
true |
read .cargo/config.toml from the source when evaluating, and apply it |
false |
not applied, without the warning |
| an attribute set | apply these settings instead, with the structure of .cargo/config.toml |
Applying .cargo/config.toml may become the default in a future release, as
it is with cargo; the warning for null announces that. Set true or false
to make your choice explicit.
Which files are read
Section titled “Which files are read”With dotCargoConfig = true, crate2nix reads the .cargo/config.toml (and legacy
.cargo/config) files in the workspace root and its parent directories, as far
as they are part of the source that Cargo.nix builds from, and merges them
like cargo does, the innermost file winning. They are read with
builtins.fromTOML when evaluating, so changing them doesn’t require
regenerating Cargo.nix.
Not read, because the build must not depend on the machine that builds it:
$CARGO_HOME/config.toml,CARGO_*andRUSTFLAGSenvironment variables,--configcommand line arguments.
Supported settings
Section titled “Supported settings”| Setting | Effect |
|---|---|
build.rustflags |
appended to the rustc flags of every crate built for the host platform |
target.<triple>.rustflags |
as above, only for crates built for that target |
target.'cfg(…)'.rustflags |
as above, only for targets matching the cfg expression |
build.rustdocflags |
passed to rustdoc for API docs and doctests |
[env] |
environment variables for all crate builds (including build scripts) |
[profile.*] |
merged into the profiles from Cargo.toml |
[unstable] build-std, build-std-features |
default for buildStd |
Settings that cargo already uses while crate2nix generate runs
cargo metadata work as before: [source] replacements and vendoring,
[registries], [patch] and [net].
rustflags precedence
Section titled “rustflags precedence”crate2nix follows cargo’s rules: if any target.<triple>.rustflags or
matching target.'cfg(…)'.rustflags exist, they are used and
build.rustflags is ignored; the matching target entries are concatenated.
cfg expressions are evaluated at Nix evaluation time for the host platform,
with the same logic that crate2nix uses for platform-specific dependencies, so
one Cargo.nix works for all platforms.
Like cargo with --target, rustflags aren’t applied to build scripts and
proc-macros when cross-compiling. Without cross-compilation they are, as with
a plain cargo build.
[env]PROTOC_NO_VENDOR = "1"SQLX_OFFLINE = { value = "true", force = true }The variables are set in the derivation of every crate, overriding variables
from the environment of buildRustCrate only with force = true, as with
cargo. relative = true isn’t supported, since there is no stable absolute
path to resolve the value against; such entries are skipped with a warning.
Ignored settings
Section titled “Ignored settings”These settings have no effect on crate2nix builds; with dotCargoConfig = true,
evaluating prints a warning if it finds them:
build.target: use nixpkgs’ cross-compilation (crossSystem,pkgsCross) instead.target.<triple>.linker,target.<triple>.runner: the linker comes from the stdenv.target.<triple>.<links>overrides of build scripts: use crate overrides.alias,build.jobs,build.target-dir,build.incremental,[term],[http],[install].
Overriding it
Section titled “Overriding it”Instead of true, dotCargoConfig takes the settings themselves, with the
structure of .cargo/config.toml as parsed by builtins.fromTOML:
pkgs.callPackage ./Cargo.nix { dotCargoConfig = { build.rustflags = [ "-C" "target-cpu=native" ]; };}To change single settings of the project’s configuration, merge them yourself:
pkgs.callPackage ./Cargo.nix { dotCargoConfig = pkgs.lib.recursiveUpdate (builtins.fromTOML (builtins.readFile ./.cargo/config.toml)) { env.OPENSSL_NO_VENDOR = "0"; };}Interactions
Section titled “Interactions”crateOverrides: the rustflags are added beforeextraRustcOptsof overrides, so overrides win.targetFeaturesandextraTargetFlagsare added in addition.-C prefer-dynamicand other flags that change how crates link work as long asbuildRustCratecan handle the result; crate2nix doesn’t check that.- JSON mode:
lib/build-from-json.nixtakes the samedotCargoConfigargument.
Decisions
Section titled “Decisions”.cargo/config.tomlisn’t applied by default. A warning says it may become the default, anddotCargoConfig = true/falsemakes the choice explicit.- It is read at evaluation time, not embedded when generating.
RUSTFLAGSfrom the environment ofcrate2nix generateare not recorded.