libexpr: Undeprecate overriding __nixPath

It being overridable was an intended feature with good use cases, and
should not have been removed. However, this feature is generally in a
bad state and needs revisiting in the future.

Fixup for 81d5f0a7d9
Fixes #599

Change-Id: I2d93e012caa65aa795bce3a71d8e56d7052ef9df
This commit is contained in:
piegames 2024-12-10 18:54:17 +01:00
parent 369e3f82f0
commit 7c76053d93
4 changed files with 7 additions and 4 deletions

View file

@ -15,4 +15,4 @@ and can be disabled via the flags for backwards compatibility (opt-out with `--e
- `rec-set-overrides`: **__overrides** is an old arcane syntax which has not been in use for more than a decade.
It is soft-deprecated with a warning only, with the plan to turn that into an error in a future release.
- `ancient-let`: **The old `let` syntax** (`let { body = …; … }`) is soft-deprecated with a warning as well. Use the regular `let … in` instead.
- `shadow-internal-symbols`: Arithmetic expressions like `5 - 3` internally expand to `__sub 5 3`, where `__sub` maps to a subtraction builtin. Shadowing such a symbols would affect the evaluation of such operations, but in a very inconsistent way, and is therefore deprecated now. **Affected symbols are:** `__sub`, `__mul`, `__div`, `__lessThan`, `__findFile` and `__nixPath`. Note that these symbols may still be used as variable names as long as they do not shadow internal operations, so e.g. `let __sub = x: y: x + y; in __sub 3 5` remains valid code.
- `shadow-internal-symbols`: Arithmetic expressions like `5 - 3` internally expand to `__sub 5 3`, where `__sub` maps to a subtraction builtin. Shadowing such a symbols would affect the evaluation of such operations, but in a very inconsistent way, and is therefore deprecated now. **Affected symbols are:** `__sub`, `__mul`, `__div`, `__lessThan`, and `__findFile`. Note that these symbols may still be used as variable names as long as they do not shadow internal operations, so e.g. `let __sub = x: y: x + y; in __sub 3 5` remains valid code.

View file

@ -626,7 +626,11 @@ template<> struct BuildAST<grammar::v1::path::searched_path> {
static void apply(const auto & in, StringState & s, State & ps) {
auto pos = ps.at(in);
std::vector<std::unique_ptr<Expr>> args{2};
args[0] = ps.mkInternalVar(pos, ps.s.nixPath);
/* Overriding __nixPath, while being barely documented, is intended and supported:
* https://github.com/NixOS/nix/commit/62a6eeb1f3da0a5954ad2da54c454eb7fc1c6e5d
* (TODO: Provide a better and officially supported and documented mechanism for doing this)
*/
args[0] = std::make_unique<ExprVar>(pos, ps.s.nixPath);
args[1] = std::make_unique<ExprString>(in.string());
s.parts.emplace_back(
pos,

View file

@ -2,4 +2,4 @@
name: shadow-internal-symbols
internalName: ShadowInternalSymbols
---
Allow shadowing the symbols `__sub`, `__mul`, `__div`, `__lessThan`, `__findFile` and `__nixPath` when used in the internal AST expansion. (`5 - 3` expands to `__sub 5 3` etc.)
Allow shadowing the symbols `__sub`, `__mul`, `__div`, `__lessThan`, `__findFile` when used in the internal AST expansion. (`5 - 3` expands to `__sub 5 3` etc.)

View file

@ -278,6 +278,5 @@ namespace nix {
);
ASSERT_THAT(v, IsStringEq("bar"));
ASSERT_THROW(eval("let __findFile = _: _: ''found''; in import <foo>"), Error);
ASSERT_THROW(eval("let __nixPath = null; in import <foo>"), Error);
}
} /* namespace nix */