1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include "lix/libutil/abstract-config.hh"
#include "lix/libutil/file-system.hh"
#include "lix/libutil/logging.hh"
#include "lix/libutil/strings.hh"
namespace nix {
static void applyConfigInner(
const std::string & contents,
const ApplyConfigOptions & options,
std::vector<std::pair<std::string, std::string>> & parsedContents
)
{
unsigned int pos = 0;
while (pos < contents.size()) {
std::string line;
while (pos < contents.size() && contents[pos] != '\n') {
line += contents[pos++];
}
pos++;
if (auto hash = line.find('#'); hash != line.npos) {
line = std::string(line, 0, hash);
}
auto tokens = tokenizeString<std::vector<std::string>>(line);
if (tokens.empty()) {
continue;
}
if (tokens.size() < 2) {
throw UsageError(
"illegal configuration line '%1%' in '%2%'", line, options.relativeDisplay()
);
}
auto include = false;
auto ignoreMissing = false;
if (tokens[0] == "include") {
include = true;
} else if (tokens[0] == "!include") {
include = true;
ignoreMissing = true;
}
if (include) {
if (tokens.size() != 2) {
throw UsageError(
"illegal configuration line '%1%' in '%2%'", line, options.relativeDisplay()
);
}
if (!options.path) {
throw UsageError("can only include configuration '%1%' from files", tokens[1]);
}
auto pathToInclude = absPath(tildePath(tokens[1], options.home), dirOf(*options.path));
if (pathExists(pathToInclude)) {
auto includeOptions = ApplyConfigOptions{
.path = pathToInclude,
.home = options.home,
};
try {
std::string includedContents = readFile(pathToInclude);
applyConfigInner(includedContents, includeOptions, parsedContents);
} catch (SysError &) {
// TODO: Do we actually want to ignore this? Or is it better to fail?
}
} else if (!ignoreMissing) {
throw Error(
"file '%1%' included from '%2%' not found", pathToInclude, *options.path
);
}
continue;
}
if (tokens[1] != "=") {
throw UsageError(
"illegal configuration line '%1%' in '%2%'", line, options.relativeDisplay()
);
}
std::string name = std::move(tokens[0]);
auto i = tokens.begin();
advance(i, 2);
parsedContents.push_back({
std::move(name),
concatStringsSep(" ", Strings(i, tokens.end())),
});
};
}
AbstractConfig::AbstractConfig(StringMap initials) : unknownSettings(std::move(initials)) {}
void AbstractConfig::applyConfig(const std::string & contents, const ApplyConfigOptions & options)
{
std::vector<std::pair<std::string, std::string>> parsedContents;
applyConfigInner(contents, options, parsedContents);
// First apply experimental-feature related settings
for (const auto & [name, value] : parsedContents) {
if (name == "experimental-features" || name == "extra-experimental-features") {
set(name, value, options);
}
}
// Then apply other settings
for (const auto & [name, value] : parsedContents) {
if (name != "experimental-features" && name != "extra-experimental-features") {
set(name, value, options);
}
}
}
void AbstractConfig::warnUnknownSettings()
{
for (const auto & s : unknownSettings) {
printTaggedWarning("unknown setting '%s'", s.first);
}
}
void AbstractConfig::reapplyUnknownSettings()
{
auto const toReapply = std::move(unknownSettings);
unknownSettings = {};
for (auto const & [name, value] : toReapply) {
set(name, value);
}
}
}
|