Rust: Difference between revisions

Add tip for using rust-analyzer from nixpkgs with the VS Code extension
DHCP (talk | contribs)
m Shell.nix example: use monospace font for RUST_SRC_PATH
 
(2 intermediate revisions by 2 users not shown)
Line 168: Line 168:
The [https://nixos.org/manual/nixpkgs/stable/#rust Nixpkgs manual] uses <code>buildRustPackage</code>.
The [https://nixos.org/manual/nixpkgs/stable/#rust Nixpkgs manual] uses <code>buildRustPackage</code>.


[https://srid.ca/rust-nix This] blog post shows how to do it using <code>dream2nix</code>. A template repo is available here: https://github.com/srid/rust-nix-template
[https://web.archive.org/web/20260108165619/https://srid.ca/rust-nix This] blog post shows how to do it using <code>dream2nix</code>. A template repo is available here: https://github.com/srid/rust-nix-template


== Using overrideAttrs with Rust Packages ==
== Using overrideAttrs with Rust Packages ==
Line 322: Line 322:
) { }
) { }
</syntaxhighlight>
</syntaxhighlight>
This will have the stable Rust compiler + the official formatter and linter inside the ephemeral shell. It'll also set the RUST_SRC_PATH environment variable to point to the right location, which tools, such as rust-analyzer, require to be set.
This will have the stable Rust compiler + the official formatter and linter inside the ephemeral shell. It'll also set the <code>RUST_SRC_PATH</code> environment variable to point to the right location, which tools, such as rust-analyzer, require to be set.


=== Custom Rust version or targets ===
=== Custom Rust version or targets ===
Line 380: Line 380:


=== VS Code integration ===
=== VS Code integration ===
The [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer] VS Code extension offers Rust support.
The [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer rust-analyzer] VS Code extension offers Rust support.


If you get the error <code>can't load standard library, try installing rust-src</code>, either configure <code>RUST_SRC_PATH</code> as shown above, or set <code>"rust-analyzer.server.path": "rust-analyzer"</code> in your VS Code settings to use rust-analyzer from nixpkgs rather than the one bundled with the extension.
If you get the error <code>can't load standard library, try installing rust-src</code>, either configure <code>RUST_SRC_PATH</code> as shown above, or set <code>"rust-analyzer.server.path": "rust-analyzer"</code> in your VS Code settings to use rust-analyzer from nixpkgs rather than the one bundled with the extension.