Compiling on My Mac Using Xcode in Devbox (Nix)
Getting Devbox (or nix) to work with MacOS compile chains can be tricky. In my cse I needed to use an older version of Xcode (15). While there is a nix package for xcode, it will not actually download Xcode. Instead the user has to download it and manually add it to the nix-store
. Here's how I did it:
-
Download Xcode from the Apple Developer site.
-
Extract the download and add it to the store:
open -W Xcode_15.4.xip nix-store --add-fixed --recursive sha256 Xcode.app devbox add darwin.xcode_15_4
Here, you'll need to keep track of where in the
nix-store
Xcode has been added. If you forget, you can run the following command from within your Devbox directory:cat .devbox/nix/profile/default/manifest.json | grep -Eio '\/nix\/store\/.*Xcode\.app'
-
Set the
DEVELOPER_DIR
,SDKROOT
,CPATH
,LIBRARY_PATH
, andPATH
environment variables in yourdevbox.json
. In particular, my devbox.json had the following:{ "env":{ "DEVELOPER_DIR": "/nix/store/dca1525f56e59ae54d9dd774e14e7a19-Xcode.app/Contents/Developer" }, "shell": { "init_hook": [ "export SDKROOT=$(xcrun --sdk macosx14.5 --show-sdk-path)", "export CPATH=$SDKROOT/usr/include", "export LIBRARY_PATH=$SDKROOT/usr/lib", "export PATH=\"$DEVELOPER_DIR/usr/bin:$DEVELOPER_DIR/Toolchains/XcodeDefault.xctoolchain/usr/bin:$PATH\"", ], } }
Note that the
$DEVELOPER_DIR
is the nix-store path of Xcode.app, plus/Contents/Developer
.Also, note that for my
$PATH
I added$DEVELOPER_DIR/usr/bin
and$DEVELOPER_DIR/Toolchains/XcodeDefault.xctoolchain/usr/bin
to the front of the path. TheToolchains
directory ensures that I use the right compiler (clang et al) for my Xcode version.
That's pretty much it!
In my particular case, running a tauri (rust underneath) app, I also needed to add
{
"packages": [
"nodejs@latest",
"libiconv@latest",
"rustup@latest",
"darwin.xcode_15_4@latest"
],
"shell": {
"init_hook": [
"projectDir=$(dirname $(readlink -f \"$0\"))",
"rustupHomeDir=\"$projectDir\"/.rustup",
"mkdir -p $rustupHomeDir",
"export RUSTUP_HOME=$rustupHomeDir",
"rustup default stable",
"cd src-tauri && cargo fetch"
]
}
}
to my devbox.json