diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index a43a00f16..d6fb58e4a 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -113,7 +113,7 @@ static void sigHandler(int signo) { } #endif -void initNix() +void initNix(bool loadConfig) { /* Turn on buffering for cerr. */ #if HAVE_PUBSETBUF @@ -121,7 +121,7 @@ void initNix() std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); #endif - initLibStore(); + initLibStore(loadConfig); #ifndef _WIN32 unix::startSignalHandlerThread(); diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index 3c657d2b7..aa44e1321 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -21,8 +21,9 @@ int handleExceptions(const std::string & programName, std::function fun) /** * Don't forget to call initPlugins() after settings are initialized! + * @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests. */ -void initNix(); +void initNix(bool loadConfig = true); void parseCmdLine(int argc, char * * argv, std::function parseArg); diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 6ce4d01bb..4fe25c7d4 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -19,6 +19,16 @@ nix_err nix_libstore_init(nix_c_context * context) NIXC_CATCH_ERRS } +nix_err nix_libstore_init_no_load_config(nix_c_context * context) +{ + if (context) + context->last_err_code = NIX_OK; + try { + nix::initLibStore(false); + } + NIXC_CATCH_ERRS +} + nix_err nix_init_plugins(nix_c_context * context) { if (context) diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index c83aca3f7..209f91f0d 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -35,6 +35,13 @@ typedef struct StorePath StorePath; */ nix_err nix_libstore_init(nix_c_context * context); +/** + * @brief Like nix_libstore_init, but does not load the Nix configuration. + * + * This is useful when external configuration is not desired, such as when running unit tests. + */ +nix_err nix_libstore_init_no_load_config(nix_c_context * context); + /** * @brief Loads the plugins specified in Nix's plugin-files setting. * diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 83e54e008..dfe3044ea 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -427,12 +427,13 @@ void assertLibStoreInitialized() { }; } -void initLibStore() { +void initLibStore(bool loadConfig) { if (initLibStoreDone) return; initLibUtil(); - loadConfFile(); + if (loadConfig) + loadConfFile(); preloadNSS(); diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index 852dba764..933fc2e5a 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -1279,9 +1279,10 @@ std::vector getUserConfigFiles(); extern const std::string nixVersion; /** - * NB: This is not sufficient. You need to call initNix() + * @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests. + * @note When using libexpr, and/or libmain, This is not sufficient. See initNix(). */ -void initLibStore(); +void initLibStore(bool loadConfig = true); /** * It's important to initialize before doing _anything_, which is why we diff --git a/tests/unit/libstore-support/tests/libstore.hh b/tests/unit/libstore-support/tests/libstore.hh index 78b162b95..267188224 100644 --- a/tests/unit/libstore-support/tests/libstore.hh +++ b/tests/unit/libstore-support/tests/libstore.hh @@ -11,7 +11,7 @@ namespace nix { class LibStoreTest : public virtual ::testing::Test { public: static void SetUpTestSuite() { - initLibStore(); + initLibStore(false); } protected: