2017-11-01 17:32:53 +02:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "eval-inline.hh"
|
2023-07-31 16:19:19 +03:00
|
|
|
#include "eval-settings.hh"
|
2017-11-01 17:32:53 +02:00
|
|
|
#include "store-api.hh"
|
2020-03-30 17:04:18 +03:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "url.hh"
|
2020-09-21 19:22:45 +03:00
|
|
|
#include "url-parts.hh"
|
2017-11-01 17:32:53 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2022-03-04 20:31:59 +02:00
|
|
|
static void prim_fetchMercurial(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
2017-11-01 17:32:53 +02:00
|
|
|
{
|
|
|
|
std::string url;
|
2020-03-30 17:04:18 +03:00
|
|
|
std::optional<Hash> rev;
|
|
|
|
std::optional<std::string> ref;
|
2022-01-21 15:44:00 +02:00
|
|
|
std::string_view name = "source";
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
NixStringContext context;
|
2017-11-01 17:32:53 +02:00
|
|
|
|
2021-10-15 07:23:04 +03:00
|
|
|
state.forceValue(*args[0], pos);
|
2017-11-01 17:32:53 +02:00
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
if (args[0]->type() == nAttrs) {
|
2017-11-01 17:32:53 +02:00
|
|
|
|
2024-03-25 19:20:18 +02:00
|
|
|
for (auto & attr : *args[0]->attrs()) {
|
2022-03-05 15:40:24 +02:00
|
|
|
std::string_view n(state.symbols[attr.name]);
|
2017-11-01 17:32:53 +02:00
|
|
|
if (n == "url")
|
2022-11-29 01:25:36 +02:00
|
|
|
url = state.coerceToString(attr.pos, *attr.value, context,
|
|
|
|
"while evaluating the `url` attribute passed to builtins.fetchMercurial",
|
|
|
|
false, false).toOwned();
|
2020-03-30 17:04:18 +03:00
|
|
|
else if (n == "rev") {
|
|
|
|
// Ugly: unlike fetchGit, here the "rev" attribute can
|
|
|
|
// be both a revision or a branch/tag name.
|
2023-01-19 14:23:04 +02:00
|
|
|
auto value = state.forceStringNoCtx(*attr.value, attr.pos, "while evaluating the `rev` attribute passed to builtins.fetchMercurial");
|
2022-01-21 15:44:00 +02:00
|
|
|
if (std::regex_match(value.begin(), value.end(), revRegex))
|
2023-11-28 15:20:27 +02:00
|
|
|
rev = Hash::parseAny(value, HashAlgorithm::SHA1);
|
2020-03-30 17:04:18 +03:00
|
|
|
else
|
|
|
|
ref = value;
|
|
|
|
}
|
2017-11-01 17:32:53 +02:00
|
|
|
else if (n == "name")
|
2023-01-19 14:23:04 +02:00
|
|
|
name = state.forceStringNoCtx(*attr.value, attr.pos, "while evaluating the `name` attribute passed to builtins.fetchMercurial");
|
2017-11-01 17:32:53 +02:00
|
|
|
else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
state.error<EvalError>("unsupported argument '%s' to 'fetchMercurial'", state.symbols[attr.name]).atPos(attr.pos).debugThrow();
|
2017-11-01 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url.empty())
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
state.error<EvalError>("'url' argument required").atPos(pos).debugThrow();
|
2017-11-01 17:32:53 +02:00
|
|
|
|
|
|
|
} else
|
2022-11-29 01:25:36 +02:00
|
|
|
url = state.coerceToString(pos, *args[0], context,
|
|
|
|
"while evaluating the first argument passed to builtins.fetchMercurial",
|
|
|
|
false, false).toOwned();
|
2017-11-01 17:32:53 +02:00
|
|
|
|
|
|
|
// FIXME: git externals probably can be used to bypass the URI
|
|
|
|
// whitelist. Ah well.
|
|
|
|
state.checkURI(url);
|
|
|
|
|
2024-06-14 19:41:09 +03:00
|
|
|
if (state.settings.pureEval && !rev)
|
2020-03-30 17:04:18 +03:00
|
|
|
throw Error("in pure evaluation mode, 'fetchMercurial' requires a Mercurial revision");
|
|
|
|
|
2020-04-07 15:00:12 +03:00
|
|
|
fetchers::Attrs attrs;
|
|
|
|
attrs.insert_or_assign("type", "hg");
|
|
|
|
attrs.insert_or_assign("url", url.find("://") != std::string::npos ? url : "file://" + url);
|
2022-02-25 17:00:00 +02:00
|
|
|
attrs.insert_or_assign("name", std::string(name));
|
2020-04-07 15:00:12 +03:00
|
|
|
if (ref) attrs.insert_or_assign("ref", *ref);
|
|
|
|
if (rev) attrs.insert_or_assign("rev", rev->gitRev());
|
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 input = fetchers::Input::fromAttrs(std::move(attrs));
|
2020-03-30 17:04:18 +03:00
|
|
|
|
2024-03-04 23:17:24 +02:00
|
|
|
auto [storePath, input2] = input.fetchToStore(state.store);
|
2017-11-01 17:32:53 +02:00
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
auto attrs2 = state.buildBindings(8);
|
2023-10-20 20:50:21 +03:00
|
|
|
state.mkStorePathString(storePath, attrs2.alloc(state.sOutPath));
|
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 (input2.getRef())
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs2.alloc("branch").mkString(*input2.getRef());
|
2020-03-30 17:04:18 +03:00
|
|
|
// Backward compatibility: set 'rev' to
|
|
|
|
// 0000000000000000000000000000000000000000 for a dirty tree.
|
2023-11-28 15:20:27 +02:00
|
|
|
auto rev2 = input2.getRev().value_or(Hash(HashAlgorithm::SHA1));
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs2.alloc("rev").mkString(rev2.gitRev());
|
|
|
|
attrs2.alloc("shortRev").mkString(rev2.gitRev().substr(0, 12));
|
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 revCount = input2.getRevCount())
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs2.alloc("revCount").mkInt(*revCount);
|
|
|
|
v.mkAttrs(attrs2);
|
2018-01-16 19:50:38 +02:00
|
|
|
|
2023-10-20 20:50:21 +03:00
|
|
|
state.allowPath(storePath);
|
2017-11-01 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 17:57:50 +03:00
|
|
|
static RegisterPrimOp r_fetchMercurial({
|
|
|
|
.name = "fetchMercurial",
|
|
|
|
.arity = 1,
|
|
|
|
.fun = prim_fetchMercurial
|
|
|
|
});
|
2017-11-01 17:32:53 +02:00
|
|
|
|
|
|
|
}
|