Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
config.hh
Go to the documentation of this file.
1#pragma once
3
4#include <cassert>
5#include <map>
6#include <set>
7
8#include <nlohmann/json_fwd.hpp>
9
10#include "types.hh"
12
13namespace nix {
14
46
47class Args;
48class AbstractSetting;
49
50class AbstractConfig
51{
52protected:
53 StringMap unknownSettings;
54
55 AbstractConfig(StringMap initials = {});
56
57public:
58
63 virtual bool set(const std::string & name, const std::string & value) = 0;
64
66 {
67 std::string value;
68 std::string description;
69 };
70
76 virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) = 0;
77
83 void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
84
88 virtual void resetOverridden() = 0;
89
94 virtual nlohmann::json toJSON() = 0;
95
100 virtual std::string toKeyValue() = 0;
101
107 virtual void convertToArgs(Args & args, const std::string & category) = 0;
108
112 void warnUnknownSettings();
113
118
119 virtual ~AbstractConfig() = default;
120};
121
137class Config : public AbstractConfig
138{
139 friend class AbstractSetting;
140
141public:
142
144 {
145 bool isAlias;
146 AbstractSetting * setting;
147 };
148
149 using Settings = std::map<std::string, SettingData>;
150
151private:
152
153 Settings _settings;
154
155public:
156
157 Config(StringMap initials = {});
158
159 bool set(const std::string & name, const std::string & value) override;
160
161 void addSetting(AbstractSetting * setting);
162
163 void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) override;
164
165 void resetOverridden() override;
166
167 nlohmann::json toJSON() override;
168
169 std::string toKeyValue() override;
170
171 void convertToArgs(Args & args, const std::string & category) override;
172};
173
174class AbstractSetting
175{
176 friend class Config;
177
178public:
179
180 const std::string name;
181 const std::string description;
182 const std::set<std::string> aliases;
183
184 int created = 123;
185
186 bool overridden = false;
187
188 std::optional<ExperimentalFeature> experimentalFeature;
189
190protected:
191
193 const std::string & name,
194 const std::string & description,
195 const std::set<std::string> & aliases,
196 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);
197
198 virtual ~AbstractSetting();
199
200 virtual void set(const std::string & value, bool append = false) = 0;
201
206 virtual bool isAppendable() = 0;
207
208 virtual std::string to_string() const = 0;
209
210 nlohmann::json toJSON();
211
212 virtual std::map<std::string, nlohmann::json> toJSONObject() const;
213
214 virtual void convertToArg(Args & args, const std::string & category);
215
216 bool isOverridden() const;
217};
218
222template<typename T>
223class BaseSetting : public AbstractSetting
224{
225protected:
226
227 T value;
228 const T defaultValue;
229 const bool documentDefault;
230
236 virtual T parse(const std::string & str) const;
237
246 virtual void appendOrSet(T newValue, bool append);
247
248public:
249
250 BaseSetting(const T & def,
251 const bool documentDefault,
252 const std::string & name,
253 const std::string & description,
254 const std::set<std::string> & aliases = {},
255 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
256 : AbstractSetting(name, description, aliases, experimentalFeature)
257 , value(def)
258 , defaultValue(def)
259 , documentDefault(documentDefault)
260 { }
261
262 operator const T &() const { return value; }
263 operator T &() { return value; }
264 const T & get() const { return value; }
265 T & get() { return value; }
266 template<typename U>
267 bool operator ==(const U & v2) const { return value == v2; }
268 template<typename U>
269 bool operator !=(const U & v2) const { return value != v2; }
270 template<typename U>
271 void operator =(const U & v) { assign(v); }
272 virtual void assign(const T & v) { value = v; }
273 template<typename U>
274 void setDefault(const U & v) { if (!overridden) value = v; }
275
282 void set(const std::string & str, bool append = false) override final;
283
288 struct trait;
289
294 bool isAppendable() override final;
295
296 virtual void override(const T & v)
297 {
298 overridden = true;
299 value = v;
300 }
301
302 std::string to_string() const override;
303
304 void convertToArg(Args & args, const std::string & category) override;
305
306 std::map<std::string, nlohmann::json> toJSONObject() const override;
307};
308
309template<typename T>
310std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
311{
312 return str << static_cast<const T &>(opt);
313}
314
315template<typename T>
316bool operator ==(const T & v1, const BaseSetting<T> & v2) { return v1 == static_cast<const T &>(v2); }
317
318template<typename T>
319class Setting : public BaseSetting<T>
320{
321public:
322 Setting(Config * options,
323 const T & def,
324 const std::string & name,
325 const std::string & description,
326 const std::set<std::string> & aliases = {},
327 const bool documentDefault = true,
328 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
329 : BaseSetting<T>(def, documentDefault, name, description, aliases, std::move(experimentalFeature))
330 {
331 options->addSetting(this);
332 }
333
334 void operator =(const T & v) { this->assign(v); }
335};
336
344class PathSetting : public BaseSetting<Path>
345{
346public:
347
348 PathSetting(Config * options,
349 const Path & def,
350 const std::string & name,
351 const std::string & description,
352 const std::set<std::string> & aliases = {});
353
354 Path parse(const std::string & str) const override;
355
356 Path operator +(const char * p) const { return value + p; }
357
358 void operator =(const Path & v) { this->assign(v); }
359};
360
366class OptionalPathSetting : public BaseSetting<std::optional<Path>>
367{
368public:
369
370 OptionalPathSetting(Config * options,
371 const std::optional<Path> & def,
372 const std::string & name,
373 const std::string & description,
374 const std::set<std::string> & aliases = {});
375
376 std::optional<Path> parse(const std::string & str) const override;
377
378 void operator =(const std::optional<Path> & v);
379};
380
381
383
384 Setting<std::set<ExperimentalFeature>> experimentalFeatures{
385 this, {}, "experimental-features",
386 R"(
387 Experimental features that are enabled.
388
389 Example:
390
391 ```
392 experimental-features = nix-command flakes
393 ```
394
395 The following experimental features are available:
396
397 {{#include experimental-features-shortlist.md}}
398
399 Experimental features are [further documented in the manual](@docroot@/development/experimental-features.md).
400 )"};
401
405 bool isEnabled(const ExperimentalFeature &) const;
406
411 void require(const ExperimentalFeature &) const;
412
417 bool isEnabled(const std::optional<ExperimentalFeature> &) const;
418
423 void require(const std::optional<ExperimentalFeature> &) const;
424};
425
426// FIXME: don't use a global variable.
427extern ExperimentalFeatureSettings experimentalFeatureSettings;
428
429}
virtual std::string toKeyValue()=0
void warnUnknownSettings()
Definition config.cc:73
void reapplyUnknownSettings()
Definition config.cc:79
virtual bool set(const std::string &name, const std::string &value)=0
virtual void getSettings(std::map< std::string, SettingInfo > &res, bool overriddenOnly=false)=0
virtual nlohmann::json toJSON()=0
void applyConfig(const std::string &contents, const std::string &path="<unknown>")
Definition config.cc:163
virtual void resetOverridden()=0
virtual void convertToArgs(Args &args, const std::string &category)=0
Definition config.hh:175
virtual bool isAppendable()=0
Definition args.hh:28
Definition config.hh:224
bool isAppendable() override final
Definition config-impl.hh:44
virtual T parse(const std::string &str) const
Definition config-impl.hh:118
void set(const std::string &str, bool append=false) override final
Definition config-impl.hh:66
virtual void appendOrSet(T newValue, bool append)
Definition config-impl.hh:55
Definition config.hh:138
void resetOverridden() override
Definition config.cc:188
void getSettings(std::map< std::string, SettingInfo > &res, bool overriddenOnly=false) override
Definition config.cc:87
friend class AbstractSetting
Definition config.hh:139
std::string toKeyValue() override
Definition config.cc:203
nlohmann::json toJSON() override
Definition config.cc:194
bool set(const std::string &name, const std::string &value) override
Definition config.cc:21
void convertToArgs(Args &args, const std::string &category) override
Definition config.cc:212
std::optional< Path > parse(const std::string &str) const override
Definition config.cc:454
Path parse(const std::string &str) const override
Definition config.cc:437
Definition config.hh:320
ExperimentalFeature
Definition experimental-features.hh:19
friend class AbstractSetting
Definition lexer.l:1888
std::ostream & str
Definition lexer.l:1728
Strings res
Definition lexer.l:2566
std::shared_ptr< T > p
Definition lexer.l:1269
virtual nlohmann::json toJSON()=0
const std::string_view & name
Definition lexer.l:1709
virtual bool set(const std::string &name, const std::string &value)=0
const T & value
Definition lexer.l:492
std::vector< Expr * > args
Definition lexer.l:6126
Definition config.hh:66
Definition globals.cc:262
Definition config.hh:144
Definition config.hh:382
void require(const ExperimentalFeature &) const
Definition config.cc:473
bool isEnabled(const ExperimentalFeature &) const
Definition config.cc:467
std::string Path
Definition types.hh:22