2020-06-29 12:14:23 +00:00
|
|
|
#include "installables.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval-inline.hh"
|
2020-06-29 17:08:50 +00:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "names.hh"
|
2021-05-17 07:48:51 +00:00
|
|
|
#include "command.hh"
|
2020-06-29 12:14:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-05-17 07:48:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:08:50 +00:00
|
|
|
App Installable::toApp(EvalState & state)
|
2020-06-29 12:14:23 +00:00
|
|
|
{
|
2020-08-07 12:13:24 +00:00
|
|
|
auto [cursor, attrPath] = getCursor(state);
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2020-06-29 17:08:50 +00:00
|
|
|
auto type = cursor->getAttr("type")->getString();
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2021-02-17 16:54:13 +00:00
|
|
|
auto checkProgram = [&](const Path & program)
|
|
|
|
{
|
|
|
|
if (!state.store->isInStore(program))
|
|
|
|
throw Error("app program '%s' is not in the Nix store", program);
|
|
|
|
};
|
|
|
|
|
2021-05-17 07:48:51 +00:00
|
|
|
std::vector<std::shared_ptr<Installable>> context;
|
|
|
|
std::string unresolvedProgram;
|
2020-06-29 12:14:23 +00:00
|
|
|
|
|
|
|
|
2021-05-17 07:48:51 +00:00
|
|
|
if (type == "app") {
|
|
|
|
auto [program, context_] = cursor->getAttr("program")->getStringWithContext();
|
|
|
|
unresolvedProgram = program;
|
2020-06-29 17:08:50 +00:00
|
|
|
|
2021-05-17 07:48:51 +00:00
|
|
|
for (auto & [path, name] : context_)
|
|
|
|
context.push_back(std::make_shared<InstallableDerivedPath>(
|
|
|
|
state.store,
|
|
|
|
DerivedPathBuilt{
|
|
|
|
.drvPath = state.store->parseStorePath(path),
|
|
|
|
.outputs = {name},
|
|
|
|
}));
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "derivation") {
|
|
|
|
auto drvPath = cursor->forceDerivation();
|
|
|
|
auto outPath = cursor->getAttr(state.sOutPath)->getString();
|
|
|
|
auto outputName = cursor->getAttr(state.sOutputName)->getString();
|
|
|
|
auto name = cursor->getAttr(state.sName)->getString();
|
2021-02-17 16:54:13 +00:00
|
|
|
auto aMeta = cursor->maybeGetAttr("meta");
|
|
|
|
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
|
|
|
|
auto mainProgram =
|
|
|
|
aMainProgram
|
|
|
|
? aMainProgram->getString()
|
|
|
|
: DrvName(name).name;
|
2021-05-17 07:48:51 +00:00
|
|
|
unresolvedProgram = outPath + "/bin/" + mainProgram;
|
|
|
|
context = {std::make_shared<InstallableDerivedPath>(
|
|
|
|
state.store,
|
|
|
|
DerivedPathBuilt{
|
|
|
|
.drvPath = drvPath,
|
|
|
|
.outputs = {outputName},
|
|
|
|
})};
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
throw Error("attribute '%s' has unsupported type '%s'", attrPath, type);
|
2021-05-17 07:48:51 +00:00
|
|
|
|
|
|
|
auto builtContext = build(state.store, Realise::Outputs, context);
|
|
|
|
auto program = resolveString(*state.store, unresolvedProgram, builtContext);
|
|
|
|
checkProgram(program);
|
|
|
|
return App {
|
|
|
|
.program = program,
|
|
|
|
};
|
2020-06-29 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|