How can I build only a subset of a given cargo workspace?
By default, cargo will build the crate at the current directory when invoked; if the current directory holds a workspace, cargo will then build all crates within that workspace.
Sometimes it can be useful to only build a subset of a given workspace (e.g. only specific binaries are needed, or some crates cannot be built for certain platforms, etc.), and cargo can be instructed to do so.
Notably, it is possible to set:
cargoExtraArgs = "-p foo -p bar";
to only build thefoo
andbar
crates only, but nothing else in the workspacecargoExtraArgs = "--bin baz";
to only build thebaz
binary (from whatever crate defines it)cargoExtraArgs = "--workspace --exclude qux";
to build the entire cargo workspace except for thequx
crate.
Consider setting pname = "NAME_OF_THE_EXECUTABLE";
when building a single
executable from the workspace. Having the name of the package match the
executable name will allow the result to easily run via nix run
without
further configuration.