Skip to content

Commit

Permalink
Merge pull request #11527 from Mic92/macos-test-fix
Browse files Browse the repository at this point in the history
Fix macOS tests with meson
  • Loading branch information
edolstra authored Sep 18, 2024
2 parents ec2b015 + 2b76426 commit 0ed2ab0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
# TODO: Remove the darwin check once
# https://github.com/NixOS/nixpkgs/pull/291814 is available
++ lib.optional (stdenv.cc.isClang && !stdenv.buildPlatform.isDarwin) pkgs.buildPackages.bear
++ lib.optional (stdenv.cc.isClang && stdenv.hostPlatform == stdenv.buildPlatform) pkgs.buildPackages.clang-tools;
++ lib.optional (stdenv.cc.isClang && stdenv.hostPlatform == stdenv.buildPlatform) (lib.hiPrio pkgs.buildPackages.clang-tools);

buildInputs = attrs.buildInputs or []
++ [
Expand Down
5 changes: 0 additions & 5 deletions misc/launchd/org.nixos.nix-daemon.plist.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>OBJC_DISABLE_INITIALIZE_FORK_SAFETY</key>
<string>YES</string>
</dict>
<key>Label</key>
<string>org.nixos.nix-daemon</string>
<key>KeepAlive</key>
Expand Down
5 changes: 0 additions & 5 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,6 @@ in {
preInstallCheck =
lib.optionalString (! doBuild) ''
mkdir -p src/nix-channel
''
# See https://github.com/NixOS/nix/issues/2523
# Occurs often in tests since https://github.com/NixOS/nix/pull/9900
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
'';

separateDebugInfo = !stdenv.hostPlatform.isStatic;
Expand Down
14 changes: 13 additions & 1 deletion src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mutex>
#include <thread>

#include <curl/curl.h>
#include <nlohmann/json.hpp>

#ifndef _WIN32
Expand Down Expand Up @@ -363,10 +364,21 @@ void initLibStore(bool loadConfig) {

preloadNSS();

/* Because of an objc quirk[1], calling curl_global_init for the first time
after fork() will always result in a crash.
Up until now the solution has been to set OBJC_DISABLE_INITIALIZE_FORK_SAFETY
for every nix process to ignore that error.
Instead of working around that error we address it at the core -
by calling curl_global_init here, which should mean curl will already
have been initialized by the time we try to do so in a forked process.
[1] https://github.com/apple-oss-distributions/objc4/blob/01edf1705fbc3ff78a423cd21e03dfc21eb4d780/runtime/objc-initialize.mm#L614-L636
*/
curl_global_init(CURL_GLOBAL_ALL);
#if __APPLE__
/* On macOS, don't use the per-session TMPDIR (as set e.g. by
sshd). This breaks build users because they don't have access
to the TMPDIR, in particular in ‘nix-store --serve’. */
#if __APPLE__
if (hasPrefix(defaultTempDir(), "/var/folders/"))
unsetenv("TMPDIR");
#endif
Expand Down
7 changes: 0 additions & 7 deletions tests/functional/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ mkMesonDerivation (finalAttrs: {
"--print-errorlogs"
];

preCheck =
# See https://github.com/NixOS/nix/issues/2523
# Occurs often in tests since https://github.com/NixOS/nix/pull/9900
lib.optionalString stdenv.hostPlatform.isDarwin ''
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
'';

doCheck = true;

installPhase = ''
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/libstore-support/tests/nix_api_store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "tests/nix_api_util.hh"

#include "file-system.hh"
#include <filesystem>

#include "nix_api_store.h"
#include "nix_api_store_internal.h"
Expand Down Expand Up @@ -47,7 +48,9 @@ protected:
if (fs::create_directory(nixDir)) break;
}
#else
auto tmpl = nix::defaultTempDir() + "/tests_nix-store.XXXXXX";
// resolve any symlinks in i.e. on macOS /tmp -> /private/tmp
// because this is not allowed for a nix store.
auto tmpl = nix::absPath(std::filesystem::path(nix::defaultTempDir()) / "tests_nix-store.XXXXXX", true);
nixDir = mkdtemp((char *) tmpl.c_str());
#endif

Expand All @@ -61,6 +64,10 @@ protected:
const char ** params[] = {p1, p2, p3, nullptr};

store = nix_store_open(ctx, "local", params);
if (!store) {
std::string errMsg = nix_err_msg(nullptr, ctx, nullptr);
ASSERT_NE(store, nullptr) << "Could not open store: " << errMsg;
};
}
};
}

0 comments on commit 0ed2ab0

Please sign in to comment.