Split the parsing of an App and its resolving

That way things (like `nix flake check`) can evaluate the `app` outputs
without having to build anything
This commit is contained in:
regnat 2021-05-17 17:49:20 +02:00
parent bd6cf25952
commit ca96f52194
4 changed files with 42 additions and 39 deletions

View file

@ -23,6 +23,12 @@ struct App
// FIXME: add args, sandbox settings, metadata, ... // FIXME: add args, sandbox settings, metadata, ...
}; };
struct UnresolvedApp
{
App unresolved;
App resolve(ref<Store>);
};
struct Installable struct Installable
{ {
virtual ~Installable() { } virtual ~Installable() { }
@ -33,7 +39,7 @@ struct Installable
DerivedPath toDerivedPath(); DerivedPath toDerivedPath();
App toApp(EvalState & state); UnresolvedApp toApp(EvalState & state);
virtual std::pair<Value *, Pos> toValue(EvalState & state) virtual std::pair<Value *, Pos> toValue(EvalState & state)
{ {

View file

@ -58,33 +58,24 @@ std::string resolveString(Store & store, const std::string & toResolve, const Bu
return rewriteStrings(toResolve, rewrites); return rewriteStrings(toResolve, rewrites);
} }
App Installable::toApp(EvalState & state) UnresolvedApp Installable::toApp(EvalState & state)
{ {
auto [cursor, attrPath] = getCursor(state); auto [cursor, attrPath] = getCursor(state);
auto type = cursor->getAttr("type")->getString(); auto type = cursor->getAttr("type")->getString();
auto checkProgram = [&](const Path & program)
{
if (!state.store->isInStore(program))
throw Error("app program '%s' is not in the Nix store", program);
};
std::vector<std::shared_ptr<Installable>> context;
std::string unresolvedProgram;
if (type == "app") { if (type == "app") {
auto [program, context_] = cursor->getAttr("program")->getStringWithContext(); auto [program, context] = cursor->getAttr("program")->getStringWithContext();
unresolvedProgram = program;
for (auto & [path, name] : context_)
context.push_back(std::make_shared<InstallableDerivedPath>( std::vector<StorePathWithOutputs> context2;
state.store, for (auto & [path, name] : context)
DerivedPathBuilt{ context2.push_back({state.store->parseStorePath(path), {name}});
.drvPath = state.store->parseStorePath(path),
.outputs = {name}, return UnresolvedApp{App {
})); .context = std::move(context2),
.program = program,
}};
} }
else if (type == "derivation") { else if (type == "derivation") {
@ -98,24 +89,33 @@ App Installable::toApp(EvalState & state)
aMainProgram aMainProgram
? aMainProgram->getString() ? aMainProgram->getString()
: DrvName(name).name; : DrvName(name).name;
unresolvedProgram = outPath + "/bin/" + mainProgram; auto program = outPath + "/bin/" + mainProgram;
context = {std::make_shared<InstallableDerivedPath>( return UnresolvedApp { App {
state.store, .context = { { drvPath, {outputName} } },
DerivedPathBuilt{ .program = program,
.drvPath = drvPath, }};
.outputs = {outputName},
})};
} }
else else
throw Error("attribute '%s' has unsupported type '%s'", attrPath, type); throw Error("attribute '%s' has unsupported type '%s'", attrPath, type);
}
auto builtContext = build(state.store, Realise::Outputs, context); App UnresolvedApp::resolve(ref<Store> store)
auto program = resolveString(*state.store, unresolvedProgram, builtContext); {
checkProgram(program); auto res = unresolved;
return App {
.program = program, std::vector<std::shared_ptr<Installable>> installableContext;
};
for (auto & ctxElt : unresolved.context)
installableContext.push_back(
std::make_shared<InstallableDerivedPath>(store, ctxElt.toDerivedPath()));
auto builtContext = build(store, Realise::Outputs, installableContext);
res.program = resolveString(*store, unresolved.program, builtContext);
if (store->isInStore(res.program))
throw Error("app program '%s' is not in the Nix store", res.program);
return res;
} }
} }

View file

@ -69,8 +69,7 @@ struct CmdBundle : InstallableCommand
{ {
auto evalState = getEvalState(); auto evalState = getEvalState();
auto app = installable->toApp(*evalState); auto app = installable->toApp(*evalState).resolve(store);
store->buildPaths(toDerivedPaths(app.context));
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath(".")); auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false }; const flake::LockFlags lockFlags{ .writeLockFile = false };

View file

@ -178,9 +178,7 @@ struct CmdRun : InstallableCommand, RunCommon
{ {
auto state = getEvalState(); auto state = getEvalState();
auto app = installable->toApp(*state); auto app = installable->toApp(*state).resolve(store);
state->store->buildPaths(toDerivedPaths(app.context));
Strings allArgs{app.program}; Strings allArgs{app.program};
for (auto & i : args) allArgs.push_back(i); for (auto & i : args) allArgs.push_back(i);