zig: fix darwin SDK lookup

This commit is contained in:
Niklas Korz
2025-02-06 16:26:33 +01:00
parent 62eb60588f
commit 0ae3ecb356
2 changed files with 40 additions and 15 deletions
+33 -14
View File
@@ -4,6 +4,7 @@
fetchFromGitHub,
cmake,
llvmPackages,
xcbuild,
targetPackages,
libxml2,
zlib,
@@ -30,10 +31,15 @@ stdenv.mkDerivation (finalAttrs: {
patches = args.patches or [ ];
nativeBuildInputs = [
cmake
(lib.getDev llvmPackages.llvm.dev)
];
nativeBuildInputs =
[
cmake
(lib.getDev llvmPackages.llvm.dev)
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
# provides xcode-select, which is required for SDK detection
xcbuild
];
buildInputs =
[
@@ -81,16 +87,29 @@ stdenv.mkDerivation (finalAttrs: {
# Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
# work in Nix's sandbox. Use env from our coreutils instead.
postPatch =
if lib.versionAtLeast finalAttrs.version "0.12" then
''
substituteInPlace lib/std/zig/system.zig \
--replace-fail "/usr/bin/env" "${coreutils}/bin/env"
''
else
''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
--replace-fail "/usr/bin/env" "${coreutils}/bin/env"
'';
(
if lib.versionAtLeast finalAttrs.version "0.12" then
''
substituteInPlace lib/std/zig/system.zig \
--replace-fail "/usr/bin/env" "${coreutils}/bin/env"
''
else
''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
--replace-fail "/usr/bin/env" "${coreutils}/bin/env"
''
)
# Zig tries to access xcrun and xcode-select at the absolute system path to query the macOS SDK
# location, which does not work in the darwin sandbox.
# Upstream issue: https://github.com/ziglang/zig/issues/22600
# Note that while this fix is already merged upstream and will be included in 0.14+,
# we can't fetchpatch the upstream commit as it won't cleanly apply on older versions,
# so we substitute the paths instead.
+ lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder finalAttrs.version "0.14") ''
substituteInPlace lib/std/zig/system/darwin.zig \
--replace-fail /usr/bin/xcrun xcrun \
--replace-fail /usr/bin/xcode-select xcode-select
'';
postBuild =
if lib.versionAtLeast finalAttrs.version "0.13" then
+7 -1
View File
@@ -2,12 +2,18 @@
lib,
makeSetupHook,
zig,
stdenv,
xcbuild,
}:
makeSetupHook {
name = "zig-hook";
propagatedBuildInputs = [ zig ];
propagatedBuildInputs =
[ zig ]
# while xcrun is already included in the darwin stdenv, Zig also needs
# xcode-select (provided by xcbuild) for SDK detection
++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild ];
substitutions = {
# This zig_default_flags below is meant to avoid CPU feature impurity in