mirror of
https://github.com/ilyakooo0/nixpkgs.git
synced 2025-01-02 17:09:09 +03:00
docs/rust: improve docs for nightly usage
This commit is contained in:
parent
3b455ed737
commit
b373d299ba
@ -13,7 +13,7 @@ into your `configuration.nix` or bring them into scope with `nix-shell -p rustc
|
|||||||
|
|
||||||
For other versions such as daily builds (beta and nightly),
|
For other versions such as daily builds (beta and nightly),
|
||||||
use either `rustup` from nixpkgs (which will manage the rust installation in your home directory),
|
use either `rustup` from nixpkgs (which will manage the rust installation in your home directory),
|
||||||
or use a community maintained [Rust overlay](#using-community-rust-overlays).
|
or use [community maintained Rust toolchains](#using-community-maintained-rust-toolchains).
|
||||||
|
|
||||||
## `buildRustPackage`: Compiling Rust applications with Cargo {#compiling-rust-applications-with-cargo}
|
## `buildRustPackage`: Compiling Rust applications with Cargo {#compiling-rust-applications-with-cargo}
|
||||||
|
|
||||||
@ -686,31 +686,61 @@ $ cargo build
|
|||||||
$ cargo test
|
$ cargo test
|
||||||
```
|
```
|
||||||
|
|
||||||
### Controlling Rust Version Inside `nix-shell` {#controlling-rust-version-inside-nix-shell}
|
## Using community maintained Rust toolchains {#using-community-maintained-rust-toolchains}
|
||||||
|
|
||||||
To control your rust version (i.e. use nightly) from within `shell.nix` (or
|
::: {.note}
|
||||||
other nix expressions) you can use the following `shell.nix`
|
Note: The following projects cannot be used within nixpkgs since [IFD](#ssec-import-from-derivation) is disallowed.
|
||||||
|
To package things that require Rust nightly, `RUSTC_BOOTSTRAP = true;` can sometimes be used as a hack.
|
||||||
|
:::
|
||||||
|
|
||||||
|
There are two community maintained approaches to Rust toolchain management:
|
||||||
|
- [oxalica's Rust overlay](https://github.com/oxalica/rust-overlay)
|
||||||
|
- [fenix](https://github.com/nix-community/fenix)
|
||||||
|
|
||||||
|
Despite their names, both projects provides a similar set of packages and overlays under different APIs.
|
||||||
|
|
||||||
|
Oxalica's overlay allows you to select a particular Rust version without you providing a hash or a flake input,
|
||||||
|
but comes with a larger git repository than fenix.
|
||||||
|
|
||||||
|
Fenix also provides rust-analyzer nightly in addition to the Rust toolchains.
|
||||||
|
|
||||||
|
Both oxalica's overlay and fenix better integrate with nix and cache optimizations.
|
||||||
|
Because of this and ergonomics, either of those community projects
|
||||||
|
should be preferred to the Mozilla's Rust overlay ([nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla)).
|
||||||
|
|
||||||
|
The following documentation demonstrates examples using fenix and oxalica's Rust overlay
|
||||||
|
with `nix-shell` and building derivations. More advanced usages like flake usage
|
||||||
|
are documented in their own repositories.
|
||||||
|
|
||||||
|
### Using Rust nightly with `nix-shell` {#using-rust-nightly-with-nix-shell}
|
||||||
|
|
||||||
|
Here is a simple `shell.nix` that provides Rust nightly (default profile) using fenix:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
# Latest Nightly
|
with import <nixpkgs> { };
|
||||||
with import <nixpkgs> {};
|
let
|
||||||
let src = fetchFromGitHub {
|
fenix = callPackage
|
||||||
owner = "mozilla";
|
(fetchFromGitHub {
|
||||||
repo = "nixpkgs-mozilla";
|
owner = "nix-community";
|
||||||
# commit from: 2019-05-15
|
repo = "fenix";
|
||||||
rev = "9f35c4b09fd44a77227e79ff0c1b4b6a69dff533";
|
# commit from: 2023-03-03
|
||||||
hash = "sha256-18h0nvh55b5an4gmlgfbvwbyqj91bklf1zymis6lbdh75571qaz0=";
|
rev = "e2ea04982b892263c4d939f1cc3bf60a9c4deaa1";
|
||||||
};
|
hash = "sha256-AsOim1A8KKtMWIxG+lXh5Q4P2bhOZjoUhFWJ1EuZNNk=";
|
||||||
|
})
|
||||||
|
{ };
|
||||||
in
|
in
|
||||||
with import "${src.out}/rust-overlay.nix" pkgs pkgs;
|
mkShell {
|
||||||
stdenv.mkDerivation {
|
|
||||||
name = "rust-env";
|
name = "rust-env";
|
||||||
buildInputs = [
|
nativeBuildInputs = [
|
||||||
# Note: to use stable, just replace `nightly` with `stable`
|
# Note: to use stable, just replace `default` with `stable`
|
||||||
latest.rustChannels.nightly.rust
|
fenix.default.toolchain
|
||||||
|
|
||||||
# Add some extra dependencies from `pkgs`
|
# Example Build-time Additional Dependencies
|
||||||
pkg-config openssl
|
pkg-config
|
||||||
|
];
|
||||||
|
buildInputs = [
|
||||||
|
# Example Run-time Additional Dependencies
|
||||||
|
openssl
|
||||||
];
|
];
|
||||||
|
|
||||||
# Set Environment Variables
|
# Set Environment Variables
|
||||||
@ -718,116 +748,66 @@ stdenv.mkDerivation {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Now run:
|
Save this to `shell.nix`, then run:
|
||||||
|
|
||||||
```ShellSession
|
```ShellSession
|
||||||
$ rustc --version
|
$ rustc --version
|
||||||
rustc 1.26.0-nightly (188e693b3 2018-03-26)
|
rustc 1.69.0-nightly (13471d3b2 2023-03-02)
|
||||||
```
|
```
|
||||||
|
|
||||||
To see that you are using nightly.
|
To see that you are using nightly.
|
||||||
|
|
||||||
## Using community Rust overlays {#using-community-rust-overlays}
|
Oxalica's Rust overlay has more complete examples of `shell.nix` (and cross compilation) under its
|
||||||
|
[`examples` directory](https://github.com/oxalica/rust-overlay/tree/e53e8853aa7b0688bc270e9e6a681d22e01cf299/examples).
|
||||||
|
|
||||||
There are two community maintained approaches to Rust toolchain management:
|
### Using Rust nightly in a derivation with `buildRustPackage` {#using-rust-nightly-in-a-derivation-with-buildrustpackage}
|
||||||
- [oxalica's Rust overlay](https://github.com/oxalica/rust-overlay)
|
|
||||||
- [fenix](https://github.com/nix-community/fenix)
|
|
||||||
|
|
||||||
Oxalica's overlay allows you to select a particular Rust version and components.
|
You can also use Rust nightly to build rust packages using `makeRustPlatform`.
|
||||||
See [their documentation](https://github.com/oxalica/rust-overlay#rust-overlay) for more
|
The below snippet demonstrates invoking `buildRustPackage` with a Rust toolchain from oxalica's overlay:
|
||||||
detailed usage.
|
|
||||||
|
|
||||||
Fenix is an alternative to `rustup` and can also be used as an overlay.
|
|
||||||
|
|
||||||
Both oxalica's overlay and fenix better integrate with nix and cache optimizations.
|
|
||||||
Because of this and ergonomics, either of those community projects
|
|
||||||
should be preferred to the Mozilla's Rust overlay (`nixpkgs-mozilla`).
|
|
||||||
|
|
||||||
### How to select a specific `rustc` and toolchain version {#how-to-select-a-specific-rustc-and-toolchain-version}
|
|
||||||
|
|
||||||
You can consume the oxalica overlay and use it to grab a specific Rust toolchain version.
|
|
||||||
Here is an example `shell.nix` showing how to grab the current stable toolchain:
|
|
||||||
```nix
|
```nix
|
||||||
{ pkgs ? import <nixpkgs> {
|
with import <nixpkgs>
|
||||||
overlays = [
|
{
|
||||||
(import (fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}:
|
|
||||||
pkgs.mkShell {
|
|
||||||
nativeBuildInputs = with pkgs; [
|
|
||||||
pkg-config
|
|
||||||
rust-bin.stable.latest.minimal
|
|
||||||
];
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You can try this out by:
|
|
||||||
1. Saving that to `shell.nix`
|
|
||||||
2. Executing `nix-shell --pure --command 'rustc --version'`
|
|
||||||
|
|
||||||
As of writing, this prints out `rustc 1.56.0 (09c42c458 2021-10-18)`.
|
|
||||||
|
|
||||||
### How to use an overlay toolchain in a derivation {#how-to-use-an-overlay-toolchain-in-a-derivation}
|
|
||||||
|
|
||||||
You can also use an overlay's Rust toolchain with `buildRustPackage`.
|
|
||||||
The below snippet demonstrates invoking `buildRustPackage` with an oxalica overlay selected Rust toolchain:
|
|
||||||
```nix
|
|
||||||
with import <nixpkgs> {
|
|
||||||
overlays = [
|
overlays = [
|
||||||
(import (fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
|
(import (fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
let
|
||||||
|
rustPlatform = makeRustPlatform {
|
||||||
|
cargo = rust-bin.stable.latest.minimal;
|
||||||
|
rustc = rust-bin.stable.latest.minimal;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
|
||||||
rustPlatform.buildRustPackage rec {
|
rustPlatform.buildRustPackage rec {
|
||||||
pname = "ripgrep";
|
pname = "ripgrep";
|
||||||
version = "12.1.1";
|
version = "12.1.1";
|
||||||
nativeBuildInputs = [
|
|
||||||
rust-bin.stable.latest.minimal
|
|
||||||
];
|
|
||||||
|
|
||||||
src = fetchFromGitHub {
|
src = fetchFromGitHub {
|
||||||
owner = "BurntSushi";
|
owner = "BurntSushi";
|
||||||
repo = "ripgrep";
|
repo = "ripgrep";
|
||||||
rev = version;
|
rev = version;
|
||||||
hash = "sha256-1hqps7l5qrjh9f914r5i6kmcz6f1yb951nv4lby0cjnp5l253kps=";
|
hash = "sha256-+s5RBC3XSgb8omTbUNLywZnP6jSxZBKSS1BmXOjRF8M=";
|
||||||
};
|
};
|
||||||
|
|
||||||
cargoSha256 = "03wf9r2csi6jpa7v5sw5lpxkrk4wfzwmzx7k3991q3bdjzcwnnwp";
|
cargoHash = "sha256-l1vL2ZdtDRxSGvP0X/l3nMw8+6WF67KPutJEzUROjg8=";
|
||||||
|
|
||||||
|
doCheck = false;
|
||||||
|
|
||||||
meta = with lib; {
|
meta = with lib; {
|
||||||
description = "A fast line-oriented regex search tool, similar to ag and ack";
|
description = "A fast line-oriented regex search tool, similar to ag and ack";
|
||||||
homepage = "https://github.com/BurntSushi/ripgrep";
|
homepage = "https://github.com/BurntSushi/ripgrep";
|
||||||
license = licenses.unlicense;
|
license = with licenses; [ mit unlicense ];
|
||||||
maintainers = [ maintainers.tailhook ];
|
maintainers = with maintainers; [ tailhook ];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Follow the below steps to try that snippet.
|
Follow the below steps to try that snippet.
|
||||||
1. create a new directory
|
|
||||||
1. save the above snippet as `default.nix` in that directory
|
1. save the above snippet as `default.nix` in that directory
|
||||||
1. cd into that directory and run `nix-build`
|
2. cd into that directory and run `nix-build`
|
||||||
|
|
||||||
### Rust overlay installation {#rust-overlay-installation}
|
Fenix also has examples with `buildRustPackage`,
|
||||||
|
[crane](https://github.com/ipetkov/crane),
|
||||||
You can use this overlay by either changing your local nixpkgs configuration,
|
[naersk](https://github.com/nix-community/naersk),
|
||||||
or by adding the overlay declaratively in a nix expression, e.g. in `configuration.nix`.
|
and cross compilation in its [Examples](https://github.com/nix-community/fenix#examples) section.
|
||||||
For more information see [the manual on installing overlays](#sec-overlays-install).
|
|
||||||
|
|
||||||
### Declarative Rust overlay installation {#declarative-rust-overlay-installation}
|
|
||||||
|
|
||||||
This snippet shows how to use oxalica's Rust overlay.
|
|
||||||
Add the following to your `configuration.nix`, `home-configuration.nix`, `shell.nix`, or similar:
|
|
||||||
|
|
||||||
```nix
|
|
||||||
{ pkgs ? import <nixpkgs> {
|
|
||||||
overlays = [
|
|
||||||
(import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
|
|
||||||
# Further overlays go here
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that this will fetch the latest overlay version when rebuilding your system.
|
|
||||||
|
Loading…
Reference in New Issue
Block a user