Rust: Difference between revisions
→Cross-compiling: add link to easy rust crate |
m →Shell.nix example: use monospace font for RUST_SRC_PATH |
||
| (8 intermediate revisions by 7 users not shown) | |||
| Line 37: | Line 37: | ||
== Installating with bindgen support == | == Installating with bindgen support == | ||
By default crates using <code>bindgen</code> will not compile. To add | By default crates using <code>bindgen</code> will not compile. To add bindgen support add the <code>rustPlatform.bindgenHook</code> to your <code>nativeBuildInputs</code>. | ||
Here's an example <code>shell.nix</code>: | Here's an example <code>shell.nix</code>: | ||
| Line 152: | Line 152: | ||
* use [https://crates.io/crates/cargo-xwin cargo-xwin] with rustup install or the [https://search.nixos.org/packages?show=cargo-xwin&type=packages&query=cargo+windows nix cargo plugin] | * use [https://crates.io/crates/cargo-xwin cargo-xwin] with rustup install or the [https://search.nixos.org/packages?show=cargo-xwin&type=packages&query=cargo+windows nix cargo plugin] | ||
* run cargo commands prefixed by xwin, e.g. <code>cargo xwin run --target x86_64-pc-windows-msvc</code> | |||
== Unofficial overlays == | == Unofficial overlays == | ||
| Line 167: | 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 226: | 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" | ||
|- | |- | ||
| [https://github.com/NixOS/nixpkgs/blob/ | ! Name !! Cargo.lock solution !! Derivations !! Build logic !! Supports cross !! Notes | ||
|- | |||
| <code>[https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md buildRustPackage]</code> | |||
| Checksum | | Checksum | ||
| 1 | | 1 | ||
| Line 325: | 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 382: | Line 379: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === VS Code integration === | ||
The [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer | 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. | ||