MacOS SDK, Xcode on Nix Darwin Reoccurance

Similar problem as MacOS SDK, Xcode on Nix Darwin

bash-5.2$ git clone https://github.com/HappyCerberus/rtsp-simple-server
[...]
bash-5.2$ cd rtsp-simple-server
bash-5.2$ vim ../flake.nix
bash-5.2$ cat ../flake.nix
{
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.devenv.url = "github:cachix/devenv";

  outputs = { self, nixpkgs, flake-utils, devenv }@inputs:
    flake-utils.lib.eachDefaultSystem (system:
      let
        # pkgs = nixpkgs.legacyPackages.${system};
        pkgs = import nixpkgs {
          inherit system;
          config.allowUnfree = true;
        };
        osxlibs = pkgs.lib.lists.optionals pkgs.stdenv.isDarwin [
          pkgs.darwin.apple_sdk.frameworks.Security
          pkgs.darwin.apple_sdk.frameworks.Foundation
          # pkgs.darwin.xcode_14_1
        ];
      in {
        devShells.default = devenv.lib.mkShell {
          inherit inputs pkgs;
          modules = [{
            packages = [
              pkgs.go_1_22
              pkgs.gopls
              pkgs.dotnet-sdk_8
              # pkgs.x
              pkgs.bazel
              pkgs.bazel-buildtools
            ] ++ osxlibs;
            services.mysql = [removed as not relevant];
          }];
        };
      });
}
bash-5.2$ xcode-select --print-path
/nix/store/l26jpz45c1sdvi52v06ajr6qz7qz39b7-xcodebuild-0.1.2-pre/Applications/Xcode.app/Contents/Developer
bash-5.2$  xcrun --show-sdk-path -sdk macosx
/nix/store/57c709q4vgap6fnf5pqqyv1ihji7npj6-SDKs/MacOSX11.0.sdk
bash-5.2$ xcodebuild -showsdks
macosx SDKs:
        MacOSX11.0                      -sdk MacOSX11.0
        MacOSX11.0                      -sdk MacOSX11.0

bash-5.2$ bazel clean --expunge
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
bash-5.2$ bazel run //:gazelle --macos_sdk_version=11.0
Starting local Bazel server and connecting to it...
DEBUG: /private/var/tmp/_bazel_brmay/0a26928f540191a8dc5976d7a99d512b/external/bazel_tools/tools/os
x/xcode_configure.bzl:93:14: Invoking xcodebuild failed, developer dir: /Applications/Xcode.app/Con
tents/Developer ,return code 1, stderr: xcodebuild: error: SDK "" cannot be located.
, stdout:
DEBUG: /private/var/tmp/_bazel_brmay/0a26928f540191a8dc5976d7a99d512b/external/bazel_gazelle/intern
al/go_repository.bzl:189:18: org_golang_x_mod: gazelle: finding module path for import golang.org/x
/xerrors: finding module path for import golang.org/x/xerrors: //go:build comment without // +build
 comment
INFO: Analyzed target //:gazelle (90 packages loaded, 7651 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_brmay/0a26928f540191a8dc5976d7a99d512b/external/com_github_bazelbuil
d_buildtools/tables/BUILD.bazel:3:11: GoCompilePkg external/com_github_bazelbuild_buildtools/tables
/go_default_library.a [for tool] failed: I/O exception during sandboxed execution: xcrun failed wit
h code 1.
This most likely indicates that SDK version [11.0] for platform [MacOSX] is unsupported for the tar
get version of xcode.
Process exited with status 1
stdout: stderr: 2024-08-07 11:00:50.133 xcodebuild[80051:3078863] Writing error result bundle to /v
ar/folders/sm/mrjx7rx104ndks35phx674700000gr/T/ResultBundle_2024-07-08_11-00-0050.xcresult
xcodebuild: error: SDK "macosx11.0" cannot be located.
2024-08-07 11:00:51.611 xcodebuild[80063:3078904] Writing error result bundle to /var/folders/sm/mrjx7rx104ndks35phx674700000gr/T/ResultBundle_2024-07-08_11-00-0051.xcresult
xcodebuild: error: SDK "macosx11.0" cannot be located.
xcrun: error: unable to lookup item 'Path' in SDK 'macosx11.0'
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 54.311s, Critical Path: 4.44s
INFO: 23 processes: 21 internal, 2 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
ERROR: Build failed. Not running target

What am I doing wrong?

Have tried various supposed solutions, so far nothing works.

Hi there! I am the author of the post you linked.
I don’t quite remember how I solved this specific issue, but this is what I have in my flake.nix:

