Rust: Difference between revisions
→Installation via rustup: use bindgenHook and remove now uneccesary attrs |
m Format table so the header is clearly distinguished from the rest of the rows. |
||
| (10 intermediate revisions by 7 users not shown) | |||
| Line 12: | Line 12: | ||
Here's an example <code>shell.nix</code>: | Here's an example <code>shell.nix</code>: | ||
< | <syntaxhighlight lang="nix"> | ||
let | let | ||
# Pinned nixpkgs, deterministic. Last updated: 2/12/21. | # Pinned nixpkgs, deterministic. Last updated: 2/12/21. | ||
| Line 27: | Line 27: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
nativeBuildInputs = [ | nativeBuildInputs = [ | ||
cargo | cargo | ||
| Line 33: | Line 34: | ||
} | } | ||
) { } | ) { } | ||
</ | </syntaxhighlight> | ||
== 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>: | ||
< | <syntaxhighlight lang="nix"> | ||
{ | { | ||
pkgs ? import <nixpkgs> { }, | pkgs ? import <nixpkgs> { }, | ||
| Line 53: | Line 54: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
nativeBuildInputs = [ | nativeBuildInputs = [ | ||
cargo | cargo | ||
| Line 67: | Line 69: | ||
} | } | ||
) { } | ) { } | ||
</ | </syntaxhighlight> | ||
This also works, when compiling rust crates: | This also works, when compiling rust crates: | ||
| Line 109: | Line 111: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
nativeBuildInputs = [ | nativeBuildInputs = [ | ||
rustup | rustup | ||
| Line 145: | Line 148: | ||
* [https://github.com/jraygauthier/jrg-rust-cross-experiment/tree/master/simple-static-rustup-target-windows simple-static-rustup-target-windows] | * [https://github.com/jraygauthier/jrg-rust-cross-experiment/tree/master/simple-static-rustup-target-windows simple-static-rustup-target-windows] | ||
** [https://github.com/jraygauthier/jrg-rust-cross-experiment/blob/master/simple-static-rustup-target-windows/shell.nix shell.nix] | ** [https://github.com/jraygauthier/jrg-rust-cross-experiment/blob/master/simple-static-rustup-target-windows/shell.nix shell.nix] | ||
=== To Windows via a 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 163: | Line 171: | ||
== Using overrideAttrs with Rust Packages == | == Using overrideAttrs with Rust Packages == | ||
There are two ways to use <code>overrideAttrs</code> with Rust packages: | |||
[ | * Using [[Import From Derivation]]: | ||
<p> | |||
== | <syntaxhighlight lang="nix"> | ||
nil = pkgs.nil.overrideAttrs ( | |||
finalAttrs: previousAttrs: { | |||
version = "unstable-2024-09-19"; | |||
< | |||
src = pkgs.fetchFromGitHub { | |||
owner = "oxalica"; | |||
repo = "nil"; | |||
rev = "c8e8ce72442a164d89d3fdeaae0bcc405f8c015a"; | |||
hash = "sha256-mIuOP4I51eFLquRaxMKx67pHmhatZrcVPjfHL98v/M8="; | |||
}; | |||
} | |||
}) | # Requires IFD | ||
cargoDeps = pkgs.rustPlatform.importCargoLock { | |||
</ | lockFile = finalAttrs.src + "/Cargo.lock"; | ||
allowBuiltinFetchGit = true; | |||
}; | |||
cargoHash = null; | |||
} | |||
); | |||
</syntaxhighlight> | |||
</p> | |||
* Overriding <code>cargoDeps</code>: | |||
<p> | |||
<syntaxhighlight lang="nix"> | |||
nil = pkgs.nil.overrideAttrs ( | |||
finalAttrs: previousAttrs: { | |||
version = "unstable-2024-09-19"; | |||
src = pkgs.fetchFromGitHub { | |||
owner = "oxalica"; | |||
repo = "nil"; | |||
rev = "c8e8ce72442a164d89d3fdeaae0bcc405f8c015a"; | |||
hash = "sha256-mIuOP4I51eFLquRaxMKx67pHmhatZrcVPjfHL98v/M8="; | |||
}; | |||
# Doesn't require IFD | |||
cargoDeps = previousAttrs.cargoDeps.overrideAttrs { | |||
name = "nil-vendor.tar.gz"; | |||
inherit (finalAttrs) src; | |||
#outputHash = pkgs.lib.fakeHash; | |||
outputHash = "sha256-RWgknkeGNfP2wH1X6nc+b10Qg1QX3UeewDdeWG0RIE8="; | |||
#}; | |||
} | |||
); | |||
</syntaxhighlight> | |||
</p> | |||
== Packaging Rust projects with nix == | == Packaging Rust projects with nix == | ||
| Line 186: | 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 | |- | ||
! Name !! Cargo.lock solution !! Derivations !! Build logic !! Supports cross !! Notes | |||
|- | |- | ||
| [https://github.com/NixOS/nixpkgs/blob/ | | <code>[https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md buildRustPackage]</code> | ||
| Checksum | | Checksum | ||
| 1 | | 1 | ||
| Line 252: | Line 289: | ||
== Shell.nix example == | == Shell.nix example == | ||
< | <syntaxhighlight lang="nix"> | ||
{ | { | ||
pkgs ? import <nixpkgs> { }, | pkgs ? import <nixpkgs> { }, | ||
| Line 268: | Line 305: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
nativeBuildInputs = [ | nativeBuildInputs = [ | ||
rustc | rustc | ||
| Line 283: | Line 321: | ||
} | } | ||
) { } | ) { } | ||
</ | </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 RUST_SRC_PATH environment variable to point to the right location, which tools, such as rust-analyzer, require to be set. | ||
| Line 321: | Line 359: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
# host/target agnostic programs | # host/target agnostic programs | ||
depsBuildBuild = [ | depsBuildBuild = [ | ||
| Line 341: | Line 380: | ||
=== VSCode integration === | === VSCode integration === | ||
The | The [https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer rust-lang.rust-analyzer] VSCode extension offers Rust support. | ||
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. | ||
| Line 366: | Line 405: | ||
}: | }: | ||
mkShell { | mkShell { | ||
strictDeps = true; | |||
nativeBuildInputs = [ | nativeBuildInputs = [ | ||
my-rust-toolchain | my-rust-toolchain | ||
| Line 435: | Line 475: | ||
[[Category:Languages]] | [[Category:Languages]] | ||
[[Category:Rust]] | |||