crate2nix
crate2nix copied to clipboard
nix-shell support
Provide a derivation that provides a good basis for a shell.nix
and includes the following buildInputs:
-
cargo
,rustc
,binutils
,clippy
,rustfmt
and others? Mayberls
andrust-analyzer
? - All buildInputs that are needed for a
cargo test
run. Accumulating on non-rust buildInputs over all crates in the build file via thedefaultCrateOverrides
mechanism is a good start.
{ lib, mkShell, allCrates, cargo }:
let
depsCollector = n: a: a.completeBuildDeps ++ a.completeDeps ++ [a];
allRustDeps = lib.mapAttrsToList depsCollector allCrates;
uniqueRustDeps = lib.unique (builtins.concatLists allRustDeps);
inputs = builtins.map (a: a.buildInputs) uniqueRustDeps;
uniqueInputs = lib.unique (builtins.concatLists inputs);
nativeInputs = builtins.map (a: a.nativeBuildInputs) uniqueRustDeps;
uniqueNativeInputs = lib.unique (builtins.concatLists nativeInputs);
getUniqueValue = list: (assert [(lib.head list)] == lib.unique list; lib.head list);
envVars = lib.zipAttrs (builtins.map (lib.filterAttrs (n: v: n == lib.toUpper n)) uniqueRustDeps);
verifiedEnvVars = lib.mapAttrs (n: v: getUniqueValue v) envVars;
shellAttrs = verifiedEnvVars // {
name = "rust-build-shell";
buildInputs = uniqueInputs;
nativeBuildInputs = uniqueNativeInputs ++ [
cargo
];
};
in mkShell shellAttrs
is what I came up locally a little while ago.
(nb: cargo here is from the mozilla overlay so it does include all the cargo-tools you’d expect)
@nagisa How do you extract allCrates
from the file generated by crate2nix ?
When I try to use cargo and rustup from a nix-shell on a non nix-os system, I get sometimes errors about the glibc version.
I would really appreciate this feature too !
If someone know what I can do to create a nix expression for an environment where I can build a https://rustwasm.github.io/docs/wasm-pack/ project, it would be really nice !