Skip to content

Commit

Permalink
Mark experimental features on settings
Browse files Browse the repository at this point in the history
We hide them in various ways if the experimental feature isn't enabled.

To do this, we had to move the experimental features list out of
libnixstore, because the setting machinary itself depends on it. To do
that, we made a new `ExperimentalFeatureSettings`.
  • Loading branch information
Ericson2314 committed Mar 20, 2023
1 parent 296831f commit aa663b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
12 changes: 8 additions & 4 deletions src/libfetchers/fetch-settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,25 @@ struct FetchSettings : public Config
Path or URI of the global flake registry.
When empty, disables the global flake registry.
)"};
)",
{}, true, Xp::Flakes};


Setting<bool> useRegistries{this, true, "use-registries",
"Whether to use flake registries to resolve flake references."};
"Whether to use flake registries to resolve flake references.",
{}, true, Xp::Flakes};

Setting<bool> acceptFlakeConfig{this, false, "accept-flake-config",
"Whether to accept nix configuration from a flake without prompting."};
"Whether to accept nix configuration from a flake without prompting.",
{}, true, Xp::Flakes};

Setting<std::string> commitLockFileSummary{
this, "", "commit-lockfile-summary",
R"(
The commit summary to use when committing changed flake lock files. If
empty, the summary is generated based on the action performed.
)"};
)",
{}, true, Xp::Flakes};
};

// FIXME: don't use a global variable.
Expand Down
25 changes: 17 additions & 8 deletions src/libutil/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ void AbstractConfig::reapplyUnknownSettings()
set(s.first, s.second);
}

// Whether we should process the option. Excludes aliases, which are handled elsewhere, and disabled features.
static bool applicable(const Config::SettingData & sd)
{
return !sd.isAlias
&& experimentalFeatureSettings.isEnabled(sd.setting->experimentalFeature);
}

void Config::getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly)
{
for (auto & opt : _settings)
if (!opt.second.isAlias && (!overriddenOnly || opt.second.setting->overridden))
if (applicable(opt.second) && (!overriddenOnly || opt.second.setting->overridden))
res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description});
}

Expand Down Expand Up @@ -147,34 +154,36 @@ nlohmann::json Config::toJSON()
{
auto res = nlohmann::json::object();
for (auto & s : _settings)
if (!s.second.isAlias) {
if (applicable(s.second))
res.emplace(s.first, s.second.setting->toJSON());
}
return res;
}

std::string Config::toKeyValue()
{
auto res = std::string();
for (auto & s : _settings)
if (!s.second.isAlias) {
if (applicable(s.second))
res += fmt("%s = %s\n", s.first, s.second.setting->to_string());
}
return res;
}

void Config::convertToArgs(Args & args, const std::string & category)
{
for (auto & s : _settings)
if (!s.second.isAlias)
if (applicable(s.second))
s.second.setting->convertToArg(args, category);
}

AbstractSetting::AbstractSetting(
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases)
: name(name), description(stripIndentation(description)), aliases(aliases)
const std::set<std::string> & aliases,
std::optional<ExperimentalFeature> experimentalFeature)
: name(name)
, description(stripIndentation(description))
, aliases(aliases)
, experimentalFeature(experimentalFeature)
{
}

Expand Down
15 changes: 10 additions & 5 deletions src/libutil/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ public:

bool overridden = false;

std::optional<ExperimentalFeature> experimentalFeature;

protected:

AbstractSetting(
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases);
const std::set<std::string> & aliases,
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);

virtual ~AbstractSetting()
{
Expand Down Expand Up @@ -241,8 +244,9 @@ public:
const bool documentDefault,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: AbstractSetting(name, description, aliases)
const std::set<std::string> & aliases = {},
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
: AbstractSetting(name, description, aliases, experimentalFeature)
, value(def)
, defaultValue(def)
, documentDefault(documentDefault)
Expand Down Expand Up @@ -297,8 +301,9 @@ public:
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {},
const bool documentDefault = true)
: BaseSetting<T>(def, documentDefault, name, description, aliases)
const bool documentDefault = true,
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
: BaseSetting<T>(def, documentDefault, name, description, aliases, experimentalFeature)
{
options->addSetting(this);
}
Expand Down

0 comments on commit aa663b7

Please sign in to comment.