6#include "lix/libexpr/gc-alloc.hh"
19#include "lix/libutil/backed-string-view.hh"
24#include <unordered_map>
33enum RepairFlag : bool;
34struct MemoryInputAccessor;
58 std::vector<std::string>
args;
71 const char *
doc =
nullptr;
76 std::function<PrimOpImpl>
fun;
84std::ostream & operator<<(std::ostream & output,
PrimOp & primOp);
101 const char *
doc =
nullptr;
109using ValMap = GcMap<std::string, Value *>;
117void printEnvBindings(
const EvalState &es,
const Expr & expr,
const Env & env);
122void copyContext(
const Value & v, NixStringContext & context);
126std::ostream & operator << (std::ostream & os,
const ValueType t);
138 std::shared_ptr<Pos> pos;
143 std::shared_ptr<const DebugTrace> parent;
149 std::weak_ptr<const DebugTrace> latestTrace;
156 bool inDebugger =
false;
157 std::map<const Expr *, const std::shared_ptr<const StaticEnv>> exprEnvs;
165 : positions(positions)
167 , errorCallback(errorCallback)
169 assert(errorCallback);
174 const std::shared_ptr<const StaticEnv> staticEnvFor(
const Expr & expr)
const
176 if (
auto i = exprEnvs.find(&expr); i != exprEnvs.end()) {
184 friend struct DebugState;
185 template<std::derived_from<EvalError> T>
186 friend class EvalErrorBuilder;
191 const std::shared_ptr<const DebugTrace> entry =
nullptr;
193 explicit TraceFrame(std::shared_ptr<const DebugTrace> entry): entry(std::move(entry)) {}
196 TraceFrame(std::nullptr_t) {}
205 for (
auto current = latestTrace.lock(); current; current = current->parent) {
206 co_yield current.get();
213 const Symbol outPath, drvPath, type, meta, name, value, system, overrides, outputs, outputName,
214 ignoreNulls, file, line, column, functor, toString, right, wrong, structuredAttrs,
215 allowedReferences, allowedRequisites, disallowedReferences, disallowedRequisites, maxSize,
216 maxClosureSize, builder, args, contentAddressed, impure, outputHash, outputHashAlgo,
217 outputHashMode, recurseForDerivations, description, self, startSet, operator_, key,
218 path, prefix, outputSpecified;
230 std::shared_ptr<void *> valueAllocCache;
235 std::shared_ptr<void *> env1AllocCache;
240 unsigned long nrEnvs = 0;
241 unsigned long nrValuesInEnvs = 0;
242 unsigned long nrValues = 0;
243 unsigned long nrAttrsets = 0;
244 unsigned long nrAttrsInAttrsets = 0;
245 unsigned long nrListElems = 0;
250 EvalMemory(
const EvalMemory &) =
delete;
251 EvalMemory(EvalMemory &&) =
delete;
252 EvalMemory & operator=(
const EvalMemory &) =
delete;
253 EvalMemory & operator=(EvalMemory &&) =
delete;
255 inline Value * allocValue();
256 inline Env & allocEnv(
size_t size);
258 Bindings * allocBindings(
size_t capacity);
259 Value newList(
size_t length);
266 const Statistics getStats()
const {
return stats; }
278 explicit EvalBuiltins(
282 const Path & storeDir,
306 unsigned int baseEnvDispl = 0;
308 void createBaseEnv(
const SearchPath & searchPath,
const Path & storeDir);
312 void addConstant(
const std::string & name,
Value * v,
Constant info);
319 Value & get(
const std::string & name);
324 std::optional<std::string> name;
326 std::vector<std::string> args;
334 std::optional<Doc> getDoc(
Value & v);
352 std::map<SourcePath, std::shared_ptr<CachedEvalFile>>
fileEval;
360 template<std::derived_from<EvalError> T,
typename... Args>
381 const SearchPath & searchPath()
const {
return searchPath_; }
389 using is_transparent = void;
392 std::map<std::string, AllowedPath, ComponentLess> children;
394 bool allowAllChildren =
false;
401 std::optional<AllowedPath> allowedPaths;
406 std::map<SourcePath, StorePath> srcToStore;
408 std::map<std::string, std::optional<std::string>> searchPathResolved;
413 std::unordered_map<Path, CheckedSourcePath> resolvedPaths;
443 void checkURI(
const std::string & uri);
462 template<
typename T,
typename E>
463 struct PathResult :
private std::variant<T, EvalErrorBuilder<E>>
465 PathResult(T p) : std::variant<T, EvalErrorBuilder<E>>(std::move(p)) {}
472 [](T & p) -> T {
return std::move(p); },
477 static_cast<std::variant<T, EvalErrorBuilder<E>
> &>(*this)
485 kj::Promise<Result<PathResult<SourcePath, ThrownError>>>
findFile(
const std::string_view path);
486 kj::Promise<Result<PathResult<SourcePath, ThrownError>>>
496 kj::Promise<Result<std::optional<std::string>>>
499 kj::Promise<Result<PathResult<StorePath, EvalError>>> copyPathToStore(
500 NixStringContext & context,
const SourcePath & path, RepairFlag repair = NoRepair
514 unsigned long nrLookups = 0;
515 unsigned long nrAvoided = 0;
516 unsigned long nrOpUpdates = 0;
517 unsigned long nrOpUpdateValuesCopied = 0;
518 unsigned long nrListConcats = 0;
519 unsigned long nrPrimOpCalls = 0;
520 unsigned long nrFunctionCalls = 0;
521 unsigned long nrThunks = 0;
523 bool countCalls =
false;
525 std::map<std::string, size_t> primOpCalls;
526 std::map<ExprLambda *, size_t> functionCalls;
527 std::map<PosIdx, size_t> attrSelects;
534 friend class EvalBuiltins;
535 friend class EvalState;
537 EvalState * activeEval =
nullptr;
546 EvalBuiltins builtins;
565 std::unique_ptr<DebugState> debug;
573 std::function<
ReplExitStatus(EvalState & es, ValMap
const & extraEnv)> debugRepl =
nullptr
576 Evaluator(
const Evaluator &) =
delete;
577 Evaluator(Evaluator &&) =
delete;
578 Evaluator & operator=(
const Evaluator &) =
delete;
579 Evaluator & operator=(Evaluator &&) =
delete;
593 std::shared_ptr<StaticEnv> & staticEnv,
606 std::shared_ptr<StaticEnv> & staticEnv,
623 std::shared_ptr<StaticEnv> & staticEnv,
632 std::shared_ptr<StaticEnv> & staticEnv,
639 return mem.buildBindings(symbols, capacity);
686 friend class Evaluator;
688 explicit EvalState(
AsyncIoRoot & aio, Evaluator & ctx);
694 EvalState(
const EvalState &) =
delete;
695 EvalState(EvalState &&) =
delete;
696 EvalState & operator=(
const EvalState &) =
delete;
697 EvalState & operator=(EvalState &&) =
delete;
706 void resetFileCache();
743 NixFloat forceFloat(
Value & v,
const PosIdx pos, std::string_view errorCtx);
744 bool forceBool(
Value & v,
const PosIdx pos, std::string_view errorCtx);
746 void forceAttrs(
Value & v,
const PosIdx pos, std::string_view errorCtx);
747 inline void forceList(
Value & v,
const PosIdx pos, std::string_view errorCtx);
752 std::string_view forceString(
Value & v,
const PosIdx pos, std::string_view errorCtx);
753 std::string_view forceString(
Value & v, NixStringContext & context,
const PosIdx pos, std::string_view errorCtx);
754 std::string_view forceStringNoCtx(
Value & v,
const PosIdx pos, std::string_view errorCtx);
760 [[nodiscard]] StringMap
realiseContext(
const NixStringContext & context);
769 std::optional<std::string> tryAttrsToString(
const PosIdx pos,
Value & v,
770 NixStringContext & context,
bool coerceMore =
false,
bool copyToStore =
true);
781 std::string_view errorCtx,
782 bool coerceMore =
false,
bool copyToStore =
true,
783 bool canonicalizePath =
true);
823 inline Value * lookupVar(
Env * env,
const ExprVar & var,
bool noEval);
825 friend struct ExprVar;
826 friend struct ExprSet;
827 friend struct ExprLet;
832 size_t callDepth = 0;
842 bool isFunctor(
Value & fun);
849 Value * args[] = {&arg};
850 callFunction(fun, 1, args, vRes, pos);
883 const SingleDerivedPath::Built & b,
884 std::optional<StorePath> optStaticOutputPath,
885 const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
896 void concatLists(
Value & v,
size_t nrLists,
Value * * lists,
const PosIdx pos, std::string_view errorCtx);
904 std::string mkOutputStringRaw(
905 const SingleDerivedPath::Built & b,
906 std::optional<StorePath> optStaticOutputPath,
907 const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
913 std::string mkSingleDerivedPathStringRaw(
923std::string_view showType(
ValueType type,
bool withArticle =
true);
924std::string showType(
const Value & v);
926static constexpr std::string_view corepkgsPrefix{
"/__corepkgs__/"};
Definition backed-string-view.hh:17
Definition attr-set.hh:128
Definition attr-set.hh:48
Definition source-path.hh:89
Env & env
Definition eval.hh:290
std::vector< std::pair< std::string, Constant > > constantInfos
Definition eval.hh:303
std::shared_ptr< StaticEnv > staticEnv
Definition eval.hh:295
Definition eval-error.hh:61
Definition eval-error.hh:25
void allowPath(const Path &path)
Definition eval.cc:371
kj::Promise< Result< std::optional< std::string > > > resolveSearchPathPath(const SearchPath::Path &path)
Definition eval.cc:2837
void allowAndSetStorePathString(const StorePath &storePath, Value &v)
Definition eval.cc:392
CheckedSourcePath checkSourcePath(const SourcePath &path)
Definition eval.cc:399
CheckedSourcePath resolveExprPath(SourcePath path)
Definition eval.cc:2717
Path toRealPath(const Path &path, const NixStringContext &context)
Definition eval.cc:518
void mkStorePathString(const StorePath &storePath, Value &v)
Definition eval.cc:880
kj::Promise< Result< PathResult< SourcePath, ThrownError > > > findFile(const std::string_view path)
Definition eval.cc:2799
StringMap realiseContext(const NixStringContext &context)
Definition primops.cc:45
SourcePath coerceToPath(const PosIdx pos, Value &v, NixStringContext &context, std::string_view errorCtx)
Definition eval.cc:2417
StorePath coerceToStorePath(const PosIdx pos, Value &v, NixStringContext &context, std::string_view errorCtx)
Definition eval.cc:2426
void forceValueDeep(Value &v)
Definition eval.cc:2097
void autoCallFunction(Bindings &args, Value &fun, Value &res, PosIdx pos)
Definition eval.cc:1756
NixInt forceInt(Value &v, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2134
void forceValue(Value &v, const PosIdx pos)
Definition eval-inline.hh:69
void mkOutputString(Value &value, const SingleDerivedPath::Built &b, std::optional< StorePath > optStaticOutputPath, const ExperimentalFeatureSettings &xpSettings=experimentalFeatureSettings)
Definition eval.cc:905
std::pair< SingleDerivedPath, std::string_view > coerceToSingleDerivedPathUnchecked(const PosIdx pos, Value &v, std::string_view errorCtx)
Definition eval.cc:2435
void evalFile(const SourcePath &path, Value &v)
Definition eval.cc:991
bool eqValues(Value &v1, Value &v2, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2492
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:2292
SingleDerivedPath coerceToSingleDerivedPath(const PosIdx pos, Value &v, std::string_view errorCtx)
Definition eval.cc:2465
void mkSingleDerivedPathString(const SingleDerivedPath &p, Value &v)
Definition eval.cc:943
bool isDerivation(Value &v)
Definition eval.cc:2261
void eval(Expr &e, Value &v)
Definition eval.cc:1037
void forceFunction(Value &v, const PosIdx pos, std::string_view errorCtx)
Definition eval.cc:2200
bool evalBool(Env &env, Expr &e)
Definition eval.cc:1051
void printStatistics()
Definition eval.cc:2599
void evalLazily(Expr &e, Value &v)
Definition eval.cc:860
box_ptr< EvalState > begin(AsyncIoRoot &aio)
Definition eval.cc:352
Expr & parseExprFromString(std::string s, const SourcePath &basePath, std::shared_ptr< StaticEnv > &staticEnv, const FeatureSettings &xpSettings=featureSettings)
Definition eval.cc:2755
const ref< Store > store
Definition eval.hh:558
RepairFlag repair
Definition eval.hh:553
bool fullGC()
Definition eval.cc:2570
Expr & parseExprFromFile(const CheckedSourcePath &path)
Definition eval.cc:2742
ref< Store > buildStore
Definition eval.hh:563
void maybePrintStats()
Definition eval.cc:2584
Definition pos-table.hh:13
Definition store-api.hh:195
Definition symbol-table.hh:73
Definition symbol-table.hh:50
Definition eval-cache.hh:38
void(EvalState &state, Value **args, Value &v) PrimOpImpl
Definition eval.hh:42
ReplExitStatus
Definition repl-exit-status.hh:9
const char * doc
Definition eval.hh:101
bool impureOnly
Definition eval.hh:106
ValueType type
Definition eval.hh:96
Generator< const DebugTrace * > traces()
Definition eval.hh:203
const char * doc
Definition eval.hh:331
std::shared_ptr< RegexCache > regexes
Definition eval.hh:347
std::map< SourcePath, std::shared_ptr< CachedEvalFile > > fileEval
Definition eval.hh:352
Definition nixexpr.hh:446
Definition nixexpr.hh:356
Definition nixexpr.hh:111
Definition nixexpr.hh:104
Definition generator.hh:236
Definition position.hh:19
std::vector< std::string > args
Definition eval.hh:58
std::function< PrimOpImpl > fun
Definition eval.hh:76
std::optional< ExperimentalFeature > experimentalFeature
Definition eval.hh:81
std::string name
Definition eval.hh:52
size_t arity
Definition eval.hh:66
const char * doc
Definition eval.hh:71
Definition primops.cc:2553
Definition search-path.hh:83
Definition search-path.hh:16
Definition derived-path.hh:101
Definition source-path.hh:23
Definition nixexpr.hh:606
std::string Path
Definition types.hh:28
constexpr NeverAsync always_progresses
Definition types.hh:181
ValueType
Definition value.hh:48
std::shared_ptr< Value * > RootValue
Definition value.hh:848