1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 08:31:16 +02:00

Modularize config settings

Allow global config settings to be defined in multiple Config
classes. For example, this means that libutil can have settings and
evaluator settings can be moved out of libstore. The Config classes
are registered in a new GlobalConfig class to which config files
etc. are applied.

Relevant to https://github.com/NixOS/nix/issues/2009 in that it
removes the need for ad hoc handling of useCaseHack, which was the
underlying cause of that issue.
This commit is contained in:
Eelco Dolstra 2018-03-27 18:41:31 +02:00
parent e606cd412f
commit 737ed88f35
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
15 changed files with 195 additions and 133 deletions

View file

@ -12,6 +12,40 @@ class AbstractSetting;
class JSONPlaceholder;
class JSONObject;
class AbstractConfig
{
protected:
StringMap unknownSettings;
AbstractConfig(const StringMap & initials = {})
: unknownSettings(initials)
{ }
public:
virtual bool set(const std::string & name, const std::string & value) = 0;
struct SettingInfo
{
std::string value;
std::string description;
};
virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) = 0;
void applyConfigFile(const Path & path);
virtual void resetOverriden() = 0;
virtual void toJSON(JSONObject & out) = 0;
virtual void convertToArgs(Args & args, const std::string & category) = 0;
void warnUnknownSettings();
void reapplyUnknownSettings();
};
/* A class to simplify providing configuration settings. The typical
use is to inherit Config and add Setting<T> members:
@ -27,7 +61,7 @@ class JSONObject;
};
*/
class Config
class Config : public AbstractConfig
{
friend class AbstractSetting;
@ -48,31 +82,23 @@ private:
Settings _settings;
StringMap extras;
public:
Config(const StringMap & initials)
: extras(initials)
Config(const StringMap & initials = {})
: AbstractConfig(initials)
{ }
void set(const std::string & name, const std::string & value);
bool set(const std::string & name, const std::string & value) override;
void addSetting(AbstractSetting * setting);
void handleUnknownSettings();
void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) override;
StringMap getSettings(bool overridenOnly = false);
void resetOverriden() override;
const Settings & _getSettings() { return _settings; }
void toJSON(JSONObject & out) override;
void applyConfigFile(const Path & path);
void resetOverriden();
void toJSON(JSONObject & out);
void convertToArgs(Args & args, const std::string & category);
void convertToArgs(Args & args, const std::string & category) override;
};
class AbstractSetting
@ -209,4 +235,27 @@ public:
void operator =(const Path & v) { this->assign(v); }
};
struct GlobalConfig : public AbstractConfig
{
typedef std::vector<Config*> ConfigRegistrations;
static ConfigRegistrations * configRegistrations;
bool set(const std::string & name, const std::string & value) override;
void getSettings(std::map<std::string, SettingInfo> & res, bool overridenOnly = false) override;
void resetOverriden() override;
void toJSON(JSONObject & out) override;
void convertToArgs(Args & args, const std::string & category) override;
struct Register
{
Register(Config * config);
};
};
extern GlobalConfig globalConfig;
}