diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 87ee4f68a..25bf42e1b 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1657,6 +1657,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, if (state.restricted) throw Error(format("‘%1%’ is not allowed in restricted mode") % who); string url; + string name; state.forceValue(*args[0]); @@ -1665,9 +1666,11 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, state.forceAttrs(*args[0], pos); for (auto & attr : *args[0]->attrs) { - string name(attr.name); - if (name == "url") + string n(attr.name); + if (n == "url") url = state.forceStringNoCtx(*attr.value, *attr.pos); + else if (n == "name") + name = state.forceStringNoCtx(*attr.value, *attr.pos); else throw EvalError(format("unsupported argument ‘%1%’ to ‘%2%’, at %3%") % attr.name % who % attr.pos); } @@ -1678,7 +1681,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, } else url = state.forceStringNoCtx(*args[0], pos); - Path res = downloadFileCached(url, unpack); + Path res = downloadFileCached(url, unpack, name); mkString(v, res, PathSet({res})); } diff --git a/src/libstore/download.cc b/src/libstore/download.cc index 822e9a8db..3bc02e3c4 100644 --- a/src/libstore/download.cc +++ b/src/libstore/download.cc @@ -188,7 +188,7 @@ DownloadResult downloadFile(string url, const DownloadOptions & options) } -Path downloadFileCached(const string & url, bool unpack) +Path downloadFileCached(const string & url, bool unpack, string name) { Path cacheDir = getEnv("XDG_CACHE_HOME", getEnv("HOME", "") + "/.cache") + "/nix/tarballs"; createDirs(cacheDir); @@ -223,9 +223,10 @@ Path downloadFileCached(const string & url, bool unpack) storePath = ""; } - string name; - auto p = url.rfind('/'); - if (p != string::npos) name = string(url, p + 1); + if (name == "") { + auto p = url.rfind('/'); + if (p != string::npos) name = string(url, p + 1); + } if (!skip) { diff --git a/src/libstore/download.hh b/src/libstore/download.hh index c1cb25b90..8d025d1cf 100644 --- a/src/libstore/download.hh +++ b/src/libstore/download.hh @@ -20,7 +20,7 @@ struct DownloadResult DownloadResult downloadFile(string url, const DownloadOptions & options); -Path downloadFileCached(const string & url, bool unpack); +Path downloadFileCached(const string & url, bool unpack, string name = ""); MakeError(DownloadError, Error)