1---
2title: Rust
3author: Matthias Beyer
4date: 2017-03-05
5---
6
7# User's Guide to the Rust Infrastructure
8
9To install the rust compiler and cargo put
10
11```
12rustStable.rustc
13rustStable.cargo
14```
15
16into the `environment.systemPackages` or bring them into scope with
17`nix-shell -p rustStable.rustc -p rustStable.cargo`.
18
19There are also `rustBeta` and `rustNightly` package sets available.
20These are not updated very regulary. For daily builds use either rustup from
21nixpkgs or use the [Rust nightlies overlay](#using-the-rust-nightlies-overlay).
22
23## Packaging Rust applications
24
25Rust applications are packaged by using the `buildRustPackage` helper from `rustPlatform`:
26
27```
28with rustPlatform;
29
30buildRustPackage rec {
31 name = "ripgrep-${version}";
32 version = "0.4.0";
33
34 src = fetchFromGitHub {
35 owner = "BurntSushi";
36 repo = "ripgrep";
37 rev = "${version}";
38 sha256 = "0y5d1n6hkw85jb3rblcxqas2fp82h3nghssa4xqrhqnz25l799pj";
39 };
40
41 depsSha256 = "0q68qyl2h6i0qsz82z840myxlnjay8p1w5z7hfyr8fqp7wgwa9cx";
42
43 meta = with stdenv.lib; {
44 description = "A utility that combines the usability of The Silver Searcher with the raw speed of grep";
45 homepage = https://github.com/BurntSushi/ripgrep;
46 license = with licenses; [ unlicense ];
47 maintainers = [ maintainers.tailhook ];
48 platforms = platforms.all;
49 };
50}
51```
52
53`buildRustPackage` requires a `depsSha256` attribute which is computed over
54all crate sources of this package. Currently it is obtained by inserting a
55fake checksum into the expression and building the package once. The correct
56checksum can be then take from the failed build.
57
58To install crates with nix there is also an experimental project called
59[nixcrates](https://github.com/fractalide/nixcrates).
60
61## Using the Rust nightlies overlay
62
63Mozilla provides an overlay for nixpkgs to bring a nightly version of Rust into scope.
64This overlay can _also_ be used to install recent unstable or stable versions
65of Rust, if desired.
66
67To use this overlay, clone
68[nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla),
69and create a symbolic link to the file
70[rust-overlay.nix](https://github.com/mozilla/nixpkgs-mozilla/blob/master/rust-overlay.nix)
71in the `~/.config/nixpkgs/overlays` directory.
72
73 $ git clone https://github.com/mozilla/nixpkgs-mozilla.git
74 $ mkdir -p ~/.config/nixpkgs/overlays
75 $ ln -s $(pwd)/nixpkgs-mozilla/rust-overlay.nix ~/.config/nixpkgs/overlays/rust-overlay.nix
76
77The latest version can be installed with the following command:
78
79 $ nix-env -Ai nixos.rustChannels.stable.rust
80
81Or using the attribute with nix-shell:
82
83 $ nix-shell -p nixos.rustChannels.stable.rust
84
85To install the beta or nightly channel, "stable" should be substituted by
86"nightly" or "beta", or
87use the function provided by this overlay to pull a version based on a
88build date.
89
90The overlay automatically updates itself as it uses the same source as
91[rustup](https://www.rustup.rs/).