Skip to content

Commit d7d19f8

Browse files
committed
macos: fix issues with macOS SDKs
- Remove the default SDK from stdenv. This is currently not easy to do. - Ensure users can set their own SDK if necessary via `packages`.
1 parent 68a6d54 commit d7d19f8

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/modules/top-level.nix

+33-12
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ in
6969
stdenv = lib.mkOption {
7070
type = types.package;
7171
description = "The stdenv to use for the developer environment.";
72-
default = pkgs.stdenv;
72+
default =
73+
if pkgs.stdenv.isDarwin
74+
then
75+
pkgs.stdenv.override
76+
(prev: {
77+
# Remove the default apple-sdk on macOS.
78+
# Prefer to expose the system SDK, or let the user specify the SDK in `packages`.
79+
extraBuildInputs =
80+
builtins.filter (x: !lib.hasPrefix "apple-sdk" x.pname) prev.extraBuildInputs;
81+
})
82+
else pkgs.stdenv;
7383
defaultText = lib.literalExpression "pkgs.stdenv";
7484
};
7585

@@ -297,17 +307,28 @@ in
297307
ln -snf ${lib.escapeShellArg config.devenv.runtime} ${lib.escapeShellArg config.devenv.dotfile}/run
298308
'';
299309

300-
shell = performAssertions (
301-
(pkgs.mkShell.override { stdenv = config.stdenv; }) ({
302-
hardeningDisable = config.hardeningDisable;
303-
name = "devenv-shell";
304-
packages = config.packages;
305-
shellHook = ''
306-
${lib.optionalString config.devenv.debug "set -x"}
307-
${config.enterShell}
308-
'';
309-
} // config.env)
310-
);
310+
shell =
311+
let
312+
# `mkShell` merges `packages` into `nativeBuildInputs`.
313+
# This distinction is generally not important for devShells, except when it comes to setup hooks and their run order.
314+
# On macOS, the default apple-sdk is added the stdenv via `extraBuildInputs`.
315+
# If we don't remove it from stdenv, then it's setup hooks will clobber any SDK added to `packages`.
316+
isAppleSDK = pkg: builtins.match ".*apple-sdk.*" (pkg.pname or "") != null;
317+
partitionedPkgs = builtins.partition isAppleSDK config.packages;
318+
buildInputs = partitionedPkgs.right;
319+
nativeBuildInputs = partitionedPkgs.wrong;
320+
in
321+
performAssertions (
322+
(pkgs.mkShell.override { stdenv = config.stdenv; }) ({
323+
name = "devenv-shell";
324+
hardeningDisable = config.hardeningDisable;
325+
inherit buildInputs nativeBuildInputs;
326+
shellHook = ''
327+
${lib.optionalString config.devenv.debug "set -x"}
328+
${config.enterShell}
329+
'';
330+
} // config.env)
331+
);
311332

312333
infoSections."env" = lib.mapAttrsToList (name: value: "${name}: ${toString value}") config.env;
313334
infoSections."packages" = builtins.map (package: package.name) (builtins.filter (package: !(builtins.elem package.name (builtins.attrNames config.scripts))) config.packages);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{ pkgs, ... }:
2+
3+
{
4+
packages = [ pkgs.apple-sdk ];
5+
6+
# Test that the above SDK is picked up by xcode-select.
7+
enterTest = ''
8+
if [ -v DEVELOPER_DIR ]; then
9+
echo "DEVELOPER_DIR is not set."
10+
exit 1
11+
fi
12+
13+
xcode-select -p | grep -q /nix/store
14+
'';
15+
}

tests/macos-no-default-sdk/devenv.nix

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
# Test that there is no default SDK set on macOS.
3+
enterTest = ''
4+
variables_to_check=(
5+
"DEVELOPER_DIR"
6+
"DEVELOPER_DIR_FOR_BUILD"
7+
"SDKROOT"
8+
"NIX_APPLE_SDK_VERSION"
9+
)
10+
11+
for var in "''${variables_to_check[@]}"; do
12+
if [ -v "$var" ]; then
13+
echo "$var is set. Expected no default Apple SDK." >&2
14+
exit 1
15+
fi
16+
done
17+
'';
18+
}

0 commit comments

Comments
 (0)