From a588b1dfbc83c0915f2b4cf097d4892730b03482 Mon Sep 17 00:00:00 2001 From: exfalso <0slemi0@gmail.com> Date: Thu, 24 Oct 2019 11:39:08 +0100 Subject: [PATCH 1/2] build-support/rust: Add target option --- pkgs/build-support/rust/default.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index 27601e481c68..4f7c842ca483 100644 --- a/pkgs/build-support/rust/default.nix +++ b/pkgs/build-support/rust/default.nix @@ -18,6 +18,7 @@ verifyCargoDeps ? false , buildType ? "release" , meta ? {} +, target ? null , cargoVendorDir ? null , ... } @ args: @@ -50,12 +51,13 @@ let rustHostConfig = { x86_64-pc-mingw32 = "x86_64-pc-windows-gnu"; }.${hostConfig} or hostConfig; + rustTarget = if target == null then rustHostConfig else target; ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc"; cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++"; ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; - releaseDir = "target/${rustHostConfig}/${buildType}"; + releaseDir = "target/${rustTarget}/${buildType}"; in stdenv.mkDerivation (args // { @@ -88,7 +90,7 @@ stdenv.mkDerivation (args // { [target."${stdenv.buildPlatform.config}"] "linker" = "${ccForBuild}" ${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) '' - [target."${rustHostConfig}"] + [target."${rustTarget}"] "linker" = "${ccForHost}" ${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633 stdenv.lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) '' @@ -133,7 +135,7 @@ stdenv.mkDerivation (args // { "CXX_${stdenv.hostPlatform.config}"="${cxxForHost}" \ cargo build \ ${stdenv.lib.optionalString (buildType == "release") "--release"} \ - --target ${rustHostConfig} \ + --target ${rustTarget} \ --frozen ${concatStringsSep " " cargoBuildFlags} ) From bb7184d6a62f3eefdadb069091f8985871279b15 Mon Sep 17 00:00:00 2001 From: exfalso <0slemi0@gmail.com> Date: Fri, 1 Nov 2019 14:19:39 +0000 Subject: [PATCH 2/2] buildRustPackage: Add readme comment on target option --- doc/languages-frameworks/rust.section.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 83b7b159bd63..5fb5d380d8b2 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -68,6 +68,17 @@ build-time. When `verifyCargoDeps` is set to `true`, the build will also verify that the `cargoSha256` is not out of date by comparing the `Cargo.lock` file in both the `cargoDeps` and `src`. Note that this option changes the value of `cargoSha256` since it also copies the `Cargo.lock` in it. To avoid breaking backward-compatibility this option is not enabled by default but hopefully will be in the future. +### Building a crate for a different target + +To build your crate with a different cargo `--target` simply specify the `target` attribute: + +```nix +pkgs.rustPlatform.buildRustPackage { + (...) + target = "x86_64-fortanix-unknown-sgx"; +} +``` + ## Compiling Rust crates using Nix instead of Cargo ### Simple operation