Cargo profiles
Today, crate2nix only knows a release switch: with release = true (the
default), crates are built with -C opt-level=3, otherwise with
-C debuginfo=2. The [profile.*] sections of Cargo.toml are ignored.
With profile support, crate2nix builds crates the way cargo would with the same profile: optimization level, debug info, LTO, codegen units, panic strategy, per-package overrides and custom profiles.
Choosing a profile
Section titled “Choosing a profile”Cargo.nix takes a cargoProfile argument, the name of the profile, like
cargo build --profile:
let cargoNix = pkgs.callPackage ./Cargo.nix { cargoProfile = "release-lto"; };incargoNix.rootCrate.buildnix build -f Cargo.nix --argstr cargoProfile dev rootCrate.buildThe default is null, which means: "release" if release = true (the
default), "dev" if release = false. So existing setups get the release
profile, now including the settings from your Cargo.toml. If both are set,
cargoProfile wins.
You can also choose the profile per crate you build:
cargoNix.rootCrate.build.override { cargoProfile = "dev"; }Like features, this changes the profile of the whole dependency graph of
that build.
Which profile is used for which kind of build follows cargo:
| Build | Profile |
|---|---|
build |
the selected profile P |
runTests, buildTests, doctests |
test if P is dev, otherwise P |
buildTargets with "benches" |
bench if P is dev or release, otherwise P |
| build scripts and proc-macros | P with its build-override section applied |
Example
Section titled “Example”# Cargo.toml (workspace root)[profile.release]debug = "line-tables-only"lto = "thin"codegen-units = 16
[profile.release.package.image]opt-level = 3
[profile.release.package."*"]debug = false
[profile.release-small]inherits = "release"opt-level = "z"lto = truepanic = "abort"strip = true{ packages.default = (pkgs.callPackage ./Cargo.nix { }).rootCrate.build; packages.small = (pkgs.callPackage ./Cargo.nix { cargoProfile = "release-small"; }).rootCrate.build;}Where profiles come from
Section titled “Where profiles come from”crate2nix generate reads the profiles like cargo does and writes them into
Cargo.nix (and into the JSON output):
- the
[profile.*]sections of the workspace root’sCargo.toml(profiles in other members’ manifests are ignored with a warning, as by cargo), - cargo’s built-in defaults for
dev,release,testandbench.
Profiles are fully resolved at generation time: inherits chains are
followed, so Cargo.nix contains the effective settings of every profile.
After changing a profile, regenerate Cargo.nix.
With dotCargoConfig = true,
[profile.*] sections from .cargo/config.toml are merged over these when
evaluating, like cargoProfiles.
Settings
Section titled “Settings”| Cargo setting | crate2nix |
|---|---|
opt-level |
-C opt-level=… |
debug |
-C debuginfo=…; accepts true, false, 0, 1, 2, "none", "line-directives-only", "line-tables-only", "limited", "full" |
split-debuginfo |
-C split-debuginfo=… |
strip |
-C strip=…; like cargo, strip defaults to "debuginfo" if debug is off for all crates. Note that nixpkgs’ stdenv also strips binaries unless dontStrip = true. |
debug-assertions |
-C debug-assertions=… |
overflow-checks |
-C overflow-checks=… |
lto |
-C lto=… for final artifacts (binaries, cdylib, staticlib, test and bench executables). Libraries in the graph are built with -C embed-bitcode=yes only when LTO is on (see below). |
panic |
-C panic=…; see below |
codegen-units |
codegenUnits of buildRustCrate |
rpath |
-C rpath |
incremental |
ignored: Nix builds are never incremental |
build-override |
applied to build scripts, proc-macros and their dependencies |
package.<name> |
applied to the crate <name>; package."*" to all non-workspace crates |
The profile also sets the environment variables cargo sets for build scripts:
PROFILE (release or debug, depending on whether the profile inherits from
release or dev), OPT_LEVEL and DEBUG.
LTO and embedded bitcode
Section titled “LTO and embedded bitcode”- With
lto = false(the default forrelease), cargo builds dependencies with-C embed-bitcode=no, which makes builds faster and libraries smaller. crate2nix does the same (#130). - With
lto = true/"fat"or"thin", libraries are built with embedded bitcode and the final artifacts with-C lto=…. lto = "off"disables LTO completely, like-C lto=off.
LTO can only be applied when the final artifact is linked, i.e. in the
derivation of the workspace member that has the binary. With many codegen
units and fat LTO, this step can take long; codegen-units applies to the
LTO step, too
(#155).
Panic strategy
Section titled “Panic strategy”panic = "abort" applies to the crates of the build, but, like cargo,
not to tests, benchmarks, build scripts and proc-macros, which always unwind.
A crate that’s needed both by a binary and by a build script is built twice,
once per strategy.
Codegen units
Section titled “Codegen units”If a profile doesn’t set codegen-units, crate2nix uses cargo’s default (16
for non-incremental builds), not buildRustCrate’s default of one codegen
unit. This compiles faster, but can make the code slightly slower than with
earlier crate2nix versions; the release notes point this out. Set
codegen-units = 1 in your profile to keep the old behaviour.
Changing profiles from Nix
Section titled “Changing profiles from Nix”When you can’t change Cargo.toml, you can change the profiles from Nix with
cargoProfiles. It is merged over the profiles from Cargo.toml, one setting
at a time:
pkgs.callPackage ./Cargo.nix { cargoProfiles = { release.debug = 1; release.package.image.opt-level = 3; };}The structure is that of the [profile] table of Cargo.toml, as parsed by
builtins.fromTOML. Since inherits chains are resolved when Cargo.nix is
generated, a change to release doesn’t propagate to profiles that inherit
from it. This is part of the first version only if it stays simple; otherwise
it comes later.
Interactions
Section titled “Interactions”crateOverrides: flags fromextraRustcOptsin an override come after the profile’s flags and win, so you can still tweak single crates, e.g. to add debug info to one crate (#345) without changingCargo.toml. An override can also setcodegenUnits.targetFeaturesandextraTargetFlagsare independent of the profile and apply to all profiles.- Feature unification: the profile doesn’t affect features; with different profiles, the dependency derivations differ and aren’t shared.
- Cross-compilation:
build-overrideapplies to everything built for the build platform (build scripts, proc-macros). The other settings apply to the crates built for the host platform. release: deprecated in favour ofcargoProfile, but fully functional: it still selectsreleaseordev.
Limitations
Section titled “Limitations”- Cargo’s implicit
build-overridedefaults (e.g.opt-level = 0for build scripts in release builds unless they’re also normal dependencies) aren’t applied in the first version: build scripts get the profile’s settings plus an explicitbuild-overridesection. - Profile settings that need cargo’s unstable features (
-Zflags,trim-paths,profile-rustflags) aren’t supported yet. - Changing the profile rebuilds every crate, since the flags are part of each derivation. This is also the case with cargo.
- Switching an existing setup to profile support changes the flags of every
crate (e.g.
-C embed-bitcode=no,debugfrom your profile), so the first build after upgrading rebuilds everything.
Decisions
Section titled “Decisions”- The argument is called
cargoProfile. codegen-unitsdefaults to cargo’s default, with a warning in the release notes.releaseis deprecated once profiles are implemented, but keeps working.- Changing profiles from Nix (
cargoProfiles) is included if it’s easy. - Cargo’s implicit
build-overridedefaults are skipped in the first version.