I’m trying to set up a shell.nix for rust development:
let
# Last updated: 2/12/21
pkgs = import (fetchTarball("https://github.com/NixOS/nixpkgs/archive/a58a0b5098f0c2a389ee70eb69422a052982d990.tar.gz")) {};
# Rolling updates, not deterministic.
# pkgs = import (fetchTarball("channel:nixpkgs-unstable")) {};
in pkgs.mkShell {
buildInputs = [
pkgs.cargo
pkgs.rustc
# Necessary for the openssl-sys crate:
pkgs.openssl
pkgs.pkg-config
];
}
This is great in that it gives me cargo and rustc in the path, and I can build my project, but I’m not able to use rust-analyzer in vscode:
rust-analyzer failed to load workspace: Failed to find sysroot for Cargo.toml file /home/skainswo/dev/cuddlefish/api/Cargo.toml. Is rust-src installed?: can't load standard library from sysroot /nix/store/l3icq4dzpk5cwq3mf4459cd7ydrxvy0i-rustc-1.49.0 (discovered via `rustc --print sysroot`) try installing the Rust source the same way you installed rustc
Ok, so I’m missing some kind of package called rust-src, which I’m assuming contains extra goodies. But a quick search reveals no nix packages: NixOS Search.
Also, our wiki entry on rust (Rust - NixOS Wiki) looks a bit sad atm. I’d be more than happy to update it, although I still haven’t figured out how to get things working for my own purposes yet…
Anyone know how to use rust-analyzer with the nixpkgs versions of rustc and cargo?
This did the trick for me, thank you!! Out of curiosity, why doesn’t cargo or rustc set up this env var automatically? I feel like it would be convenient for users to have a single derivation they could include and get a full, working rust development environment.
Perhaps we could include a new convenience derivation like rust-everything (or rust-moar or just-let-me-rust) or something to bring in rustc, cargo, rustLibSrc, rustfmt, etc?
Note that if you do that, running cargo commands in a shell that does not share an exact env with your editor may end up rebuilding things from scratch (because rust assumes certain changes in environment mean it needs to do so).
This can make rust-analyzer as well as your normal builds extremely slow, so try to stick to compiling with the editor.
In general I’d recommend using rustc and cargo on a per-project basis. Mixing globally installed Rust stuff with local environments is likely to be un-fun.
These are side projects where I have a couple of hours every so many weeks to work on them. I really can’t afford spending most of that time fighting with things that are broken in Nix.
Switching to Rustup also fixed the thing where Rust would rebuild my entire project on every change.