init: Add flag to avoid loading configuration

This commit is contained in:
Robert Hensing 2024-04-19 23:41:56 +02:00
parent 8c4c2156bd
commit e05b58b060
6 changed files with 27 additions and 7 deletions

View file

@ -113,7 +113,7 @@ static void sigHandler(int signo) { }
#endif #endif
void initNix() void initNix(bool loadConfig)
{ {
/* Turn on buffering for cerr. */ /* Turn on buffering for cerr. */
#if HAVE_PUBSETBUF #if HAVE_PUBSETBUF
@ -121,7 +121,7 @@ void initNix()
std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf)); std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
#endif #endif
initLibStore(); initLibStore(loadConfig);
#ifndef _WIN32 #ifndef _WIN32
unix::startSignalHandlerThread(); unix::startSignalHandlerThread();

View file

@ -21,8 +21,9 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
/** /**
* Don't forget to call initPlugins() after settings are initialized! * 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, void parseCmdLine(int argc, char * * argv,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg); std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg);

View file

@ -19,6 +19,16 @@ nix_err nix_libstore_init(nix_c_context * context)
NIXC_CATCH_ERRS 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) nix_err nix_init_plugins(nix_c_context * context)
{ {
if (context) if (context)

View file

@ -35,6 +35,13 @@ typedef struct StorePath StorePath;
*/ */
nix_err nix_libstore_init(nix_c_context * context); 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. * @brief Loads the plugins specified in Nix's plugin-files setting.
* *

View file

@ -427,12 +427,13 @@ void assertLibStoreInitialized() {
}; };
} }
void initLibStore() { void initLibStore(bool loadConfig) {
if (initLibStoreDone) return; if (initLibStoreDone) return;
initLibUtil(); initLibUtil();
loadConfFile(); if (loadConfig)
loadConfFile();
preloadNSS(); preloadNSS();

View file

@ -1279,9 +1279,10 @@ std::vector<Path> getUserConfigFiles();
extern const std::string nixVersion; 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 * It's important to initialize before doing _anything_, which is why we