Apple sdk cross compilation

Hello everybody,

I am currently trying to setup a dev shell for developing a cross platform zig project. I use the zig-overlay from mitchellh and got it working for linux and windows no problem.

Now I am at a stage, where I need to compile some objective-c for macos, and need to link to their frameworks for that. I found, that for nix there exists the apple-sdk package. Now the problem is, I need to compile for aarch64, but I couldn’t figure out how I can include the apple-sdk for that architecture.

This is what I have came up with after trying and failing for a while:

{
        inputs = {
                nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
                flake-utils.url = "github:numtide/flake-utils";
                zig.url = "github:mitchellh/zig-overlay";
        };
        outputs = { self, nixpkgs, flake-utils, zig }:
                flake-utils.lib.eachDefaultSystem
                (system:
                        let
                                pkgs = import nixpkgs {
                                        inherit system;
                                };
                                aarch64-darwin-packages = import nixpkgs {
                                        crossSystem = { config = "aarch64-darwin"; };
                                        inherit system;
                                };
                        in
                        {
                                devShells.default = pkgs.mkShellNoCC {
                                        name = "nix-rat-dev";
                                        packages = with pkgs; [
                                                zig.packages.${system}."0.13.0"
                                                zls
                                                aarch64-darwin-packages.apple-sdk_15
                                        ];
                                };
                        }
                );
}

This is how I activate it:

NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nix develop --impure -c $SHELL

(I also wouldn’t mind losing the --impure and NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 parts)

But it fails with the following:

error:
       … while calling the 'derivationStrict' builtin
         at <nix/derivation-internal.nix>:34:12:
           33|
           34|   strict = derivationStrict drvAttrs;
             |            ^
           35|

       … while evaluating derivation 'nix-rat-dev'
         whose name attribute is located at /nix/store/pgg4vm83q0kr4hxzcwhdgdiv2yfnh3dw-source/pkgs/stdenv/generic/make-derivation.nix:375:7

       … while evaluating attribute 'nativeBuildInputs' of derivation 'nix-rat-dev'
         at /nix/store/pgg4vm83q0kr4hxzcwhdgdiv2yfnh3dw-source/pkgs/stdenv/generic/make-derivation.nix:419:7:
          418|       depsBuildBuild              = elemAt (elemAt dependencies 0) 0;
          419|       nativeBuildInputs           = elemAt (elemAt dependencies 0) 1;
             |       ^
          420|       depsBuildTarget             = elemAt (elemAt dependencies 0) 2;

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: assertion '((pkgs).stdenv.cc.libcxx != null)' failed
       at /nix/store/pgg4vm83q0kr4hxzcwhdgdiv2yfnh3dw-source/pkgs/stdenv/adapters.nix:57:5:
           56|     assert stdenv.cc.libcxx != null;
           57|     assert pkgs.stdenv.cc.libcxx != null;
             |     ^
           58|     # only unified libcxx / libcxxabi stdenv's are supported

And I couldn’t really make heads or tails from that.

I would really appreciate your help.

What is your host system? Cross-compilation to Linux from Darwin is not currently supported.

I knew I missed something, sorry. My host system is an x86_64-linux-gnu and I want to get the apple frameworks for a aarch64-darwin. So it’s not possible to download the framework for another architecture?

Ok, after trying around a bit more, it seems it is not possible to donwload the apple sdk’s on non apple architectures. If I am wrong, please correct me. Or if anybody has a different solution, for obtaining the aarch64 sdk on an x86_64 linux, I’d be happy about any help.

You could try apple-sdk.src to get the unprocessed SDK, but the SDK package in nixpkgs is not currently supported for cross-compilation from Linux. It would be nice to have eventually, but that’s likely 25.11 or 26.05.

1 Like

For anybody interested, this is how I ended up solving it. I don’t know how this behaves with different host systems, but it works for my usecase, for now:

{
	inputs = {
		nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
		flake-utils.url = "github:numtide/flake-utils";
		zig.url = "github:mitchellh/zig-overlay";
	};
	outputs = { self, nixpkgs, flake-utils, zig }:
		flake-utils.lib.eachDefaultSystem
		(system:
			let
				pkgs = import nixpkgs {
					inherit system;
				};

				apple-sdk = pkgs.stdenv.mkDerivation {
						name = "apple-sdk_15.2";
						src = pkgs.fetchzip {
							url = "https://github.com/joseluisq/macosx-sdks/releases/download/15.2/MacOSX15.2.sdk.tar.xz";
							sha256 = "sha256:0fgj0pvjclq2pfsq3f3wjj39906xyj6bsgx1da933wyc918p4zi3";
						};

						phases = [ "installPhase" ];

						installPhase = ''
							mkdir -p "$out"

							cp -r "$src"/* "$out"

							ls "$out"
						'';
					};
			in
				with pkgs;
			{
				devShells.default = pkgs.mkShellNoCC {
					name = "nix-rat-dev";
					packages = [ 
						zig.packages.${system}."0.13.0"
						zls
						apple-sdk
					];

					APPLE_SDK_PATH = "${apple-sdk}";
				};
			}
		);
}