Skip to content

Filtering local sources

The sources of local crates (workspace members and path dependencies) are copied into the Nix store. By default, crate2nix removes files that never matter for a build and would only cause rebuilds:

  • version control files: .git, .gitignore
  • build output: target, result symlinks, …
  • editor and IDE files: .idea, .vscode, *.iml, backup files (*~, *.bak, *.tmp) and swap files
  • the generated Cargo.nix and tests.nix

Every other file of the crate’s directory ends up in the source, so editing a README.md or a CI configuration next to Cargo.toml rebuilds the crate. crate2nix doesn’t look at .gitignore, and it ignores include/exclude in Cargo.toml, because they only apply to cargo package.

Cargo.nix takes a localCratesFileset argument: a function that returns the files of a local crate as a lib.fileset. It is called with an attribute set describing the crate:

Attribute Description
src The crate’s directory, from localCratesSrc
relativePath The directory relative to Cargo.nix, e.g. "/crates/foo"
crateName The name of the crate
version The version of the crate
packageId The crate’s package ID in Cargo.nix
features The features that the crate is built with

Further attributes may be added in later versions, so match the argument with ....

The default is available as defaultLocalCratesFileset on the result of Cargo.nix and in tools.nix (crate2nix.tools.${system}.defaultLocalCratesFileset in the flake). It takes the same attribute set. To remove all Markdown files from the sources:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
localCratesFileset = { src, ... }@crate:
pkgs.lib.fileset.difference
(cargoNix.defaultLocalCratesFileset crate)
(pkgs.lib.fileset.fileFilter (file: file.hasExt "md") src);
};
in
cargoNix.rootCrate.build

Only the files tracked by git (if ./. is the root of a git checkout, which isn’t the case in a pure flake evaluation), besides the default exclusions:

let
tracked = pkgs.lib.fileset.gitTracked ./.;
cargoNix = pkgs.callPackage ./Cargo.nix {
localCratesFileset = crate:
pkgs.lib.fileset.intersection tracked
(cargoNix.defaultLocalCratesFileset crate);
};
in
cargoNix.rootCrate.build

Different files for different crates:

pkgs.callPackage ./Cargo.nix {
localCratesFileset = { src, crateName, ... }@crate:
if crateName == "my-server"
then pkgs.lib.fileset.unions [ (src + "/Cargo.toml") (src + "/src") ]
else crate2nix.tools.${system}.defaultLocalCratesFileset crate;
}
  • localCratesFileset applies to the source of every local crate, i.e. every crate whose source is a local directory. Crates from crates.io, git or other registries are fetched as is.
  • The fileset must only contain files in src: the crate’s source is lib.fileset.toSource { root = src; fileset = ...; }. src must be a path to a directory, so localCratesFileset doesn’t work with a localCratesSrc that returns a store path (e.g. a derivation or a string), nor for out-of-tree sources, whose directories are symlinks.
  • Without localCratesFileset, crate2nix doesn’t use lib.fileset at all but the equivalent lib.cleanSourceWith filter, which also works for sources in the store and keeps empty directories.
  • A src set by a crateOverrides entry or by crateConfig wins over localCratesFileset.

Choosing the source directory: localCratesSrc

Section titled “Choosing the source directory: localCratesSrc”

Cargo.nix refers to local crates relative to its own location (./.). With localCratesSrc, you can choose the directory that contains them instead, e.g. a fetched or patched copy of the repository:

pkgs.callPackage ./Cargo.nix {
localCratesSrc = path: patchedSrc + path;
}

localCratesSrc is a function from the crate’s directory relative to the directory of Cargo.nix (usually the workspace root) to its source. The relative path starts with a /, e.g. "/crates/foo", and is "" for a crate in the same directory as Cargo.nix. The default is path: ./. + path.

crate2nix applies localCratesFileset, or the default filter, to the result of localCratesSrc.

This is also what makes the faster generatedCargoNix possible: the generated Cargo.nix can live in a different store path than the sources.

If localCratesSrc returns a derivation, e.g. a patched copy of the repository, the default filter needs it at evaluation time, i.e. it is import from derivation.

Crates outside of the directory of Cargo.nix get a path starting with /.., e.g. "/../shared". That works with the default and other Nix paths, but not with a source in the Nix store, which doesn’t contain its parent directory.

In the rare case that crate2nix can only refer to a crate with an absolute path, it uses that path as is: it doesn’t call localCratesSrc, and relativePath is null.

Recipe: crates that read files outside of their directory

Section titled “Recipe: crates that read files outside of their directory”

Build scripts often read files from a sibling directory, e.g. ../proto/service.proto for code generation (#346). Since only the crate’s own directory ends up in its source, the build fails.

Use a crate override to give the crate a source that contains both directories, and point buildRustCrate at the crate inside it with workspace_member:

let
cargoNix = pkgs.callPackage ./Cargo.nix {
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
my-grpc-service = attrs: {
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
(cargoNix.defaultLocalCratesFileset { src = ./my-grpc-service; })
./proto
];
};
workspace_member = "my-grpc-service";
nativeBuildInputs = (attrs.nativeBuildInputs or [ ]) ++ [ pkgs.protobuf ];
};
};
};
in
cargoNix.workspaceMembers.my-grpc-service.build