Allow loading config files into other config objects

This gives us some hope of moving away from global variables.
This commit is contained in:
John Ericson 2024-06-24 11:55:36 -04:00
parent b46e13840b
commit cb0c868da4
2 changed files with 12 additions and 6 deletions

View file

@ -124,12 +124,12 @@ Settings::Settings()
}; };
} }
void loadConfFile() void loadConfFile(AbstractConfig & config)
{ {
auto applyConfigFile = [&](const Path & path) { auto applyConfigFile = [&](const Path & path) {
try { try {
std::string contents = readFile(path); std::string contents = readFile(path);
globalConfig.applyConfig(contents, path); config.applyConfig(contents, path);
} catch (SystemError &) { } } catch (SystemError &) { }
}; };
@ -137,7 +137,7 @@ void loadConfFile()
/* We only want to send overrides to the daemon, i.e. stuff from /* We only want to send overrides to the daemon, i.e. stuff from
~/.nix/nix.conf or the command line. */ ~/.nix/nix.conf or the command line. */
globalConfig.resetOverridden(); config.resetOverridden();
auto files = settings.nixUserConfFiles; auto files = settings.nixUserConfFiles;
for (auto file = files.rbegin(); file != files.rend(); file++) { for (auto file = files.rbegin(); file != files.rend(); file++) {
@ -146,7 +146,7 @@ void loadConfFile()
auto nixConfEnv = getEnv("NIX_CONFIG"); auto nixConfEnv = getEnv("NIX_CONFIG");
if (nixConfEnv.has_value()) { if (nixConfEnv.has_value()) {
globalConfig.applyConfig(nixConfEnv.value(), "NIX_CONFIG"); config.applyConfig(nixConfEnv.value(), "NIX_CONFIG");
} }
} }
@ -438,7 +438,7 @@ void initLibStore(bool loadConfig) {
initLibUtil(); initLibUtil();
if (loadConfig) if (loadConfig)
loadConfFile(); loadConfFile(globalConfig);
preloadNSS(); preloadNSS();

View file

@ -1284,7 +1284,13 @@ extern Settings settings;
*/ */
void initPlugins(); void initPlugins();
void loadConfFile(); /**
* Load the configuration (from `nix.conf`, `NIX_CONFIG`, etc.) into the
* given configuration object.
*
* Usually called with `globalConfig`.
*/
void loadConfFile(AbstractConfig & config);
// Used by the Settings constructor // Used by the Settings constructor
std::vector<Path> getUserConfigFiles(); std::vector<Path> getUserConfigFiles();