5#include "nix/expr/eval-error.hh"
15#include "nix/util/source-accessor.hh"
17#include "nix/expr/repl-exit-status.hh"
19#include "nix/expr/counter.hh"
22#include "nix/expr/config.hh"
24#include <boost/unordered/unordered_flat_map.hpp>
25#include <boost/unordered/concurrent_flat_map_fwd.hpp>
51struct SingleDerivedPath;
52enum RepairFlag : bool;
53struct MemorySourceAccessor;
54struct MountedSourceAccessor;
68 CallDepth(
size_t & count)
134 std::optional<std::string>
doc;
165std::ostream & operator<<(std::ostream & output,
const PrimOp & primOp);
182 const char *
doc =
nullptr;
191 map<std::string, Value *, std::less<std::string>, traceable_allocator<std::pair<const std::string, Value *>>>
194typedef boost::unordered_flat_map<PosIdx, DocComment, std::hash<PosIdx>> DocCommentMap;
202void printEnvBindings(
const EvalState & es,
const Expr & expr,
const Env & env);
213std::ostream & operator<<(std::ostream & os,
const ValueType t);
239 std::variant<Pos, PosIdx> pos;
255 [&](
Pos pos) {
return pos; },
263 Symbol with, outPath, drvPath, type, meta, name, value, system, overrides, outputs, outputName, ignoreNulls, file,
264 line, column, functor, toString, right, wrong, structuredAttrs, json, allowedReferences, allowedRequisites,
265 disallowedReferences, disallowedRequisites, maxSize, maxClosureSize, builder, args, contentAddressed, impure,
266 outputHash, outputHashAlgo, outputHashMode, recurseForDerivations, description, self, epsilon, startSet,
267 operator_, key, path, prefix, outputSpecified, requiredSystemFeatures;
271 static constexpr auto preallocate()
276 .with = alloc.create(
"<with>"),
277 .outPath = alloc.create(
"outPath"),
278 .drvPath = alloc.create(
"drvPath"),
279 .type = alloc.create(
"type"),
280 .meta = alloc.create(
"meta"),
281 .name = alloc.create(
"name"),
282 .value = alloc.create(
"value"),
283 .system = alloc.create(
"system"),
284 .overrides = alloc.create(
"__overrides"),
285 .outputs = alloc.create(
"outputs"),
286 .outputName = alloc.create(
"outputName"),
287 .ignoreNulls = alloc.create(
"__ignoreNulls"),
288 .file = alloc.create(
"file"),
289 .line = alloc.create(
"line"),
290 .column = alloc.create(
"column"),
291 .functor = alloc.create(
"__functor"),
292 .toString = alloc.create(
"__toString"),
293 .right = alloc.create(
"right"),
294 .wrong = alloc.create(
"wrong"),
295 .structuredAttrs = alloc.create(
"__structuredAttrs"),
296 .json = alloc.create(
"__json"),
297 .allowedReferences = alloc.create(
"allowedReferences"),
298 .allowedRequisites = alloc.create(
"allowedRequisites"),
299 .disallowedReferences = alloc.create(
"disallowedReferences"),
300 .disallowedRequisites = alloc.create(
"disallowedRequisites"),
301 .maxSize = alloc.create(
"maxSize"),
302 .maxClosureSize = alloc.create(
"maxClosureSize"),
303 .builder = alloc.create(
"builder"),
304 .args = alloc.create(
"args"),
305 .contentAddressed = alloc.create(
"__contentAddressed"),
306 .impure = alloc.create(
"__impure"),
307 .outputHash = alloc.create(
"outputHash"),
308 .outputHashAlgo = alloc.create(
"outputHashAlgo"),
309 .outputHashMode = alloc.create(
"outputHashMode"),
310 .recurseForDerivations = alloc.create(
"recurseForDerivations"),
311 .description = alloc.create(
"description"),
312 .self = alloc.create(
"self"),
313 .epsilon = alloc.create(
""),
314 .startSet = alloc.create(
"startSet"),
315 .operator_ = alloc.create(
"operator"),
316 .key = alloc.create(
"key"),
317 .path = alloc.create(
"path"),
318 .prefix = alloc.create(
"prefix"),
319 .outputSpecified = alloc.create(
"outputSpecified"),
320 .requiredSystemFeatures = alloc.create(
"requiredSystemFeatures"),
322 .sub = alloc.create(
"__sub"),
323 .lessThan = alloc.create(
"__lessThan"),
324 .mul = alloc.create(
"__mul"),
325 .div = alloc.create(
"__div"),
326 .or_ = alloc.create(
"or"),
327 .findFile = alloc.create(
"__findFile"),
328 .nixPath = alloc.create(
"__nixPath"),
329 .body = alloc.create(
"body"),
332 return std::pair{staticSymbols, alloc};
337 return preallocate().first;
342 return preallocate().second;
361 EvalMemory(
const EvalMemory &) =
delete;
362 EvalMemory(EvalMemory &&) =
delete;
363 EvalMemory & operator=(
const EvalMemory &) =
delete;
364 EvalMemory & operator=(EvalMemory &&) =
delete;
372 template<
typename T, std::
size_t N = 0>
386 template<
typename T, std::
size_t N = 0>
389 inline Value * allocValue();
390 inline Env & allocEnv(
size_t size);
392 Bindings * allocBindings(
size_t capacity);
396 return BindingsBuilder(*
this, symbols, allocBindings(capacity), capacity);
401 stats.nrListElems += size;
405 const Statistics & getStats() const &
419class EvalState :
public std::enable_shared_from_this<EvalState>
479 bool inDebugger =
false;
481 std::list<DebugTrace> debugTraces;
482 boost::unordered_flat_map<const Expr *, const std::shared_ptr<const StaticEnv>> exprEnvs;
484 const std::shared_ptr<const StaticEnv> getStaticEnv(
const Expr & expr)
const
486 auto i = exprEnvs.find(&expr);
487 if (i != exprEnvs.end())
490 return std::shared_ptr<const StaticEnv>();
508 template<
class T,
typename...
Args>
509 [[nodiscard, gnu::noinline]]
532 const ref<boost::concurrent_flat_map<
535 std::hash<SourcePath>,
536 std::equal_to<SourcePath>,
537 gc_root_allocator<std::pair<const SourcePath, Value *>>>>
548 struct LookupPathResolvedState
556 concurrent_flat_map<std::string, std::shared_ptr<LookupPathResolvedState>,
StringViewHash, std::equal_to<>>>
587 return mem.allocValue();
599 SourcePath
rootPath(CanonPath path);
604 SourcePath
rootPath(std::string_view path);
613 SourcePath
storePath(
const StorePath & path);
639 void checkURI(
const std::string & uri);
644 StorePath
mountInput(fetchers::Input & input,
const fetchers::Input & originalInput, ref<SourceAccessor> accessor);
650 Expr *
parseExprFromFile(
const SourcePath & path,
const std::shared_ptr<StaticEnv> & staticEnv);
656 parseExprFromString(std::string s,
const SourcePath & basePath,
const std::shared_ptr<StaticEnv> & staticEnv);
664 parseReplBindings(std::string s,
const SourcePath & basePath,
const std::shared_ptr<StaticEnv> & staticEnv);
667 std::string errorSource,
668 const SourcePath & basePath,
669 const std::shared_ptr<StaticEnv> & staticEnv);
678 void evalFile(
const SourcePath & path, Value & v,
bool mustBeTrivial =
false);
680 void resetFileCache();
685 SourcePath
findFile(
const std::string_view path);
686 SourcePath
findFile(
const LookupPath & lookupPath,
const std::string_view path,
const PosIdx pos = noPos);
695 std::shared_ptr<LookupPathResolvedState>
703 void eval(Expr * e, Value & v);
711 inline void evalAttrs(
Env & env,
Expr * e, Value & v, std::string_view errorCtx);
728 void handleEvalExceptionForThunk(
Env * env,
Expr * expr, Value & v,
const PosIdx pos);
735 void handleEvalExceptionForApp(Value & v,
const Value & savedApp);
737 void handleEvalFailed(Value & v,
PosIdx pos);
739 void tryFixupBlackHolePos(Value & v,
PosIdx pos);
752 NixInt
forceInt(Value & v,
const PosIdx pos, std::string_view errorCtx);
753 NixFloat forceFloat(Value & v,
const PosIdx pos, std::string_view errorCtx);
754 bool forceBool(Value & v,
const PosIdx pos, std::string_view errorCtx);
756 void forceAttrs(Value & v,
const PosIdx pos, std::string_view errorCtx);
758 template<
typename Callable>
759 inline void forceAttrs(Value & v, Callable getPos, std::string_view errorCtx);
761 inline void forceList(Value & v,
const PosIdx pos, std::string_view errorCtx);
766 std::string_view forceString(Value & v,
const PosIdx pos, std::string_view errorCtx);
767 std::string_view forceString(
771 std::string_view errorCtx,
773 std::string_view forceStringNoCtx(Value & v,
const PosIdx pos, std::string_view errorCtx);
780 template<
typename...
Args>
782 void addErrorTrace(
Error & e,
const Args &... formatArgs)
const;
783 template<
typename...
Args>
785 void addErrorTrace(
Error & e,
const PosIdx pos,
const Args &... formatArgs)
const;
820 template<
typename Cb>
822 -> std::invoke_result_t<Cb, Value *, bool>;
824 enum class CopyLazyPaths :
bool {
825 PreserveLazy =
false,
856 std::string_view errorCtx,
857 bool coerceMore =
false,
858 bool copyToStore =
true,
859 bool canonicalizePath =
true);
883 std::string_view errorCtx,
905 const std::shared_ptr<Env *> baseEnvP;
924 boost::unordered_flat_map<
929 traceable_allocator<std::pair<const std::string, Value *>>>
942 unsigned int baseEnvDispl = 0;
946 Value * addConstant(
const std::string & name, Value & v,
Constant info);
948 void addConstant(
const std::string & name, Value * v,
Constant info);
950 Value * addPrimOp(
PrimOp && primOp);
970 std::optional<std::string> name;
972 std::vector<std::string> args;
986 std::optional<Doc>
getDoc(Value & v);
990 inline Value * lookupVar(
Env * env,
const ExprVar & var,
bool noEval);
992 friend struct ExprVar;
993 friend struct ExprAttrs;
994 friend struct ExprLet;
1001 const std::shared_ptr<StaticEnv> & staticEnv);
1008 const std::shared_ptr<StaticEnv> & staticEnv);
1014 size_t callDepth = 0;
1027 bool eqValues(Value & v1, Value & v2,
const PosIdx pos, std::string_view errorCtx);
1038 bool isFunctor(
const Value &
fun)
const;
1040 void callFunction(Value &
fun, std::span<Value * const> args, Value & vRes,
const PosIdx pos);
1042 void callFunction(Value &
fun, Value & arg, Value & vRes,
const PosIdx pos)
1044 callFunction(
fun, std::to_array({&arg}), vRes, pos);
1053 BindingsBuilder buildBindings(
size_t capacity)
1055 return mem.buildBindings(symbols, capacity);
1058 ListBuilder buildList(
size_t size)
1060 return mem.buildList(size);
1068 void mkThunk_(Value & v, Expr * expr);
1069 void mkPos(Value & v, PosIdx pos);
1101 const SingleDerivedPath::Built & b,
1102 std::optional<StorePath> optStaticOutputPath,
1103 const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
1115 void concatLists(Value & v, std::span<Value * const> lists,
const PosIdx pos, std::string_view errorCtx);
1145 realiseContext(
const NixStringContext & context, StorePathSet * maybePaths =
nullptr,
bool isIFD =
true);
1155 std::optional<SymlinkResolution> resolveSymlinks = SymlinkResolution::Full,
1156 CopyLazyPaths copyLazyPaths = CopyLazyPaths::PreserveLazy);
1167 realiseString(Value & str, StorePathSet * storePathsOutMaybe,
bool isIFD =
true,
const PosIdx pos = noPos);
1170 bool callPathFilter(Value * filterFun,
const SourcePath & path, PosIdx pos);
1172 DocComment getDocCommentForPos(PosIdx pos);
1180 std::string mkOutputStringRaw(
1181 const SingleDerivedPath::Built & b,
1182 std::optional<StorePath> optStaticOutputPath,
1183 const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
1189 std::string mkSingleDerivedPathStringRaw(
const SingleDerivedPath & p);
1193 Counter nrOpUpdates;
1194 Counter nrOpUpdateValuesCopied;
1195 Counter nrListConcats;
1196 Counter nrPrimOpCalls;
1197 Counter nrFunctionCalls;
1199 const bool countCalls;
1201 typedef boost::concurrent_flat_map<std::string, size_t, StringViewHash, std::equal_to<>> PrimOpCalls;
1202 const ref<PrimOpCalls> primOpCalls;
1204 typedef boost::concurrent_flat_map<ExprLambda *, size_t> FunctionCalls;
1205 const ref<FunctionCalls> functionCalls;
1208 MultiEvalProfiler profiler;
1210 void incrFunctionCall(ExprLambda * fun);
1212 typedef boost::concurrent_flat_map<PosIdx, size_t, std::hash<PosIdx>> AttrSelects;
1213 const ref<AttrSelects> attrSelects;
1215 friend struct ExprOpUpdate;
1216 friend struct ExprOpConcatLists;
1217 friend struct ExprVar;
1218 friend struct ExprString;
1219 friend struct ExprInt;
1220 friend struct ExprFloat;
1221 friend struct ExprPath;
1222 friend struct ExprSelect;
1223 friend void prim_getAttr(
EvalState & state, CallSite callSite, Value *
const * args, Value & v);
1224 friend void prim_match(
EvalState & state, CallSite callSite, Value *
const * args, Value & v);
1225 friend void prim_split(
EvalState & state, CallSite callSite, Value *
const * args, Value & v);
1227 friend struct Value;
1228 friend class ListBuilder;
1231struct DebugTraceStacker
1235 ~DebugTraceStacker()
1237 evalState.debugTraces.pop_front();
1250std::string_view showType(
ValueType type,
bool withArticle =
true);
1251std::string showType(
const Value & v);
1263bool isAllowedURI(std::string_view uri,
const Strings & allowedPaths);
Definition attr-set.hh:448
Definition attr-set.hh:67
Definition eval-error.hh:148
void * allocBytes(size_t n)
Definition eval-inline.hh:16
void * allocBytesAtomic(size_t n)
Definition eval-inline.hh:60
Exprs exprs
Definition eval.hh:413
SourcePath rootPath(CanonPath path)
Definition paths.cc:8
const ref< MountedSourceAccessor > storeFS
Definition eval.hh:441
std::optional< Doc > getDoc(Value &v)
Definition eval.cc:605
SourcePath coerceToPath(const PosIdx pos, Value &v, NixStringContext &context, std::string_view errorCtx)
Definition eval.cc:2654
void maybePrintStats()
Definition eval.cc:3066
void allowPathLegacy(const std::string &path)
Definition eval.cc:401
const Attr * getAttr(Symbol attrSym, const Bindings *attrSet, std::string_view errorCtx)
Definition eval.cc:2429
StorePath coerceToStorePath(const PosIdx pos, Value &v, NixStringContext &context, std::string_view errorCtx)
Definition eval.cc:2674
void forceValueDeep(Value &v)
Definition eval.cc:2329
bool evalBool(Env &env, Expr *e)
void allowPath(const StorePath &storePath)
Definition eval.cc:407
const ref< Store > buildStore
Definition eval.hh:470
RepairFlag repair
Definition eval.hh:436
Env & baseEnv
Definition eval.hh:914
NixInt forceInt(Value &v, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2375
void forceValue(Value &v, const PosIdx pos)
Definition eval-inline.hh:142
void mkOutputString(Value &value, const SingleDerivedPath::Built &b, std::optional< StorePath > optStaticOutputPath, const ExperimentalFeatureSettings &xpSettings=experimentalFeatureSettings)
Definition eval.cc:1027
void allowClosure(const StorePath &storePath)
Definition eval.cc:413
Value * allocValue()
Definition eval.hh:585
std::shared_ptr< LookupPathResolvedState > resolveLookupPathPath(const LookupPath::Path &elem, bool initAccessControl=false)
Definition eval.cc:3350
void evalFile(const SourcePath &path, Value &v, bool mustBeTrivial=false)
Definition eval.cc:1162
std::string realiseString(Value &str, StorePathSet *storePathsOutMaybe, bool isIFD=true, const PosIdx pos=noPos)
Definition primops.cc:66
const ref< MemorySourceAccessor > corepkgsFS
Definition eval.hh:451
void allowAndSetStorePathString(const StorePath &storePath, Value &v)
Definition eval.cc:424
StringMap realiseContext(const NixStringContext &context, StorePathSet *maybePaths=nullptr, bool isIFD=true)
Definition primops.cc:75
bool canDebug()
Definition eval.cc:801
SourcePath storePath(const StorePath &path)
Definition paths.cc:20
const ref< MemorySourceAccessor > internalFS
Definition eval.hh:457
std::pair< SingleDerivedPath, std::string_view > coerceToSingleDerivedPathUnchecked(const PosIdx pos, Value &v, std::string_view errorCtx, const ExperimentalFeatureSettings &xpSettings=experimentalFeatureSettings)
Definition eval.cc:2682
StorePath mountInput(fetchers::Input &input, const fetchers::Input &originalInput, ref< SourceAccessor > accessor)
Definition paths.cc:64
bool eqValues(Value &v1, Value &v2, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2955
BackedStringView coerceToString(const PosIdx pos, Value &v, NixStringContext &context, std::string_view errorCtx, bool coerceMore=false, bool copyToStore=true, bool canonicalizePath=true)
Definition eval.cc:2521
void runDebugRepl(const Error *error)
Definition eval.cc:806
bool fullGC()
Definition eval.cc:3049
SingleDerivedPath coerceToSingleDerivedPath(const PosIdx pos, Value &v, std::string_view errorCtx)
Definition eval.cc:2711
std::map< const Hash, ref< eval_cache::EvalCache > > evalCaches
Definition eval.hh:519
void ensureLazyPathCopied(const StorePath &path)
Definition paths.cc:25
boost::unordered_flat_map< std::string, Value *, StringViewHash, std::equal_to<>, traceable_allocator< std::pair< const std::string, Value * > > > internalPrimOps
Definition eval.hh:930
void mkSingleDerivedPathString(const SingleDerivedPath &p, Value &v)
Definition eval.cc:1062
Expr * parseExprFromFile(const SourcePath &path)
Definition eval.cc:3238
const ref< SourceAccessor > rootFS
Definition eval.hh:446
SourcePath realisePath(const PosIdx pos, Value &v, std::optional< SymlinkResolution > resolveSymlinks=SymlinkResolution::Full, CopyLazyPaths copyLazyPaths=CopyLazyPaths::PreserveLazy)
Definition primops.cc:170
bool isDerivation(Value &v)
Definition eval.cc:2508
const std::shared_ptr< StaticEnv > staticBaseEnv
Definition eval.hh:919
auto peelToStringOutPath(const PosIdx pos, Value &v, bool checkToStringReturn, Cb &&cb) -> std::invoke_result_t< Cb, Value *, bool >
Definition eval-inline.hh:212
Value & getBuiltins()
Definition eval.cc:591
SourcePath findFile(const std::string_view path)
Definition eval.cc:3295
ReplExitStatus(*) debugRepl(ref< EvalState > es, const ValMap &extraEnv)
Definition eval.hh:477
Value * getBool(bool b)
Definition eval.cc:974
void printStatistics()
Definition eval.cc:3079
void eval(Expr *e, Value &v)
Definition eval.cc:1208
void autoCallFunction(const Bindings &args, Value &fun, Value &res)
Definition eval.cc:1847
void assertEqValues(Value &v1, Value &v2, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2744
ExprAttrs * parseReplBindings(std::string s, const SourcePath &basePath, const std::shared_ptr< StaticEnv > &staticEnv)
Definition eval.cc:3268
void forceFunction(Value &v, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2443
EvalState(const LookupPath &lookupPath, ref< Store > store, const fetchers::Settings &fetchSettings, const EvalSettings &settings, std::shared_ptr< Store > buildStore=nullptr)
Definition eval.cc:241
const ref< Store > store
Definition eval.hh:465
Value & getBuiltin(const std::string &name)
Definition eval.cc:596
void mkStorePathString(const StorePath &storePath, Value &v)
Definition eval.cc:1004
CallDepth addCallDepth(const PosIdx pos)
Definition eval-inline.hh:202
void concatLists(Value &v, std::span< Value *const > lists, const PosIdx pos, std::string_view errorCtx)
Concatenate values with an n-ary version of the ++ operator.
Definition eval.cc:2102
void ensureLazyPathsCopied(const NixStringContext &context)
Definition paths.cc:55
Expr * parseExprFromString(std::string s, const SourcePath &basePath, const std::shared_ptr< StaticEnv > &staticEnv)
Definition eval.cc:3251
std::vector< std::pair< std::string, Constant > > constantInfos
Definition eval.hh:938
Definition nixexpr.hh:836
Definition pos-table.hh:15
Definition globals.hh:106
Definition symbol-table.hh:234
Definition store-api.hh:397
Definition symbol-table.hh:266
Definition symbol-table.hh:24
void(EvalState &state, CallSite callSite, Value *const *args, Value &v) PrimOpFun
Definition eval.hh:105
constexpr size_t maxPrimOpArity
Definition eval.hh:39
std::set< NixStringContextElem > NixStringContext
Definition context.hh:103
Definition attr-set.hh:24
PosIdx pos
Definition eval.hh:99
const char * doc
Definition eval.hh:182
bool impureOnly
Definition eval.hh:187
ValueType type
Definition eval.hh:177
Definition eval-settings.hh:50
const char * doc
Definition eval.hh:977
Definition configuration.hh:558
Definition nixexpr.hh:102
Definition nixexpr.hh:100
Definition search-path.hh:16
Definition position.hh:20
std::vector< std::string > args
Definition eval.hh:121
bool addTrace
Definition eval.hh:141
std::optional< std::string > doc
Definition eval.hh:134
bool internal
Definition eval.hh:156
void check()
Definition eval.cc:520
std::optional< ExperimentalFeature > experimentalFeature
Definition eval.hh:151
fun< PrimOpFun > impl
Definition eval.hh:146
std::string name
Definition eval.hh:115
size_t arity
Definition eval.hh:129
Definition primops.cc:3664
Definition derived-path.hh:105
Definition source-path.hh:22
Definition nixexpr.hh:897
Definition strings.hh:142
Definition fetch-settings.hh:27
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
ValueType
Definition value.hh:77