crate2nix icon indicating copy to clipboard operation
crate2nix copied to clipboard

nix-shell support

Open kolloch opened this issue 4 years ago • 4 comments

Provide a derivation that provides a good basis for a shell.nix and includes the following buildInputs:

  • cargo, rustc, binutils, clippy, rustfmt and others? Maybe rls and rust-analyzer?
  • All buildInputs that are needed for a cargo test run. Accumulating on non-rust buildInputs over all crates in the build file via the defaultCrateOverrides mechanism is a good start.

kolloch avatar Feb 19 '20 21:02 kolloch

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

nagisa avatar Feb 20 '20 14:02 nagisa

(nb: cargo here is from the mozilla overlay so it does include all the cargo-tools you’d expect)

nagisa avatar Feb 20 '20 14:02 nagisa

@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 !

rambip avatar Jul 24 '21 16:07 rambip

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 !

rambip avatar Jul 24 '21 16:07 rambip