Merge pull request #10951 from obsidiansystems/load-just-one-config

Small global config refactors
This commit is contained in:
John Ericson 2024-06-24 12:38:04 -04:00 committed by GitHub
commit fda4c78921
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 130 additions and 98 deletions

View file

@ -8,6 +8,7 @@
#include "ansicolor.hh" #include "ansicolor.hh"
#include "shared.hh" #include "shared.hh"
#include "config-global.hh"
#include "eval.hh" #include "eval.hh"
#include "eval-cache.hh" #include "eval-cache.hh"
#include "eval-inline.hh" #include "eval-inline.hh"

View file

@ -1,4 +1,5 @@
#include "users.hh" #include "users.hh"
#include "config-global.hh"
#include "globals.hh" #include "globals.hh"
#include "profiles.hh" #include "profiles.hh"
#include "eval.hh" #include "eval.hh"

View file

@ -1,4 +1,5 @@
#include "users.hh" #include "users.hh"
#include "config-global.hh"
#include "globals.hh" #include "globals.hh"
#include "fetch-settings.hh" #include "fetch-settings.hh"
#include "flake.hh" #include "flake.hh"

View file

@ -1,4 +1,5 @@
#include "fetch-settings.hh" #include "fetch-settings.hh"
#include "config-global.hh"
namespace nix { namespace nix {

View file

@ -1,5 +1,6 @@
#include "common-args.hh" #include "common-args.hh"
#include "args/root.hh" #include "args/root.hh"
#include "config-global.hh"
#include "globals.hh" #include "globals.hh"
#include "logging.hh" #include "logging.hh"
#include "loggers.hh" #include "loggers.hh"

View file

@ -3,6 +3,7 @@
# include "hook-instance.hh" # include "hook-instance.hh"
#endif #endif
#include "processes.hh" #include "processes.hh"
#include "config-global.hh"
#include "worker.hh" #include "worker.hh"
#include "builtins.hh" #include "builtins.hh"
#include "builtins/buildenv.hh" #include "builtins/buildenv.hh"

View file

@ -1,5 +1,6 @@
#include "filetransfer.hh" #include "filetransfer.hh"
#include "globals.hh" #include "globals.hh"
#include "config-global.hh"
#include "store-api.hh" #include "store-api.hh"
#include "s3.hh" #include "s3.hh"
#include "compression.hh" #include "compression.hh"

View file

@ -1,4 +1,5 @@
#include "globals.hh" #include "globals.hh"
#include "config-global.hh"
#include "current-process.hh" #include "current-process.hh"
#include "archive.hh" #include "archive.hh"
#include "args.hh" #include "args.hh"
@ -123,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 &) { }
}; };
@ -136,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++) {
@ -145,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");
} }
} }
@ -437,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();

View file

@ -1,4 +1,5 @@
#include "globals.hh" #include "globals.hh"
#include "config-global.hh"
#include "hook-instance.hh" #include "hook-instance.hh"
#include "file-system.hh" #include "file-system.hh"
#include "child.hh" #include "child.hh"

View file

@ -1,5 +1,5 @@
#include "nix_api_util.h" #include "nix_api_util.h"
#include "config.hh" #include "config-global.hh"
#include "error.hh" #include "error.hh"
#include "nix_api_util_internal.h" #include "nix_api_util_internal.h"
#include "util.hh" #include "util.hh"

View file

@ -6,7 +6,7 @@
#include <strings.h> // for strcasecmp #include <strings.h> // for strcasecmp
#include "archive.hh" #include "archive.hh"
#include "config.hh" #include "config-global.hh"
#include "posix-source-accessor.hh" #include "posix-source-accessor.hh"
#include "source-path.hh" #include "source-path.hh"
#include "file-system.hh" #include "file-system.hh"

View file

@ -0,0 +1,67 @@
#include "config-global.hh"
namespace nix {
bool GlobalConfig::set(const std::string & name, const std::string & value)
{
for (auto & config : *configRegistrations)
if (config->set(name, value))
return true;
unknownSettings.emplace(name, value);
return false;
}
void GlobalConfig::getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly)
{
for (auto & config : *configRegistrations)
config->getSettings(res, overriddenOnly);
}
void GlobalConfig::resetOverridden()
{
for (auto & config : *configRegistrations)
config->resetOverridden();
}
nlohmann::json GlobalConfig::toJSON()
{
auto res = nlohmann::json::object();
for (const auto & config : *configRegistrations)
res.update(config->toJSON());
return res;
}
std::string GlobalConfig::toKeyValue()
{
std::string res;
std::map<std::string, Config::SettingInfo> settings;
globalConfig.getSettings(settings);
for (const auto & s : settings)
res += fmt("%s = %s\n", s.first, s.second.value);
return res;
}
void GlobalConfig::convertToArgs(Args & args, const std::string & category)
{
for (auto & config : *configRegistrations)
config->convertToArgs(args, category);
}
GlobalConfig globalConfig;
GlobalConfig::ConfigRegistrations * GlobalConfig::configRegistrations;
GlobalConfig::Register::Register(Config * config)
{
if (!configRegistrations)
configRegistrations = new ConfigRegistrations;
configRegistrations->emplace_back(config);
}
ExperimentalFeatureSettings experimentalFeatureSettings;
static GlobalConfig::Register rSettings(&experimentalFeatureSettings);
}

