diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2020-10-14 03:37:29 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2020-10-14 04:20:23 +0000 |
commit | c0df12de5d938e1c263c992ebd648c4100e8c0a2 (patch) | |
tree | 535a18403402fb116a4db93c3aabf79e2e60774b /doc/languages-frameworks | |
parent | 6866f26c89dc7b2cf2e61aed461da36447ffd4be (diff) |
rust: Add support for managing target JSON in Nix
Diffstat (limited to 'doc/languages-frameworks')
-rw-r--r-- | doc/languages-frameworks/rust.section.md | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 0e1d59e1a952a..0f3cc5d8efbf9 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -63,9 +63,52 @@ The fetcher will verify that the `Cargo.lock` file is in sync with the `src` attribute, and fail the build if not. It will also will compress the vendor directory into a tar.gz archive. -### Building a crate for a different target - -To build your crate with a different cargo `--target` simply specify the `target` attribute: +### Cross compilation + +By default, Rust packages are compiled for the host platform, just like any +other package is. The `--target` passed to rust tools is computed from this. +By default, it takes the `stdenv.hostPlatform.config` and replaces components +where they are known to differ. But there are ways to customize the argument: + + - To choose a different target by name, define + `stdenv.hostPlatform.rustc.arch.config` as that name (a string), and that + name will be used instead. + + For example: + ```nix + import <nixpkgs> { + crossSystem = (import <nixpkgs/lib>).systems.examples.armhf-embedded // { + rustc.arch.config = "thumbv7em-none-eabi"; + }; + } + ``` + will result in: + ```shell + --target thumbv7em-none-eabi + ``` + + - To pass a completely custom target, define + `stdenv.hostPlatform.rustc.arch.config` with its name, and + `stdenv.hostPlatform.rustc.arch.custom` with the value. The value will be + serialized to JSON in a file called + `${stdenv.hostPlatform.rustc.arch.config}.json`, and the path of that file + will be used instead. + + For example: + ```nix + import <nixpkgs> { + crossSystem = (import <nixpkgs/lib>).systems.examples.armhf-embedded // { + rustc.arch.config = "thumb-crazy"; + rustc.arch.custom = { foo = ""; bar = ""; }; + }; + } + will result in: + ```shell + --target /nix/store/asdfasdfsadf-thumb-crazy.json # contains {"foo":"","bar":""} + ``` + +Finally, as an ad-hoc escape hatch, a computed target (string or JSON file +path) can be passed directly to `buildRustPackage`: ```nix pkgs.rustPlatform.buildRustPackage { @@ -74,6 +117,12 @@ pkgs.rustPlatform.buildRustPackage { } ``` +This is useful to avoid rebuilding Rust tools, since they are actually target +agnostic and don't need to be rebuilt. But in the future, we should always +build the Rust tools and standard library crates separately so there is no +reason not to take the `stdenv.hostPlatform.rustc`-modifying approach, and the +ad-hoc escape hatch to `buildRustPackage` can be removed. + ### Running package tests When using `buildRustPackage`, the `checkPhase` is enabled by default and runs |