Files
rust-overlay/README.md

270 lines
9.4 KiB
Markdown
Raw Normal View History

2021-01-02 02:38:48 +08:00
# rust-overlay
2021-01-02 21:39:57 +08:00
![CI](https://github.com/oxalica/rust-overlay/workflows/CI/badge.svg)
![sync-channels](https://github.com/oxalica/rust-overlay/workflows/sync-channels/badge.svg)
2021-01-02 02:38:48 +08:00
*Pure and reproducible* overlay for binary distributed rust toolchains.
2021-01-02 21:39:57 +08:00
A compatible but better replacement for rust overlay of [mozilla/nixpkgs-mozilla][mozilla].
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
Hashes of toolchain components are pre-fetched (and compressed) in tree (`manifests` directory),
so the evaluation is *pure* and no need to have network (but [nixpkgs-mozilla][mozilla] does).
It also works well with [Nix Flakes](https://nixos.wiki/wiki/Flakes).
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
- The toolchain hashes are auto-updated daily using GitHub Actions.
2021-01-05 03:38:46 -06:00
- Current oldest supported version is stable 1.29.0 and beta/nightly 2018-09-13
(which are randomly chosen).
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
## Use as a classic Nix overlay
2021-01-02 02:38:48 +08:00
You can put the code below into your `~/.config/nixpkgs/overlays.nix`.
```nix
2021-01-02 20:49:12 +08:00
[ (import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz")) ]
```
Then the provided attribute paths are available in nix command.
```bash
2021-04-06 01:38:31 +08:00
$ nix-env -iA rust-bin.stable.latest.default # Do anything you like.
2021-01-02 02:38:48 +08:00
```
2021-01-02 20:49:12 +08:00
Alternatively, you can install it into nix channels.
```bash
2021-01-02 02:38:48 +08:00
$ nix-channel --add https://github.com/oxalica/rust-overlay/archive/master.tar.gz rust-overlay
2021-01-02 20:49:12 +08:00
$ nix-channel --update
2021-01-02 02:38:48 +08:00
```
And then feel free to use it anywhere like
2021-01-02 20:49:12 +08:00
`import <nixpkgs> { overlays = [ (import <rust-overlay>) ]; }` in your nix shell environment.
2021-01-02 02:38:48 +08:00
## Use with Nix Flakes
2021-01-02 20:49:12 +08:00
This repository already has flake support.
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
NOTE: **Only the output `overlay` is stable and preferred to be used in your flake.**
Other outputs like `packages` and `defaultPackage` are for human try and are subject to change.
For a quick play, just use `nix shell` to bring the latest stable rust toolchain into scope.
(All commands below requires preview version of Nix with flake support.)
```shell
$ nix shell github:oxalica/rust-overlay
$ rustc --version
rustc 1.49.0 (e1884a8e3 2020-12-29)
$ cargo --version
cargo 1.49.0 (d00d64df9 2020-12-05)
```
### Example: NixOS Configuration
2021-01-02 20:49:12 +08:00
Here's an example of using it in nixos configuration.
2021-01-02 02:38:48 +08:00
```nix
{
description = "My configuration";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = { nixpkgs, rust-overlay, ... }: {
nixosConfigurations = {
hostname = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix # Your system configuration.
({ pkgs, ... }: {
nixpkgs.overlays = [ rust-overlay.overlay ];
2021-04-06 01:38:31 +08:00
environment.systemPackages = [ pkgs.rust-bin.stable.latest.default ];
2021-01-02 02:38:48 +08:00
})
];
};
};
};
}
```
### Example: Using `devShell` and `nix develop`
Running `nix develop` will create a shell with the default nightly Rust toolchain installed:
```nix
{
description = "A devShell example";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
in
{
devShell = pkgs.mkShell {
buildInputs = [
pkgs.openssl
pkgs.pkgconfig
pkgs.exa
pkgs.fd
pkgs.rust-bin.nightly.latest.default
];
shellHook = ''
alias ls=exa
alias find=fd
'';
};
}
);
}
```
2021-01-02 20:49:12 +08:00
## Attributes provided by the overlay
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
```nix
{
rust-bin = {
# The default dist url for fetching.
# Override it if you want to use a mirror server.
distRoot = "https://static.rust-lang.org/dist";
# Select a toolchain and aggregate components by rustup's `rust-toolchain` file format.
2021-04-06 00:39:42 +08:00
# See: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file
fromRustupToolchain = { channel, components ? [], targets ? [] }: «derivation»;
# Same as `fromRustupToolchain` but read from a `rust-toolchain` file (legacy one-line string or in TOML).
fromRustupToolchainFile = rust-toolchain-file-path: «derivation»;
2021-04-30 04:03:27 +08:00
# Select the latest nightly toolchain which have specific components or profile available.
# This helps nightly users in case of latest nightly may not contains all components they want.
#
# `selectLatestNightlyWith (toolchain: toolchain.default)` selects the latest nightly toolchain
# with all `default` components (rustc, cargo, rustfmt, ...) available.
selectLatestNightlyWith = selector: «derivation»;
# [Experimental]
# Custom toolchain from a specific rustc git revision.
# This does almost the same thing as `rustup-toolchain-install-master`. (https://crates.io/crates/rustup-toolchain-install-master)
# Parameter `components` should be an attrset with component name as key and its SRI hash as value.
fromRustcRev = { pname ? .., rev, components, target ? .. }: «derivation»;
2021-01-02 20:49:12 +08:00
stable = {
# The latest stable toolchain.
latest = {
2021-04-06 00:39:42 +08:00
# Profiles, predefined component sets.
# See: https://rust-lang.github.io/rustup/concepts/profiles.html
minimal = «derivation»; # Only `cargo`, `rustc` and `rust-std`.
default = «derivation»; # The default profile of `rustup`. Good for general use.
complete = «derivation»; # Do not use it. It almost always fails.
# Pre-aggregated package provided by upstream, the most commonly used package in `mozilla-overlay`.
# It consists of an uncertain number of components, usually more than the `default` profile of `rustup`
# but less than `complete` profile.
2021-01-02 20:49:12 +08:00
rust = «derivation»;
2021-04-06 00:39:42 +08:00
2021-01-02 20:49:12 +08:00
# Individial components.
rustc = «derivation»;
cargo = «derivation»;
rust-std = «derivation»;
# ... other components
};
"1.49.0" = { /* toolchain */ };
"1.48.0" = { /* toolchain */ };
# ... other versions.
};
2021-01-02 02:38:48 +08:00
2021-01-05 03:38:46 -06:00
beta = {
# The latest beta toolchain.
latest = { /* toolchain */ };
"2021-01-01" = { /* toolchain */ };
"2020-12-30" = { /* toolchain */ };
# ... other versions.
};
2021-01-02 20:49:12 +08:00
nightly = {
# The latest nightly toolchain.
2021-04-30 04:03:27 +08:00
# It is preferred to use `selectLatestNightlyWith` instead of this since
# nightly toolchain may have components (like `rustfmt` or `rls`) missing,
# making `default` profile unusable.
2021-01-02 20:49:12 +08:00
latest = { /* toolchain */ };
"2020-12-31" = { /* toolchain */ };
"2020-12-30" = { /* toolchain */ };
# ... other versions.
};
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
# ... Some internal attributes omitted.
};
2021-01-02 02:38:48 +08:00
2021-01-02 20:49:12 +08:00
# These are for compatibility with nixpkgs-mozilla and
# provide same toolchains as `rust-bin.*`.
latest.rustChannels = /* ... */;
rustChannelOf = /* ... */;
rustChannelOfTargets = /* ... */;
rustChannels = /* ... */;
2021-01-02 02:38:48 +08:00
}
```
2021-01-02 20:49:12 +08:00
Some examples (assume `nixpkgs` had the overlay applied):
2021-04-06 00:39:42 +08:00
- Latest stable/beta/nightly rust with almost all components (provided the same as `mozilla-overlay`):
2021-01-05 03:38:46 -06:00
`nixpkgs.rust-bin.{stable,beta,nightly}.latest.rust`
2021-04-30 04:09:36 +08:00
- Latest stable/beta/nightly rust with `default` or `minimal` profile (provided the same as default behavior of `rustup install`).
2021-04-06 00:39:42 +08:00
`nixpkgs.rust-bin.{stable,beta,nightly}.latest.{default,minimal}`
2021-05-15 03:45:47 +08:00
Note: Directly using `nightly.latest.*` is not recommended since your build will fail when
some components missing on some days. Use `selectLatestNightlyWith` instead, see example below.
2021-04-06 00:39:42 +08:00
2021-01-05 03:38:46 -06:00
- A specific version of stable rust:
2021-04-06 01:38:31 +08:00
`nixpkgs.rust-bin.stable."1.48.0".default`
2021-01-05 03:38:46 -06:00
- A specific date of beta rust:
2021-04-06 01:38:31 +08:00
`nixpkgs.rust-bin.beta."2021-01-01".default`
2021-01-02 20:49:12 +08:00
- A specific date of nightly rust:
2021-04-06 01:38:31 +08:00
`nixpkgs.rust-bin.nightly."2020-12-31".default`
2021-01-02 20:49:12 +08:00
- Latest stable rust with additional component `rust-src` and extra target
`arm-unknown-linux-gnueabihf`:
```nix
2021-04-06 01:38:31 +08:00
nixpkgs.rust-bin.stable.latest.default.override {
2021-01-02 20:49:12 +08:00
extensions = [ "rust-src" ];
targets = [ "arm-unknown-linux-gnueabihf" ];
}
```
2021-04-06 00:39:42 +08:00
2021-04-30 04:03:27 +08:00
- Select the latest nightly toolchain with default components and `llvm-tools-preview` all available.
It may select toolchain earlier than `rust-bin.nightly.latest` due to lack of components.
```nix
rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
extensions = [ "llvm-tools-preview" ];
})
```
2021-04-06 00:39:42 +08:00
- If you already have a [`rust-toolchain` file for rustup][rust-toolchain],
2021-01-04 01:16:17 +08:00
you can simply use `fromRustupToolchainFile` to get the customized toolchain derivation.
2021-01-02 20:49:12 +08:00
2021-01-04 01:16:17 +08:00
```nix
nixpkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain
```
2021-04-06 00:39:42 +08:00
- *\[Experimental\]*
Toolchain with specific rustc git revision.
This is useful for development of rust components like [MIRI](https://github.com/rust-lang/miri).
Note: the example below may not built since upstream CI periodly removes old artifacts.
```nix
rust-bin.fromRustcRev {
rev = "a2cd91ceb0f156cb442d75e12dc77c3d064cdde4";
components = {
rustc = "sha256-x+OkPVStX00AiC3GupIdGzWluIK1BnI4ZCBbg72+ZuI=";
rust-src = "sha256-13PpzzYtd769Xkb0QzHpNfYCOnLMWFolc9QyYq98z2k=";
};
}
```
2021-04-06 00:39:42 +08:00
2021-02-03 20:49:55 +08:00
- See more examples in directory `examples`.
2021-01-04 01:16:17 +08:00
For more details, see also the source code of `./rust-overlay.nix`.
2021-01-02 20:49:12 +08:00
[mozilla]: https://github.com/mozilla/nixpkgs-mozilla
2021-04-06 00:39:42 +08:00
[rust-toolchain]: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file