From 0ae3ecb356dec276e1cfb3d445b5c0dde54d5757 Mon Sep 17 00:00:00 2001 From: Niklas Korz Date: Wed, 5 Feb 2025 23:17:55 +0100 Subject: [PATCH] zig: fix darwin SDK lookup --- pkgs/development/compilers/zig/generic.nix | 47 +++++++++++++++------- pkgs/development/compilers/zig/hook.nix | 8 +++- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/pkgs/development/compilers/zig/generic.nix b/pkgs/development/compilers/zig/generic.nix index 9042271fddd1..159ca35a813a 100644 --- a/pkgs/development/compilers/zig/generic.nix +++ b/pkgs/development/compilers/zig/generic.nix @@ -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 diff --git a/pkgs/development/compilers/zig/hook.nix b/pkgs/development/compilers/zig/hook.nix index 99adbdeb2b7b..0eba0d5bed12 100644 --- a/pkgs/development/compilers/zig/hook.nix +++ b/pkgs/development/compilers/zig/hook.nix @@ -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