systems: set rust.platform.env as rustc does

rustc allows code to perform compile-time tests against target_env [0], 
which is a function of the target triple. There isn't provision in 
Nixpkgs for learning target_env, however. That doesn't affect rustc's 
evaluation of cfg guards in Rust code - rustc knows perfectly well what 
target_env is - but it _does_ affect the env vars passed to a build 
script set by buildRustCrate, which is presently hard-coded to gnu, and 
also affects any Nix code looking to branch on target_env.

Being able to access target_env is very relevant to, for example, WASI 
as (using Rust terminology) wasm32-wasip1 and wasm32-wasip2 differ only 
by target_env, with identical target_arch and target_os values. Properly 
reflecting target_env may also fix some random musl issues if you're a 
bit lucky.

This commit adds a new attr, rust.platform.env, which reflects the 
target_env that rustc will set for the target, and wires it up in 
buildRustCrate. In isolation, this change mostly only affects build 
scripts checking target_env, but crate2nix will greatly benefit from 
being able to accurately resolve the dep graph (which can also vary 
depending on target_env).

The target triple -> target_env function resists Kolmogorov compression: 
it's irregular and, though there are some patterns, there are lots of 
special cases. So, I have done the stupidest possible thing and scraped 
out all of the targets with non-empty target_env values and dumped that 
into an attrset. This attrset will progressively get out of date as 
rustc adds new platforms, but updating it should be simple enough - I've 
included the generation script as a comment.

There are some other configuration options not being reflected in Nix. I 
have left those alone, but, in the future, maybe this can be extended to 
just dumping all of them into an attrset and then reflecting them in 
rust.platform. It might even make sense to convert the existing code 
producing rust.platform to just looking up from an attrset and to 'do 
what rustc does'.

I would have liked to have added a test targetting a platform with a 
non-empty non-GNU target_env, but all of the yaks were quite hairy.

Fixes https://github.com/NixOS/nixpkgs/issues/436832

