I’m trying to create a devShell for an existing large project that, among other things, runs React Native. When running react-native run-ios it runs an xcodebuild which relies on clang flags that are only valid for the XCode command line tools version of clang.
It seems like nix develop in and of itself depends on clang-wrapper so clang is in my devShells PATH ahead of XCode’s tools which causes my builds to fail when we run xcodebuild as it tries to pass arguments to clang which the devShell version does not support.
$ which clang
/nix/store/nwfcj1p4l74krm4n6szrb0c534p4ai2s-clang-wrapper-11.1.0/bin/clang
Unfortunately, just shoving all of /Library/Developer/CommandLineTools/usr/bin in front of my PATH when running this command leads to other issues of it then using ld instead of clang for linking which it seems others have run into without any answers.
My flake.nix isn’t doing anything special, just some packages and a few env vars to setup Apple framework SDKs, and I confirmed with just a simple flake that clang is still shadowed by the dev shell:
{
outputs = { self, nixpkgs }:
let pkgs = import nixpkgs { system = "aarch64-darwin"; };
in {
devShells.aarch64-darwin.default = pkgs.mkShell {
packages = with pkgs; [ cowsay ];
};
};
}
Is there any way to run react native build tooling inside a nix shell? I’ve struggled to find any examples and the docs only really talk about the quirks with creating xcode ios derivations, not devshells that work with existing build tools.
To clarify, I’m currently just trying to get a dev shell suitable to run our existing build pipelines (yarn, node, react-native cli), not fully convert to a nix derivation. I’ve been able to figure out all the other bits, but the react-native build is still stumping me.
A friend of mine ran into this problem, I found a solution:
It’s not nix develop that depends on clang, but rather pkgs.mkShell. It’s a wrapper around stdenv.mkDerivation (see Nixpkgs 23.11 manual | Nix & NixOS). So it uses the stdenv build environment, which includes a C compiler, seemingly clang on macOS.
There’s also stdenvNoCC, which doesn’t include a C compiler. That’s useful for builds that don’t need one, and can also help here. So the solution is just to use pkgs.mkShellNoCC instead of pkgs.mkShell, which is an (sadly undocumented) variant of mkShell that overrides stdenv with stdenvNoCC.
(it’s defined in all-packages.nix from nixpkgs: mkShellNoCC = mkShell.override { stdenv = stdenvNoCC; };)
I haven’t tested this myself with react-native, but the friend confirmed it works with react-native and mkShellNoCC.
Note: https://github.com/viperML/mkshell-minimal as suggested by @azuwis should work too as it also doesn’t include a C compiler, but mkShellNoCC is included in nixpkgs anyway, so it might be easier to use.
I’m very interested in the next step for this. I’m trying to build openjdk, and that heavily relies on XCode for building on macOS. I’m trying to see if I can build a nix shell for developing idk, but I don’t easily see how to bring in XCode and other dependencies into a nix-shell. I’ve managed to do this:
@eisfunke I was getting the same clang-11 errors building my react native app and I struggled for days before even realizing my .nix configuration was the root cause. My initial attempt was to pass compiler/toolchain flags to xcodebuild but your solution is cleanest. Thank you!