Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig: fix darwin SDK lookup #376427

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pkgs/development/compilers/zig/0.11/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
stdenv,
testers,
zlib,
xcbuild,
}:

stdenv.mkDerivation (finalAttrs: {
Expand Down Expand Up @@ -58,12 +59,22 @@ stdenv.mkDerivation (finalAttrs: {

strictDeps = true;

# 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 = ''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
'';
postPatch =
# 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.
''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
''
# Zig tries to access xcrun at the absolute system path to query the macOS SDK
# location, which does not work in the darwin sandbox.
# We can just provide "xcrun" instead as it is part of the darwin stdenv.
# Upstream issue: https://github.com/ziglang/zig/issues/22600
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace lib/std/zig/system/darwin.zig \
--replace-fail /usr/bin/xcrun xcrun \
--replace-fail /usr/bin/xcode-select '${lib.getExe' xcbuild "xcode-select"}'
'';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that this is duplicated three times over the span of three compilers. This is something unlikely to change in the future compilers as well, so this will continue to be duplicated in 0.14, 0.15, ...

Copy link
Contributor

@WeetHet WeetHet Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also applies to code above, but I don't know how you can select a range of lines in GitHub PR reviews

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already addressed by #331011

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing, I didn't know about that PR, sorry :(


postBuild = ''
stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
Expand Down
23 changes: 17 additions & 6 deletions pkgs/development/compilers/zig/0.12/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
stdenv,
testers,
zlib,
xcbuild,
}:

stdenv.mkDerivation (finalAttrs: {
Expand Down Expand Up @@ -70,12 +71,22 @@ stdenv.mkDerivation (finalAttrs: {
# https://github.com/NixOS/nixpkgs/issues/317055#issuecomment-2148438395
strictDeps = !stdenv.cc.isClang;

# 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 = ''
substituteInPlace lib/std/zig/system.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
'';
postPatch =
# 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.
''
substituteInPlace lib/std/zig/system.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
''
# Zig tries to access xcrun at the absolute system path to query the macOS SDK
# location, which does not work in the darwin sandbox.
# We can just provide "xcrun" instead as it is part of the darwin stdenv.
# Upstream issue: https://github.com/ziglang/zig/issues/22600
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace lib/std/zig/system/darwin.zig \
--replace-fail /usr/bin/xcrun xcrun \
--replace-fail /usr/bin/xcode-select '${lib.getExe' xcbuild "xcode-select"}'
'';

postBuild = ''
stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
Expand Down
23 changes: 17 additions & 6 deletions pkgs/development/compilers/zig/0.13/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
stdenv,
testers,
zlib,
xcbuild,
}:

stdenv.mkDerivation (finalAttrs: {
Expand Down Expand Up @@ -70,12 +71,22 @@ stdenv.mkDerivation (finalAttrs: {
# https://github.com/NixOS/nixpkgs/issues/317055#issuecomment-2148438395
strictDeps = !stdenv.cc.isClang;

# 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 = ''
substituteInPlace lib/std/zig/system.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
'';
postPatch =
# 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.
''
substituteInPlace lib/std/zig/system.zig \
--replace "/usr/bin/env" "${lib.getExe' coreutils "env"}"
''
# Zig tries to access xcrun at the absolute system path to query the macOS SDK
# location, which does not work in the darwin sandbox.
# We can just provide "xcrun" instead as it is part of the darwin stdenv.
# Upstream issue: https://github.com/ziglang/zig/issues/22600
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace lib/std/zig/system/darwin.zig \
--replace-fail /usr/bin/xcrun xcrun \
--replace-fail /usr/bin/xcode-select '${lib.getExe' xcbuild "xcode-select"}'
'';

postBuild = ''
stage3/bin/zig build langref
Expand Down
29 changes: 20 additions & 9 deletions pkgs/development/compilers/zig/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, zlib
, coreutils
, callPackage
, xcbuild
, ...
}:

Expand Down Expand Up @@ -50,15 +51,25 @@ stdenv.mkDerivation (finalAttrs: {
export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache";
'';

# 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 args.version "0.12" then ''
substituteInPlace lib/std/zig/system.zig \
--replace "/usr/bin/env" "${coreutils}/bin/env"
'' else ''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
--replace-fail "/usr/bin/env" "${coreutils}/bin/env"
'';
postPatch =
# 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.
(if lib.versionAtLeast args.version "0.12" then ''
substituteInPlace lib/std/zig/system.zig \
--replace "/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 at the absolute system path to query the macOS SDK
# location, which does not work in the darwin sandbox.
# We can just provide "xcrun" instead as it is part of the darwin stdenv.
# Upstream issue: https://github.com/ziglang/zig/issues/22600
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace lib/std/zig/system/darwin.zig \
--replace-fail /usr/bin/xcrun xcrun \
--replace-fail /usr/bin/xcode-select '${lib.getExe' xcbuild "xcode-select"}'
'';

doInstallCheck = true;
installCheckPhase = ''
Expand Down