Make VSCode/VScodium recognize rust-analyzer from project flake

How do I make VSCodium use the rust-analyzer binary from my project’s flake.nix devShell?

The rust-lang.rust-analyzer plugin bundles a rust-analyzer binary that it defaults to, but it is too old for the nightly compiler. Is there a simple way to sync the rust-analyzer plugin in VSCodium with the path determined by the flake’s devShell?

I have a VSCodium installation with the following configuration:

{pkgs, ...}: {
  environment.systemPackages = with pkgs; [
    (vscode-with-extensions.override {
      vscode = vscodium;
      vscodeExtensions = with vscode-extensions; [
        # Nix
        bbenoist.nix
        kamadorueda.alejandra

        # Rust
        rust-lang.rust-analyzer
        tamasfe.even-better-toml
        serayuzgur.crates

        # Writing
        yzhang.markdown-all-in-one
        nvarner.typst-lsp
        stkb.rewrap

        # General
        eamodio.gitlens
        asvetliakov.vscode-neovim
      ];
    })
  ];
}
1 Like

The plugin setting rust-analyzer.server.path will override the bundled binary in settings.conf:

{
    "rust-analyzer.server.path": "rust-analyzer"
}

I can set this in either

  • ~/.config/VSCodium/User/settings.json
  • .vscode/settings.json (in the project directory)

An additional option specifically for projects that use direnv is to use the mkhl.direnv VSCodium plugin, which can load the project flake’s devShell environment regardless of what directory VSCodium was started from.


Another hint I received, which I didn’t make use of, suggests an alternative way to configure rust-analyzer:

Here, pkgs.rust-analyzer-unwrapped is installed instead, and the RUST_SRC_PATH environment variable is used to determine what version of Rust should be analysed. It still boils down to VSCodium not receiving the environment in which it was started, in spite of running it like codium . in the project directory. Perhaps there are other ways to achieve this.

1 Like

Thanks so much for this…been bugging the hell out of me.

1 Like