diff options
author | eldritch horrors <pennae@lix.systems> | 2024-03-04 06:22:47 +0100 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-03-04 07:11:25 +0100 |
commit | 4018fcb9b8d622e7bac1310a689c90dac9109137 (patch) | |
tree | a1f1571a076242962b07449234b71b4c1e7c12d8 | |
parent | a2d5e803cf16e048f30f0334114759f81f6c5d20 (diff) |
Merge pull request #9233 from bouk/bouk/apply-config-inner
config: add included files into parsedContents before applying
(cherry picked from commit 82359eba6b692691ef08a71196ef25a61bc4d3d3)
Change-Id: Idde3177010fec7b8bafe6088c3c23d5caf491845
-rw-r--r-- | src/libstore/globals.cc | 11 | ||||
-rw-r--r-- | src/libutil/config.cc | 26 | ||||
-rw-r--r-- | src/libutil/config.hh | 6 | ||||
-rwxr-xr-x | tests/functional/init.sh | 3 |
4 files changed, 25 insertions, 21 deletions
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 9c25d9868..0aecd2b6a 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -111,7 +111,14 @@ Settings::Settings() 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 ~/.nix/nix.conf or the command line. */ @@ -119,7 +126,7 @@ void loadConfFile() auto files = settings.nixUserConfFiles; for (auto file = files.rbegin(); file != files.rend(); file++) { - globalConfig.applyConfigFile(*file); + applyConfigFile(*file); } auto nixConfEnv = getEnv("NIX_CONFIG"); diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 5748a93d7..2535b410f 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -86,10 +86,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}); } -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()) { std::string line; @@ -120,7 +119,12 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string throw UsageError("illegal configuration line '%1%' in '%2%'", line, path); auto p = absPath(tokens[1], dirOf(path)); 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) { throw Error("file '%1%' included from '%2%' not found", p, path); } @@ -140,6 +144,12 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string 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 for (const auto & [name, value] : parsedContents) @@ -152,14 +162,6 @@ void AbstractConfig::applyConfig(const std::string & contents, const std::string set(name, value); } -void AbstractConfig::applyConfigFile(const Path & path) -{ - try { - std::string contents = readFile(path); - applyConfig(contents, path); - } catch (SysError &) { } -} - void Config::resetOverridden() { for (auto & s : _settings) diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 082119dbe..3e232d224 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -83,12 +83,6 @@ public: 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 */ virtual void resetOverridden() = 0; diff --git a/tests/functional/init.sh b/tests/functional/init.sh index c420e8c9f..d697b1a30 100755 --- a/tests/functional/init.sh +++ b/tests/functional/init.sh @@ -20,7 +20,7 @@ cat > "$NIX_CONF_DIR"/nix.conf <<EOF build-users-group = keep-derivations = false sandbox = false -experimental-features = nix-command flakes +experimental-features = nix-command gc-reserved-space = 0 substituters = flake-registry = $TEST_ROOT/registry.json @@ -31,6 +31,7 @@ EOF cat > "$NIX_CONF_DIR"/nix.conf.extra <<EOF fsync-metadata = false +extra-experimental-features = flakes !include nix.conf.extra.not-there EOF |