Feature unification in workspaces
By default, crate2nix resolves the features of dependencies separately for
each workspace member: workspaceMembers.<name>.build gets exactly the features
that this member needs, as with cargo build -p <name>.
When two members enable different features of the same dependency, that
dependency is built twice, once per feature set. Since a crate’s derivation
depends on the derivations of its dependencies, every crate that depends on it,
directly or indirectly, is built twice as well. In a large workspace, crates
such as tokio, serde_json or rustls can end up in many different feature
sets, and so can everything built on top of them.
With featureUnification = "workspace", crate2nix resolves the features of
all workspace members together instead. Each crate then has one feature set,
the union of what all members need, and one derivation that the builds of all
members share.
Enabling it
Section titled “Enabling it”featureUnification is an argument of the generated Cargo.nix:
let cargoNix = pkgs.callPackage ./Cargo.nix { featureUnification = "workspace"; };incargoNix.allWorkspaceMembersFrom the command line:
nix build -f ./Cargo.nix --argstr featureUnification workspace workspaceMembers.my_member.buildWith tools.nix or the flake’s tools, pass it
when calling the generated file:
let generated = crate2nix.tools.${system}.generatedCargoNix { name = "my-workspace"; src = ./.; }; cargoNix = pkgs.callPackage generated { featureUnification = "workspace"; };in...The possible values are:
| Value | Behaviour |
|---|---|
"package" (default) |
Each member’s build resolves features on its own, like cargo build -p <member>. |
"workspace" |
All members are resolved together, like cargo build for the whole workspace. |
Cargo.nix files generated by older versions of crate2nix don’t have this
argument. The default doesn’t change anything: the derivations are the same as
before.
What gets unified
Section titled “What gets unified”- Normal builds:
workspaceMembers.<name>.build,rootCrate.buildandallWorkspaceMembersuse the features that all members together enable, so every dependency is built once for all of them. - Test builds (
runTests = true): all members’ test builds share a second feature set that also includes the features enabled by thedev-dependenciesof all members, ascargo testfor the whole workspace does. These features don’t leak into the normal builds. The crate of the tested member itself is the exception: itsdev-dependenciesare linked into it, so it differs from the same crate in the other members’ test builds. All crates that are not workspace members are shared. - Features of the members: every member is resolved with
rootFeatures([ "default" ]by default). If you override the features of one member, e.g.workspaceMembers.my_member.build.override { features = [ "extra" ]; }, that call resolves the workspace again: this member with the given features, the other members withrootFeatures. Its dependencies can then differ from the shared ones. As with cargo, an override can’t remove features that other members enable on this member: if another member depends onmy_libwith its default features,workspaceMembers.my_lib.build.override { features = [ ]; }still buildsmy_libwithdefault. - Platform-specific dependencies are resolved for the platform that is built for, as without unification.
- Build dependencies and proc-macros: with cargo’s feature
resolver “2”
(edition 2021 or later), the crates built for the build platform get
features of their own, as with cargo; with
"workspace", those are unified over all members, too. With resolver “1”, every crate has one feature set for both roles.
A project with a single package behaves the same with both values.
If the Cargo.nix was generated from several Cargo.toml files (-f more
than once) or includes fetched sources with their own workspaces, "workspace"
unifies the members of all of them together, which goes further than cargo,
which only unifies within one workspace.
When to use it
Section titled “When to use it”Use it when you build several members of a workspace, for example
allWorkspaceMembers or several services from the same repository, and the
number of derivations, the build time or the size of your binary cache matters
more to you than building each member with the minimal set of features.
It also makes evaluation cheaper. As every member gets the same features, the
builds of all members share one table of crate derivations (lazily: evaluating
one member still only instantiates its own dependencies, though features are
resolved for the whole workspace). That holds for
workspaceMembers.<name>.build, its test build
(.build.override { runTests = true; }) and allWorkspaceMembers, as long as
they use rootFeatures and the defaultCrateOverrides of the Cargo.nix
call. A build with other features or crate overrides gets crate derivations of
its own, as with "package".
Trade-offs
Section titled “Trade-offs”Unifying features changes what each member is compiled with. It matches
building the whole workspace with cargo (cargo build at the workspace root),
so problems that show up there show up here too.
- Bigger binaries: a member is linked against dependencies compiled with
features it doesn’t use, for example the
fsfeature oftokiobecause another member needs it. - Conflicting features: some crates behave differently, or fail at runtime,
when several of their features are enabled at once. For example, if one
member enables the
ringcrypto provider ofrustlsand another one enablesaws-lc-rs,rustlscan no longer pick a provider by itself and panics at runtime unless the program installs one explicitly. - More rebuilds: a change of features in one member changes the shared feature set and thus rebuilds the dependencies of all members.
Relation to cargo
Section titled “Relation to cargo”cargo controls this with the resolver.feature-unification setting (unstable
at the time of writing, see the cargo
documentation):
"selected"(cargo’s default) unifies the features of the packages selected for one cargo invocation.crate2nixbuilds each member in its own derivation, so its default,"package", corresponds to onecargo build -p <member>per member, and"workspace"to selecting all members (cargo build --workspace)."workspace"unifies the features of all workspace members, whichever packages are built.crate2nix’s"workspace"does the same."package"resolves each selected package on its own.crate2nix’s default,"package", does the same for each member’s build.