Develop shell environment setup for macOS

Trying to build this rust project using the below shell.nix config but ran in to errors on Catalina and Big Sur, but works on High Sierra.

Is it possible to run this on Catalina or Big Sur? I’m not sure what is it that I’m missing…

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> {
    overlays = [ moz_overlay ];
  };
  ruststable = (nixpkgs.latest.rustChannels.stable.rust.override {
    extensions = [ "rust-src" "rust-analysis" ];}
  );
  frameworks = nixpkgs.darwin.apple_sdk.frameworks;
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "rust";
    buildInputs = [ rustup ruststable
     frameworks.CoreAudio
    ];
  }
[nix-shell:~/project/invaders]$ cargo build
   Compiling coreaudio-sys v0.2.4
error: failed to run custom build command for `coreaudio-sys v0.2.4`

Caused by:
  process didn't exit successfully: `/Users/hywong/project/invaders/target/debug/build/coreaudio-sys-cf96514dfab3d7e5/build-script-build` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=COREAUDIO_SDK_PATH
  cargo:rustc-link-lib=framework=AudioUnit
  cargo:rustc-link-lib=framework=CoreAudio
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS

  --- stderr
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1189:83: error: expected ','
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1190:98: error: expected ','
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1200:85: error: expected ','
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUComponent.h:1595:72: error: expected ','
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUComponent.h:1629:72: error: expected ','
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:48:1: error: expected ')'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:48:1: note: to match this '('
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:48:19: error: a parameter list without types is only allowed in a function definition
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:93:24: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:116:1: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:116:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:137:1: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:138:65: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:137:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:177:19: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:202:20: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:241:32: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:271:32: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:299:21: error: unknown type name 'os_workgroup_t'
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:311:25: error: unknown type name 'os_workgroup_t'
  fatal error: too many errors emitted, stopping now [-ferror-limit=]
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1189:83: error: expected ',', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1190:98: error: expected ',', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:1200:85: error: expected ',', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUComponent.h:1595:72: error: expected ',', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUComponent.h:1629:72: error: expected ',', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:48:1: error: expected ')', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:48:19: error: a parameter list without types is only allowed in a function definition, err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:93:24: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:116:1: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:116:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:137:1: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:138:65: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:137:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:177:19: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:202:20: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:241:32: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:271:32: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:299:21: error: unknown type name 'os_workgroup_t', err: true
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/os/workgroup_object.h:311:25: error: unknown type name 'os_workgroup_t', err: true
  fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true
  thread 'main' panicked at 'unable to generate bindings: ()', /Users/hywong/.cargo/registry/src/github.com-1ecc6299db9ec823/coreaudio-sys-0.2.4/build.rs:102:39
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Not sure whether it will help, but there’s also darwin.apple_sdk.frameworks.Foundation and others available.

@abnopanda would you mind sharing if you were able to fix this or not? I’m seeing the same error with more-or-less the same inputs, and this is the only thread on the internet that seems relevant!

@brianhicks I would also like to know how you solved this issue :​P

Okay, I think I have found the main reason: it appears to be an incompatibility between Nix-provided clang and Apple-provided clang. Running PATH="/usr/bin:$PATH" cargo build solves the problem, because it makes cargo use the system clang rather than Nix clang. I have no idea how to specify this declaratively, though.

I had to dig some code up. This may not be 100% correct but I think it’s what solved the issue. I added this in my mkShell invocation.

  COREAUDIO_SDK_PATH = if pkgs.stdenv.isDarwin then
  # The coreaudio-sys crate is configured to look for things in whatever the
  # output of `xcrun --sdk macosx --show-sdk-path` is. However, this does not
  # always contain the right frameworks, and it uses system versions instead of
  # what we control via Nix. Instead of having to run a lot of extra scripts
  # to set our systems up to build, we can just create a SDK directory with
  # the same layout as the `MacOSX{version}.sdk` that XCode produces.
    pkgs.symlinkJoin {
      name = "sdk";
      paths = with pkgs.darwin.apple_sdk.frameworks; [
        AudioToolbox
        AudioUnit
        CoreAudio
        CoreFoundation
        CoreMIDI
        OpenAL
      ];
      postBuild = ''
        mkdir $out/System
        mv $out/Library $out/System
      '';
    }
  else
  # TODO: I'm not 100% confident that this being blank won't cause issues for
  # Nix-on-Linux development. It may be sufficient to use the pkgs.symlinkJoin
  # above regardless of system! That'd set us up for cross-compilation as well.
    "";
2 Likes

Is your code base publicly available somewhere? I gave this snippet a try but was still running into the following error:

that snippet was from github.com/roc-lang/roc, but it looks like someone has refactored the Nix stuff in the intervening time so that snippet is no longer in the codebase. That was the repo that failed, though, and looking around may give some ideas how to solve it now!

1 Like