Skip to content

Commit e19ca9c

Browse files
committed
Reorganize and clean up examples
Fix #43
1 parent e650abf commit e19ca9c

File tree

2 files changed

+93
-70
lines changed

2 files changed

+93
-70
lines changed

Diff for: README.md

+93-69
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $ cargo --version
5050
cargo 1.49.0 (d00d64df9 2020-12-05)
5151
```
5252

53-
### Example: NixOS Configuration
53+
### Flake example: NixOS Configuration
5454

5555
Here's an example of using it in nixos configuration.
5656
```nix
@@ -79,7 +79,7 @@ Here's an example of using it in nixos configuration.
7979
}
8080
```
8181

82-
### Example: Using `devShell` and `nix develop`
82+
### Flake example: Using `devShell` and `nix develop`
8383

8484
Running `nix develop` will create a shell with the default nightly Rust toolchain installed:
8585

@@ -95,20 +95,21 @@ Running `nix develop` will create a shell with the default nightly Rust toolchai
9595
9696
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
9797
flake-utils.lib.eachDefaultSystem (system:
98-
let
98+
let
9999
overlays = [ (import rust-overlay) ];
100100
pkgs = import nixpkgs {
101101
inherit system overlays;
102102
};
103103
in
104+
with pkgs;
104105
{
105-
devShell = pkgs.mkShell {
106+
devShell = mkShell {
106107
buildInputs = [
107-
pkgs.openssl
108-
pkgs.pkgconfig
109-
pkgs.exa
110-
pkgs.fd
111-
pkgs.rust-bin.nightly.latest.default
108+
openssl
109+
pkgconfig
110+
exa
111+
fd
112+
rust-bin.nightly.latest.default
112113
];
113114
114115
shellHook = ''
@@ -122,7 +123,85 @@ Running `nix develop` will create a shell with the default nightly Rust toolchai
122123
123124
```
124125

125-
## Attributes provided by the overlay
126+
## Usage Examples
127+
128+
- Latest stable or beta rust profile.
129+
130+
```nix
131+
rust-bin.stable.latest.default # Stable rust, default profile. If not sure, always choose this.
132+
rust-bin.beta.latest.default # Wanna test beta compiler.
133+
rust-bin.stable.latest.minimal # I don't need anything other than rustc, cargo, rust-std. Bye rustfmt, clippy, etc.
134+
rust-bin.beta.latest.minimal
135+
```
136+
137+
It provices the same components as which installed by `rustup install`'s `default` or `minimal` profiles.
138+
139+
Almost always, `default` is what you want for development.
140+
141+
*Note: For difference between `default` and `minimal` profiles, see
142+
[rustup - Profiles][rust-profiles]*
143+
144+
- Latest stable or beta rust profile, **with extra components or target support**.
145+
146+
```nix
147+
rust-bin.stable.latest.default.override {
148+
extensions = [ "rust-src" ];
149+
targets = [ "arm-unknown-linux-gnueabihf" ];
150+
}
151+
```
152+
153+
- Latest **nightly** rust profile.
154+
155+
```nix
156+
rust-bin.selectLatestNightlyWith (toolchain: toolchain.default) # or `toolchain.minimal`
157+
```
158+
159+
*Note: Don't use `rust-bin.nightly.latest`. Your build would fail when some components missing on some days.
160+
Always use `selectLatestNightlyWith` instead.*
161+
162+
- Latest **nightly** rust profile, **with extra components or target support**.
163+
164+
```nix
165+
rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
166+
extensions = [ "rust-src" ];
167+
targets = [ "arm-unknown-linux-gnueabihf" ];
168+
})
169+
```
170+
171+
- A specific version of rust:
172+
```nix
173+
rust-bin.stable."1.48.0".default
174+
rust-bin.beta."2021-01-01".default
175+
rust-bin.nightly."2020-12-31".default
176+
```
177+
178+
*Note: All of them are `override`-able like examples above.*
179+
180+
- If you already have a [`rust-toolchain` file for rustup][rust-toolchain],
181+
you can simply use `fromRustupToolchainFile` to get the customized toolchain derivation.
182+
183+
```nix
184+
rust-bin.fromRustupToolchainFile ./rust-toolchain
185+
```
186+
187+
- Toolchain with specific rustc git revision.
188+
189+
**Warning: This may not always work (including the example below) since upstream CI periodly purges old artifacts.**
190+
191+
This is useful for development of rust components like [MIRI][miri], which requires a specific revision of rust.
192+
```nix
193+
rust-bin.fromRustcRev {
194+
rev = "a2cd91ceb0f156cb442d75e12dc77c3d064cdde4";
195+
components = {
196+
rustc = "sha256-x+OkPVStX00AiC3GupIdGzWluIK1BnI4ZCBbg72+ZuI=";
197+
rust-src = "sha256-13PpzzYtd769Xkb0QzHpNfYCOnLMWFolc9QyYq98z2k=";
198+
};
199+
}
200+
```
201+
202+
- There also an cross-compilation example in [`examples/cross-aarch64`].
203+
204+
## Reference: All attributes provided by the overlay
126205

