How to write nix expressions properly to get loaded rust extensions into nix-shell

My development environment setup for Rust is based on the blog How I Start: Nix which in my opinion describes the setup procedure quite comprehensive.
Meanwhile I’m trying to add some extensions to the rust setup and I’m facing difficulty to do it by my self.
Below is my nix/rust.nix which nix-shell loads errorless. But is seems the content of extensions remains unevaluated. It doesn’t matter what extensions contain. No error appears at all.
Could any one explain why extensions are ignored and give an advice how it could be fixed?

nix/rust.nix 
{ sources ? import ./sources.nix }:

let
  pkgs =
    import sources.nixpkgs { overlays = [ (import sources.nixpkgs-mozilla) ]; };
  rust = (
    pkgs.latest.rustChannels.nightly.rust.override {
      extensions = [ "rust-src" "rust-analysis" "rustfmt-preview" ];
    }
  );
  channel = "nightly";
  date = "2021-01-14";
  targets = [ ];
  chan = pkgs.rustChannelOfTargets channel date targets;
in 
with rust;
chan

Thank you,
Alexei

I suspect this line isn’t doing what you might think it does?

I would try dropping or moving the rust = ... and instead ending the file as:

in chan.rust.override {
  extensions = [ "rust-src" "rust-analysis" "rustfmt-preview" ];
}

Or however you choose to refactor that, to make it explicit that you want the rust attribute specifically from your chan.

with is often considered a footgun or antipattern due to the ambiguity/implicitness, but the idiomatic usage lets you write this:

with pkgs; [ foo bar ]

In place of:

[ pkgs.foo pkgs.bar ]

Which maybe doesn’t really line up with your intent.

1 Like

Thank you for your prompt replay @shanesveller. The variant you’ve proposed is indeed much easier and smarter. I stay by chan.override yet because nix-shell complained about error: attribute 'rust' missing.

Thank you one more time!