Rust-src not found, and other misadventures of developing rust on NixOS

That works but a more generic solution is to use direnv to invoke shells automatically and then set up vscode to import its environments with e.g. GitHub - direnv/direnv-vscode: unclutter your .profile

2 Likes

I’m using the direnv-vscode extension which @TLATER suggested, but I wonder if there is a way to keep using basic Rust from nixpkgs (because it’s the only option I know which integrates with openssl smoothly) while still having access to rust-src?

I didn’t have this issue before and now I keep getting

2025-03-20T14:22:24.113403218Z ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/mrxn6452hz1zfp376y2iirxmvp5018i8-rustc-1.84.1

My flake is as basic of a dev env setup as it gets:

{
  description = "GEOsurge - generative engine optimization innovation flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    flake-utils = {
      url = "github:numtide/flake-utils";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
      let
        pkgs = import nixpkgs { inherit system; };
      in
      {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            rustc
            cargo
            rustfmt
            clippy
            rust-analyzer
            pkg-config
            sqlx-cli
            postgresql
            sqlite
            nodejs
            pnpm
            zip
            unzip
            rsync
            openssl.dev
            openssl

            chromedriver
            chromium
            xvfb-run

            typescript
            nodePackages.typescript

            rclone

            csvlens

            act
            docker
            docker-compose

            shellcheck
          ];
        };

        devShell = self.devShells.${system}.default;
      }
    );
}

Any suggestions are welcome!

You could always use fenix if you have more specific requirements on the toolchain; it works just fine with openssl IME.

In fairness, I’ve also not had any issues with missing rust-src with the default toolchain.

I migrated from fenix to default toolchain because of openssl issues :upside_down_face:

I’ll see if someone else has the same issue. Thanks for a fast response.

You’re using VSCode / codium too?

Try replacing that with packages = with pkgs; [ ... ? I don’t think the pkg-config hook is executed with this setup, so no pkg-config libraries (such as openssl) will be picked up.

2 Likes

Here is what is working for me with direnv-vscode:

flake.nix:

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

  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
      rust-overlay,
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        overlays = [ rust-overlay.overlays.default ];
        pkgs = import nixpkgs { inherit overlays system; };
        rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
      in
      {
        devShell = pkgs.mkShell {
          packages = with pkgs; [
            rust
          ];
        };
      }
    );
}

rust-toolchain.toml:

[toolchain]
channel = "stable"
components = ["rust-analyzer", "rust-src"]
profile = "default"

.envrc:

use flake