[0]: https://doc.rust-lang.org/reference/conditional-compilation.html#r-cfg.target_env
This commit is contained in:
Sam Pointon
2026-06-15 16:21:13 +01:00
parent 1833189acf
commit 4a3079eda3
4 changed files with 232 additions and 1 deletions
+11
View File
@@ -23,6 +23,7 @@ let
platforms = import ./platforms.nix { inherit lib; };
examples = import ./examples.nix { inherit lib; };
architectures = import ./architectures.nix { inherit lib; };
rustc-target-env = import ./rustc-target-env.nix;
/**
Elaborated systems contain functions, which means that they don't satisfy
@@ -449,6 +450,16 @@ let
else
final.parsed.cpu.name;
# https://doc.rust-lang.org/reference/conditional-compilation.html#target_env
# Accomodate system definitions written before Nixpkgs learned about target_env.
env =
if rust ? platform.env then
rust.platform.env
else if rustc-target-env ? ${final.rust.rustcTargetSpec} then
rustc-target-env.${final.rust.rustcTargetSpec}
else
"";
# https://doc.rust-lang.org/reference/conditional-compilation.html#target_os
os =
if rust ? platform then
+160
View File
@@ -0,0 +1,160 @@
# As of rustc 1.96.0. Empty `target_env` values are omitted.
#
# Generation script:
# #!/bin/bash
# rustc --print target-list | while read -r target ; do
# env=$(rustc --print cfg --target "$target" | grep '^target_env=' | sed 's/# ^target_env="//;s/"$//')
# [[ -z "$env" ]] && continue
# printf ' %s = "%s";\n' "$target" "$env"
# done
{
aarch64-apple-ios-macabi = "macabi";
aarch64-apple-ios-sim = "sim";
aarch64-apple-tvos-sim = "sim";
aarch64-apple-visionos-sim = "sim";
aarch64-apple-watchos-sim = "sim";
aarch64-pc-windows-gnullvm = "gnu";
aarch64-pc-windows-msvc = "msvc";
aarch64-unknown-linux-gnu = "gnu";
aarch64-unknown-linux-gnu_ilp32 = "gnu";
aarch64-unknown-linux-musl = "musl";
aarch64-unknown-linux-ohos = "ohos";
aarch64-unknown-managarm-mlibc = "mlibc";
aarch64-unknown-nto-qnx700 = "nto70";
aarch64-unknown-nto-qnx710 = "nto71";
aarch64-unknown-nto-qnx710_iosock = "nto71_iosock";
aarch64-unknown-nto-qnx800 = "nto80";
aarch64-unknown-redox = "relibc";
aarch64-uwp-windows-msvc = "msvc";
aarch64-wrs-vxworks = "gnu";
aarch64_be-unknown-linux-gnu = "gnu";
aarch64_be-unknown-linux-gnu_ilp32 = "gnu";
aarch64_be-unknown-linux-musl = "musl";
arm-unknown-linux-gnueabi = "gnu";
arm-unknown-linux-gnueabihf = "gnu";
arm-unknown-linux-musleabi = "musl";
arm-unknown-linux-musleabihf = "musl";
arm64ec-pc-windows-msvc = "msvc";
armeb-unknown-linux-gnueabi = "gnu";
armv4t-unknown-linux-gnueabi = "gnu";
armv5te-unknown-linux-gnueabi = "gnu";
armv5te-unknown-linux-musleabi = "musl";
armv5te-unknown-linux-uclibceabi = "uclibc";
armv6k-nintendo-3ds = "newlib";
armv7-rtems-eabihf = "newlib";
armv7-sony-vita-newlibeabihf = "newlib";
armv7-unknown-linux-gnueabi = "gnu";
armv7-unknown-linux-gnueabihf = "gnu";
armv7-unknown-linux-musleabi = "musl";
armv7-unknown-linux-musleabihf = "musl";
armv7-unknown-linux-ohos = "ohos";
armv7-unknown-linux-uclibceabi = "uclibc";
armv7-unknown-linux-uclibceabihf = "uclibc";
armv7-wrs-vxworks-eabihf = "gnu";
armv7a-vex-v5 = "v5";
csky-unknown-linux-gnuabiv2 = "gnu";
csky-unknown-linux-gnuabiv2hf = "gnu";
hexagon-unknown-linux-musl = "musl";
i386-apple-ios = "sim";
i586-unknown-linux-gnu = "gnu";
i586-unknown-linux-musl = "musl";
i586-unknown-redox = "relibc";
i686-pc-nto-qnx700 = "nto70";
i686-pc-windows-gnu = "gnu";
i686-pc-windows-gnullvm = "gnu";
i686-pc-windows-msvc = "msvc";
i686-unknown-hurd-gnu = "gnu";
i686-unknown-linux-gnu = "gnu";
i686-unknown-linux-musl = "musl";
i686-uwp-windows-gnu = "gnu";
i686-uwp-windows-msvc = "msvc";
i686-win7-windows-gnu = "gnu";
i686-win7-windows-msvc = "msvc";
i686-wrs-vxworks = "gnu";
loongarch64-unknown-linux-gnu = "gnu";
loongarch64-unknown-linux-musl = "musl";
loongarch64-unknown-linux-ohos = "ohos";
m68k-unknown-linux-gnu = "gnu";
mips-unknown-linux-gnu = "gnu";
mips-unknown-linux-musl = "musl";
mips-unknown-linux-uclibc = "uclibc";
mips64-openwrt-linux-musl = "musl";
mips64-unknown-linux-gnuabi64 = "gnu";
mips64-unknown-linux-muslabi64 = "musl";
mips64el-unknown-linux-gnuabi64 = "gnu";
mips64el-unknown-linux-muslabi64 = "musl";
mipsel-unknown-linux-gnu = "gnu";
mipsel-unknown-linux-musl = "musl";
mipsel-unknown-linux-uclibc = "uclibc";
mipsisa32r6-unknown-linux-gnu = "gnu";
mipsisa32r6el-unknown-linux-gnu = "gnu";
mipsisa64r6-unknown-linux-gnuabi64 = "gnu";
mipsisa64r6el-unknown-linux-gnuabi64 = "gnu";
powerpc-unknown-linux-gnu = "gnu";
powerpc-unknown-linux-gnuspe = "gnu";
powerpc-unknown-linux-musl = "musl";
powerpc-unknown-linux-muslspe = "musl";
powerpc-wrs-vxworks = "gnu";
powerpc-wrs-vxworks-spe = "gnu";
powerpc64-unknown-linux-gnu = "gnu";
powerpc64-unknown-linux-musl = "musl";
powerpc64-wrs-vxworks = "gnu";
powerpc64le-unknown-linux-gnu = "gnu";
powerpc64le-unknown-linux-musl = "musl";
riscv32-wrs-vxworks = "gnu";
riscv32gc-unknown-linux-gnu = "gnu";
riscv32gc-unknown-linux-musl = "musl";
riscv32imac-esp-espidf = "newlib";
riscv32imafc-esp-espidf = "newlib";
riscv32imc-esp-espidf = "newlib";
riscv64-wrs-vxworks = "gnu";
riscv64a23-unknown-linux-gnu = "gnu";
riscv64gc-unknown-linux-gnu = "gnu";
riscv64gc-unknown-linux-musl = "musl";
riscv64gc-unknown-managarm-mlibc = "mlibc";
riscv64gc-unknown-redox = "relibc";
s390x-unknown-linux-gnu = "gnu";
s390x-unknown-linux-musl = "musl";
sparc-unknown-linux-gnu = "gnu";
sparc64-unknown-linux-gnu = "gnu";
thumbv7a-pc-windows-msvc = "msvc";
thumbv7a-uwp-windows-msvc = "msvc";
thumbv7neon-unknown-linux-gnueabihf = "gnu";
thumbv7neon-unknown-linux-musleabihf = "musl";
wasm32-wali-linux-musl = "musl";
wasm32-wasip1 = "p1";
wasm32-wasip1-threads = "p1";
wasm32-wasip2 = "p2";
wasm32-wasip3 = "p3";
x86_64-apple-ios = "sim";
x86_64-apple-ios-macabi = "macabi";
x86_64-apple-tvos = "sim";
x86_64-apple-watchos-sim = "sim";
x86_64-fortanix-unknown-sgx = "sgx";
x86_64-pc-nto-qnx710 = "nto71";
x86_64-pc-nto-qnx710_iosock = "nto71_iosock";
x86_64-pc-nto-qnx800 = "nto80";
x86_64-pc-windows-gnu = "gnu";
x86_64-pc-windows-gnullvm = "gnu";
x86_64-pc-windows-msvc = "msvc";
x86_64-unikraft-linux-musl = "musl";
x86_64-unknown-hurd-gnu = "gnu";
x86_64-unknown-l4re-uclibc = "uclibc";
x86_64-unknown-linux-gnu = "gnu";
x86_64-unknown-linux-gnuasan = "gnu";
x86_64-unknown-linux-gnumsan = "gnu";
x86_64-unknown-linux-gnutsan = "gnu";
x86_64-unknown-linux-gnux32 = "gnu";
x86_64-unknown-linux-musl = "musl";
x86_64-unknown-linux-ohos = "ohos";
x86_64-unknown-managarm-mlibc = "mlibc";
x86_64-unknown-redox = "relibc";
x86_64-uwp-windows-gnu = "gnu";
x86_64-uwp-windows-msvc = "msvc";
x86_64-win7-windows-gnu = "gnu";
x86_64-win7-windows-msvc = "msvc";
x86_64-wrs-vxworks = "gnu";
xtensa-esp32-espidf = "newlib";
xtensa-esp32s2-espidf = "newlib";
xtensa-esp32s3-espidf = "newlib";
}
@@ -148,7 +148,7 @@ in
export CARGO_CFG_TARGET_OS=${stdenv.hostPlatform.rust.platform.os}
export CARGO_CFG_TARGET_FAMILY="unix"
export CARGO_CFG_UNIX=1
export CARGO_CFG_TARGET_ENV="gnu"
export CARGO_CFG_TARGET_ENV=${stdenv.hostPlatform.rust.platform.env}
export CARGO_CFG_TARGET_ENDIAN=${
if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then "little" else "big"
}
@@ -1008,6 +1008,66 @@ rec {
];
};
crateWasm32TargetEnv = assertOutputs {
name = "gnu64-crate-target-env";
mkCrate = mkCrate pkgsCross.wasm32-unknown-none.buildRustCrate;
crateArgs = {
crateName = "wasm32-crate-target-env";
crateBin = [ { name = "wasm32-crate-target-env"; } ];
src = symlinkJoin {
name = "wasm32-crate-target-env-sources";
paths = [
(mkFile "build.rs" ''
fn main() {
assert_eq!(std::env::var("CARGO_CFG_TARGET_ENV"), Ok("".to_string()));
}
'')
(mkFile "src/main.rs" ''
use std::env;
#[cfg(target_env = "")]
fn main() {
let name: String = env::args().nth(0).unwrap();
println!("executed {}", name);
}
'')
];
};
};
expectedFiles = [
"./bin/wasm32-crate-target-env.wasm"
];
};
crateGnu64TargetEnv = assertOutputs {
name = "gnu64-crate-target-env";
mkCrate = mkCrate pkgsCross.gnu64.buildRustCrate;
crateArgs = {
crateName = "gnu64-crate-target-env";
crateBin = [ { name = "gnu64-crate-target-env"; } ];
src = symlinkJoin {
name = "gnu64-crate-target-env-sources";
paths = [
(mkFile "build.rs" ''
fn main() {
assert_eq!(std::env::var("CARGO_CFG_TARGET_ENV"), Ok("gnu".to_string()));
}
'')
(mkFile "src/main.rs" ''
use std::env;
#[cfg(target_env = "gnu")]
fn main() {
let name: String = env::args().nth(0).unwrap();
println!("executed {}", name);
}
'')
];
};
};
expectedFiles = [
"./bin/gnu64-crate-target-env"
];
};
brotliTest =
let
pkg = brotliCrates.brotli_2_5_0 { };