Nix-shell and Rust and rust-analyzer

I found an unavoidable error when writing Rust code. This is caused by the fact that the rust-analyzer extension of VS Code (or other editors) cannot automatically run in the nix-shell environment.

Currently, apart from executing nix-shell in the terminal, and then entering code to start, so that the VS Code process inherits this environment, I have no way to avoid its errors.

I submitted a related feature request to the rust-analyzer project (issue here), but the possibility of support may be low. Because this is more like an editor or system defect.

I don’t know if you have a better way to solve this problem.

Did you declaratively install vscode with the rust-analyzer nix-expression ( nixpkgs/pkgs/misc/vscode-extensions/rust-analyzer at 3b6c3bee9174dfe56fd0e586449457467abe7116 · NixOS/nixpkgs · GitHub) or did you interactively install the extension from within the editor?
Some extensions are manually packaged in nixpkgs as to resolve dependencies explicitly and rust-analyzer is one of them.

1 Like

Personally, in Emacs I solve this using direnv integration, which will load the environment that I’m expecting when I visit certain project directories:

Perhaps there is something comparable for vscode? A quick search reveals this:

1 Like

:smiley: Thanks for your help, I successfully solved this problem using this VS Code extension: Nix Environment Selector - Visual Studio Marketplace

1 Like