numtide / devshell

Per project developer environments
https://numtide.github.io/devshell/
MIT License
1.25k stars 89 forks source link

Override the rust version #174

Open traxys opened 2 years ago

traxys commented 2 years ago

When using devshell in rust projects I'd lke to be able to choose the rust version I want to use.

I can easily fetch any rust version using for example oxalica/rust-overlay as an overlay, but I don't understand how to set the language.rust.packageSet option.

If I try to set it to pkgs.rust-bin.stable.latest then it is missing rustPlatform, and when I try to set it to

let 
   rust = pkgs.rust-bin.stable.latest;
   rustPlatform = pkgs.makeRustPlatform rust;
in
   rust // {inherit rustPlatform;}

The installation fails with an error of the kind

error: builder for '/nix/store/irhlsd7j1vnzsm5m1h7sgkyxbbadf1h8-rust-lib-src.drv' failed with exit code 1;
       last 6 log lines:
       > unpacking sources
       > unpacking source archive /nix/store/b6bnb7dm03irm3g41l03dyfskjagyd1p-rustc-1.59.0-x86_64-unknown-linux-gnu.tar.xz
       > source root is rustc-1.59.0-x86_64-unknown-linux-gnu
       > setting SOURCE_DATE_EPOCH to timestamp 1645594340 of file rustc-1.59.0-x86_64-unknown-linux-gnu/version
       > installing
       > mv: cannot stat 'library': No such file or directory
       For full logs, run 'nix log /nix/store/irhlsd7j1vnzsm5m1h7sgkyxbbadf1h8-rust-lib-src.drv'.
error: 1 dependencies of derivation '/nix/store/wxyrlz7006ldrkaw026l3gmxh6zbmiy8-devshell-env.bash.drv' failed to build
error: 1 dependencies of derivation '/nix/store/wi4gcjs3c64vzm42yjbalx964xccm108-devshell-dir.drv' failed to build
error: 1 dependencies of derivation '/nix/store/0rqrv246agvycpp0dpixah41mj5pw337-devshell-env.drv' failed to build

So it does not seem to be the right way to create such a package set. What would I need to do ?

jfroche commented 2 years ago

I guess the rust language extension was not intended to work out of the box with this overlay. I have something working with a new rust.nix import:

        pkgs.devshell.mkShell {
          imports = [ ./rust.nix ];
          language.rust.packageSet = pkgs.rust-bin.stable.latest;
          language.rust.tools = [
            "rustc"
            "cargo"
            "clippy"
          ];
        };

where rust.nix is a slightly modified copy of https://github.com/numtide/devshell/blob/master/extra/language/rust.nix:

{ lib, config, pkgs, ... }:
let
  cfg = config.language.rust;
in
with lib;
{
  options.language.rust = {
    packageSet = mkOption {
      # FIXME: how to make the selection possible in TOML?
      type = types.attrs;
      default = pkgs.rustPackages;
      defaultText = "pkgs.rustPlatform";
      description = "Which rust package set to use";
    };

    tools = mkOption {
      type = types.listOf types.str;
      default = [
        "rustc"
        "cargo"
        "clippy"
        "rustfmt"
      ];
      description = "Which rust tools to pull from the platform package set";
    };
  };

  config = {
    env = [{
      # Used by tools like rust-analyzer
      name = "RUST_SRC_PATH";
      value = "${cfg.packageSet.rust-src}/lib/rustlib/src/rust/library";
    }];

    devshell.packages = map (tool: cfg.packageSet.${tool}) cfg.tools;
  };
}