Compile Bevy with Nix Developer Environment on MacOS

Hey All,

Having some trouble compiling a pretty simple bevy app with nix-shell. I keep running into this compiler error:

Compiling serde_derive v1.0.183
   Compiling thiserror v1.0.44
   Compiling bytemuck v1.13.1
   Compiling bevy_reflect_derive v0.10.1
   Compiling bevy_ecs_macros v0.10.1
   Compiling objc_exception v0.1.2
   Compiling sysinfo v0.28.4
   Compiling fsevent-sys v4.1.0
   Compiling filetime v0.2.22
   Compiling num-integer v0.1.45
   Compiling block v0.1.6
The following warnings were emitted during compilation:

warning: extern/exception.m:1:10: fatal error: 'objc/objc.h' file not found
warning: #include <objc/objc.h>
warning:          ^~~~~~~~~~~~~
warning: 1 error generated.

error: failed to run custom build command for `objc_exception v0.1.2`

Caused by:
  process didn't exit successfully: `/Users/andrewplaza/Projects/insipx/portfolio-os/target/debug/build/objc_exception-967dc747fad11fc1/build-script-build` (exit status: 1)
  --- stdout
  TARGET = Some("aarch64-apple-darwin")
  OPT_LEVEL = Some("3")
  HOST = Some("aarch64-apple-darwin")
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin
  CC_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin
  CC_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CC
  HOST_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("aes,crc,dit,dotprod,dpb,dpb2,fcma,fhm,flagm,fp16,frintts,jsconv,lor,lse,neon,paca,pacg,pan,pmuv3,ras,rcpc,rcpc2,rdm,sb,sha2,sha3,ssbs,v8.1a,v8.2a,v8.3a,v8.4a,vh")
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin
  CFLAGS_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin
  CFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CFLAGS
  HOST_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = Some("-iframework /nix/store/4cxlahsfynhsvh4057p2y80dcg7gjm4h-devenv-profile/Library/Frameworks")
  running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-iframework" "/nix/store/4cxlahsfynhsvh4057p2y80dcg7gjm4h-devenv-profile/Library/Frameworks" "-o" "/Users/andrewplaza/Projects/insipx/portfolio-os/target/debug/build/objc_exception-6a080bf62f73939a/out/extern/exception.o" "-c" "extern/exception.m"
  cargo:warning=extern/exception.m:1:10: fatal error: 'objc/objc.h' file not found

  cargo:warning=#include <objc/objc.h>

  cargo:warning=         ^~~~~~~~~~~~~

  cargo:warning=1 error generated.

  exit status: 1

  --- stderr


  error occurred: Command "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-iframework" "/nix/store/4cxlahsfynhsvh4057p2y80dcg7gjm4h-devenv-profile/Library/Frameworks" "-o" "/Users/andrewplaza/Projects/insipx/portfolio-os/target/debug/build/objc_exception-6a080bf62f73939a/out/extern/exception.o" "-c" "extern/exception.m" with args "cc" did not execute successfully (status code exit status: 1).


warning: build failed, waiting for other jobs to finish...

The shell I’m using for development is this:

{ pkgs, lib, ... }:

{
  # https://devenv.sh/basics/
  env.GREET = "devenv";

  # https://devenv.sh/packages/
  packages = with pkgs;
    [
      cargo-watch
      clang
      gcc
      mold
      llvmPackages.libcxxStdenv
      llvmPackages.libclang
      vulkan-headers
      vulkan-loader
      clang
      pkg-config
    ] ++ lib.optionals pkgs.hostPlatform.isDarwin [
      darwin.apple_sdk.frameworks.Security
      darwin.apple_sdk.frameworks.CoreFoundation
      darwin.apple_sdk.frameworks.SystemConfiguration
      darwin.apple_sdk.frameworks.AGL
      darwin.apple_sdk.frameworks.CoreGraphics
      darwin.apple_sdk.frameworks.GameKit
      darwin.apple_sdk.frameworks.Carbon
    ];

  # https://devenv.sh/scripts/
  scripts.hello.exec = "echo hello from $GREET";

  languages.rust = {
    enable = true;
    channel = "nightly";
    components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" ];
  };

  enterShell = ''
    hello
    git --version
  '';

  env.LIBCLANG_PATH = "${pkgs.llvmPackages.libclang}/lib";
  # https://devenv.sh/languages/
  # languages.nix.enable = true;

  # https://devenv.sh/pre-commit-hooks/
  # pre-commit.hooks.shellcheck.enable = true;

  # https://devenv.sh/processes/
  # processes.ping.exec = "ping example.com";

  # See full reference at https://devenv.sh/reference/options/
}

I’m not sure where to get this objc.h. I’ve tried using libobjc in packages, but no dice. I’ve also looked up other posts to discourse about compiling a bevy app but couldn’t find a solution.

I am using a custom linker config:

# Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below.

[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-Clink-arg=-fuse-ld=.devenv/profile/bin/mold", "-Zshare-generics=y"]

# NOTE: you must install [Mach-O LLD Port](https://lld.llvm.org/MachO/index.html) on mac. you can easily do this by installing llvm which includes lld with the "brew" package manager:
# `brew install llvm`
[target.x86_64-apple-darwin]
rustflags = [
    "-C",
    "link-arg=-fuse-ld=.devenv/profile/bin/ld64.mold",
    "-Zshare-generics=y",
]

[target.aarch64-apple-darwin]
rustflags = [
    "-C",
    "link-arg=-fuse-ld=.devenv/profile/bin/ld64.mold",
    "-Zshare-generics=y",
]

# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
#[profile.dev]
#debug = 1

Hi @insipx, sorry to bother you with this old post but have you found a solution to this problem?
I have the same issue and haven’t found a solution yet

For those who’s gonna find this post in the future, I made it work (it works on OSX for cross system compatibility needs some ifs for the packages)

devenv.nix

{ pkgs, lib, ... }:
let frameworks = pkgs.darwin.apple_sdk.frameworks;
in {
  env.CFLAGS = lib.mkForce ("-I${pkgs.darwin.libobjc}/include/");

  # https://devenv.sh/packages/
  packages = with pkgs; [
    gcc
    darwin.libobjc
    # darwin.libiconv
    frameworks.Security
    frameworks.CoreServices
    frameworks.CoreFoundation
    frameworks.AppKit
    frameworks.Foundation
    frameworks.ApplicationServices
    frameworks.CoreGraphics
    frameworks.CoreVideo
    frameworks.Carbon
    frameworks.IOKit
    frameworks.CoreAudio
    frameworks.AudioUnit
    frameworks.QuartzCore
    frameworks.Metal
  ];

  # https://devenv.sh/languages/
  languages.rust.enable = true;
}

I still need to understand if forcing that CFLAGS variable to that value is causing other issues, but for now it solve the objc.h one and I’m able to run cargo run without problems :smiley:

1 Like