Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
configuration.hh
Go to the documentation of this file.
1#pragma once
3
4#include <filesystem>
5#include <map>
6#include <set>
7
8#include <nlohmann/json_fwd.hpp>
9
10#include "nix/util/error.hh"
12#include "nix/util/types.hh"
13#include "nix/util/fmt.hh"
15
16namespace nix {
17
49
50class Args;
51class AbstractSetting;
52
53class AbstractConfig
54{
55private:
56 /* VTable anchor to avoid weak linkage of the vtable - it breaks
57 dynamic_cast across shared libraries on Darwin. */
58 virtual void anchor();
59
60protected:
61 StringMap unknownSettings;
62
63 AbstractConfig(StringMap initials = {});
64
65public:
66
71 virtual bool set(const std::string & name, const std::string & value) = 0;
72
74 {
75 std::string value;
76 std::string description;
77 bool excludedFromFullSerialisation = false;
78 };
79
85 virtual void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) const = 0;
86
92 void applyConfig(const std::string & contents, const std::string & path = "<unknown>");
93
97 virtual void resetOverridden() = 0;
98
103 virtual nlohmann::json toJSON() = 0;
104
109 virtual std::string toKeyValue(bool overriddenOnly = false) = 0;
110
116 virtual void convertToArgs(Args & args, const std::string & category) = 0;
117
121 void warnUnknownSettings();
122
127
128 virtual ~AbstractConfig() = default;
129};
130
146class Config : public AbstractConfig
147{
148 friend class AbstractSetting;
149
150 void anchor() override;
151
152public:
153
155 {
156 bool isAlias;
157 AbstractSetting * setting;
158 };
159
160 using Settings = std::map<std::string, SettingData>;
161
162private:
163
164 Settings _settings;
165
166public:
167
168 Config(StringMap initials = {});
169
170 bool set(const std::string & name, const std::string & value) override;
171
172 void addSetting(AbstractSetting * setting);
173
174 void getSettings(std::map<std::string, SettingInfo> & res, bool overriddenOnly = false) const override;
175
176 void resetOverridden() override;
177
178 nlohmann::json toJSON() override;
179
180 std::string toKeyValue(bool overriddenOnly = false) override;
181
182 void convertToArgs(Args & args, const std::string & category) override;
183};
184
185class AbstractSetting
186{
187 friend class Config;
188
189public:
190
191 const std::string name;
192 const std::string description;
193 const StringSet aliases;
194
195 int created = 123;
196
197 bool overridden = false;
198
199 std::optional<ExperimentalFeature> experimentalFeature;
200
201 bool isOverridden() const;
202
203 virtual bool excludedFromFullSerialisation() const
204 {
205 return false;
206 }
207
208protected:
209
210 AbstractSetting(
211 const std::string & name,
212 const std::string & description,
213 const StringSet & aliases,
214 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);
215
216 virtual ~AbstractSetting();
217
218 virtual void set(const std::string & value, bool append = false) = 0;
219
224 virtual bool isAppendable() = 0;
225
226 virtual std::string to_string() const = 0;
227
228 nlohmann::json toJSON();
229
230 virtual std::map<std::string, nlohmann::json> toJSONObject() const;
231
232 virtual void convertToArg(Args & args, const std::string & category);
233};
234
241struct AbsolutePath
242{
243 AbsolutePath(std::filesystem::path p)
244 : _path(std::move(p))
245 {
246 if (!_path.is_absolute())
247 throw Error("not an absolute path: %s", PathFmt(_path));
248 }
249
250 AbsolutePath(const char * s)
251 : _path(s)
252 {
253 if (!_path.is_absolute())
254 throw Error("not an absolute path: %s", PathFmt(_path));
255 }
256
257#ifdef _WIN32
258 AbsolutePath(const wchar_t * s)
259 : _path(s)
260 {
261 if (!_path.is_absolute())
262 throw Error("not an absolute path: %s", PathFmt(_path));
263 }
264#endif
265
266 const std::filesystem::path & path() const
267 {
268 return _path;
269 }
270
271 operator const std::filesystem::path &() const
272 {
273 return _path;
274 }
275
276 std::string string() const
277 {
278 return _path.string();
279 }
280
281 const auto & native() const
282 {
283 return _path.native();
284 }
285
286 const std::filesystem::path::value_type * c_str() const
287 {
288 return _path.c_str();
289 }
290
291 bool empty() const
292 {
293 return _path.empty();
294 }
295
296 std::filesystem::path operator/(const std::filesystem::path & rhs) const
297 {
298 return _path / rhs;
299 }
300
301 bool operator==(const AbsolutePath & rhs) const
302 {
303 return _path == rhs._path;
304 }
305
306 bool operator==(const std::filesystem::path & rhs) const
307 {
308 return _path == rhs;
309 }
310
311 bool operator==(const std::string & rhs) const
312 {
313 return _path == rhs;
314 }
315
316 auto operator<=>(const AbsolutePath & rhs) const
317 {
318 return _path <=> rhs._path;
319 }
320
321 friend std::ostream & operator<<(std::ostream & os, const AbsolutePath & p)
322 {
323 return os << p._path.string();
324 }
325
326private:
327 std::filesystem::path _path;
328};
329
330template<>
331struct json_avoids_null<AbsolutePath> : std::true_type
332{};
333
337template<typename T>
338class BaseSetting : public AbstractSetting
339{
340protected:
341
342 T value;
343 const T defaultValue;
344 const bool documentDefault;
345
351 virtual T parse(const std::string & str) const;
352
361 virtual void appendOrSet(T newValue, bool append);
362
363public:
364
365 BaseSetting(
366 const T & def,
367 const bool documentDefault,
368 const std::string & name,
369 const std::string & description,
370 const StringSet & aliases = {},
371 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
372 : AbstractSetting(name, description, aliases, experimentalFeature)
373 , value(def)
374 , defaultValue(def)
375 , documentDefault(documentDefault)
376 {
377 }
378
379 operator const T &() const
380 {
381 return value;
382 }
383
384 operator T &()
385 {
386 return value;
387 }
388
389 const T & get() const
390 {
391 return value;
392 }
393
394 T & get()
395 {
396 return value;
397 }
398
399 template<typename U>
400 bool operator==(const U & v2) const
401 {
402 return value == v2;
403 }
404
405 template<typename U>
406 bool operator!=(const U & v2) const
407 {
408 return value != v2;
409 }
410
411 template<typename U>
412 void operator=(const U & v)
413 {
414 assign(v);
415 }
416
417 virtual void assign(const T & v)
418 {
419 value = v;
420 }
421
422 template<typename U>
423 void setDefault(const U & v)
424 {
425 if (!overridden)
426 value = v;
427 }
428
435 void set(const std::string & str, bool append = false) override final;
436
441 struct trait;
442
447 bool isAppendable() override final;
448
449 virtual void override(const T & v)
450 {
451 overridden = true;
452 value = v;
453 }
454
455 std::string to_string() const override;
456
457 void convertToArg(Args & args, const std::string & category) override;
458
459 std::map<std::string, nlohmann::json> toJSONObject() const override;
460};
461
462template<typename T>
463std::ostream & operator<<(std::ostream & str, const BaseSetting<T> & opt)
464{
465 return str << static_cast<const T &>(opt);
466}
467
468template<typename T>
469bool operator==(const T & v1, const BaseSetting<T> & v2)
470{
471 return v1 == static_cast<const T &>(v2);
472}
473
474template<typename T>
475class Setting : public BaseSetting<T>
476{
477public:
478 Setting(
479 Config * options,
480 const T & def,
481 const std::string & name,
482 const std::string & description,
483 const StringSet & aliases = {},
484 const bool documentDefault = true,
485 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
486 : BaseSetting<T>(def, documentDefault, name, description, aliases, std::move(experimentalFeature))
487 {
488 options->addSetting(this);
489 }
490
491 void operator=(const T & v)
492 {
493 this->assign(v);
494 }
495};
496
506template<>
507class Setting<AbsolutePath> : public BaseSetting<AbsolutePath>
508{
509public:
510 using BaseSetting<AbsolutePath>::BaseSetting;
511 using BaseSetting<AbsolutePath>::operator=;
512
513 Setting(
514 Config * options,
515 const AbsolutePath & def,
516 const std::string & name,
517 const std::string & description,
518 const StringSet & aliases = {},
519 const bool documentDefault = true,
520 std::optional<ExperimentalFeature> experimentalFeature = std::nullopt)
521 : BaseSetting<AbsolutePath>(def, documentDefault, name, description, aliases, std::move(experimentalFeature))
522 {
523 options->addSetting(this);
524 }
525
526 /* To appease -Wweak-vtables. */
527 ~Setting() override;
528
529 void operator=(const AbsolutePath & v)
530 {
531 this->assign(v);
532 }
533
534 operator const std::filesystem::path &() const
535 {
536 return this->value.path();
537 }
538};
539
540/* Delete these overloads to avoid footguns with implicit quoting of Setting<AbsolutePath> in fmt(). */
541
542template<class F, typename... Args>
543inline void formatHelper(F & f, const AbsolutePath & x, const Args &... args) = delete;
544
545template<class F, typename... Args>
546inline void formatHelper(F & f, const AbsolutePath & x) = delete;
547
548template<class F, typename... Args>
549inline void formatHelper(F & f, const Setting<AbsolutePath> & x, const Args &... args) = delete;
550
551template<class F, typename... Args>
552inline void formatHelper(F & f, const Setting<AbsolutePath> & x) = delete;
553
554template<>
555void BaseSetting<std::set<std::filesystem::path>>::appendOrSet(std::set<std::filesystem::path> newValue, bool append);
556
558{
559private:
560 void anchor() override;
561
562public:
563 Setting<std::set<ExperimentalFeature>> experimentalFeatures{
564 this,
565 {},
566 "experimental-features",
567 R"(
568 Experimental features that are enabled.
569
570 Example:
571
572 ```
573 experimental-features = nix-command flakes
574 ```
575
576 The following experimental features are available:
577
578 {{#include experimental-features-shortlist.md}}
579
580 Experimental features are [further documented in the manual](@docroot@/development/experimental-features.md).
581 )"};
582
586 bool isEnabled(const ExperimentalFeature &) const;
587
592 void require(const ExperimentalFeature &, std::string reason = "") const;
593
598 template<typename GetReason>
599 requires std::invocable<GetReason> && std::convertible_to<std::invoke_result_t<GetReason>, std::string>
600 void require(const ExperimentalFeature & feature, const GetReason & getReason) const
601 {
602 if (isEnabled(feature))
603 return;
604 require(feature, getReason());
605 }
606
611 bool isEnabled(const std::optional<ExperimentalFeature> &) const;
612
617 void require(const std::optional<ExperimentalFeature> &) const;
618};
619
620#define NIX_DECLARE_CONFIG_SERIALISER(TY) \
621 template<> \
622 TY BaseSetting<TY>::parse(const std::string & str) const; \
623 template<> \
624 std::string BaseSetting<TY>::to_string() const;
625
626// FIXME: don't use a global variable.
627extern ExperimentalFeatureSettings experimentalFeatureSettings;
628
629} // namespace nix
virtual std::string toKeyValue(bool overriddenOnly=false)=0
void warnUnknownSettings()
Definition configuration.cc:81
void reapplyUnknownSettings()
Definition configuration.cc:87
virtual bool set(const std::string &name, const std::string &value)=0
virtual nlohmann::json toJSON()=0
virtual void getSettings(std::map< std::string, SettingInfo > &res, bool overriddenOnly=false) const =0
void applyConfig(const std::string &contents, const std::string &path="<unknown>")
Definition configuration.cc:181
virtual void resetOverridden()=0
virtual void convertToArgs(Args &args, const std::string &category)=0
Definition configuration.hh:186
virtual bool isAppendable()=0
Definition args.hh:29
Definition configuration.hh:339
bool isAppendable() override final
Definition config-impl.hh:55
virtual T parse(const std::string &str) const
Definition config-impl.hh:139
void set(const std::string &str, bool append=false) override final
Definition config-impl.hh:79
virtual void appendOrSet(T newValue, bool append)
Definition config-impl.hh:70
Definition configuration.hh:147
void resetOverridden() override
Definition configuration.cc:206
std::string toKeyValue(bool overriddenOnly=false) override
Definition configuration.cc:221
void getSettings(std::map< std::string, SettingInfo > &res, bool overriddenOnly=false) const override
Definition configuration.cc:95
nlohmann::json toJSON() override
Definition configuration.cc:212
bool set(const std::string &name, const std::string &value) override
Definition configuration.cc:29
void convertToArgs(Args &args, const std::string &category) override
Definition configuration.cc:233
Definition error.hh:310
Definition configuration.hh:476
This file defines two main structs/classes used in nix error handling.
ExperimentalFeature
Definition experimental-features.hh:19
T & require(Store &store)
Definition store-cast.hh:16
Definition configuration.hh:242
Definition configuration.hh:74
Definition diagnose.cc:25
Definition configuration.hh:155
Definition configuration.hh:558
void require(const ExperimentalFeature &feature, const GetReason &getReason) const
Definition configuration.hh:600
void require(const ExperimentalFeature &, std::string reason="") const
Definition configuration.cc:576
bool isEnabled(const ExperimentalFeature &) const
Definition configuration.cc:570
Definition fmt.hh:115
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
std::set< std::string, std::less<> > StringSet
Definition types.hh:45