Skip to content

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.

featureUnification is an argument of the generated Cargo.nix:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
featureUnification = "workspace";
};
in
cargoNix.allWorkspaceMembers

From the command line:

Terminal window
nix build -f ./Cargo.nix --argstr featureUnification workspace workspaceMembers.my_member.build

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

  • Normal builds: workspaceMembers.<name>.build, rootCrate.build and allWorkspaceMembers use 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 the dev-dependencies of all members, as cargo test for the whole workspace does. These features don’t leak into the normal builds. The crate of the tested member itself is the exception: its dev-dependencies are 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 with rootFeatures. 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 on my_lib with its default features, workspaceMembers.my_lib.build.override { features = [ ]; } still builds my_lib with default.
  • 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.

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

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 fs feature of tokio because 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 ring crypto provider of rustls and another one enables aws-lc-rs, rustls can 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.

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. crate2nix builds each member in its own derivation, so its default, "package", corresponds to one cargo 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.