Merge pull request #9233 from bouk/bouk/apply-config-inner

config: add included files into parsedContents before applying
This commit is contained in:
John Ericson 2023-12-01 08:23:32 -05:00 committed by GitHub
commit 82359eba6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 21 deletions

View file

@ -116,7 +116,14 @@ Settings::Settings()
void loadConfFile() void loadConfFile()
{ {
globalConfig.applyConfigFile(settings.nixConfDir + "/nix.conf"); auto applyConfigFile = [&](const Path & path) {
try {
std::string contents = readFile(path);
globalConfig.applyConfig(contents, path);
} catch (SysError &) { }
};
applyConfigFile(settings.nixConfDir + "/nix.conf");
/* 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. */
@ -124,7 +131,7 @@ void loadConfFile()
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++) {
globalConfig.applyConfigFile(*file); applyConfigFile(*file);
} }
auto nixConfEnv = getEnv("NIX_CONFIG"); auto nixConfEnv = getEnv("NIX_CONFIG");

View file

@ -88,10 +88,9 @@ void Config::getSettings(std::map<std::string, SettingInfo> & res, bool overridd
res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description}); res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description});
} }
void AbstractConfig::applyConfig(const std::string & contents, const std::string & path) {
unsigned int pos = 0;
std::vector<std::pair<std::string, std::string>> parsedContents; static void applyConfigInner(const std::string & contents, const std::string & path, std::vector<std::pair<std::string, std::string>> & parsedContents) {
unsigned int pos = 0;
while (pos < contents.size()) { while (pos < contents.size()) {
std::string line; std::string line;
@ -122,7 +121,12 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path); throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
auto p = absPath(tokens[1], dirOf(path)); auto p = absPath(tokens[1], dirOf(path));
if (pathExists(p)) { if (pathExists(p)) {
applyConfigFile(p); try {
std::string includedContents = readFile(path);
applyConfigInner(includedContents, p, parsedContents);
} catch (SysError &) {
// TODO: Do we actually want to ignore this? Or is it better to fail?
}
} else if (!ignoreMissing) { } else if (!ignoreMissing) {
throw Error("file '%1%' included from '%2%' not found", p, path); throw Error("file '%1%' included from '%2%' not found", p, path);
} }
@ -142,6 +146,12 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string
concatStringsSep(" ", Strings(i, tokens.end())), concatStringsSep(" ", Strings(i, tokens.end())),
}); });
}; };
}
void AbstractConfig::applyConfig(const std::string & contents, const std::string & path) {
std::vector<std::pair<std::string, std::string>> parsedContents;
applyConfigInner(contents, path, parsedContents);
// First apply experimental-feature related settings // First apply experimental-feature related settings
for (const auto & [name, value] : parsedContents) for (const auto & [name, value] : parsedContents)
@ -154,14 +164,6 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string
set(name, value); set(name, value);
} }
void AbstractConfig::applyConfigFile(const Path & path)
{
try {
std::string contents = readFile(path);
applyConfig(contents, path);
} catch (SysError &) { }
}
void Config::resetOverridden() void Config::resetOverridden()
{ {
for (auto & s : _settings) for (auto & s : _settings)

View file

@ -82,12 +82,6 @@ public:
*/ */
void applyConfig(const std::string & contents, const std::string & path = "<unknown>"); void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
/**
* Applies a nix configuration file
* - path: the location of the config file to apply
*/
void applyConfigFile(const Path & path);
/** /**
* Resets the `overridden` flag of all Settings * Resets the `overridden` flag of all Settings
*/ */

View file

@ -20,7 +20,7 @@ cat > "$NIX_CONF_DIR"/nix.conf <<EOF
build-users-group = build-users-group =
keep-derivations = false keep-derivations = false
sandbox = false sandbox = false
experimental-features = nix-command flakes experimental-features = nix-command
gc-reserved-space = 0 gc-reserved-space = 0
substituters = substituters =
flake-registry = $TEST_ROOT/registry.json flake-registry = $TEST_ROOT/registry.json
@ -31,6 +31,7 @@ EOF
cat > "$NIX_CONF_DIR"/nix.conf.extra <<EOF cat > "$NIX_CONF_DIR"/nix.conf.extra <<EOF
fsync-metadata = false fsync-metadata = false
extra-experimental-features = flakes
!include nix.conf.extra.not-there !include nix.conf.extra.not-there
EOF EOF