fix(rust): add rust standard library deps to the environment when the toolchain needs it

This is a fix for "error: no matching package named
`rustc-literal-escaper` found" when adding "rust-src" to the components
array in the toolchain file
This commit is contained in:
2026-10-07 19:26:39 -05:00
parent 6c8058a5fd
commit 00ab3b0148
+46 -20
View File
@@ -25,28 +25,54 @@
}: let }: let
cfg = config.krantz.rust; cfg = config.krantz.rust;
src = cfg.srcFiltered; manifest = (lib.importTOML "${cfg.srcFiltered}/Cargo.toml").package;
manifest = (lib.importTOML "${src}/Cargo.toml").package;
craneLib = craneLib =
if cfg.srcToolchain != null && builtins.pathExists cfg.srcToolchain if hasToolchain
then then (inputs.crane.mkLib rustPkgs).overrideToolchain mkRustToolchain
(inputs.crane.mkLib (pkgs.extend inputs.rust-overlay.overlays.default)).overrideToolchain (
p: p.rust-bin.fromRustupToolchainFile cfg.srcToolchain
)
else inputs.crane.mkLib pkgs; else inputs.crane.mkLib pkgs;
buildInputs = cfg.runtimeDeps; # Attrs to be included for all crane functions that build our crate
cargoBuildAttrs = let
# Include the standard library Cargo.lock if we have the "rust-src" component in our toolchain
cargoVendorDir =
lib.optionalAttrs (
hasToolchain
&& lib.pipe cfg.srcToolchain [
builtins.readFile
fromTOML
(t: t ? toolchain.components && builtins.elem "rust-src" t.toolchain.components)
]
) {
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles cfg.srcFiltered) cargoConfigs;
cargoLockList = [
"${cfg.srcFiltered}/Cargo.lock"
"${(mkRustToolchain rustPkgs).passthru.availableComponents.rust-src}/lib/rustlib/src/rust/library/Cargo.lock"
];
};
};
in
rec {
src = cfg.srcFiltered;
# build dependencies buildInputs = cfg.runtimeDeps;
nativeBuildInputs = cfg.buildDeps;
# Build *just* the cargo dependencies, so we can reuse nativeBuildInputs = cfg.buildDeps;
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly ({ # Build *just* the cargo dependencies, so we can reuse
inherit src buildInputs nativeBuildInputs; # all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly ({
inherit src buildInputs nativeBuildInputs;
}
// cargoVendorDir
// cfg.cargoArtifacts.extraAttrs);
} }
// cfg.cargoArtifacts.extraAttrs); // cargoVendorDir;
hasToolchain = cfg.srcToolchain != null && builtins.pathExists cfg.srcToolchain;
rustPkgs = pkgs.extend inputs.rust-overlay.overlays.default;
mkRustToolchain = p: p.rust-bin.fromRustupToolchainFile cfg.srcToolchain;
in { in {
options.krantz.rust = { options.krantz.rust = {
enable = lib.mkEnableOption "building a cargo project with crane"; enable = lib.mkEnableOption "building a cargo project with crane";
@@ -206,8 +232,8 @@
# Accessible through 'nix build', 'nix shell', 'nix run', etc # Accessible through 'nix build', 'nix shell', 'nix run', etc
packages = lib.mkIf cfg.package.enable { packages = lib.mkIf cfg.package.enable {
${cfg.package.name} = craneLib.buildPackage ({ ${cfg.package.name} = craneLib.buildPackage ({
inherit src buildInputs nativeBuildInputs cargoArtifacts;
} }
// cargoBuildAttrs
// cfg.package.extraAttrs); // cfg.package.extraAttrs);
}; };
@@ -224,10 +250,10 @@
}; };
checks = lib.mkIf cfg.checks.enable { checks = lib.mkIf cfg.checks.enable {
clippy = lib.mkIf cfg.checks.linter (craneLib.cargoClippy { clippy = lib.mkIf cfg.checks.linter (craneLib.cargoClippy ({
inherit src buildInputs nativeBuildInputs cargoArtifacts; cargoClippyExtraArgs = "-- --deny warnings";
cargoClippyExtraArgs = "-- --deny warnings"; }
}); // cargoBuildAttrs));
}; };
}; };
}); });