Rust: Difference between revisions

Ind E (talk | contribs)
m fix typo bindgen misspelled as bindegen
m Update that buildRustPackage can now import Cargo.lock
 
(6 intermediate revisions by 5 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 227: Line 227:
At the time of writing, there are now no less than 8 different solutions for building Rust code with Nix. In the following table they are compared:
At the time of writing, there are now no less than 8 different solutions for building Rust code with Nix. In the following table they are compared:


{|
{| class="wikitable" style="margin:auto"
| Name
| Cargo.lock solution
| Derivations
| Build logic
| Supports cross
| Notes
|-
|-
| [https://github.com/NixOS/nixpkgs/blob/4fc53b59aecbc25c0e173163d60155f8fca14bfd/doc/languages-frameworks/rust.section.md <code>buildRustPackage</code>]
! Name !! Cargo.lock solution !! Derivations !! Build logic !! Supports cross !! Notes
| Checksum
|-
| <code>[https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md buildRustPackage]</code>
| Checksum or <code>[https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md#importing-a-cargolock-file-importing-a-cargolock-file Import]</code>
| 1
| 1
| cargo
| cargo
Line 326: 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 383: Line 379:
</syntaxhighlight>
</syntaxhighlight>


=== VSCode integration ===
=== VS Code integration ===
The [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer rust-lang.rust-analyzer] VSCode 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.


You can use the [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector arrterian.nix-env-selector] extension to enable your nix-shell inside VSCode and have these settings picked up by other extensions.
You can use the [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector arrterian.nix-env-selector] extension to enable your nix-shell inside VSCode and have these settings picked up by other extensions.