Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nix-shell environment #9506

Closed
Hentioe opened this issue Jul 6, 2021 · 9 comments
Closed

Support nix-shell environment #9506

Hentioe opened this issue Jul 6, 2021 · 9 comments

Comments

@Hentioe
Copy link

Hentioe commented Jul 6, 2021

When developing a Rust project on NixOS, there will usually be compilation errors in projects that rely on external C libraries. This is because pkg-config under NixOS cannot find any shared libraries by default.

We NixOS users will create a default.nix file in the current directory, for example:

with import <nixpkgs> {};
stdenv.mkDerivation {
   name = "dev-environment";
   buildInputs = [pkg-config imagemagick llvmPackages_12.libclang.lib ];

   LIBCLANG_PATH = "${llvmPackages_12.libclang.lib}/lib";
}

This file can control all aspects of the nix-shell environment (including installed packages, shared libraries, etc.). Then when we entered nix-shell, the customized environment that was able to successfully build the project was born.

Unfortunately, we can hardly avoid rust-analyzer's error report, because rust-analyzer will not execute cargo or other commands in nix-shell.

If possible, I would like rust-analyzer to use nix-shell to execute commands when it recognizes that there is a default.nix file in the current directory, or to launch itself through nix-shell.

@matklad
Copy link
Member

matklad commented Jul 6, 2021

I think it should be the responsibility of the client to spawn rust-analyzer itself in the nix-shell environment. For vscode https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector seems like the right extension.

@Hentioe
Copy link
Author

Hentioe commented Jul 6, 2021

I think it should be the responsibility of the client to spawn rust-analyzer itself in the nix-shell environment. For vscode https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector seems like the right extension.

Yes, in fact, I also think that this is something that the system should solve or VS Code provides support. I have never found a good solution, so I made this feature request. Fortunately, the VS Code extension you sent is very useful, and the initial test has made rust-analyzer run perfectly. Thank you so much!

🧡 💛 💚 💙 💜

@Hentioe
Copy link
Author

Hentioe commented Jul 6, 2021

I hope this issue can help NixOS users who search for related issues in the future. To avoid disturbing you, I will close it first!

@Hentioe Hentioe closed this as completed Jul 6, 2021
@Veykril
Copy link
Member

Veykril commented Jul 6, 2021

We could probably add a small section for this to the manual for vscode + nix setup? cc #9504

@lnicola
Copy link
Member

lnicola commented Jul 6, 2021

I'm not familiar with NixOS, but what about non-Code users? Can you run your editor inside nix-shell? Can you make a wrapper script and configure it as the LSP binary?

@Hentioe
Copy link
Author

Hentioe commented Jul 6, 2021

I'm not familiar with NixOS, but what about non-Code users? Can you run your editor inside nix-shell? Can you make a wrapper script and configure it as the LSP binary?

Yes, I used to enter nix-shell first and then start VS Code in the shell to avoid this problem. Now I have found a VS Code extension that can automatically apply this environment, so I don't think I need to wrap the rust-analyzer program myself. Thank you for the solution ideas. 😄

@ashebanow
Copy link

FWIW, my solution to this is to use the nix-direnv and direnv nixpkgs to set up my environment rather than manually invoking nix-shell. Then the direnv extension in VSCode loads everything for you based on that. rust-analyzer works perfectly once everything is set up.

@Hentioe
Copy link
Author

Hentioe commented Jan 8, 2024

direnv can load our nix-shell environment by default when opening a terminal. I haven't used the VS Code extension for it, but the way you use it, it should also allow VS Code to load the environment configured in the nix-shell so that rust-analyzer can start successfully.

There is also the Nix Environment Selector extension mentioned in this issue, which is also perfect for making VS code automatically load the environment configured in the nix-shell, and it can also switch to a different shell.nix.

@woile
Copy link

woile commented Aug 30, 2024

I've started using the extension direnv-vscode and it works well with rust-analyzer 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants