fix(nix): build the Windows SDK the same on every host

xwin makes case-alias symlinks on a case-sensitive filesystem and none on a
case-insensitive one, so the SDK hash depended on the host. Build with
--disable-symlinks, pin that tree's hash, add the CA bundle xwin needs, and
add the lowercase aliases the linker asks for in a separate derivation.
This commit is contained in:
LGUG2Z committed 2026-10-30 18:22:44 -07:00
1 parent 048cf3b951
commit 3ae77df287
1 file changed
+71 -14
+71 -14
View File
@@ -35,11 +35,17 @@
pkgs.stdenvNoCC.mkDerivation {
name = "windows-sdk-${windowsSdkVersion}-crt-${windowsCrtVersion}";
nativeBuildInputs = [ pkgs.xwin ];
nativeBuildInputs = [
pkgs.xwin
pkgs.cacert
];
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = "sha256-UFQjsFVBwcF/9e9tVFoG0Z1JySxyTnFqoaRwr/tUWzA=";
# hash of `xwin splat --disable-symlinks`: without the flag xwin makes
# case-alias symlinks on a case-sensitive filesystem and none on a
# case-insensitive one, so the hash depends on the host
outputHash = "sha256-YjmEfoxtcmQXvUc+WtaElnyP/4osNvtSI7X6MDaNTjg=";
buildCommand = ''
export HOME=$(mktemp -d)
@@ -51,14 +57,65 @@
--cache-dir $out/.xwin-cache \
--sdk-version ${windowsSdkVersion} \
--crt-version ${windowsCrtVersion} \
splat --output $out
splat --disable-symlinks --output $out
rm -rf $out/.xwin-cache
'';
};
# the linker asks for `kernel32.lib` of a file that is `Kernel32.lib`, so
# on a case-sensitive host add a lowercase alias for every name with a
# capital in it, and the spellings the headers include each other by (the
# SDK itself is made without symlinks)
caseFolded =
{ pkgs, windowsSdk }:
if pkgs.stdenv.hostPlatform.isDarwin then
windowsSdk
else
pkgs.runCommandLocal "${windowsSdk.name}-case-folded" { } ''
cp -rs ${windowsSdk} $out
chmod -R u+w $out
cd $out
find . -name '*[A-Z]*' | while read -r path; do
directory=$(dirname "$path")
name=$(basename "$path")
lower=$(echo "$name" | tr 'A-Z' 'a-z')
if [ ! -e "$directory/$lower" ] && [ ! -L "$directory/$lower" ]; then
ln -s "$name" "$directory/$lower"
fi
done
# headers include each other with spellings that differ from the
# file name (`DriverSpecs.h` for `driverspecs.h`), which xwin
# covers with symlinks unless told not to
grep -Rhoi -E '^[[:space:]]*#[[:space:]]*include[[:space:]]*[<"][^>"]+[>"]' \
crt/include sdk/include 2>/dev/null \
| sed -E 's/.*[<"]([^>"]+)[>"]/\1/; s#\\#/#g; s#.*/##' \
| sort -u > $TMPDIR/spellings
find crt/include sdk/include \( -type f -o -type l \) \
| awk -v spellings=$TMPDIR/spellings '
BEGIN {
while ((getline line < spellings) > 0)
known[tolower(line)] = known[tolower(line)] "\n" line
}
{
n = split($0, parts, "/"); base = parts[n]
dir = substr($0, 1, length($0) - length(base))
m = split(known[tolower(base)], names, "\n")
for (i = 1; i <= m; i++)
if (names[i] != "" && names[i] != base)
print dir "\t" base "\t" names[i]
}' \
| while IFS=$'\t' read -r directory name spelling; do
if [ ! -e "$directory$spelling" ] && [ ! -L "$directory$spelling" ]; then
ln -s "$name" "$directory$spelling"
fi
done
'';
mkMsvcEnv =
{ pkgs, windowsSdk }:
let
sdk = caseFolded { inherit pkgs windowsSdk; };
clangVersion = pkgs.lib.versions.major pkgs.llvmPackages.clang.version;
in
{
@@ -76,10 +133,10 @@
"-Wno-unused-command-line-argument"
"-fuse-ld=lld-link"
"/imsvc${pkgs.llvmPackages.libclang.lib}/lib/clang/${clangVersion}/include"
"/imsvc${windowsSdk}/crt/include"
"/imsvc${windowsSdk}/sdk/include/ucrt"
"/imsvc${windowsSdk}/sdk/include/um"
"/imsvc${windowsSdk}/sdk/include/shared"
"/imsvc${sdk}/crt/include"
"/imsvc${sdk}/sdk/include/ucrt"
"/imsvc${sdk}/sdk/include/um"
"/imsvc${sdk}/sdk/include/shared"
];
CXXFLAGS_x86_64_pc_windows_msvc = builtins.concatStringsSep " " [
@@ -87,18 +144,18 @@
"-Wno-unused-command-line-argument"
"-fuse-ld=lld-link"
"/imsvc${pkgs.llvmPackages.libclang.lib}/lib/clang/${clangVersion}/include"
"/imsvc${windowsSdk}/crt/include"
"/imsvc${windowsSdk}/sdk/include/ucrt"
"/imsvc${windowsSdk}/sdk/include/um"
"/imsvc${windowsSdk}/sdk/include/shared"
"/imsvc${sdk}/crt/include"
"/imsvc${sdk}/sdk/include/ucrt"
"/imsvc${sdk}/sdk/include/um"
"/imsvc${sdk}/sdk/include/shared"
];
# target-specific rust flags with linker flavor and library search paths
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS = builtins.concatStringsSep " " [
"-Clinker-flavor=lld-link"
"-Lnative=${windowsSdk}/crt/lib/x86_64"
"-Lnative=${windowsSdk}/sdk/lib/um/x86_64"
"-Lnative=${windowsSdk}/sdk/lib/ucrt/x86_64"
"-Lnative=${sdk}/crt/lib/x86_64"
"-Lnative=${sdk}/sdk/lib/um/x86_64"
"-Lnative=${sdk}/sdk/lib/ucrt/x86_64"
];
# cargo target