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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#include <fnmatch.h>
#include <lix/config.h>
#include <lix/libstore/derivations.hh>
#include <lix/libstore/local-fs-store.hh>
#include <lix/libutil/json.hh>
#include "constituents.hh"
#include "lix/libutil/async.hh"
#include "drv.hh"
#include <sstream>
namespace {
// This is copied from `libutil/topo-sort.hh` in CppNix and slightly modified.
// However, I needed a way to use strings as identifiers to sort, but still be
// able to put AggregateJob objects into this function since I'd rather not have
// to transform back and forth between a list of strings and AggregateJobs in
// resolveNamedConstituents.
auto topoSort(const std::set<AggregateJob> &items)
-> std::vector<AggregateJob> {
std::vector<AggregateJob> sorted;
std::set<std::string> visited;
std::set<std::string> parents;
std::map<std::string, AggregateJob> dictIdentToObject;
for (const auto &it : items) {
dictIdentToObject.insert({it.name, it});
}
std::function<void(const std::string &path, const std::string *parent)>
dfsVisit;
dfsVisit = [&](const std::string &path, const std::string *parent) {
if (parents.contains(path)) {
dictIdentToObject.erase(path);
dictIdentToObject.erase(*parent);
std::set<std::string> remaining;
for (auto &[k, _] : dictIdentToObject) {
remaining.insert(k);
}
throw DependencyCycle(path, *parent, remaining);
}
if (!visited.insert(path).second) {
return;
}
parents.insert(path);
std::set<std::string> references = dictIdentToObject[path].dependencies;
for (const auto &i : references) {
/* Don't traverse into items that don't exist in our starting set.
*/
if (i != path &&
dictIdentToObject.find(i) != dictIdentToObject.end()) {
dfsVisit(i, &path);
}
}
sorted.push_back(dictIdentToObject[path]);
parents.erase(path);
};
for (auto &[i, _] : dictIdentToObject) {
dfsVisit(i, nullptr);
}
return sorted;
}
} // namespace
auto resolveNamedConstituents(const std::map<std::string, nix::JSON> &jobs)
-> std::variant<std::vector<AggregateJob>, DependencyCycle> {
std::set<AggregateJob> aggregateJobs;
for (auto const &[jobName, job] : jobs) {
auto named = job.find("namedConstituents");
if (named != job.end() && !named->empty()) {
std::unordered_map<std::string, std::string> brokenJobs;
std::set<std::string> results;
auto isBroken = [&brokenJobs,
&jobName](const std::string &childJobName,
const nix::JSON &job) -> bool {
if (job.find("error") != job.end()) {
std::string error = job["error"];
printError(
"aggregate job '%s' references broken job '%s': %s",
jobName, childJobName, error);
brokenJobs[childJobName] = error;
return true;
}
return false;
};
for (const std::string childJobName : *named) {
auto childJobIter = jobs.find(childJobName);
if (childJobIter == jobs.end()) {
printError(
"aggregate job '%s' references non-existent job '%s'",
jobName, childJobName);
brokenJobs[childJobName] = "does not exist";
} else if (!isBroken(childJobName, childJobIter->second)) {
results.insert(childJobName);
}
}
aggregateJobs.insert(AggregateJob(jobName, results, brokenJobs));
}
}
try {
return topoSort(aggregateJobs);
} catch (DependencyCycle &e) {
return e;
}
}
void rewriteAggregates(std::map<std::string, nix::JSON> &jobs,
const std::vector<AggregateJob> &aggregateJobs,
nix::ref<nix::Store> &store, nix::Path &gcRootsDir,
nix::AsyncIoRoot &aio) {
for (const auto &aggregateJob : aggregateJobs) {
auto &job = jobs.find(aggregateJob.name)->second;
auto drvPath = store->parseStorePath(std::string(job["drvPath"]));
auto drv = aio.blockOn(store->readDerivation(drvPath));
if (aggregateJob.brokenJobs.empty()) {
for (const auto &childJobName : aggregateJob.dependencies) {
auto childDrvPath = store->parseStorePath(
std::string(jobs.find(childJobName)->second["drvPath"]));
auto childDrv = aio.blockOn(store->readDerivation(childDrvPath));
job["constituents"].push_back(
store->printStorePath(childDrvPath));
drv.inputDrvs[childDrvPath] = {childDrv.outputs.begin()->first};
}
std::string drvName(drvPath.name());
assert(drvName.ends_with(nix::drvExtension));
drvName.resize(drvName.size() - nix::drvExtension.size());
auto hashModulo = aio.blockOn(hashDerivationModulo(*store, drv, true));
auto h = hashModulo.hashes.find("out");
if (h == hashModulo.hashes.end()) {
continue;
}
auto outPath = store->makeOutputPath("out", h->second, drvName);
drv.env["out"] = store->printStorePath(outPath);
drv.outputs.insert_or_assign(
"out", nix::DerivationOutput::InputAddressed{.path = outPath});
auto newDrvPath = aio.blockOn(nix::writeDerivation(*store, drv));
auto newDrvPathS = store->printStorePath(newDrvPath);
register_gc_root(gcRootsDir, newDrvPathS, store, aio);
printError("rewrote aggregate derivation %s -> %s",
store->printStorePath(drvPath), newDrvPathS);
job["drvPath"] = newDrvPathS;
job["outputs"]["out"] = store->printStorePath(outPath);
}
job.erase("namedConstituents");
if (!aggregateJob.brokenJobs.empty()) {
std::stringstream ss;
for (const auto &[jobName, error] : aggregateJob.brokenJobs) {
ss << jobName << ": " << error << "\n";
}
job["error"] = ss.str();
}
nix::logger->writeToStdout(job.dump());
}
}
|