127206
```nix
128207
{
@@ -144,11 +223,10 @@ Running `nix develop` will create a shell with the default nightly Rust toolchai
144223
# with all `default` components (rustc, cargo, rustfmt, ...) available.
145224
selectLatestNightlyWith = selector: «derivation»;
146225
147-
# [Experimental]
148226
# Custom toolchain from a specific rustc git revision.
149227
# This does almost the same thing as `rustup-toolchain-install-master`. (https://crates.io/crates/rustup-toolchain-install-master)
150228
# Parameter `components` should be an attrset with component name as key and its SRI hash as value.
151-
fromRustcRev = { pname ? .., rev, components, target ? .. }: «derivation»;
229+
fromRustcRev = { pname ? , rev, components, target ? }: «derivation»;
152230
153231
stable = {
154232
# The latest stable toolchain.
@@ -206,64 +284,10 @@ Running `nix develop` will create a shell with the default nightly Rust toolchai
206284
}
207285
```
208286

209-
Some examples (assume `nixpkgs` had the overlay applied):
210-
211-
- Latest stable/beta/nightly rust with almost all components (provided the same as `mozilla-overlay`):
212-
`nixpkgs.rust-bin.{stable,beta,nightly}.latest.rust`
213-
- Latest stable/beta/nightly rust with `default` or `minimal` profile (provided the same as default behavior of `rustup install`).
214-
`nixpkgs.rust-bin.{stable,beta,nightly}.latest.{default,minimal}`
215-
216-
Note: Directly using `nightly.latest.*` is not recommended since your build will fail when
217-
some components missing on some days. Use `selectLatestNightlyWith` instead, see example below.
218-
219-
- A specific version of stable rust:
220-
`nixpkgs.rust-bin.stable."1.48.0".default`
221-
- A specific date of beta rust:
222-
`nixpkgs.rust-bin.beta."2021-01-01".default`
223-
- A specific date of nightly rust:
224-
`nixpkgs.rust-bin.nightly."2020-12-31".default`
225-
- Latest stable rust with additional component `rust-src` and extra target
226-
`arm-unknown-linux-gnueabihf`:
227-
228-
```nix
229-
nixpkgs.rust-bin.stable.latest.default.override {
230-
extensions = [ "rust-src" ];
231-
targets = [ "arm-unknown-linux-gnueabihf" ];
232-
}
233-
```
234-
235-
- Select the latest nightly toolchain with default components and `llvm-tools-preview` all available.
236-
It may select toolchain earlier than `rust-bin.nightly.latest` due to lack of components.
237-
```nix
238-
rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
239-
extensions = [ "llvm-tools-preview" ];
240-
})
241-
```
242-
243-
- If you already have a [`rust-toolchain` file for rustup][rust-toolchain],
244-
you can simply use `fromRustupToolchainFile` to get the customized toolchain derivation.
245-
246-
```nix
247-
nixpkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain
248-
```
249-
250-
- *\[Experimental\]*
251-
Toolchain with specific rustc git revision.
252-
This is useful for development of rust components like [MIRI](https://github.com/rust-lang/miri).
253-
Note: the example below may not built since upstream CI periodly removes old artifacts.
254-
```nix
255-
rust-bin.fromRustcRev {
256-
rev = "a2cd91ceb0f156cb442d75e12dc77c3d064cdde4";
257-
components = {
258-
rustc = "sha256-x+OkPVStX00AiC3GupIdGzWluIK1BnI4ZCBbg72+ZuI=";
259-
rust-src = "sha256-13PpzzYtd769Xkb0QzHpNfYCOnLMWFolc9QyYq98z2k=";
260-
};
261-
}
262-
```
263-
264-
- See more examples in directory `examples`.
265-
266287
For more details, see also the source code of `./rust-overlay.nix`.
267288

268289
[mozilla]: https://github.com/mozilla/nixpkgs-mozilla
269290
[rust-toolchain]: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file
291+
[rust-profiles]: https://rust-lang.github.io/rustup/concepts/profiles.html
292+
[miri]: https://github.com/rust-lang/miri
293+
[`examples/cross-aarch64`]: https://github.com/oxalica/rust-overlay/tree/master/examples/cross-aarch64

Diff for: rust-overlay.nix

-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ in {
587587
{
588588
inherit fromRustupToolchain fromRustupToolchainFile;
589589
inherit selectLatestNightlyWith;
590-
# Experimental feature.
591590
inherit fromRustcRev;
592591
};
593592

0 commit comments

Comments
 (0)