2020-03-30 17:04:18 +03:00
|
|
|
|
#include "primops.hh"
|
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "fetchers.hh"
|
2020-04-08 15:12:22 +03:00
|
|
|
|
#include "filetransfer.hh"
|
2020-04-07 15:29:45 +03:00
|
|
|
|
#include "registry.hh"
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <iomanip>
|
2021-07-02 20:20:07 +03:00
|
|
|
|
#include <regex>
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
void emitTreeAttrs(
|
|
|
|
|
EvalState & state,
|
|
|
|
|
const fetchers::Tree & tree,
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
const fetchers::Input & input,
|
2020-07-28 23:46:39 +03:00
|
|
|
|
Value & v,
|
2021-09-15 19:31:42 +03:00
|
|
|
|
bool emptyRevFallback,
|
|
|
|
|
bool forceDirty)
|
2020-03-30 17:04:18 +03:00
|
|
|
|
{
|
2022-02-24 19:09:00 +02:00
|
|
|
|
assert(input.isLocked());
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
|
auto attrs = state.buildBindings(8);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
auto storePath = state.store->printStorePath(tree.storePath);
|
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc(state.sOutPath).mkString(storePath, {storePath});
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
// FIXME: support arbitrary input attributes.
|
|
|
|
|
|
|
|
|
|
auto narHash = input.getNarHash();
|
|
|
|
|
assert(narHash);
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("narHash").mkString(narHash->to_string(SRI, true));
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2020-04-29 23:39:58 +03:00
|
|
|
|
if (input.getType() == "git")
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("submodules").mkBool(
|
2021-09-22 18:25:25 +03:00
|
|
|
|
fetchers::maybeGetBoolAttr(input.attrs, "submodules").value_or(false));
|
2020-04-29 23:39:58 +03:00
|
|
|
|
|
2021-09-15 19:31:42 +03:00
|
|
|
|
if (!forceDirty) {
|
|
|
|
|
|
|
|
|
|
if (auto rev = input.getRev()) {
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("rev").mkString(rev->gitRev());
|
|
|
|
|
attrs.alloc("shortRev").mkString(rev->gitShortRev());
|
2021-09-15 19:31:42 +03:00
|
|
|
|
} else if (emptyRevFallback) {
|
|
|
|
|
// Backwards compat for `builtins.fetchGit`: dirty repos return an empty sha1 as rev
|
|
|
|
|
auto emptyHash = Hash(htSHA1);
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("rev").mkString(emptyHash.gitRev());
|
|
|
|
|
attrs.alloc("shortRev").mkString(emptyHash.gitShortRev());
|
2021-09-15 19:31:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto revCount = input.getRevCount())
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("revCount").mkInt(*revCount);
|
2021-09-15 19:31:42 +03:00
|
|
|
|
else if (emptyRevFallback)
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("revCount").mkInt(0);
|
2021-09-15 19:31:42 +03:00
|
|
|
|
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
if (auto lastModified = input.getLastModified()) {
|
2022-01-04 18:39:16 +02:00
|
|
|
|
attrs.alloc("lastModified").mkInt(*lastModified);
|
|
|
|
|
attrs.alloc("lastModifiedDate").mkString(
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
fmt("%s", std::put_time(std::gmtime(&*lastModified), "%Y%m%d%H%M%S")));
|
2020-04-02 19:39:41 +03:00
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
|
v.mkAttrs(attrs);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 18:29:47 +03:00
|
|
|
|
std::string fixURI(std::string uri, EvalState & state, const std::string & defaultScheme = "file")
|
2020-03-30 17:04:18 +03:00
|
|
|
|
{
|
2020-04-29 23:39:58 +03:00
|
|
|
|
state.checkURI(uri);
|
2021-07-02 20:20:07 +03:00
|
|
|
|
return uri.find("://") != std::string::npos ? uri : defaultScheme + "://" + uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string fixURIForGit(std::string uri, EvalState & state)
|
|
|
|
|
{
|
2021-11-17 09:41:26 +02:00
|
|
|
|
/* Detects scp-style uris (e.g. git@github.com:NixOS/nix) and fixes
|
|
|
|
|
* them by removing the `:` and assuming a scheme of `ssh://`
|
|
|
|
|
* */
|
2021-11-17 14:49:10 +02:00
|
|
|
|
static std::regex scp_uri("([^/]*)@(.*):(.*)");
|
|
|
|
|
if (uri[0] != '/' && std::regex_match(uri, scp_uri))
|
2021-07-02 20:20:07 +03:00
|
|
|
|
return fixURI(std::regex_replace(uri, scp_uri, "$1@$2/$3"), state, "ssh");
|
|
|
|
|
else
|
|
|
|
|
return fixURI(uri, state);
|
2020-04-29 23:39:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 15:57:48 +03:00
|
|
|
|
struct FetchTreeParams {
|
|
|
|
|
bool emptyRevFallback = false;
|
|
|
|
|
bool allowNameArgument = false;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-29 23:39:58 +03:00
|
|
|
|
static void fetchTree(
|
2021-10-06 18:29:47 +03:00
|
|
|
|
EvalState & state,
|
|
|
|
|
const Pos & pos,
|
|
|
|
|
Value * * args,
|
|
|
|
|
Value & v,
|
2021-10-06 18:39:02 +03:00
|
|
|
|
std::optional<std::string> type,
|
2021-07-08 15:57:48 +03:00
|
|
|
|
const FetchTreeParams & params = FetchTreeParams{}
|
2020-04-29 23:39:58 +03:00
|
|
|
|
) {
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
fetchers::Input input;
|
2020-03-30 17:04:18 +03:00
|
|
|
|
PathSet context;
|
|
|
|
|
|
2021-10-16 02:56:17 +03:00
|
|
|
|
state.forceValue(*args[0], pos);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
|
if (args[0]->type() == nAttrs) {
|
2020-03-30 17:04:18 +03:00
|
|
|
|
state.forceAttrs(*args[0], pos);
|
|
|
|
|
|
|
|
|
|
fetchers::Attrs attrs;
|
|
|
|
|
|
2021-10-06 18:39:02 +03:00
|
|
|
|
if (auto aType = args[0]->attrs->get(state.sType)) {
|
|
|
|
|
if (type)
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError({
|
2021-10-06 18:39:02 +03:00
|
|
|
|
.msg = hintfmt("unexpected attribute 'type'"),
|
|
|
|
|
.errPos = pos
|
2022-04-08 21:34:27 +03:00
|
|
|
|
});
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
|
|
|
|
|
}
|
2021-10-06 18:39:02 +03:00
|
|
|
|
type = state.forceStringNoCtx(*aType->value, *aType->pos);
|
|
|
|
|
} else if (!type)
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError({
|
2021-10-06 18:39:02 +03:00
|
|
|
|
.msg = hintfmt("attribute 'type' is missing in call to 'fetchTree'"),
|
|
|
|
|
.errPos = pos
|
2022-04-08 21:34:27 +03:00
|
|
|
|
});
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2021-10-06 18:39:02 +03:00
|
|
|
|
|
|
|
|
|
attrs.emplace("type", type.value());
|
|
|
|
|
|
2020-03-30 17:04:18 +03:00
|
|
|
|
for (auto & attr : *args[0]->attrs) {
|
2021-10-06 18:39:02 +03:00
|
|
|
|
if (attr.name == state.sType) continue;
|
2021-10-16 03:21:38 +03:00
|
|
|
|
state.forceValue(*attr.value, *attr.pos);
|
2021-10-06 18:29:47 +03:00
|
|
|
|
if (attr.value->type() == nPath || attr.value->type() == nString) {
|
2022-01-21 17:20:54 +02:00
|
|
|
|
auto s = state.coerceToString(*attr.pos, *attr.value, context, false, false).toOwned();
|
2021-10-06 18:29:47 +03:00
|
|
|
|
attrs.emplace(attr.name,
|
|
|
|
|
attr.name == "url"
|
|
|
|
|
? type == "git"
|
|
|
|
|
? fixURIForGit(s, state)
|
|
|
|
|
: fixURI(s, state)
|
|
|
|
|
: s);
|
|
|
|
|
}
|
2020-12-17 15:45:45 +02:00
|
|
|
|
else if (attr.value->type() == nBool)
|
2020-10-26 17:58:58 +02:00
|
|
|
|
attrs.emplace(attr.name, Explicit<bool>{attr.value->boolean});
|
2020-12-29 03:40:04 +02:00
|
|
|
|
else if (attr.value->type() == nInt)
|
2020-12-08 22:16:06 +02:00
|
|
|
|
attrs.emplace(attr.name, uint64_t(attr.value->integer));
|
2020-03-30 17:04:18 +03:00
|
|
|
|
else
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = TypeError("fetchTree argument '%s' is %s while a string, Boolean or integer is expected",
|
|
|
|
|
attr.name, showType(*attr.value));
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 15:57:48 +03:00
|
|
|
|
if (!params.allowNameArgument)
|
|
|
|
|
if (auto nameIter = attrs.find("name"); nameIter != attrs.end())
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError({
|
2021-07-08 15:57:48 +03:00
|
|
|
|
.msg = hintfmt("attribute 'name' isn’t supported in call to 'fetchTree'"),
|
|
|
|
|
.errPos = pos
|
2022-04-08 21:34:27 +03:00
|
|
|
|
});
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2021-07-08 14:29:58 +03:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
input = fetchers::Input::fromAttrs(std::move(attrs));
|
2020-04-29 23:39:58 +03:00
|
|
|
|
} else {
|
2022-01-21 17:20:54 +02:00
|
|
|
|
auto url = state.coerceToString(pos, *args[0], context, false, false).toOwned();
|
2020-04-29 23:39:58 +03:00
|
|
|
|
|
|
|
|
|
if (type == "git") {
|
|
|
|
|
fetchers::Attrs attrs;
|
|
|
|
|
attrs.emplace("type", "git");
|
2021-07-02 20:20:07 +03:00
|
|
|
|
attrs.emplace("url", fixURIForGit(url, state));
|
2020-04-29 23:39:58 +03:00
|
|
|
|
input = fetchers::Input::fromAttrs(std::move(attrs));
|
|
|
|
|
} else {
|
2021-07-02 20:20:07 +03:00
|
|
|
|
input = fetchers::Input::fromURL(fixURI(url, state));
|
2020-04-29 23:39:58 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 00:53:46 +02:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
if (!evalSettings.pureEval && !input.isDirect())
|
2020-02-20 23:14:44 +02:00
|
|
|
|
input = lookupInRegistries(state.store, input).first;
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2022-02-24 19:09:00 +02:00
|
|
|
|
if (evalSettings.pureEval && !input.isLocked())
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError("in pure evaluation mode, 'fetchTree' requires a locked input, at %s", pos);
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
|
auto [tree, input2] = input.fetch(state.store);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2021-10-07 15:07:51 +03:00
|
|
|
|
state.allowPath(tree.storePath);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2021-09-15 19:31:42 +03:00
|
|
|
|
emitTreeAttrs(state, tree, input2, v, params.emptyRevFallback, false);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 23:39:58 +03:00
|
|
|
|
static void prim_fetchTree(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
|
{
|
2021-10-25 16:53:01 +03:00
|
|
|
|
settings.requireExperimentalFeature(Xp::Flakes);
|
2021-07-08 15:57:48 +03:00
|
|
|
|
fetchTree(state, pos, args, v, std::nullopt, FetchTreeParams { .allowNameArgument = false });
|
2020-04-29 23:39:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 15:43:16 +02:00
|
|
|
|
// FIXME: document
|
2020-10-06 14:36:55 +03:00
|
|
|
|
static RegisterPrimOp primop_fetchTree("fetchTree", 1, prim_fetchTree);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
|
2022-02-25 17:00:00 +02:00
|
|
|
|
const std::string & who, bool unpack, std::string name)
|
2020-03-30 17:04:18 +03:00
|
|
|
|
{
|
|
|
|
|
std::optional<std::string> url;
|
|
|
|
|
std::optional<Hash> expectedHash;
|
|
|
|
|
|
2021-10-16 05:03:01 +03:00
|
|
|
|
state.forceValue(*args[0], pos);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
|
if (args[0]->type() == nAttrs) {
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
state.forceAttrs(*args[0], pos);
|
|
|
|
|
|
|
|
|
|
for (auto & attr : *args[0]->attrs) {
|
2022-02-25 17:00:00 +02:00
|
|
|
|
std::string n(attr.name);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
if (n == "url")
|
|
|
|
|
url = state.forceStringNoCtx(*attr.value, *attr.pos);
|
|
|
|
|
else if (n == "sha256")
|
2020-06-12 18:09:42 +03:00
|
|
|
|
expectedHash = newHashAllowEmpty(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
else if (n == "name")
|
|
|
|
|
name = state.forceStringNoCtx(*attr.value, *attr.pos);
|
2022-04-08 21:34:27 +03:00
|
|
|
|
else {
|
|
|
|
|
auto e = EvalError({
|
|
|
|
|
.msg = hintfmt("unsupported argument '%s' to '%s'", attr.name, who),
|
|
|
|
|
.errPos = *attr.pos
|
|
|
|
|
});
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
2020-05-12 19:52:26 +03:00
|
|
|
|
}
|
2022-04-08 21:34:27 +03:00
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
if (!url)
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError({
|
2021-01-21 01:27:36 +02:00
|
|
|
|
.msg = hintfmt("'url' argument required"),
|
2020-06-24 00:30:13 +03:00
|
|
|
|
.errPos = pos
|
2022-04-08 21:34:27 +03:00
|
|
|
|
});
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
} else
|
|
|
|
|
url = state.forceStringNoCtx(*args[0], pos);
|
|
|
|
|
|
|
|
|
|
url = resolveUri(*url);
|
|
|
|
|
|
|
|
|
|
state.checkURI(*url);
|
|
|
|
|
|
|
|
|
|
if (name == "")
|
|
|
|
|
name = baseNameOf(*url);
|
|
|
|
|
|
|
|
|
|
if (evalSettings.pureEval && !expectedHash)
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError("in pure evaluation mode, '%s' requires a 'sha256' argument", who);
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2022-02-27 16:25:22 +02:00
|
|
|
|
// early exit if pinned and already in the store
|
|
|
|
|
if (expectedHash && expectedHash->type == htSHA256) {
|
|
|
|
|
auto expectedPath =
|
|
|
|
|
unpack
|
|
|
|
|
? state.store->makeFixedOutputPath(FileIngestionMethod::Recursive, *expectedHash, name, {})
|
|
|
|
|
: state.store->makeFixedOutputPath(FileIngestionMethod::Flat, *expectedHash, name, {});
|
|
|
|
|
|
2022-03-01 13:11:10 +02:00
|
|
|
|
if (state.store->isValidPath(expectedPath)) {
|
2022-02-27 16:59:34 +02:00
|
|
|
|
state.allowAndSetStorePathString(expectedPath, v);
|
2022-02-27 16:25:22 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 13:11:10 +02:00
|
|
|
|
// TODO: fetching may fail, yet the path may be substitutable.
|
|
|
|
|
// https://github.com/NixOS/nix/issues/4313
|
2020-03-30 17:04:18 +03:00
|
|
|
|
auto storePath =
|
|
|
|
|
unpack
|
2020-09-29 14:05:19 +03:00
|
|
|
|
? fetchers::downloadTarball(state.store, *url, name, (bool) expectedHash).first.storePath
|
|
|
|
|
: fetchers::downloadFile(state.store, *url, name, (bool) expectedHash).storePath;
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
if (expectedHash) {
|
|
|
|
|
auto hash = unpack
|
|
|
|
|
? state.store->queryPathInfo(storePath)->narHash
|
2021-10-07 15:07:51 +03:00
|
|
|
|
: hashFile(htSHA256, state.store->toRealPath(storePath));
|
2020-03-30 17:04:18 +03:00
|
|
|
|
if (hash != *expectedHash)
|
2022-04-08 21:34:27 +03:00
|
|
|
|
{
|
|
|
|
|
auto e = EvalError((unsigned int) 102, "hash mismatch in file downloaded from '%s':\n specified: %s\n got: %s",
|
|
|
|
|
*url, expectedHash->to_string(Base32, true), hash.to_string(Base32, true));
|
|
|
|
|
state.debugLastTrace(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 16:59:34 +02:00
|
|
|
|
state.allowAndSetStorePathString(storePath, v);
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void prim_fetchurl(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
|
{
|
|
|
|
|
fetch(state, pos, args, v, "fetchurl", false, "");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 15:31:10 +03:00
|
|
|
|
static RegisterPrimOp primop_fetchurl({
|
|
|
|
|
.name = "__fetchurl",
|
|
|
|
|
.args = {"url"},
|
|
|
|
|
.doc = R"(
|
|
|
|
|
Download the specified URL and return the path of the downloaded
|
|
|
|
|
file. This function is not available if [restricted evaluation
|
|
|
|
|
mode](../command-ref/conf-file.md) is enabled.
|
|
|
|
|
)",
|
|
|
|
|
.fun = prim_fetchurl,
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-30 17:04:18 +03:00
|
|
|
|
static void prim_fetchTarball(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
|
{
|
|
|
|
|
fetch(state, pos, args, v, "fetchTarball", true, "source");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 15:31:10 +03:00
|
|
|
|
static RegisterPrimOp primop_fetchTarball({
|
|
|
|
|
.name = "fetchTarball",
|
|
|
|
|
.args = {"args"},
|
|
|
|
|
.doc = R"(
|
|
|
|
|
Download the specified URL, unpack it and return the path of the
|
|
|
|
|
unpacked tree. The file must be a tape archive (`.tar`) compressed
|
|
|
|
|
with `gzip`, `bzip2` or `xz`. The top-level path component of the
|
|
|
|
|
files in the tarball is removed, so it is best if the tarball
|
|
|
|
|
contains a single directory at top level. The typical use of the
|
|
|
|
|
function is to obtain external Nix expression dependencies, such as
|
|
|
|
|
a particular version of Nixpkgs, e.g.
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
with import (fetchTarball https://github.com/NixOS/nixpkgs/archive/nixos-14.12.tar.gz) {};
|
|
|
|
|
|
|
|
|
|
stdenv.mkDerivation { … }
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-26 15:21:24 +03:00
|
|
|
|
The fetched tarball is cached for a certain amount of time (1
|
|
|
|
|
hour by default) in `~/.cache/nix/tarballs/`. You can change the
|
|
|
|
|
cache timeout either on the command line with `--tarball-ttl`
|
|
|
|
|
*number-of-seconds* or in the Nix configuration file by adding
|
|
|
|
|
the line `tarball-ttl = ` *number-of-seconds*.
|
2020-08-24 15:31:10 +03:00
|
|
|
|
|
2021-10-26 15:21:24 +03:00
|
|
|
|
Note that when obtaining the hash with `nix-prefetch-url` the
|
2020-08-24 15:31:10 +03:00
|
|
|
|
option `--unpack` is required.
|
|
|
|
|
|
|
|
|
|
This function can also verify the contents against a hash. In that
|
|
|
|
|
case, the function takes a set instead of a URL. The set requires
|
|
|
|
|
the attribute `url` and the attribute `sha256`, e.g.
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
with import (fetchTarball {
|
|
|
|
|
url = "https://github.com/NixOS/nixpkgs/archive/nixos-14.12.tar.gz";
|
|
|
|
|
sha256 = "1jppksrfvbk5ypiqdz4cddxdl8z6zyzdb2srq8fcffr327ld5jj2";
|
|
|
|
|
}) {};
|
|
|
|
|
|
|
|
|
|
stdenv.mkDerivation { … }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This function is not available if [restricted evaluation
|
|
|
|
|
mode](../command-ref/conf-file.md) is enabled.
|
|
|
|
|
)",
|
|
|
|
|
.fun = prim_fetchTarball,
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-21 12:31:01 +02:00
|
|
|
|
static void prim_fetchGit(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
2020-04-29 23:39:58 +03:00
|
|
|
|
{
|
2021-07-08 15:57:48 +03:00
|
|
|
|
fetchTree(state, pos, args, v, "git", FetchTreeParams { .emptyRevFallback = true, .allowNameArgument = true });
|
2020-04-29 23:39:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 15:31:10 +03:00
|
|
|
|
static RegisterPrimOp primop_fetchGit({
|
|
|
|
|
.name = "fetchGit",
|
|
|
|
|
.args = {"args"},
|
|
|
|
|
.doc = R"(
|
|
|
|
|
Fetch a path from git. *args* can be a URL, in which case the HEAD
|
|
|
|
|
of the repo at that URL is fetched. Otherwise, it can be an
|
|
|
|
|
attribute with the following attributes (all except `url` optional):
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- url\
|
2020-08-24 15:31:10 +03:00
|
|
|
|
The URL of the repo.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- name\
|
2020-08-24 15:31:10 +03:00
|
|
|
|
The name of the directory the repo should be exported to in the
|
|
|
|
|
store. Defaults to the basename of the URL.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- rev\
|
2020-08-24 15:31:10 +03:00
|
|
|
|
The git revision to fetch. Defaults to the tip of `ref`.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- ref\
|
2020-08-24 15:31:10 +03:00
|
|
|
|
The git ref to look for the requested revision under. This is
|
|
|
|
|
often a branch or tag name. Defaults to `HEAD`.
|
|
|
|
|
|
|
|
|
|
By default, the `ref` value is prefixed with `refs/heads/`. As
|
|
|
|
|
of Nix 2.3.0 Nix will not prefix `refs/heads/` if `ref` starts
|
|
|
|
|
with `refs/`.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- submodules\
|
2020-08-24 15:31:10 +03:00
|
|
|
|
A Boolean parameter that specifies whether submodules should be
|
|
|
|
|
checked out. Defaults to `false`.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- allRefs\
|
2020-09-08 20:45:28 +03:00
|
|
|
|
Whether to fetch all refs of the repository. With this argument being
|
|
|
|
|
true, it's possible to load a `rev` from *any* `ref` (by default only
|
|
|
|
|
`rev`s from the specified `ref` are supported).
|
|
|
|
|
|
2020-08-24 15:31:10 +03:00
|
|
|
|
Here are some examples of how to use `fetchGit`.
|
|
|
|
|
|
|
|
|
|
- To fetch a private repository over SSH:
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "git@github.com:my-secret/repository.git";
|
|
|
|
|
ref = "master";
|
|
|
|
|
rev = "adab8b916a45068c044658c4158d81878f9ed1c3";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- To fetch an arbitrary reference:
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "https://github.com/NixOS/nix.git";
|
|
|
|
|
ref = "refs/heads/0.5-release";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- If the revision you're looking for is in the default branch of
|
|
|
|
|
the git repository you don't strictly need to specify the branch
|
|
|
|
|
name in the `ref` attribute.
|
|
|
|
|
|
|
|
|
|
However, if the revision you're looking for is in a future
|
|
|
|
|
branch for the non-default branch you will need to specify the
|
|
|
|
|
the `ref` attribute as well.
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "https://github.com/nixos/nix.git";
|
|
|
|
|
rev = "841fcbd04755c7a2865c51c1e2d3b045976b7452";
|
|
|
|
|
ref = "1.11-maintenance";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> **Note**
|
2021-10-25 16:53:01 +03:00
|
|
|
|
>
|
2020-08-24 15:31:10 +03:00
|
|
|
|
> It is nice to always specify the branch which a revision
|
|
|
|
|
> belongs to. Without the branch being specified, the fetcher
|
|
|
|
|
> might fail if the default branch changes. Additionally, it can
|
|
|
|
|
> be confusing to try a commit from a non-default branch and see
|
|
|
|
|
> the fetch fail. If the branch is specified the fault is much
|
|
|
|
|
> more obvious.
|
|
|
|
|
|
|
|
|
|
- If the revision you're looking for is in the default branch of
|
|
|
|
|
the git repository you may omit the `ref` attribute.
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "https://github.com/nixos/nix.git";
|
|
|
|
|
rev = "841fcbd04755c7a2865c51c1e2d3b045976b7452";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- To fetch a specific tag:
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "https://github.com/nixos/nix.git";
|
|
|
|
|
ref = "refs/tags/1.9";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- To fetch the latest version of a remote branch:
|
|
|
|
|
|
|
|
|
|
```nix
|
|
|
|
|
builtins.fetchGit {
|
|
|
|
|
url = "ssh://git@github.com/nixos/nix.git";
|
|
|
|
|
ref = "master";
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> **Note**
|
2021-10-25 16:53:01 +03:00
|
|
|
|
>
|
2020-08-24 15:31:10 +03:00
|
|
|
|
> Nix will refetch the branch in accordance with
|
|
|
|
|
> the option `tarball-ttl`.
|
|
|
|
|
|
|
|
|
|
> **Note**
|
2021-10-25 16:53:01 +03:00
|
|
|
|
>
|
2020-08-24 15:31:10 +03:00
|
|
|
|
> This behavior is disabled in *Pure evaluation mode*.
|
|
|
|
|
)",
|
|
|
|
|
.fun = prim_fetchGit,
|
|
|
|
|
});
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
}
|