Skip to content

Rust analyzer does not work in nix flake based development #6105

@michaelfortunato

Description

@michaelfortunato

When I use nix develop and then head into neovim, most tokens have a red squiggly underline and LSP actions, like go to definition, are not completely working.

https://asciinema.org/a/XzG8hoWcLV5NLPSkIgKGSo80i

I googled a bit and found this https://vi.stackexchange.com/questions/45071/rust-analyser-in-neovim-is-underlining-almost-all-words-red-in-working-code. Seems like rust-src is involved.

Using https://zero-to-nix.com/start/init-flake/ on a Rust project, I get

# This flake was initially generated by fh, the CLI for FlakeHub (version 0.1.22)
{

  # Flake inputs
  inputs = {
    flake-schemas.url = "https://flakehub.com/f/DeterminateSystems/flake-schemas/*";

    nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*";

    rust-overlay = {
      url = "https://flakehub.com/f/oxalica/rust-overlay/0.1.*";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  # Flake outputs that other flakes can use
  outputs = { self, flake-schemas, nixpkgs, rust-overlay }:
    let
      # Nixpkgs overlays
      overlays = [
        rust-overlay.overlays.default
        (final: prev: {
          rustToolchain = final.rust-bin.stable.latest.default.override { extensions = [ "rust-src"]; };
        })
      ];

      # Helpers for producing system-specific outputs
      supportedSystems = [ "aarch64-darwin" ];
      forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
        pkgs = import nixpkgs { inherit overlays system; };
      });
    in {
      # Schemas tell Nix about the structure of your flake's outputs
      schemas = flake-schemas.schemas;

      # Development environments
      devShells = forEachSupportedSystem ({ pkgs }: {
        default = pkgs.mkShell {
          # Pinned packages available in the environment
          packages = with pkgs; [
            rustToolchain
            cargo-edit
            cargo-outdated
            cargo-watch
            rust-analyzer
            curl
            git
            jq
            nixpkgs-fmt
          ];

          # Environment variables
          env = {
            RUST_BACKTRACE = "1";
            RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
          };
        };
      });
    };
}

And my developmenet environment works well.
So maybe we want to adopt that overlay in the above flake and add it to our flake.nix.

...
overlays = [
        rust-overlay.overlays.default
        (final: prev: {
          rustToolchain = final.rust-bin.stable.latest.default.override { extensions = [ "rust-src"]; };
        })
      ];

outputs = inputs@{ flake-parts, crane, nixpkgs, fenix, rust-manifest, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } {

Image

Here is the salient extract from the LspLog

[ERROR][2025-03-26 10:42:05] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.nix-profile/bin/nixd"	"stderr"	"I[10:42:05.762] 92884: <-- shutdown(17)\nI[10:42:05.762] 92884: --> reply:shutdown(17)\n"
[START][2025-03-26 10:43:12] LSP logging initiated
[ERROR][2025-03-26 10:43:12] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:43:12.435527-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:43:20] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:43:20.045001-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:43:20] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:43:20.315771-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:43:42] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:43:42.168023-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[START][2025-03-26 10:51:18] LSP logging initiated
[ERROR][2025-03-26 10:51:18] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:51:18.291206-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:51:18] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:51:18.599051-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:51:18] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:51:18.842555-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[ERROR][2025-03-26 10:51:19] ...p/_transport.lua:36	"rpc"	"/Users/michaelfortunato/.local/share/nvim/mason/bin/rust-analyzer"	"stderr"	"2025-03-26T10:51:19.400147-05:00 ERROR can't load standard library, try installing `rust-src` sysroot_path=/nix/store/w351i7f68y33knpn5134zjnnnzr815ki-rust-with-components-2025-02-20\n"
[START][2025-03-26 12:59:13] LSP logging initiated

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions