Have any of you tried using VSCode and Rust together on NixOS?
I’ve used vim for many years, and things like Rust support were just available because I would always start vim in a shell that had a Rust client already present in the shell.
But my new job is all VSCode. Since VSCode is a gui app instead of a shell app, it doesn’t have the benefit of having my normal development environment. As a compromise, I’d like to figure out how to add things like the rust language server, rustc, and rustfmt to the VSCode installation so that I have the tools available for dev, I have them available for VSCode, but i don’t have them littered around in my normal shells.
Do any of you have any ideas for how the rust language server works and how i can bundle it in with VSCode?
Of course, it would be really incredible if I can have per-project Rust versions, because I tend to pin a Rust version in my nix-shell, but I’m totally willing to compromise on that.
This suggestion seems to work. I’ll probably trip on it when I start doing multiple projects.
Unfortunately, the rls in the 20.03 repository seems to not work with rust 1.43 (found crate core compiled by an incompatible version of rustc), and trying to build a new rls fails because rls uses features from the “nightly” build.
Looks like I might have to finally give in and create an FHS environment that uses Rustup.
Where does that install the rust compilers? I just noticed rustup and am messing with it, but I’m accustomed to most third party auto-updaters being really incompatible with the nix store.
Mostly I’ve shied away from these and built custom Rust derivations because I’ve wanted reliable builds, and either stable is too far behind, or unstable is advancing too quickly and dropping my compilers at unexpected times.
rustup does have a dependency on stdenv.cc as part of its patchelf step, which means it does depend on gcc, but that doesn’t put cc in the PATH. It can’t wrap rustup to add it because that won’t affect invocations of rustup’s installed binaries (such as cargo and rustc). So basically, it looks like you need to ensure cc is in your environment to use as a linker. You can pass a flag to rustc to explicitly tell it where to find the linker, but that can’t be done automatically.
This suggests two reasonable courses of action:
Install nixos.stdenv.cc into your environment (or nixos.gcc, which should be the same thing).
Stop using rustup and instead either install nixos.rls (and probably nixos.rustPlatform.rust too so you can run rustc and cargo yourself), or switch to the mozilla overlay if you want a version of rust other than what nixpkgs has.
Sticking stdenv.cc into your environment is probably the simplest approach. And if you don’t want it available globally you could use something like direnv or lorri to only make it available in certain directories, as long as you launch vscode from those directories.
I would not recommend install a C/C++ compiler into your profile but use a nix-shell or direnv for the reasons stated here: How do I install rust? - #8 by Mic92
So, I don’t know why the native RLS was angry with me before. It was the first tool that did not like the custom Rust derivation I’ve written, but maybe it’s happy with the mozilla one. But, mysteriously, I don’t have cc explicitely in this shell, and that’s a touch weird.
FWIW, I had a relatively painless setup by just leaning on VS Code and Rust’s native “package managers”. That is, I added vscode and rustup to my systemPackages, then installed rust toolchain via rustup and VS Code extensions via the Marketplace*
*: it is a bit more elaborate in reality, as I use override for the latest vscode and code --list-extensions to sync the list of extensions I use.