diff options
author | Maximilian Bosch <maximilian@mbosch.me> | 2021-09-28 22:12:41 +0200 |
---|---|---|
committer | Maximilian Bosch <maximilian@mbosch.me> | 2021-09-29 11:57:15 +0200 |
commit | 2b02ce0e481b653bcc5b403ef5d9e3670a88e8e5 (patch) | |
tree | b0dcdea5d4fa242c2be33b8af2c78e179f0aaf6f /src/libexpr/eval.cc | |
parent | a0bb5c4130268edbd368a4a07d4a30f1064e9c86 (diff) |
libexpr: throw a more helpful eval-error if a builtin is not available due to a missing feature-flag
I found it somewhat confusing to have an error like
error: attribute 'getFlake' missing
if the required experimental-feature (`flakes`) is not enabled. Instead,
I'd expect Nix to throw an error just like it's the case when using e.g. `nix
flake` without `flakes` being enabled.
With this change, the error looks like this:
$ nix-instantiate -E 'builtins.getFlake "nixpkgs"'
error: Cannot call 'builtins.getFlake' because experimental Nix feature 'flakes' is disabled. You can enable it via '--extra-experimental-features flakes'.
at «string»:1:1:
1| builtins.getFlake "nixpkgs"
| ^
I didn't use `settings.requireExperimentalFeature` here on purpose
because this doesn't contain a position. Also, it doesn't seem as if we
need to catch the error and check for the missing feature here since
this already happens at evaluation time.
Diffstat (limited to 'src/libexpr/eval.cc')
-rw-r--r-- | src/libexpr/eval.cc | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index bc41a2cd9..800839a8d 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -465,6 +465,23 @@ EvalState::~EvalState() } +void EvalState::requireExperimentalFeatureOnEvaluation( + const std::string & feature, + const std::string_view fName, + const Pos & pos) +{ + if (!settings.isExperimentalFeatureEnabled(feature)) { + throw EvalError({ + .msg = hintfmt( + "Cannot call '%2%' because experimental Nix feature '%1%' is disabled. You can enable it via '--extra-experimental-features %1%'.", + feature, + fName + ), + .errPos = pos + }); + } +} + Path EvalState::checkSourcePath(const Path & path_) { if (!allowedPaths) return path_; |