2015-07-20 05:30:16 +03:00
|
|
|
#include "builtins.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
#include "filetransfer.hh"
|
2015-10-30 12:27:47 +02:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "archive.hh"
|
2015-10-30 13:33:40 +02:00
|
|
|
#include "compression.hh"
|
2015-07-20 05:30:16 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-02-12 17:34:59 +02:00
|
|
|
void builtinFetchurl(
|
|
|
|
const BasicDerivation & drv,
|
|
|
|
const std::map<std::string, Path> & outputs,
|
|
|
|
const std::string & netrcData)
|
2015-07-20 05:30:16 +03:00
|
|
|
{
|
2017-02-16 16:42:49 +02:00
|
|
|
/* Make the host's netrc data available. Too bad curl requires
|
|
|
|
this to be stored in a file. It would be nice if we could just
|
|
|
|
pass a pointer to the data. */
|
|
|
|
if (netrcData != "") {
|
|
|
|
settings.netrcFile = "netrc";
|
|
|
|
writeFile(settings.netrcFile, netrcData, 0600);
|
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:02 +02:00
|
|
|
auto out = get(drv.outputs, "out");
|
|
|
|
if (!out)
|
|
|
|
throw Error("'builtin:fetchurl' requires an 'out' output");
|
|
|
|
|
2024-02-13 15:14:20 +02:00
|
|
|
if (!(drv.type().isFixed() || drv.type().isImpure()))
|
|
|
|
throw Error("'builtin:fetchurl' must be a fixed-output or impure derivation");
|
2024-02-01 22:46:01 +02:00
|
|
|
|
2024-02-12 17:34:59 +02:00
|
|
|
auto storePath = outputs.at("out");
|
|
|
|
auto mainUrl = drv.env.at("url");
|
2022-05-04 08:44:32 +03:00
|
|
|
bool unpack = getOr(drv.env, "unpack", "") == "1";
|
2018-03-28 14:32:44 +03:00
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
/* Note: have to use a fresh fileTransfer here because we're in
|
2018-03-28 14:32:44 +03:00
|
|
|
a forked process. */
|
2020-04-07 00:43:43 +03:00
|
|
|
auto fileTransfer = makeFileTransfer();
|
2018-03-28 14:32:44 +03:00
|
|
|
|
|
|
|
auto fetch = [&](const std::string & url) {
|
|
|
|
|
|
|
|
auto source = sinkToSource([&](Sink & sink) {
|
|
|
|
|
|
|
|
/* No need to do TLS verification, because we check the hash of
|
|
|
|
the result anyway. */
|
2020-04-07 00:43:43 +03:00
|
|
|
FileTransferRequest request(url);
|
2018-03-28 14:32:44 +03:00
|
|
|
request.verifyTLS = false;
|
|
|
|
request.decompress = false;
|
|
|
|
|
2018-08-06 16:40:29 +03:00
|
|
|
auto decompressor = makeDecompressionSink(
|
2018-09-05 22:22:37 +03:00
|
|
|
unpack && hasSuffix(mainUrl, ".xz") ? "xz" : "none", sink);
|
2020-04-07 00:43:43 +03:00
|
|
|
fileTransfer->download(std::move(request), *decompressor);
|
2018-08-06 16:40:29 +03:00
|
|
|
decompressor->finish();
|
2018-03-28 14:32:44 +03:00
|
|
|
});
|
|
|
|
|
2018-09-05 22:22:37 +03:00
|
|
|
if (unpack)
|
2018-08-06 16:40:29 +03:00
|
|
|
restorePath(storePath, *source);
|
|
|
|
else
|
|
|
|
writeFile(storePath, *source);
|
2018-03-28 14:32:44 +03:00
|
|
|
|
|
|
|
auto executable = drv.env.find("executable");
|
|
|
|
if (executable != drv.env.end() && executable->second == "1") {
|
|
|
|
if (chmod(storePath.c_str(), 0755) == -1)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("making '%1%' executable", storePath);
|
2018-03-28 14:32:44 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-07 02:13:14 +03:00
|
|
|
/* Try the hashed mirrors first. */
|
2024-02-13 15:14:20 +02:00
|
|
|
auto dof = std::get_if<DerivationOutput::CAFixed>(&out->raw);
|
|
|
|
if (dof && dof->ca.method.getFileIngestionMethod() == FileIngestionMethod::Flat)
|
2020-08-07 02:13:14 +03:00
|
|
|
for (auto hashedMirror : settings.hashedMirrors.get())
|
|
|
|
try {
|
|
|
|
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
|
2024-02-01 23:01:02 +02:00
|
|
|
fetch(hashedMirror + printHashAlgo(dof->ca.hash.algo) + "/" + dof->ca.hash.to_string(HashFormat::Base16, false));
|
2020-08-07 02:13:14 +03:00
|
|
|
return;
|
|
|
|
} catch (Error & e) {
|
|
|
|
debug(e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise try the specified URL. */
|
2018-03-28 14:32:44 +03:00
|
|
|
fetch(mainUrl);
|
2015-07-20 05:30:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|