2023-03-17 14:33:48 +00:00
|
|
|
#pragma once
|
2023-04-01 03:18:41 +00:00
|
|
|
///@file
|
2023-03-17 14:33:48 +00:00
|
|
|
|
2020-06-16 14:19:49 +00:00
|
|
|
#include <cassert>
|
2017-04-13 13:55:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2020-09-16 07:06:35 +00:00
|
|
|
#include <nlohmann/json_fwd.hpp>
|
2020-08-20 09:02:16 +00:00
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
#include "types.hh"
|
|
|
|
#include "experimental-features.hh"
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* The Config class provides Nix runtime configurations.
|
|
|
|
*
|
|
|
|
* What is a Configuration?
|
|
|
|
* A collection of uniquely named Settings.
|
|
|
|
*
|
|
|
|
* What is a Setting?
|
|
|
|
* Each property that you can set in a configuration corresponds to a
|
|
|
|
* `Setting`. A setting records value and description of a property
|
|
|
|
* with a default and optional aliases.
|
|
|
|
*
|
|
|
|
* A valid configuration consists of settings that are registered to a
|
|
|
|
* `Config` object instance:
|
|
|
|
*
|
|
|
|
* Config config;
|
|
|
|
* Setting<std::string> systemSetting{&config, "x86_64-linux", "system", "the current system"};
|
|
|
|
*
|
|
|
|
* The above creates a `Config` object and registers a setting called "system"
|
|
|
|
* via the variable `systemSetting` with it. The setting defaults to the string
|
|
|
|
* "x86_64-linux", it's description is "the current system". All of the
|
|
|
|
* registered settings can then be accessed as shown below:
|
|
|
|
*
|
|
|
|
* std::map<std::string, Config::SettingInfo> settings;
|
|
|
|
* config.getSettings(settings);
|
|
|
|
* config["system"].description == "the current system"
|
|
|
|
* config["system"].value == "x86_64-linux"
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The above retrieves all currently known settings from the `Config` object
|
|
|
|
* and adds them to the `settings` map.
|
|
|
|
*/
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
class Args;
|
|
|
|
class AbstractSetting;
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
class AbstractConfig
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
StringMap unknownSettings;
|
|
|
|
|
|
|
|
AbstractConfig(const StringMap & initials = {})
|
|
|
|
: unknownSettings(initials)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Sets the value referenced by `name` to `value`. Returns true if the
|
|
|
|
* setting is known, false otherwise.
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual bool set(const std::string & name, const std::string & value) = 0;
|
|
|
|
|
|
|
|
struct SettingInfo
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
std::string description;
|
|
|
|
};
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Adds the currently known settings to the given result map `res`.
|
|
|
|
* - res: map to store settings in
|
2021-03-26 15:14:38 +00:00
|
|
|
* - overriddenOnly: when set to true only overridden settings will be added to `res`
|
2020-05-27 14:37:24 +00:00
|
|
|
*/
|
2021-03-26 15:14:38 +00:00
|
|
|
virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) = 0;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Parses the configuration in `contents` and applies it
|
|
|
|
* - contents: configuration contents to be parsed and applied
|
|
|
|
* - path: location of the configuration file
|
|
|
|
*/
|
2020-05-27 12:39:02 +00:00
|
|
|
void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
|
2020-05-27 14:37:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a nix configuration file
|
|
|
|
* - path: the location of the config file to apply
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void applyConfigFile(const Path & path);
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Resets the `overridden` flag of all Settings
|
|
|
|
*/
|
2021-03-26 15:14:38 +00:00
|
|
|
virtual void resetOverridden() = 0;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Outputs all settings to JSON
|
|
|
|
* - out: JSONObject to write the configuration to
|
|
|
|
*/
|
2020-08-20 09:02:16 +00:00
|
|
|
virtual nlohmann::json toJSON() = 0;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2021-07-15 16:17:18 +00:00
|
|
|
/**
|
|
|
|
* Outputs all settings in a key-value pair format suitable to be used as
|
|
|
|
* `nix.conf`
|
|
|
|
*/
|
|
|
|
virtual std::string toKeyValue() = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Converts settings to `Args` to be used on the command line interface
|
|
|
|
* - args: args to write to
|
|
|
|
* - category: category of the settings
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
virtual void convertToArgs(Args & args, const std::string & category) = 0;
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Logs a warning for each unregistered setting
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void warnUnknownSettings();
|
|
|
|
|
2020-05-27 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* Re-applies all previously attempted changes to unknown settings
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
void reapplyUnknownSettings();
|
|
|
|
};
|
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* A class to simplify providing configuration settings. The typical
|
|
|
|
* use is to inherit Config and add Setting<T> members:
|
|
|
|
*
|
|
|
|
* class MyClass : private Config
|
|
|
|
* {
|
|
|
|
* Setting<int> foo{this, 123, "foo", "the number of foos to use"};
|
|
|
|
* Setting<std::string> bar{this, "blabla", "bar", "the name of the bar"};
|
|
|
|
*
|
|
|
|
* MyClass() : Config(readConfigFile("/etc/my-app.conf"))
|
|
|
|
* {
|
|
|
|
* std::cout << foo << "\n"; // will print 123 unless overridden
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*/
|
2018-03-27 16:41:31 +00:00
|
|
|
class Config : public AbstractConfig
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
friend class AbstractSetting;
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
public:
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
struct SettingData
|
|
|
|
{
|
2017-05-03 14:08:48 +00:00
|
|
|
bool isAlias;
|
2017-04-13 13:55:38 +00:00
|
|
|
AbstractSetting * setting;
|
2017-05-03 14:08:48 +00:00
|
|
|
SettingData(bool isAlias, AbstractSetting * setting)
|
|
|
|
: isAlias(isAlias), setting(setting)
|
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
typedef std::map<std::string, SettingData> Settings;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Settings _settings;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
Config(const StringMap & initials = {})
|
|
|
|
: AbstractConfig(initials)
|
2017-04-13 13:55:38 +00:00
|
|
|
{ }
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
bool set(const std::string & name, const std::string & value) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
void addSetting(AbstractSetting * setting);
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
void resetOverridden() override;
|
2017-04-20 12:58:16 +00:00
|
|
|
|
2020-08-20 09:02:16 +00:00
|
|
|
nlohmann::json toJSON() override;
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2021-07-15 16:17:18 +00:00
|
|
|
std::string toKeyValue() override;
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractSetting
|
|
|
|
{
|
|
|
|
friend class Config;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
const std::string description;
|
|
|
|
const std::set<std::string> aliases;
|
|
|
|
|
|
|
|
int created = 123;
|
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
bool overridden = false;
|
2017-04-20 12:58:16 +00:00
|
|
|
|
2023-01-16 22:51:04 +00:00
|
|
|
std::optional<ExperimentalFeature> experimentalFeature;
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
AbstractSetting(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
2023-01-16 22:51:04 +00:00
|
|
|
const std::set<std::string> & aliases,
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
virtual ~AbstractSetting()
|
|
|
|
{
|
|
|
|
// Check against a gcc miscompilation causing our constructor
|
2017-04-20 12:58:16 +00:00
|
|
|
// not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
|
2017-04-13 13:55:38 +00:00
|
|
|
assert(created == 123);
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:17:39 +00:00
|
|
|
virtual void set(const std::string & value, bool append = false) = 0;
|
|
|
|
|
|
|
|
virtual bool isAppendable()
|
|
|
|
{ return false; }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
virtual std::string to_string() const = 0;
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2020-09-09 09:35:33 +00:00
|
|
|
nlohmann::json toJSON();
|
|
|
|
|
|
|
|
virtual std::map<std::string, nlohmann::json> toJSONObject();
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2017-06-07 16:41:20 +00:00
|
|
|
virtual void convertToArg(Args & args, const std::string & category);
|
2017-06-07 14:17:17 +00:00
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
bool isOverridden() const { return overridden; }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* A setting of type T.
|
|
|
|
*/
|
2017-04-20 14:52:53 +00:00
|
|
|
template<typename T>
|
|
|
|
class BaseSetting : public AbstractSetting
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
T value;
|
2020-09-09 09:36:48 +00:00
|
|
|
const T defaultValue;
|
2021-12-01 15:08:23 +00:00
|
|
|
const bool documentDefault;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
BaseSetting(const T & def,
|
2021-12-01 15:08:23 +00:00
|
|
|
const bool documentDefault,
|
2017-04-13 13:55:38 +00:00
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
2023-01-16 22:51:04 +00:00
|
|
|
const std::set<std::string> & aliases = {},
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
|
|
|
|
: AbstractSetting(name, description, aliases, experimentalFeature)
|
2017-04-13 13:55:38 +00:00
|
|
|
, value(def)
|
2020-09-09 09:36:48 +00:00
|
|
|
, defaultValue(def)
|
2021-12-01 15:08:23 +00:00
|
|
|
, documentDefault(documentDefault)
|
2017-04-20 14:52:53 +00:00
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
operator const T &() const { return value; }
|
2017-04-13 18:53:23 +00:00
|
|
|
operator T &() { return value; }
|
|
|
|
const T & get() const { return value; }
|
2022-10-22 13:25:35 +00:00
|
|
|
template<typename U>
|
|
|
|
bool operator ==(const U & v2) const { return value == v2; }
|
|
|
|
template<typename U>
|
|
|
|
bool operator !=(const U & v2) const { return value != v2; }
|
|
|
|
template<typename U>
|
|
|
|
void operator =(const U & v) { assign(v); }
|
2017-04-20 14:52:53 +00:00
|
|
|
virtual void assign(const T & v) { value = v; }
|
2022-10-22 13:25:35 +00:00
|
|
|
template<typename U>
|
|
|
|
void setDefault(const U & v) { if (!overridden) value = v; }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2020-10-29 17:17:39 +00:00
|
|
|
void set(const std::string & str, bool append = false) override;
|
|
|
|
|
|
|
|
bool isAppendable() override;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2017-11-21 17:50:56 +00:00
|
|
|
virtual void override(const T & v)
|
|
|
|
{
|
2021-03-26 15:14:38 +00:00
|
|
|
overridden = true;
|
2017-11-21 17:50:56 +00:00
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:17:53 +00:00
|
|
|
std::string to_string() const override;
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2017-06-07 16:41:20 +00:00
|
|
|
void convertToArg(Args & args, const std::string & category) override;
|
2017-06-07 14:17:17 +00:00
|
|
|
|
2020-09-16 07:06:35 +00:00
|
|
|
std::map<std::string, nlohmann::json> toJSONObject() override;
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
str << (const T &) opt;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
bool operator ==(const T & v1, const BaseSetting<T> & v2) { return v1 == (const T &) v2; }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Setting : public BaseSetting<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Setting(Config * options,
|
|
|
|
const T & def,
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
2021-12-01 15:08:23 +00:00
|
|
|
const std::set<std::string> & aliases = {},
|
2023-01-16 22:51:04 +00:00
|
|
|
const bool documentDefault = true,
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
|
|
|
|
: BaseSetting<T>(def, documentDefault, name, description, aliases, experimentalFeature)
|
2017-04-20 14:52:53 +00:00
|
|
|
{
|
|
|
|
options->addSetting(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator =(const T & v) { this->assign(v); }
|
|
|
|
};
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* A special setting for Paths. These are automatically canonicalised
|
|
|
|
* (e.g. "/foo//bar/" becomes "/foo/bar").
|
|
|
|
*/
|
2017-04-20 14:52:53 +00:00
|
|
|
class PathSetting : public BaseSetting<Path>
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
bool allowEmpty;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
PathSetting(Config * options,
|
|
|
|
bool allowEmpty,
|
|
|
|
const Path & def,
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases = {})
|
2021-12-01 15:08:23 +00:00
|
|
|
: BaseSetting<Path>(def, true, name, description, aliases)
|
2017-04-13 13:55:38 +00:00
|
|
|
, allowEmpty(allowEmpty)
|
|
|
|
{
|
2017-04-20 14:52:53 +00:00
|
|
|
options->addSetting(this);
|
2017-04-13 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 17:17:39 +00:00
|
|
|
void set(const std::string & str, bool append = false) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
Path operator +(const char * p) const { return value + p; }
|
2017-04-20 14:52:53 +00:00
|
|
|
|
|
|
|
void operator =(const Path & v) { this->assign(v); }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
struct GlobalConfig : public AbstractConfig
|
|
|
|
{
|
|
|
|
typedef std::vector<Config*> ConfigRegistrations;
|
|
|
|
static ConfigRegistrations * configRegistrations;
|
|
|
|
|
|
|
|
bool set(const std::string & name, const std::string & value) override;
|
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2021-03-26 15:14:38 +00:00
|
|
|
void resetOverridden() override;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2020-08-20 09:02:16 +00:00
|
|
|
nlohmann::json toJSON() override;
|
2018-03-27 16:41:31 +00:00
|
|
|
|
2021-07-15 16:17:18 +00:00
|
|
|
std::string toKeyValue() override;
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
|
|
|
|
|
|
|
struct Register
|
|
|
|
{
|
|
|
|
Register(Config * config);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extern GlobalConfig globalConfig;
|
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
struct ExperimentalFeatureSettings : Config {
|
|
|
|
|
|
|
|
Setting<std::set<ExperimentalFeature>> experimentalFeatures{this, {}, "experimental-features",
|
|
|
|
"Experimental Nix features to enable."};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given experimental feature is enabled.
|
|
|
|
*/
|
|
|
|
bool isEnabled(const ExperimentalFeature &) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Require an experimental feature be enabled, throwing an error if it is
|
|
|
|
* not.
|
|
|
|
*/
|
|
|
|
void require(const ExperimentalFeature &) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `std::nullopt` pointer means no feature, which means there is nothing that could be
|
|
|
|
* disabled, and so the function returns true in that case.
|
|
|
|
*/
|
|
|
|
bool isEnabled(const std::optional<ExperimentalFeature> &) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `std::nullopt` pointer means no feature, which means there is nothing that could be
|
|
|
|
* disabled, and so the function does nothing in that case.
|
|
|
|
*/
|
|
|
|
void require(const std::optional<ExperimentalFeature> &) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: don't use a global variable.
|
|
|
|
extern ExperimentalFeatureSettings experimentalFeatureSettings;
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
}
|