diff options
Diffstat (limited to 'doc/languages-frameworks/rust.section.md')
-rw-r--r-- | doc/languages-frameworks/rust.section.md | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 7789ef857ee..882eb9c4afa 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -2,13 +2,14 @@ To install the rust compiler and cargo put -``` -rustc -cargo +```nix +environment.systemPackages = [ + rustc + cargo +]; ``` -into the `environment.systemPackages` or bring them into -scope with `nix-shell -p rustc cargo`. +into your `configuration.nix` or bring them into scope with `nix-shell -p rustc cargo`. 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), @@ -18,7 +19,7 @@ or use Mozilla's [Rust nightlies overlay](#using-the-rust-nightlies-overlay). Rust applications are packaged by using the `buildRustPackage` helper from `rustPlatform`: -``` +```nix { lib, rustPlatform }: rustPlatform.buildRustPackage rec { @@ -49,7 +50,7 @@ package. `cargoHash256` is used for traditional Nix SHA-256 hashes, such as the one in the example above. `cargoHash` should instead be used for [SRI](https://www.w3.org/TR/SRI/) hashes. For example: -``` +```nix cargoHash = "sha256-l1vL2ZdtDRxSGvP0X/l3nMw8+6WF67KPutJEzUROjg8="; ``` @@ -59,13 +60,13 @@ expression and building the package once. The correct checksum can then be taken from the failed build. A fake hash can be used for `cargoSha256` as follows: -``` +```nix cargoSha256 = lib.fakeSha256; ``` For `cargoHash` you can use: -``` +```nix cargoHash = lib.fakeHash; ``` @@ -255,7 +256,7 @@ Otherwise, some steps may fail because of the modified directory structure of `t source code in a reproducible way. If it is missing or out-of-date one can use the `cargoPatches` attribute to update or add it. -``` +```nix rustPlatform.buildRustPackage rec { (...) cargoPatches = [ @@ -481,7 +482,7 @@ an example for a minimal `hello` crate: Now, the file produced by the call to `carnix`, called `hello.nix`, looks like: -``` +```nix # Generated by carnix 0.6.5: carnix -o hello.nix --src ./. Cargo.lock --standalone { stdenv, buildRustCrate, fetchgit }: let kernel = stdenv.buildPlatform.parsed.kernel.name; @@ -510,7 +511,7 @@ dependencies, for instance by adding a single line `libc="*"` to our `Cargo.lock`. Then, `carnix` needs to be run again, and produces the following nix file: -``` +```nix # Generated by carnix 0.6.5: carnix -o hello.nix --src ./. Cargo.lock --standalone { stdenv, buildRustCrate, fetchgit }: let kernel = stdenv.buildPlatform.parsed.kernel.name; @@ -565,7 +566,7 @@ Some crates require external libraries. For crates from Starting from that file, one can add more overrides, to add features or build inputs by overriding the hello crate in a seperate file. -``` +```nix with import <nixpkgs> {}; ((import ./hello.nix).hello {}).override { crateOverrides = defaultCrateOverrides // { @@ -585,7 +586,7 @@ derivation depend on the crate's version, the `attrs` argument of the override above can be read, as in the following example, which patches the derivation: -``` +```nix with import <nixpkgs> {}; ((import ./hello.nix).hello {}).override { crateOverrides = defaultCrateOverrides // { @@ -606,7 +607,7 @@ dependencies. For instance, to override the build inputs for crate `libc` in the example above, where `libc` is a dependency of the main crate, we could do: -``` +```nix with import <nixpkgs> {}; ((import hello.nix).hello {}).override { crateOverrides = defaultCrateOverrides // { @@ -622,27 +623,27 @@ general. A number of other parameters can be overridden: - The version of rustc used to compile the crate: - ``` + ```nix (hello {}).override { rust = pkgs.rust; }; ``` - Whether to build in release mode or debug mode (release mode by default): - ``` + ```nix (hello {}).override { release = false; }; ``` - Whether to print the commands sent to rustc when building (equivalent to `--verbose` in cargo: - ``` + ```nix (hello {}).override { verbose = false; }; ``` - Extra arguments to be passed to `rustc`: - ``` + ```nix (hello {}).override { extraRustcOpts = "-Z debuginfo=2"; }; ``` @@ -654,7 +655,7 @@ general. A number of other parameters can be overridden: `postInstall`. As an example, here is how to create a new module before running the build script: - ``` + ```nix (hello {}).override { preConfigure = '' echo "pub const PATH=\"${hi.out}\";" >> src/path.rs" @@ -668,7 +669,7 @@ One can also supply features switches. For example, if we want to compile `diesel_cli` only with the `postgres` feature, and no default features, we would write: -``` +```nix (callPackage ./diesel.nix {}).diesel { default = false; postgres = true; @@ -691,7 +692,7 @@ Using the example `hello` project above, we want to do the following: A typical `shell.nix` might look like: -``` +```nix with import <nixpkgs> {}; stdenv.mkDerivation { @@ -713,7 +714,7 @@ stdenv.mkDerivation { ``` You should now be able to run the following: -``` +```ShellSesssion $ nix-shell --pure $ cargo build $ cargo test @@ -723,7 +724,7 @@ $ cargo test To control your rust version (i.e. use nightly) from within `shell.nix` (or other nix expressions) you can use the following `shell.nix` -``` +```nix # Latest Nightly with import <nixpkgs> {}; let src = fetchFromGitHub { @@ -751,7 +752,7 @@ stdenv.mkDerivation { ``` Now run: -``` +```ShellSession $ rustc --version rustc 1.26.0-nightly (188e693b3 2018-03-26) ``` @@ -786,7 +787,7 @@ in the `~/.config/nixpkgs/overlays` directory. 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/mozilla/nixpkgs-mozilla/archive/master.tar.gz)) |