DeterminateSystems / fh

The official CLI for FlakeHub: search for flakes, and add new inputs to your Nix flake.
https://flakehub.com
Apache License 2.0
118 stars 8 forks source link

Usable rust-analizer #73

Closed vic closed 10 months ago

vic commented 11 months ago

I just initialized a rust project of mine using fh init. The whole thing generates a very nice looking flake, when prompted about rust-analyzer I confirmed it being added to the environment. However when trying to use some actix_web macros (project direnv-loaded into my editor), rust-analyzer is giving some compiler errors due to it not finding a sysroot.

rust-analyzer failed to load workspace: Failed to find sysroot [...]

Turns out it's a somewhat common issue, and you have to enable the rust-src extension when selecting a rustToolchain from oxalica/rust-overlay. The following changes to the fh-generated flake fixed the issue for me:

diff --git a/flake.nix b/flake.nix
index 5b567c3..5c59c36 100644
--- a/flake.nix
+++ b/flake.nix
@@ -20,7 +20,9 @@
       overlays = [
         rust-overlay.overlays.default
         (final: prev: {
-          rustToolchain = final.rust-bin.stable.latest.default;
+          rustToolchain = final.rust-bin.stable.latest.default.override {
+            extensions = [ "rust-src" ];
+          };
         })
       ];

@@ -52,7 +53,7 @@

           env = {
             RUST_BACKTRACE = "1";
+            RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";  # for rust-analyzer
           };
         };

I believe most people will be enabling rust-analyzer for using it on their IDEs, and expect everything to be ready for working on their rust code.

My question is, would it be desirable to also add env.RUST_SRC_PATH (and enable rust-src) extension when people confirm to enable rust-analyzer ? If so, I'd gladly help with a PR for the rust handler.