Rust: Difference between revisions

Remove Neovim Completion Section. This section references racer, which hasn't been relevant for rust autocomplete in years now (the last commit on the project was 2 years ago). It's also not a valid, working snippet - nor does it configure neovim in a way you'd expect.
m dream2nix update link
 
(3 intermediate revisions by 2 users not shown)
Line 21: Line 21:
in pkgs.mkShell {
in pkgs.mkShell {
   buildInputs = [ pkgs.cargo pkgs.rustc ];
   buildInputs = [ pkgs.cargo pkgs.rustc ];
}
</syntaxHighlight>
== Installating with bindgen support ==
By default crates using <code>bindgen</code> will not compile. To add bindegen support add the <code>rustPlatform.bindegenHook</code> to your <code>nativeBuildInputs</code>.
Here's an example <code>shell.nix</code>:
<syntaxHighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  nativeBuildInputs = [
    pkgs.cargo
    pkgs.rustc
    pkgs.rustPlatform.bindgenHook
    # optional: add pkg-config support
    pkgs.pkg-config
  ];
  buildInputs = [
    # add desired native packages
    # ...
  ];
  # ...
}
</syntaxHighlight>
This also works, when compiling rust crates:
<syntaxHighlight lang="nix">
{
  rustPlatform,
  pkg-config,
  ...
}:
rustPlatform.buildRustPackage {
  # ...
  nativeBuildInputs = [
    rustPlatform.bindgenHook
    pkg-config
  ];
  buildInputs = [
    # add desired native packages
    # ...
  ];
}
}
</syntaxHighlight>
</syntaxHighlight>
Line 76: Line 120:


A minimal example of the <code>rust-toolchain.toml</code>:
A minimal example of the <code>rust-toolchain.toml</code>:
<syntaxHighlight lang="toml">
<syntaxhighlight lang="toml">
[toolchain]
[toolchain]
channel = "stable"
channel = "stable" # This can also be "nightly" if you want a nightly rust
</syntaxHighlight>
                  # or nightly-20XX-XX-XX for a specific nightly.
</syntaxhighlight>


The important part is that this also works with complex setups using bindgen and precompiled C libraries. To add a new C library in the search path of bindgen and rustc edit the variables <code>BINDGEN_EXTRA_CLANG_ARGS</code> and <code>RUSTFLAGS</code>
The important part is that this also works with complex setups using bindgen and precompiled C libraries. To add a new C library in the search path of bindgen and rustc edit the variables <code>BINDGEN_EXTRA_CLANG_ARGS</code> and <code>RUSTFLAGS</code>
Line 99: Line 144:


# https://github.com/cachix/devenv/blob/main/examples/rust/devenv.nix and <code>devenv shell</code>
# https://github.com/cachix/devenv/blob/main/examples/rust/devenv.nix and <code>devenv shell</code>
== Rust Nightlies ==
# Use one of the overlays above,
# Or, use rustup


== Developing Rust projects using Nix ==
== Developing Rust projects using Nix ==
Line 185: Line 225:
| Inspired by naersk, with [https://discourse.nixos.org/t/introducing-crane-composable-and-cacheable-builds-with-cargo/17275/4 better support for composing Cargo invocations as completely separate derivations]
| Inspired by naersk, with [https://discourse.nixos.org/t/introducing-crane-composable-and-cacheable-builds-with-cargo/17275/4 better support for composing Cargo invocations as completely separate derivations]
|-
|-
| [https://github.com/nix-community/dream2nix/blob/main/docs/src/subsystems/rust.md <code>dream2nix</code>]
| [https://dream2nix.dev/reference/rust-crane <code>dream2nix</code>]
| Codegen
| Codegen
| 1 or 2
| 1 or 2
| cargo (via <code>buildRustPackage</code> or <code>crane</code>)
| cargo (via <code>buildRustPackage</code> or <code>crane</code>)
| Yes
| Yes
| A framework for unifying 2nix converters across languages
| A framework for unifying 2nix converters across languages (Experimental)
|}
|}


Line 278: Line 318:
=== Building with a different Rust version than the one in Nixpkgs ===
=== Building with a different Rust version than the one in Nixpkgs ===


The following uses the [https://github.com/nix-community/fenix fenix] overlay and <code>makeRustPlatform</code> to build a crate with Rust nightly:
The following uses the [https://github.com/nix-community/fenix fenix] overlay and <code>makeRustPlatform</code> to build a crate with Rust Nightly:


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">