2023-03-17 16:33:48 +02:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2023-03-17 16:33:48 +02:00
|
|
|
|
2020-06-16 17:19:49 +03:00
|
|
|
#include <cassert>
|
2017-04-13 16:55:38 +03:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2020-09-16 10:06:35 +03:00
|
|
|
#include <nlohmann/json_fwd.hpp>
|
2020-08-20 12:02:16 +03:00
|
|
|
|
2023-03-17 16:33:48 +02:00
|
|
|
#include "types.hh"
|
|
|
|
#include "experimental-features.hh"
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-27 17:37:24 +03: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 16:55:38 +03:00
|
|
|
class Args;
|
|
|
|
class AbstractSetting;
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
class AbstractConfig
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
StringMap unknownSettings;
|
|
|
|
|
|
|
|
AbstractConfig(const StringMap & initials = {})
|
|
|
|
: unknownSettings(initials)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Sets the value referenced by `name` to `value`. Returns true if the
|
|
|
|
* setting is known, false otherwise.
|
|
|
|
*/
|
2018-03-27 19:41:31 +03:00
|
|
|
virtual bool set(const std::string & name, const std::string & value) = 0;
|
|
|
|
|
|
|
|
struct SettingInfo
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
std::string description;
|
|
|
|
};
|
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Adds the currently known settings to the given result map `res`.
|
|
|
|
* - res: map to store settings in
|
2021-03-26 17:14:38 +02:00
|
|
|
* - overriddenOnly: when set to true only overridden settings will be added to `res`
|
2020-05-27 17:37:24 +03:00
|
|
|
*/
|
2021-03-26 17:14:38 +02:00
|
|
|
virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) = 0;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2020-05-27 17:37:24 +03: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 15:39:02 +03:00
|
|
|
void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
|
2020-05-27 17:37:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a nix configuration file
|
|
|
|
* - path: the location of the config file to apply
|
|
|
|
*/
|
2018-03-27 19:41:31 +03:00
|
|
|
void applyConfigFile(const Path & path);
|
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Resets the `overridden` flag of all Settings
|
|
|
|
*/
|
2021-03-26 17:14:38 +02:00
|
|
|
virtual void resetOverridden() = 0;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Outputs all settings to JSON
|
|
|
|
* - out: JSONObject to write the configuration to
|
|
|
|
*/
|
2020-08-20 12:02:16 +03:00
|
|
|
virtual nlohmann::json toJSON() = 0;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2021-07-15 19:17:18 +03: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 17:37:24 +03: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 19:41:31 +03:00
|
|
|
virtual void convertToArgs(Args & args, const std::string & category) = 0;
|
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Logs a warning for each unregistered setting
|
|
|
|
*/
|
2018-03-27 19:41:31 +03:00
|
|
|
void warnUnknownSettings();
|
|
|
|
|
2020-05-27 17:37:24 +03:00
|
|
|
/**
|
|
|
|
* Re-applies all previously attempted changes to unknown settings
|
|
|
|
*/
|
2018-03-27 19:41:31 +03:00
|
|
|
void reapplyUnknownSettings();
|
|
|
|
};
|
|
|
|
|
2023-03-27 04:12:25 +03: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 19:41:31 +03:00
|
|
|
class Config : public AbstractConfig
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
friend class AbstractSetting;
|
|
|
|
|
2017-06-07 17:49:54 +03:00
|
|
|
public:
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
struct SettingData
|
|
|
|
{
|
2017-05-03 17:08:48 +03:00
|
|
|
bool isAlias;
|
2017-04-13 16:55:38 +03:00
|
|
|
AbstractSetting * setting;
|
2017-05-03 17:08:48 +03:00
|
|
|
SettingData(bool isAlias, AbstractSetting * setting)
|
|
|
|
: isAlias(isAlias), setting(setting)
|
|
|
|
{ }
|
2017-04-13 16:55:38 +03:00
|
|
|
};
|
|
|
|
|
2017-06-07 17:49:54 +03:00
|
|
|
typedef std::map<std::string, SettingData> Settings;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Settings _settings;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
Config(const StringMap & initials = {})
|
|
|
|
: AbstractConfig(initials)
|
2017-04-13 16:55:38 +03:00
|
|
|
{ }
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
bool set(const std::string & name, const std::string & value) override;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
void addSetting(AbstractSetting * setting);
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2021-03-26 17:14:38 +02:00
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
|
2017-04-13 21:53:23 +03:00
|
|
|
|
2021-03-26 17:14:38 +02:00
|
|
|
void resetOverridden() override;
|
2017-04-20 15:58:16 +03:00
|
|
|
|
2020-08-20 12:02:16 +03:00
|
|
|
nlohmann::json toJSON() override;
|
2017-04-20 18:34:47 +03:00
|
|
|
|
2021-07-15 19:17:18 +03:00
|
|
|
std::string toKeyValue() override;
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
2017-04-13 16:55:38 +03: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 17:14:38 +02:00
|
|
|
bool overridden = false;
|
2017-04-20 15:58:16 +03:00
|
|
|
|
2023-01-17 00:51:04 +02:00
|
|
|
std::optional<ExperimentalFeature> experimentalFeature;
|
|
|
|
|
2017-04-13 16:55:38 +03:00
|
|
|
protected:
|
|
|
|
|
|
|
|
AbstractSetting(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
2023-01-17 00:51:04 +02:00
|
|
|
const std::set<std::string> & aliases,
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
virtual ~AbstractSetting()
|
|
|
|
{
|
|
|
|
// Check against a gcc miscompilation causing our constructor
|
2017-04-20 15:58:16 +03:00
|
|
|
// not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
|
2017-04-13 16:55:38 +03:00
|
|
|
assert(created == 123);
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:17:39 +02:00
|
|
|
virtual void set(const std::string & value, bool append = false) = 0;
|
|
|
|
|
|
|
|
virtual bool isAppendable()
|
|
|
|
{ return false; }
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
virtual std::string to_string() const = 0;
|
2017-04-13 21:53:23 +03:00
|
|
|
|
2020-09-09 12:35:33 +03:00
|
|
|
nlohmann::json toJSON();
|
|
|
|
|
|
|
|
virtual std::map<std::string, nlohmann::json> toJSONObject();
|
2017-04-20 18:34:47 +03:00
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
virtual void convertToArg(Args & args, const std::string & category);
|
2017-06-07 17:17:17 +03:00
|
|
|
|
2021-03-26 17:14:38 +02:00
|
|
|
bool isOverridden() const { return overridden; }
|
2017-04-13 16:55:38 +03:00
|
|
|
};
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* A setting of type T.
|
|
|
|
*/
|
2017-04-20 17:52:53 +03:00
|
|
|
template<typename T>
|
|
|
|
class BaseSetting : public AbstractSetting
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
T value;
|
2020-09-09 12:36:48 +03:00
|
|
|
const T defaultValue;
|
2021-12-01 17:08:23 +02:00
|
|
|
const bool documentDefault;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-04-20 17:52:53 +03:00
|
|
|
BaseSetting(const T & def,
|
2021-12-01 17:08:23 +02:00
|
|
|
const bool documentDefault,
|
2017-04-13 16:55:38 +03:00
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
2023-01-17 00:51:04 +02:00
|
|
|
const std::set<std::string> & aliases = {},
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
|
|
|
|
: AbstractSetting(name, description, aliases, experimentalFeature)
|
2017-04-13 16:55:38 +03:00
|
|
|
, value(def)
|
2020-09-09 12:36:48 +03:00
|
|
|
, defaultValue(def)
|
2021-12-01 17:08:23 +02:00
|
|
|
, documentDefault(documentDefault)
|
2017-04-20 17:52:53 +03:00
|
|
|
{ }
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
operator const T &() const { return value; }
|
2017-04-13 21:53:23 +03:00
|
|
|
operator T &() { return value; }
|
|
|
|
const T & get() const { return value; }
|
2022-10-22 16:25:35 +03: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 17:52:53 +03:00
|
|
|
virtual void assign(const T & v) { value = v; }
|
2022-10-22 16:25:35 +03:00
|
|
|
template<typename U>
|
|
|
|
void setDefault(const U & v) { if (!overridden) value = v; }
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2020-10-29 19:17:39 +02:00
|
|
|
void set(const std::string & str, bool append = false) override;
|
|
|
|
|
|
|
|
bool isAppendable() override;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
2017-11-21 19:50:56 +02:00
|
|
|
virtual void override(const T & v)
|
|
|
|
{
|
2021-03-26 17:14:38 +02:00
|
|
|
overridden = true;
|
2017-11-21 19:50:56 +02:00
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:17:53 +02:00
|
|
|
std::string to_string() const override;
|
2017-04-20 18:34:47 +03:00
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
void convertToArg(Args & args, const std::string & category) override;
|
2017-06-07 17:17:17 +03:00
|
|
|
|
2020-09-16 10:06:35 +03:00
|
|
|
std::map<std::string, nlohmann::json> toJSONObject() override;
|
2017-04-13 16:55:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-04-20 17:52:53 +03:00
|
|
|
std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
|
2017-04-13 16:55:38 +03:00
|
|
|
{
|
|
|
|
str << (const T &) opt;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-13 21:53:23 +03:00
|
|
|
template<typename T>
|
2017-04-20 17:52:53 +03: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 17:08:23 +02:00
|
|
|
const std::set<std::string> & aliases = {},
|
2023-01-17 00:51:04 +02:00
|
|
|
const bool documentDefault = true,
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
|
|
|
|
: BaseSetting<T>(def, documentDefault, name, description, aliases, experimentalFeature)
|
2017-04-20 17:52:53 +03:00
|
|
|
{
|
|
|
|
options->addSetting(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator =(const T & v) { this->assign(v); }
|
|
|
|
};
|
2017-04-13 21:53:23 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* A special setting for Paths. These are automatically canonicalised
|
|
|
|
* (e.g. "/foo//bar/" becomes "/foo/bar").
|
|
|
|
*/
|
2017-04-20 17:52:53 +03:00
|
|
|
class PathSetting : public BaseSetting<Path>
|
2017-04-13 16:55:38 +03: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 17:08:23 +02:00
|
|
|
: BaseSetting<Path>(def, true, name, description, aliases)
|
2017-04-13 16:55:38 +03:00
|
|
|
, allowEmpty(allowEmpty)
|
|
|
|
{
|
2017-04-20 17:52:53 +03:00
|
|
|
options->addSetting(this);
|
2017-04-13 16:55:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:17:39 +02:00
|
|
|
void set(const std::string & str, bool append = false) override;
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
|
|
Path operator +(const char * p) const { return value + p; }
|
2017-04-20 17:52:53 +03:00
|
|
|
|
|
|
|
void operator =(const Path & v) { this->assign(v); }
|
2017-04-13 16:55:38 +03:00
|
|
|
};
|
|
|
|
|
2018-03-27 19:41:31 +03: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 17:14:38 +02:00
|
|
|
void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2021-03-26 17:14:38 +02:00
|
|
|
void resetOverridden() override;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2020-08-20 12:02:16 +03:00
|
|
|
nlohmann::json toJSON() override;
|
2018-03-27 19:41:31 +03:00
|
|
|
|
2021-07-15 19:17:18 +03:00
|
|
|
std::string toKeyValue() override;
|
|
|
|
|
2018-03-27 19:41:31 +03:00
|
|
|
void convertToArgs(Args & args, const std::string & category) override;
|
|
|
|
|
|
|
|
struct Register
|
|
|
|
{
|
|
|
|
Register(Config * config);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extern GlobalConfig globalConfig;
|
|
|
|
|
2023-03-17 16:33:48 +02:00
|
|
|
|
|
|
|
struct ExperimentalFeatureSettings : Config {
|
|
|
|
|
2023-04-05 05:57:11 +03:00
|
|
|
Setting<std::set<ExperimentalFeature>> experimentalFeatures{
|
|
|
|
this, {}, "experimental-features",
|
|
|
|
R"(
|
|
|
|
Experimental features that are enabled.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```
|
|
|
|
experimental-features = nix-command flakes
|
|
|
|
```
|
|
|
|
|
|
|
|
Experimental features available:
|
|
|
|
|
2023-04-06 19:24:11 +03:00
|
|
|
{{#include experimental-features-shortlist.md}}
|
2023-04-06 18:37:02 +03:00
|
|
|
|
|
|
|
Experimental features are [further documented](@docroot@/contributing/experimental-features.md) in the contribution guide.
|
2023-04-05 05:57:11 +03:00
|
|
|
)"};
|
2023-03-17 16:33:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 16:55:38 +03:00
|
|
|
}
|