From 14080f3e4b9e7c45754b9750424250cadcc066e1 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 27 Nov 2015 14:20:29 -0500 Subject: [PATCH] Use __toString when coercing sets to strings. For example, "${{ foo = "bar"; __toString = x: x.foo; }}" evaluates to "bar". With this, we can delay calling functions like mkDerivation, buildPythonPackage, etc. until we actually need a derivation, enabling overrides and other modifications to happen by simple attribute set update. --- src/libexpr/eval.cc | 10 +++++++++- src/libexpr/eval.hh | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index df1600bc1..3b21c078f 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -261,6 +261,7 @@ EvalState::EvalState(const Strings & _searchPath) , sLine(symbols.create("line")) , sColumn(symbols.create("column")) , sFunctor(symbols.create("__functor")) + , sToString(symbols.create("__toString")) , baseEnv(allocEnv(128)) , staticBaseEnv(false, 0) { @@ -1389,7 +1390,14 @@ string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context, } if (v.type == tAttrs) { - Bindings::iterator i = v.attrs->find(sOutPath); + auto i = v.attrs->find(sToString); + if (i != v.attrs->end()) { + forceValue(*i->value, pos); + Value v1; + callFunction(*i->value, v, v1, pos); + return coerceToString(pos, v1, context, coerceMore, copyToStore); + } + i = v.attrs->find(sOutPath); if (i == v.attrs->end()) throwTypeError("cannot coerce a set to a string, at %1%", pos); return coerceToString(pos, *i->value, context, coerceMore, copyToStore); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index eb55f6d4d..2d7b7bcdc 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -69,7 +69,7 @@ public: const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName, sValue, sSystem, sOverrides, sOutputs, sOutputName, sIgnoreNulls, - sFile, sLine, sColumn, sFunctor; + sFile, sLine, sColumn, sFunctor, sToString; Symbol sDerivationNix; /* If set, force copying files to the Nix store even if they