# NOTE: on MacOS, using the clang wrapper in pkgs.mkShell is interfering
# with Bazel compilation, as Bazel is trying to access some Mac-specific
# frameworks using the wrapped clang, and can't find them.
mkShell = if pkgs.stdenv.isDarwin then pkgs.mkShellNoCC else pkgs.mkShell;

So essentially, mkShell is bringing in some wrappers that obfuscate some of the toolchains installed on the host machine, outside the Nix store.

Additionally, this is what I have on my devshell init:

shellHook = lib.optionalString stdenv.isDarwin ''
    # NOTE: on macOS, Go and other derivations bring in an 'xcodebuild' dependency
    # that will mess with the native Xcode.app, preventing developers from running
    # the iOS app on their machines using 'pnpm expo:start:ios' or 'pnpm run ios'.
    #
    # Here we're filtering the /bin path to the 'xcodebuild' dependency brought in,
    # so that 'xcodebuild' resolves to the version installed outside the devshell.
    export PATH="$(echo "$PATH" | tr ':' '\n' | grep -v xcodebuild | paste -sd ':')"
    export PATH="$(echo "$PATH" | tr ':' '\n' | grep -v XcodeDefault | paste -sd ':')"
    export PATH="$(echo "$PATH" | tr ':' '\n' | grep -v coreutils | paste -sd ':')"
  '';

It is mostly relevant for the code path that builds the React Native mobile apps in my monorepo, but maybe it also helps in this case.

Happy debugging!

2 Likes

That looks awful…

But will try, thanks for the suggestion.

Yeah tell me about it… that’s what you get when working on MacOS and with tooling that makes so many assumptions on how things are set up on your machine :sweat_smile:

My latest attempt:

(devenv) bash-5.2$ bazel run //:gazelle  --sandbox_debug
Starting local Bazel server and connecting to it...
DEBUG: /private/var/tmp/_bazel_brmay/97218223d142e1e57c833f3d7b357552/external/bazel_gazelle/internal/go_repository.bzl:189:18: org_golang_x_mod: gaz
elle: finding module path for import golang.org/x/xerrors: finding module path for import golang.org/x/xerrors: //go:build comment without // +build
comment
INFO: Analyzed target //:gazelle (90 packages loaded, 7652 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_brmay/97218223d142e1e57c833f3d7b357552/external/org_golang_x_mod/module/BUILD.bazel:3:11: GoCompilePkg external/org_go
lang_x_mod/module/module.a [for tool] failed: (Exit 1): process-wrapper failed: error executing command
  (cd /private/var/tmp/_bazel_brmay/97218223d142e1e57c833f3d7b357552/sandbox/processwrapper-sandbox/16/execroot/__main__ && \
  exec env - \
    APPLE_SDK_PLATFORM=MacOSX \
    APPLE_SDK_VERSION_OVERRIDE=14.5 \
    CGO_ENABLED=1 \
    DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
    GOARCH=arm64 \
    GOOS=darwin \
    GOPATH='' \
    GOROOT=external/go_sdk \
    GOROOT_FINAL=GOROOT \
    PATH=external/local_config_cc:/bin:/usr/bin \
    SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk \
    TMPDIR=/tmp/brmay/nix-shell.fg2zct \
    XCODE_VERSION_OVERRIDE=15.4.0.15F31d \
    ZERO_AR_DATE=1 \
  /var/tmp/_bazel_brmay/install/ea5ec5be56ce8b5a215d22d37e0d49d6/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/private/var/tmp/_bazel_brmay/97218223d142e1e57c833f3d7b357552/sandbox/processwrapper-sandbox/16/stats.out' bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_arm64 -src external/org_golang_x_mod/module/module.go -arc 'golang.org/x/mod/semver=golang.org/x/mod/semver=bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/org_golang_x_mod/semver/semver.x' -importpath golang.org/x/mod/module -p golang.org/x/mod/module -package_list bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/go_sdk/packages.txt -o bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/org_golang_x_mod/module/module.a -x bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/org_golang_x_mod/module/module.x -gcflags '' -asmflags '')
compilepkg: missing strict dependencies:
        /private/var/tmp/_bazel_brmay/97218223d142e1e57c833f3d7b357552/sandbox/processwrapper-sandbox/16/execroot/__main__/external/org_golang_x_mod/module/module.go: import of "golang.org/x/xerrors"
No dependencies were provided.
Check that imports in Go sources match importpath attributes in deps.
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 127.869s, Critical Path: 2.23s
INFO: 31 processes: 19 internal, 12 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
ERROR: Build failed. Not running target

That makes no sense to me. I might end up just doing this in a Linux VM or something.