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