Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,9 @@ struct ExprParseFile : Expr, gc
{
// FIXME: make this a reference (see below).
SourcePath path;
bool mustBeTrivial;

ExprParseFile(SourcePath & path, bool mustBeTrivial)
ExprParseFile(SourcePath & path)
: path(path)
, mustBeTrivial(mustBeTrivial)
{
}

Expand All @@ -1096,11 +1094,6 @@ struct ExprParseFile : Expr, gc
state, *e, state.baseEnv, e->getPos(), "while evaluating the file '%s':", path.to_string())
: nullptr;

// Enforce that 'flake.nix' is a direct attrset, not a
// computation.
if (mustBeTrivial && !(dynamic_cast<ExprAttrs *>(e)))
state.error<EvalError>("file '%s' must be an attribute set", path).debugThrow();

state.eval(e, v);
} catch (Error & e) {
state.addErrorTrace(e, "while evaluating the file '%s':", path.to_string());
Expand All @@ -1109,7 +1102,7 @@ struct ExprParseFile : Expr, gc
}
};

void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
void EvalState::evalFile(const SourcePath & path, Value & v)
{
auto resolvedPath = getConcurrent(*importResolutionCache, path);

Expand All @@ -1129,7 +1122,7 @@ void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
// https://github.com/NixOS/nix/pull/13930 is merged. That will ensure
// the post-condition that `expr` is unreachable after
// `forceValue()` returns.
auto expr = new ExprParseFile{*resolvedPath, mustBeTrivial};
auto expr = new ExprParseFile{*resolvedPath};

fileEvalCache->try_emplace_and_cvisit(
*resolvedPath,
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/include/nix/expr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public:
* form. Optionally enforce that the top-level expression is
* trivial (i.e. doesn't require arbitrary computation).
*/
void evalFile(const SourcePath & path, Value & v, bool mustBeTrivial = false);
void evalFile(const SourcePath & path, Value & v);

void resetFileCache();

Expand Down
16 changes: 5 additions & 11 deletions src/libflake/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,9 @@ using namespace flake;

namespace flake {

static void forceTrivialValue(EvalState & state, Value & value, const PosIdx pos)
{
if (value.isThunk() && value.isTrivial())
state.forceValue(value, pos);
}

static void expectType(EvalState & state, ValueType type, Value & value, const PosIdx pos)
{
forceTrivialValue(state, value, pos);
state.forceValue(value, pos);
if (value.type() != type)
throw Error("expected %s but got %s at %s", showType(type), showType(value.type()), state.positions[pos]);
}
Expand Down Expand Up @@ -151,7 +145,7 @@ static FlakeInput parseFlakeInput(
for (auto & attr : *value->attrs()) {
try {
if (attr.name == sUrl) {
forceTrivialValue(state, *attr.value, pos);
state.forceValue(*attr.value, pos);
if (attr.value->type() == nString)
url = attr.value->string_view();
else if (attr.value->type() == nPath) {
Expand Down Expand Up @@ -224,6 +218,7 @@ static std::pair<std::map<FlakeId, FlakeInput>, fetchers::Attrs> parseFlakeInput
expectType(state, nAttrs, *value, pos);

for (auto & inputAttr : *value->attrs()) {
state.forceValue(*inputAttr.value, pos);
auto inputName = state.symbols[inputAttr.name];
if (inputName == "self") {
if (!allowSelf)
Expand Down Expand Up @@ -251,9 +246,8 @@ static Flake readFlake(
auto flakeDir = rootDir / CanonPath(resolvedRef.subdir);
auto flakePath = flakeDir / "flake.nix";

// NOTE evalFile forces vInfo to be an attrset because mustBeTrivial is true.
Value vInfo;
state.evalFile(flakePath, vInfo, true);
state.evalFile(flakePath, vInfo);

Flake flake{
.originalRef = originalRef,
Expand Down Expand Up @@ -302,7 +296,7 @@ static Flake readFlake(
expectType(state, nAttrs, *nixConfig->value, nixConfig->pos);

for (auto & setting : *nixConfig->value->attrs()) {
forceTrivialValue(state, *setting.value, setting.pos);
state.forceValue(*setting.value, setting.pos);
if (setting.value->type() == nString)
flake.config.settings.emplace(
state.symbols[setting.name], std::string(state.forceStringNoCtx(*setting.value, setting.pos, "")));
Expand Down
Loading