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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
#include "lix/libexpr/eval.hh"
#include "lix/libexpr/eval-settings.hh"
#include "lix/libexpr/extra-primops.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libfetchers/fetchers.hh"
#include "lix/libstore/filetransfer.hh"
#include "lix/libfetchers/registry.hh"
#include "lix/libutil/regex.hh"
#include "lix/libutil/url.hh"
#include <ctime>
#include <iomanip>
#include <regex>
namespace nix {
Value emitTreeAttrs(
Evaluator & state,
const fetchers::Tree & tree,
const fetchers::Input & input,
bool emptyRevFallback,
bool forceDirty
)
{
assert(input.isLocked());
auto attrs = state.buildBindings(10);
attrs.insert(state.symbols.sym_outPath, state.paths.mkStorePathString(tree.storePath));
// FIXME: support arbitrary input attributes.
auto narHash = input.getNarHash();
assert(narHash);
attrs.insert("narHash", {NewValueAs::string, narHash->to_string()});
if (input.getType() == "git")
attrs.insert(
"submodules",
{NewValueAs::boolean, fetchers::maybeGetBoolAttr(input.attrs, "submodules").value_or(false)}
);
if (!forceDirty) {
if (auto rev = input.getRev()) {
attrs.insert("rev", {NewValueAs::string, rev->gitRev()});
attrs.insert("shortRev", {NewValueAs::string, rev->gitShortRev()});
} else if (emptyRevFallback) {
// Backwards compat for `builtins.fetchGit`: dirty repos return an empty sha1 as rev
auto emptyHash = Hash(HashType::SHA1);
attrs.insert("rev", {NewValueAs::string, emptyHash.gitRev()});
attrs.insert("shortRev", {NewValueAs::string, emptyHash.gitShortRev()});
}
if (auto revCount = input.getRevCount())
attrs.insert("revCount", {NewValueAs::integer, NixInt::Inner(*revCount)});
else if (emptyRevFallback)
attrs.insert("revCount", {NewValueAs::integer, 0});
}
if (auto dirtyRev = fetchers::maybeGetStrAttr(input.attrs, "dirtyRev")) {
attrs.insert("dirtyRev", {NewValueAs::string, *dirtyRev});
attrs.insert(
"dirtyShortRev", {NewValueAs::string, *fetchers::maybeGetStrAttr(input.attrs, "dirtyShortRev")}
);
}
if (auto lastModified = input.getLastModified()) {
attrs.insert("lastModified", {NewValueAs::integer, *lastModified});
attrs.insert(
"lastModifiedDate",
{NewValueAs::string, fmt("%s", std::put_time(std::gmtime(&*lastModified), "%Y%m%d%H%M%S"))}
);
}
return {NewValueAs::attrs, attrs};
}
std::string fixURI(std::string uri, EvalState & state, const std::string & defaultScheme = "file")
{
state.ctx.paths.checkURI(uri);
if (uri.find("://") == std::string::npos) {
const auto p = ParsedURL {
.scheme = defaultScheme,
.authority = "",
.path = uri
};
return p.to_string();
} else {
return uri;
}
}
std::string fixURIForGit(std::string uri, EvalState & state)
{
/* Detects scp-style uris (e.g. git@github.com:NixOS/nix) and fixes
* them by removing the `:` and assuming a scheme of `ssh://`
* */
static std::regex scp_uri = regex::parse("([^/]*)@(.*):(.*)");
if (uri[0] != '/' && std::regex_match(uri, scp_uri))
return fixURI(std::regex_replace(uri, scp_uri, "$1@$2/$3"), state, "ssh");
else
return fixURI(uri, state);
}
struct FetchTreeParams {
bool emptyRevFallback = false;
bool allowNameArgument = false;
};
static Value fetchTree(
EvalState & state,
const PosIdx pos,
Value ** args,
std::optional<std::string> type,
const FetchTreeParams & params = FetchTreeParams{}
)
{
fetchers::Input input;
NixStringContext context;
state.forceValue(*args[0], pos);
if (args[0]->type() == nAttrs) {
state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.fetchTree");
fetchers::Attrs attrs;
if (auto aType = args[0]->attrs()->get(state.ctx.symbols.sym_type)) {
if (type)
state.ctx.errors.make<EvalError>(
"unexpected attribute 'type'"
).atPos(pos).debugThrow();
type = state.forceStringNoCtx(
aType->value,
aType->pos,
"while evaluating the `type` attribute passed to builtins.fetchTree"
);
} else if (!type) {
state.ctx.errors.make<EvalError>("attribute 'type' is missing in call to 'fetchTree'")
.atPos(pos)
.debugThrow();
}
attrs.emplace("type", type.value());
for (auto & attr : *args[0]->attrs()) {
if (attr.name == state.ctx.symbols.sym_type) {
continue;
}
state.forceValue(attr.value, attr.pos);
if (attr.value.type() == nPath || attr.value.type() == nString) {
auto s =
state
.coerceToString(
attr.pos, attr.value, context, "", StringCoercionMode::Strict, false
)
.toOwned();
attrs.emplace(state.ctx.symbols[attr.name],
state.ctx.symbols[attr.name] == "url"
? type == "git"
? fixURIForGit(s, state)
: fixURI(s, state)
: s);
} else if (attr.value.type() == nBool) {
attrs.emplace(state.ctx.symbols[attr.name], Explicit<bool>{attr.value.boolean()});
} else if (attr.value.type() == nInt) {
auto intValue = attr.value.integer().value;
if (intValue < 0) {
state.ctx.errors.make<EvalError>("negative value given for fetchTree attr %1%: %2%", state.ctx.symbols[attr.name], intValue).atPos(pos).debugThrow();
}
unsigned long asUnsigned = intValue;
attrs.emplace(state.ctx.symbols[attr.name], asUnsigned);
} else {
state.ctx.errors
.make<TypeError>(
"fetchTree argument '%s' is %s while a string, Boolean or integer is "
"expected",
state.ctx.symbols[attr.name],
showType(attr.value)
)
.debugThrow();
}
}
if (!params.allowNameArgument)
if (auto nameIter = attrs.find("name"); nameIter != attrs.end())
state.ctx.errors.make<EvalError>(
"attribute 'name' isn’t supported in call to 'fetchTree'"
).atPos(pos).debugThrow();
// HACK: When using `fetchGit`, locking with only the hash should happen
// as we don't care about flake shenanigans about `lastModified`
if (type == "git" && attrs.contains("narHash")) {
using namespace std::literals::string_literals;
attrs["type"] = "\0git-locked"s;
}
input = fetchers::Input::fromAttrs(std::move(attrs));
} else {
auto url = state.coerceToString(pos, *args[0], context,
"while evaluating the first argument passed to the fetcher",
StringCoercionMode::Strict, false).toOwned();
if (type == "git") {
fetchers::Attrs attrs;
attrs.emplace("type", "git");
attrs.emplace("url", fixURIForGit(url, state));
input = fetchers::Input::fromAttrs(std::move(attrs));
} else {
input = fetchers::Input::fromURL(fixURI(url, state));
}
}
if (!evalSettings.pureEval && !input.isDirect())
input = state.aio.blockOn(lookupInRegistries(state.ctx.store, input)).first;
if (evalSettings.pureEval && !input.isLocked()) {
state.ctx.errors.make<EvalError>("in pure evaluation mode, 'fetchTree' requires a locked input").atPos(pos).debugThrow();
}
auto [tree, input2] = state.aio.blockOn(input.fetch(state.ctx.store));
state.ctx.paths.allowPath(tree.storePath);
return emitTreeAttrs(state.ctx, tree, input2, params.emptyRevFallback, false);
}
Value prim_fetchTree(EvalState & state, Value ** args)
{
return fetchTree(state, noPos, args, std::nullopt, FetchTreeParams{.allowNameArgument = false});
}
static Value fetch(
EvalState & state, const PosIdx pos, Value ** args, const std::string & who, bool unpack, std::string name
)
{
std::optional<std::string> url;
std::optional<Hash> expectedHash;
state.forceValue(*args[0], pos);
if (args[0]->type() == nAttrs) {
for (auto & attr : *args[0]->attrs()) {
std::string_view n(state.ctx.symbols[attr.name]);
if (n == "url")
url = state.forceStringNoCtx(
attr.value, attr.pos, "while evaluating the url we should fetch"
);
else if (n == "sha256") {
expectedHash = newHashAllowEmpty(
state.forceStringNoCtx(
attr.value,
attr.pos,
"while evaluating the sha256 of the content we should fetch"
),
HashType::SHA256
);
} else if (n == "name")
name = state.forceStringNoCtx(
attr.value,
attr.pos,
"while evaluating the name of the content we should fetch"
);
else {
state.ctx.errors.make<EvalError>("unsupported argument '%s' to '%s'", n, who)
.atPos(pos)
.debugThrow();
}
}
if (!url)
state.ctx.errors.make<EvalError>(
"'url' argument required").atPos(pos).debugThrow();
} else
url = state.forceStringNoCtx(*args[0], pos, "while evaluating the url we should fetch");
if (who == "fetchTarball")
url = evalSettings.resolvePseudoUrl(*url);
state.ctx.paths.checkURI(*url);
if (name == "")
name = baseNameOf(*url);
if (evalSettings.pureEval && !expectedHash)
state.ctx.errors.make<EvalError>("in pure evaluation mode, '%s' requires a 'sha256' argument", who).atPos(pos).debugThrow();
// early exit if pinned and already in the store
if (expectedHash && expectedHash->type == HashType::SHA256) {
auto expectedPath = state.ctx.store->makeFixedOutputPath(
name,
FixedOutputInfo {
.method = unpack ? FileIngestionMethod::Recursive : FileIngestionMethod::Flat,
.hash = *expectedHash,
.references = {}
});
if (state.aio.blockOn(state.ctx.store->isValidPath(expectedPath))) {
return state.ctx.paths.allowAndSetStorePathString(expectedPath);
}
}
// TODO: fetching may fail, yet the path may be substitutable.
// https://github.com/NixOS/nix/issues/4313
auto storePath = unpack
? state.aio
.blockOn(fetchers::downloadTarball(state.ctx.store, *url, name, (bool) expectedHash))
.tree.storePath
: state.aio
.blockOn(fetchers::downloadFile(state.ctx.store, *url, name, (bool) expectedHash))
.storePath;
if (expectedHash) {
auto hash = unpack
? state.aio.blockOn(state.ctx.store->queryPathInfo(storePath))->narHash
: hashFile(HashType::SHA256, state.ctx.store->toRealPath(storePath));
if (hash != *expectedHash) {
state.ctx.errors
.make<EvalError>(
"hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s",
*url,
expectedHash->to_string(),
hash.to_string()
)
.withExitStatus(102)
.debugThrow();
}
}
return state.ctx.paths.allowAndSetStorePathString(storePath);
}
Value prim_fetchurl(EvalState & state, Value ** args)
{
return fetch(state, noPos, args, "fetchurl", false, "");
}
Value prim_fetchTarball(EvalState & state, Value ** args)
{
return fetch(state, noPos, args, "fetchTarball", true, "source");
}
Value prim_fetchGit(EvalState & state, Value ** args)
{
return fetchTree(
state, noPos, args, "git", FetchTreeParams{.emptyRevFallback = true, .allowNameArgument = true}
);
}
}
|