Wrong rustc version when running nix develop

Hello! Brand new to using nix, been at it a few days. I’ve gotten my system more or less up and going using flakes and wanted to brush up on my rust using rustlings. They have a flake.nix and shell.nix. Pulled it down and ran nix develop and was met with an error (error and flake.nix bellow).

I realize I need to change the version of rustc it’s using…but I’m not entirely sure how to do that. Looking at this other post it seems like I could use the mozilla overlay for rustc but once again…not really sure how to do that. I get what an overlay is, how to generate one, and it goes in nixpkgs.overlays. I attempted to do this but realized I don’t have enough idea of what everything else is doing to know where to place that code. I’ll place my attempt and error at the bottom.

If anyone can point me in the direction on the proper way to solve this, that would be awesome! Thanks in advanced.

Error when running nix develop (no changes of my own)

error: builder for '/nix/store/r1sxxq6083m1bcvhp768vlhmwgyzz44c-rustlings.drv' failed with exit code 101;
       last 10 log lines:
       > Finished cargoSetupPostUnpackHook
       > patching sources
       > Executing cargoSetupPostPatchHook
       > Validating consistency between /build/source/Cargo.lock and /build/cargo-vendor-dir/Cargo.lock
       > Finished cargoSetupPostPatchHook
       > configuring
       > building
       > Executing cargoBuildHook
       > ++ env CC_x86_64-unknown-linux-gnu=/nix/store/0vln6gxc9b2wr9yhbn0aspy6badlj638-gcc-wrapper-11.3.0/bin/cc CXX_x86_64-unknown-linux-gnu=/nix/store/0vln6gxc9b2wr9yhbn0aspy6badlj638-gcc-wrapper-11.3.0/bin/c++ CC_x86_64-unknown-linux-gnu=/nix/store/0vln6gxc9b2wr9yhbn0aspy6badlj638-gcc-wrapper-11.3.0/bin/cc CXX_x86_64-unknown-linux-gnu=/nix/store/0vln6gxc9b2wr9yhbn0aspy6badl
j638-gcc-wrapper-11.3.0/bin/c++ cargo build -j 16 --target x86_64-unknown-linux-gnu --frozen --release
       > error: package `clap_lex v0.5.1` cannot be built because it requires rustc 1.70.0 or newer, while the currently active rustc version is 1.64.0
       For full logs, run 'nix log /nix/store/r1sxxq6083m1bcvhp768vlhmwgyzz44c-rustlings.drv'.
error: 1 dependencies of derivation '/nix/store/kn2568q7w9sr9xsr4kk7r1y43f76c1kk-nix-shell-env.drv' failed to build

And here is the flake.nix at the time of writing this.

{
  description = "Small exercises to get you used to reading and writing Rust code";

  inputs = {
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };

  outputs = { self, flake-utils, nixpkgs, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        cargoBuildInputs = with pkgs; lib.optionals stdenv.isDarwin [
          darwin.apple_sdk.frameworks.CoreServices
        ];

        rustlings =
          pkgs.rustPlatform.buildRustPackage {
            name = "rustlings";
            version = "5.6.0";

            buildInputs = cargoBuildInputs;
            nativeBuildInputs = [pkgs.git];

            src = with pkgs.lib; cleanSourceWith {
              src = self;
              # a function that returns a bool determining if the path should be included in the cleaned source
              filter = path: type:
                let
                  # filename
                  baseName = builtins.baseNameOf (toString path);
                  # path from root directory
                  path' = builtins.replaceStrings [ "${self}/" ] [ "" ] path;
                  # checks if path is in the directory
                  inDirectory = directory: hasPrefix directory path';
                in
                inDirectory "src" ||
                inDirectory "tests" ||
                hasPrefix "Cargo" baseName ||
                baseName == "info.toml";
            };

            cargoLock.lockFile = ./Cargo.lock;
          };
      in
      {
        devShell = pkgs.mkShell {
          RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

          buildInputs = with pkgs; [
            cargo
            rustc
            rust-analyzer
            rustlings
            rustfmt
            clippy
          ] ++ cargoBuildInputs;
        };
        apps = let
          rustlings-app = {
            type = "app";
            program = "${rustlings}/bin/rustlings";
          };
        in {
          default = rustlings-app;
          rustlings = rustlings-app;
        };
        packages = {
          inherit rustlings;
          default = rustlings;
        };
      });
}

My sorry attempt at fixing this issue

{
  description = "Small exercises to get you used to reading and writing Rust code";

  inputs = {
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    moz_overlay = {
      url = "github:mozilla/nixpkgs-mozilla";
    };
  };

  outputs = { self, flake-utils, nixpkgs, moz_overlay, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        nixpkgs.overlays = [ moz_overlay ];
        pkgs = nixpkgs.legacyPackages.${system};

        cargoBuildInputs = with pkgs; lib.optionals stdenv.isDarwin [
          darwin.apple_sdk.frameworks.CoreServices
        ];

        rustlings =
          pkgs.rustPlatform.buildRustPackage {
            name = "rustlings";
            version = "5.6.0";

            buildInputs = cargoBuildInputs;
            nativeBuildInputs = [pkgs.git];

            src = with pkgs.lib; cleanSourceWith {
              src = self;
              # a function that returns a bool determining if the path should be included in the cleaned source
              filter = path: type:
                let
                  # filename
                  baseName = builtins.baseNameOf (toString path);
                  # path from root directory
                  path' = builtins.replaceStrings [ "${self}/" ] [ "" ] path;
                  # checks if path is in the directory
                  inDirectory = directory: hasPrefix directory path';
                in
                inDirectory "src" ||
                inDirectory "tests" ||
                hasPrefix "Cargo" baseName ||
                baseName == "info.toml";
            };

            cargoLock.lockFile = ./Cargo.lock;
          };
      in
      {
        devShell = pkgs.mkShell {
          RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

          buildInputs = with pkgs; [
            cargo
            pkgs.latest.rustChannels.nightly.rustc
            rust-analyzer
            rustlings
            rustfmt
            clippy
          ] ++ cargoBuildInputs;
        };
        apps = let
          rustlings-app = {
            type = "app";
            program = "${rustlings}/bin/rustlings";
          };
        in {
          default = rustlings-app;
          rustlings = rustlings-app;
        };
        packages = {
          inherit rustlings;
          default = rustlings;
        };
      });
}

The error I get when running with my “fix”

error:
       … while evaluating the attribute 'devShell'

         at /nix/store/yad92rjajv9nz1fpvcqkcbwaa0q3pyc5-source/flake.nix:56:9:

           55|       {
           56|         devShell = pkgs.mkShell {
             |         ^
           57|           RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

       error: attribute 'legacyPackages' missing

       at /nix/store/yad92rjajv9nz1fpvcqkcbwaa0q3pyc5-source/flake.nix:20:16:

           19|         nixpkgs.overlays = [ moz_overlay ];
           20|         pkgs = nixpkgs.legacyPackages.${system};
             |                ^
           21|

The maintainers of the rustlings repo forgot to update the flake.lock file. After checking out the repo and cd’ing into it, run nix flake update before you do nix develop.

1 Like