Resolve the program path in nix run

Fix #4768
This commit is contained in:
regnat 2021-05-17 09:48:51 +02:00
parent 2105084645
commit f0c0052d4b

View file

@ -3,9 +3,61 @@
#include "eval-inline.hh" #include "eval-inline.hh"
#include "eval-cache.hh" #include "eval-cache.hh"
#include "names.hh" #include "names.hh"
#include "command.hh"
namespace nix { namespace nix {
struct InstallableDerivedPath : Installable
{
ref<Store> store;
const DerivedPath derivedPath;
InstallableDerivedPath(ref<Store> store, const DerivedPath & derivedPath)
: store(store)
, derivedPath(derivedPath)
{
}
std::string what() override { return derivedPath.to_string(*store); }
DerivedPaths toDerivedPaths() override
{
return {derivedPath};
}
std::optional<StorePath> getStorePath() override
{
return std::nullopt;
}
};
/**
* Return the rewrites that are needed to resolve a string whose context is
* included in `dependencies`
*/
StringPairs resolveRewrites(Store & store, const BuiltPaths dependencies)
{
StringPairs res;
for (auto & dep : dependencies)
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep))
for (auto & [ outputName, outputPath ] : drvDep->outputs)
res.emplace(
downstreamPlaceholder(store, drvDep->drvPath, outputName),
store.printStorePath(outputPath)
);
return res;
}
/**
* Resolve the given string assuming the given context
*/
std::string resolveString(Store & store, const std::string & toResolve, const BuiltPaths dependencies)
{
auto rewrites = resolveRewrites(store, dependencies);
return rewriteStrings(toResolve, rewrites);
}
App Installable::toApp(EvalState & state) App Installable::toApp(EvalState & state)
{ {
auto [cursor, attrPath] = getCursor(state); auto [cursor, attrPath] = getCursor(state);
@ -18,19 +70,21 @@ App Installable::toApp(EvalState & state)
throw Error("app program '%s' is not in the Nix store", 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;
checkProgram(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 App { }));
.context = std::move(context2),
.program = program,
};
} }
else if (type == "derivation") { else if (type == "derivation") {
@ -44,16 +98,24 @@ App Installable::toApp(EvalState & state)
aMainProgram aMainProgram
? aMainProgram->getString() ? aMainProgram->getString()
: DrvName(name).name; : DrvName(name).name;
auto program = outPath + "/bin/" + mainProgram; unresolvedProgram = outPath + "/bin/" + mainProgram;
checkProgram(program); context = {std::make_shared<InstallableDerivedPath>(
return 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);
auto program = resolveString(*state.store, unresolvedProgram, builtContext);
checkProgram(program);
return App {
.program = program,
};
} }
} }