Skip to content

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.

Cargo.nix takes a cargoProfile argument, the name of the profile, like cargo build --profile:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
cargoProfile = "release-lto";
};
in
cargoNix.rootCrate.build
Terminal window
nix build -f Cargo.nix --argstr cargoProfile dev rootCrate.build

The 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
# 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 = true
panic = "abort"
strip = true
{
packages.default = (pkgs.callPackage ./Cargo.nix { }).rootCrate.build;
packages.small =
(pkgs.callPackage ./Cargo.nix { cargoProfile = "release-small"; }).rootCrate.build;
}

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’s Cargo.toml (profiles in other members’ manifests are ignored with a warning, as by cargo),
  • cargo’s built-in defaults for dev, release, test and bench.

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.

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.

  • With lto = false (the default for release), 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 = "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.

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.

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.

  • crateOverrides: flags from extraRustcOpts in 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 changing Cargo.toml. An override can also set codegenUnits.
  • targetFeatures and extraTargetFlags are 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-override applies 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 of cargoProfile, but fully functional: it still selects release or dev.
  • Cargo’s implicit build-override defaults (e.g. opt-level = 0 for 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 explicit build-override section.
  • Profile settings that need cargo’s unstable features (-Z flags, 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, debug from your profile), so the first build after upgrading rebuilds everything.
  • The argument is called cargoProfile.
  • codegen-units defaults to cargo’s default, with a warning in the release notes.
  • release is deprecated once profiles are implemented, but keeps working.
  • Changing profiles from Nix (cargoProfiles) is included if it’s easy.
  • Cargo’s implicit build-override defaults are skipped in the first version.