Nix 2.93.3
Lix: A modern, delicious implementation of the Nix 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 "lix/libutil/args.hh"
16#include "lix/libutil/config.hh"
19
20namespace nix {
21
22template<> struct BaseSetting<Strings>::trait
23{
24 static constexpr bool appendable = true;
25};
26template<> struct BaseSetting<StringSet>::trait
27{
28 static constexpr bool appendable = true;
29};
30template<> struct BaseSetting<StringMap>::trait
31{
32 static constexpr bool appendable = true;
33};
34template<> struct BaseSetting<ExperimentalFeatures>::trait
35{
36 static constexpr bool appendable = true;
37};
38template<> struct BaseSetting<DeprecatedFeatures>::trait
39{
40 static constexpr bool appendable = true;
41};
42
43template<typename T>
44struct BaseSetting<T>::trait
45{
46 static constexpr bool appendable = false;
47};
48
49template<typename T>
51{
52 return trait::appendable;
53}
54
55template<> void BaseSetting<Strings>::appendOrSet(Strings newValue, bool append, const ApplyConfigOptions & options);
56template<> void BaseSetting<StringSet>::appendOrSet(StringSet newValue, bool append, const ApplyConfigOptions & options);
57template<> void BaseSetting<StringMap>::appendOrSet(StringMap newValue, bool append, const ApplyConfigOptions & options);
58template<> void BaseSetting<ExperimentalFeatures>::appendOrSet(ExperimentalFeatures newValue, bool append, const ApplyConfigOptions & options);
59template<> void BaseSetting<DeprecatedFeatures>::appendOrSet(DeprecatedFeatures newValue, bool append, const ApplyConfigOptions & options);
60
61template<typename T>
62void BaseSetting<T>::appendOrSet(T newValue, bool append, const ApplyConfigOptions & options)
63{
64 static_assert(
65 !trait::appendable,
66 "using default `appendOrSet` implementation with an appendable type");
67 assert(!append);
68 value = std::move(newValue);
69}
70
71template<typename T>
72void BaseSetting<T>::set(const std::string & str, bool append, const ApplyConfigOptions & options)
73{
74 if (experimentalFeatureSettings.isEnabled(experimentalFeature)) {
75 auto parsed = parse(str, options);
76 if (deprecated && (append || parsed != value)) {
77 warn("deprecated setting '%s' found (set to '%s')", name, str);
78 }
79 overridden = true;
80 appendOrSet(std::move(parsed), append, options);
81 } else {
82 assert(experimentalFeature);
83 warn("Ignoring setting '%s' because experimental feature '%s' is not enabled",
84 name,
85 showExperimentalFeature(*experimentalFeature));
86 }
87}
88
89template<typename T>
90void BaseSetting<T>::override(const T & v)
91{
92 overridden = true;
93 value = v;
94}
95
96template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category);
97
98template<typename T>
99void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
100{
101 args.addFlag({
102 .longName = name,
103 .description = fmt("Set the `%s` setting.", name),
104 .category = category,
105 .labels = {"value"},
106 .handler = {[this](std::string s) { set(s); }},
107 .experimentalFeature = experimentalFeature,
108 });
109
110 if (isAppendable())
111 args.addFlag({
112 .longName = "extra-" + name,
113 .description = fmt("Append to the `%s` setting.", name),
114 .category = category,
115 .labels = {"value"},
116 .handler = {[this](std::string s) { set(s, true); }},
117 .experimentalFeature = experimentalFeature,
118 });
119}
120
121#define DECLARE_CONFIG_SERIALISER(TY) \
122 template<> TY BaseSetting< TY >::parse(const std::string & str, const ApplyConfigOptions & options) const; \
123 template<> std::string BaseSetting< TY >::to_string() const;
124
125DECLARE_CONFIG_SERIALISER(std::string)
126DECLARE_CONFIG_SERIALISER(std::optional<std::string>)
127DECLARE_CONFIG_SERIALISER(std::optional<uint16_t>)
128DECLARE_CONFIG_SERIALISER(bool)
129DECLARE_CONFIG_SERIALISER(Strings)
130DECLARE_CONFIG_SERIALISER(StringSet)
131DECLARE_CONFIG_SERIALISER(StringMap)
132DECLARE_CONFIG_SERIALISER(ExperimentalFeatures)
133DECLARE_CONFIG_SERIALISER(DeprecatedFeatures)
134
135template<typename T>
136T BaseSetting<T>::parse(const std::string & str, const ApplyConfigOptions & options) const
137{
138 static_assert(std::is_integral<T>::value, "Integer required.");
139
140 if (auto n = string2Int<T>(str))
141 return *n;
142 else
143 throw UsageError("setting '%s' has invalid value '%s'", name, str);
144}
145
146template<typename T>
147std::string BaseSetting<T>::to_string() const
148{
149 static_assert(std::is_integral<T>::value, "Integer required.");
150
151 return std::to_string(value);
152}
153
154}
Definition config.hh:220
virtual void appendOrSet(T newValue, bool append, const ApplyConfigOptions &options)
Definition config-impl.hh:62
virtual T parse(const std::string &str, const ApplyConfigOptions &options) const
Definition config-impl.hh:136
bool isAppendable() override final
Definition config-impl.hh:50
void set(const std::string &str, bool append=false, const ApplyConfigOptions &options={}) override final
Definition config-impl.hh:72
std::string fmt(const std::string &s)
Definition fmt.hh:127
void warn(const std::string &fs, const Args &... args)
Definition logging.hh:305
Definition apply-config-options.hh:15