View file

@ -0,0 +1,33 @@
#pragma once
///@file
#include "config.hh"
namespace nix {
struct GlobalConfig : public AbstractConfig
{
typedef std::vector<Config *> ConfigRegistrations;
static ConfigRegistrations * configRegistrations;
bool set(const std::string & name, const std::string & value) override;
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
void resetOverridden() override;
nlohmann::json toJSON() override;
std::string toKeyValue() override;
void convertToArgs(Args & args, const std::string & category) override;
struct Register
{
Register(Config * config);
};
};
extern GlobalConfig globalConfig;
}

View file

@ -443,67 +443,6 @@ void OptionalPathSetting::operator =(const std::optional<Path> & v)
this->assign(v); this->assign(v);
} }
bool GlobalConfig::set(const std::string & name, const std::string & value)
{
for (auto & config : *configRegistrations)
if (config->set(name, value)) return true;
unknownSettings.emplace(name, value);
return false;
}
void GlobalConfig::getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly)
{
for (auto & config : *configRegistrations)
config->getSettings(res, overriddenOnly);
}
void GlobalConfig::resetOverridden()
{
for (auto & config : *configRegistrations)
config->resetOverridden();
}
nlohmann::json GlobalConfig::toJSON()
{
auto res = nlohmann::json::object();
for (const auto & config : *configRegistrations)
res.update(config->toJSON());
return res;
}
std::string GlobalConfig::toKeyValue()
{
std::string res;
std::map<std::string, Config::SettingInfo> settings;
globalConfig.getSettings(settings);
for (const auto & s : settings)
res += fmt("%s = %s\n", s.first, s.second.value);
return res;
}
void GlobalConfig::convertToArgs(Args & args, const std::string & category)
{
for (auto & config : *configRegistrations)
config->convertToArgs(args, category);
}
GlobalConfig globalConfig;
GlobalConfig::ConfigRegistrations * GlobalConfig::configRegistrations;
GlobalConfig::Register::Register(Config * config)
{
if (!configRegistrations)
configRegistrations = new ConfigRegistrations;
configRegistrations->emplace_back(config);
}
ExperimentalFeatureSettings experimentalFeatureSettings;
static GlobalConfig::Register rSettings(&experimentalFeatureSettings);
bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const
{ {
auto & f = experimentalFeatures.get(); auto & f = experimentalFeatures.get();

View file

@ -375,31 +375,6 @@ public:
void operator =(const std::optional<Path> & v); void operator =(const std::optional<Path> & v);
}; };
struct GlobalConfig : public AbstractConfig
{
typedef std::vector<Config*> ConfigRegistrations;
static ConfigRegistrations * configRegistrations;
bool set(const std::string & name, const std::string & value) override;
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
void resetOverridden() override;
nlohmann::json toJSON() override;
std::string toKeyValue() override;
void convertToArgs(Args & args, const std::string & category) override;
struct Register
{
Register(Config * config);
};
};
extern GlobalConfig globalConfig;
struct ExperimentalFeatureSettings : Config { struct ExperimentalFeatureSettings : Config {

View file

@ -1,7 +1,7 @@
#include <fcntl.h> #include <fcntl.h>
#include "error.hh" #include "error.hh"
#include "config.hh" #include "config-global.hh"
#include "fs-sink.hh" #include "fs-sink.hh"
#if _WIN32 #if _WIN32

View file

@ -3,7 +3,7 @@
#include "environment-variables.hh" #include "environment-variables.hh"
#include "terminal.hh" #include "terminal.hh"
#include "util.hh" #include "util.hh"
#include "config.hh" #include "config-global.hh"
#include "source-path.hh" #include "source-path.hh"
#include "position.hh" #include "position.hh"

View file

@ -2,6 +2,7 @@
#include "common-args.hh" #include "common-args.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "config-global.hh"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>

View file

@ -1,3 +1,4 @@
#include "config-global.hh"
#include "eval.hh" #include "eval.hh"
#include "installable-flake.hh" #include "installable-flake.hh"
#include "command-installable-value.hh" #include "command-installable-value.hh"

View file

@ -10,6 +10,7 @@
#include "serialise.hh" #include "serialise.hh"
#include "archive.hh" #include "archive.hh"
#include "globals.hh" #include "globals.hh"
#include "config-global.hh"
#include "derivations.hh" #include "derivations.hh"
#include "finally.hh" #include "finally.hh"
#include "legacy.hh" #include "legacy.hh"

View file

@ -1,4 +1,4 @@
#include "config.hh" #include "config-global.hh"
#include "primops.hh" #include "primops.hh"
using namespace nix; using namespace nix;

View file

@ -1,4 +1,4 @@
#include "config.hh" #include "config-global.hh"
#include "args.hh" #include "args.hh"
#include "nix_api_util.h" #include "nix_api_util.h"
#include "nix_api_util_internal.h" #include "nix_api_util_internal.h"