Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
config-impl.hh
Go to the documentation of this file.
1#pragma once
14
15#include "config.hh"
16#include "args.hh"
17
18namespace nix {
19
20template<> struct BaseSetting<Strings>::trait
21{
22 static constexpr bool appendable = true;
23};
24template<> struct BaseSetting<StringSet>::trait
25{
26 static constexpr bool appendable = true;
27};
28template<> struct BaseSetting<StringMap>::trait
29{
30 static constexpr bool appendable = true;
31};
32template<> struct BaseSetting<std::set<ExperimentalFeature>>::trait
33{
34 static constexpr bool appendable = true;
35};
36
37template<typename T>
38struct BaseSetting<T>::trait
39{
40 static constexpr bool appendable = false;
41};
42
43template<typename T>
45{
46 return trait::appendable;
47}
48
49template<> void BaseSetting<Strings>::appendOrSet(Strings newValue, bool append);
50template<> void BaseSetting<StringSet>::appendOrSet(StringSet newValue, bool append);
51template<> void BaseSetting<StringMap>::appendOrSet(StringMap newValue, bool append);
52template<> void BaseSetting<std::set<ExperimentalFeature>>::appendOrSet(std::set<ExperimentalFeature> newValue, bool append);
53
54template<typename T>
55void BaseSetting<T>::appendOrSet(T newValue, bool append)
56{
57 static_assert(
58 !trait::appendable,
59 "using default `appendOrSet` implementation with an appendable type");
60 assert(!append);
61
62 value = std::move(newValue);
63}
64
65template<typename T>
66void BaseSetting<T>::set(const std::string & str, bool append)
67{
68 if (experimentalFeatureSettings.isEnabled(experimentalFeature))
69 appendOrSet(parse(str), append);
70 else {
71 assert(experimentalFeature);
72 warn("Ignoring setting '%s' because experimental feature '%s' is not enabled",
73 name,
74 showExperimentalFeature(*experimentalFeature));
75 }
76}
77
78template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category);
79
80template<typename T>
81void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
82{
83 args.addFlag({
84 .longName = name,
85 .aliases = aliases,
86 .description = fmt("Set the `%s` setting.", name),
87 .category = category,
88 .labels = {"value"},
89 .handler = {[this](std::string s) { overridden = true; set(s); }},
90 .experimentalFeature = experimentalFeature,
91 });
92
93 if (isAppendable())
94 args.addFlag({
95 .longName = "extra-" + name,
96 .aliases = aliases,
97 .description = fmt("Append to the `%s` setting.", name),
98 .category = category,
99 .labels = {"value"},
100 .handler = {[this](std::string s) { overridden = true; set(s, true); }},
101 .experimentalFeature = experimentalFeature,
102 });
103}
104
105#define DECLARE_CONFIG_SERIALISER(TY) \
106 template<> TY BaseSetting< TY >::parse(const std::string & str) const; \
107 template<> std::string BaseSetting< TY >::to_string() const;
108
109DECLARE_CONFIG_SERIALISER(std::string)
110DECLARE_CONFIG_SERIALISER(std::optional<std::string>)
111DECLARE_CONFIG_SERIALISER(bool)
112DECLARE_CONFIG_SERIALISER(Strings)
113DECLARE_CONFIG_SERIALISER(StringSet)
114DECLARE_CONFIG_SERIALISER(StringMap)
115DECLARE_CONFIG_SERIALISER(std::set<ExperimentalFeature>)
116
117template<typename T>
118T BaseSetting<T>::parse(const std::string & str) const
119{
120 static_assert(std::is_integral<T>::value, "Integer required.");
121
122 try {
124 } catch (...) {
125 throw UsageError("setting '%s' has invalid value '%s'", name, str);
126 }
127}
128
129template<typename T>
130std::string BaseSetting<T>::to_string() const
131{
132 static_assert(std::is_integral<T>::value, "Integer required.");
133
134 return std::to_string(value);
135}
136
137}
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
std::string fmt(const std::string &s)
Definition fmt.hh:67
return s
Definition lexer.l:459
std::ostream & str
Definition lexer.l:1728
const std::string_view & name
Definition lexer.l:1709
virtual void appendOrSet(T newValue, bool append)
virtual bool set(const std::string &name, const std::string &value)=0
const T & value
Definition lexer.l:492
std::optional< ExperimentalFeature > experimentalFeature
Definition lexer.l:1937
std::vector< Expr * > args
Definition lexer.l:6126
virtual bool isAppendable()=0
void warn(const std::string &fs, const Args &... args)
Definition logging.hh:255
N string2IntWithUnitPrefix(std::string_view s)
Definition util.hh:88