oxalica / rust-overlay

Pure and reproducible nix overlay of binary distributed rust toolchains
MIT License
899 stars 52 forks source link

Help: `nix develop` (nix-shell replacement) example available? #30

Closed atcol closed 3 years ago

atcol commented 3 years ago

Hello. I'm new-ish to Nix and learning flakes. I am attempting to set up my Rust development environment but I'm struggling to get the rust overlay working with a Flake and this repo.

Do you have a devShell example available?

I'm trying as follows:

{
  description = "Blah";

  inputs = {
    nixpkgs.url      = "github:nixos/nixpkgs/nixos-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
    flake-utils.url  = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
        let 
          pkgs = nixpkgs.legacyPackages.${system}; 
        in
        {
          nixpkgs.overlays = [ rust-overlay.overlay ];
          devShell = pkgs.mkShell {
            buildInputs = [
              pkgs.openssl
              pkgs.exa
              pkgs.ripgrep
              pkgs.watchexec
              pkgs.tokei
              pkgs.bat
              pkgs.fd
              pkgs.terraform
              pkgs.gperftools
              pkgs.wrk
              pkgs.valgrind
              pkgs.rust-bin.nightly.latest.minimal
            ];

            shellHook = ''
              export NIX_ENFORCE_PURITY=0
              alias ls=exa
              alias find=fd
              cargo update 
              cargo install cargo-watch
              cargo install cargo-edit
              cargo install cargo-tarpaulin
              cargo install cargo-audit
              cargo install cargo-outdated
              cargo install cargo-release
              cargo install cargo-udeps
              cargo install xh 
              cargo install zenith 
              export RUST_LOG=debug
              set -o vi
            '';
          };
        }
      );
}

This fails with:


error: --- EvalError ------------------------------------------------------------------------------------------------------------------- nix
at: (30:15) in file: /nix/store/nsq6za0vhagc0h44jwij362j4z8qz1sa-source/flake.nix

    29|               pkgs.valgrind
    30|               pkgs.rust-bin.nightly.latest.minimal
      |               ^
    31|             ];

attribute 'rust-bin' missing
a-kenji commented 3 years ago

Hey! From just looking at it a little, I believe

      let 
          pkgs = nixpkgs.legacyPackages.${system}; 
        in
        {
          nixpkgs.overlays = [ rust-overlay.overlay ];
          devShell = pkgs.mkShell {

You are using the overlay to modify nixpkgs after you already took pkgs from it, so I think it isn't evaluated.

 nixpkgs.overlays = [ rust-overlay.overlay ];

From a personal project that is a little bit different, maybe you can adjust it:

utils.lib.eachDefaultSystem (system: let
      overlays = [ (import rust-overlay) ];
      pkgs = import nixpkgs {
        inherit system overlays;
      };
atcol commented 3 years ago

That worked! Thanks so much. I knew the nixpkgs.overlays felt wrong but I didn't know the syntax to move things around correctly.

Here's a working example:

{
  description = "Blah";

  inputs = {
    nixpkgs.url      = "github:nixos/nixpkgs/nixos-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
    flake-utils.url  = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
        let 
          overlays = [ (import rust-overlay) ];
          pkgs = import nixpkgs {
            inherit system overlays;
          };
        in
        {
          devShell = import ./shell.nix { inherit pkgs; };
        }
      );
}