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"
|
2022-03-17 22:29:15 +00:00
|
|
|
#include "derivations.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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-18 16:28:18 +00:00
|
|
|
std::string what() const override { return derivedPath.to_string(*store); }
|
2021-05-17 07:48:51 +00:00
|
|
|
|
|
|
|
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
|
2022-04-19 18:48:13 +00:00
|
|
|
* included in `dependencies`.
|
2021-05-17 07:48:51 +00:00
|
|
|
*/
|
2022-11-21 09:49:01 +00:00
|
|
|
StringPairs resolveRewrites(
|
|
|
|
Store & store,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 07:48:51 +00:00
|
|
|
{
|
|
|
|
StringPairs res;
|
|
|
|
for (auto & dep : dependencies)
|
2022-11-21 09:49:01 +00:00
|
|
|
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep.path))
|
2021-05-17 07:48:51 +00:00
|
|
|
for (auto & [ outputName, outputPath ] : drvDep->outputs)
|
|
|
|
res.emplace(
|
|
|
|
downstreamPlaceholder(store, drvDep->drvPath, outputName),
|
|
|
|
store.printStorePath(outputPath)
|
|
|
|
);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 18:48:13 +00:00
|
|
|
* Resolve the given string assuming the given context.
|
2021-05-17 07:48:51 +00:00
|
|
|
*/
|
2022-11-21 09:49:01 +00:00
|
|
|
std::string resolveString(
|
|
|
|
Store & store,
|
|
|
|
const std::string & toResolve,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 07:48:51 +00:00
|
|
|
{
|
|
|
|
auto rewrites = resolveRewrites(store, dependencies);
|
|
|
|
return rewriteStrings(toResolve, rewrites);
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
UnresolvedApp Installable::toApp(EvalState & state)
|
2020-06-29 12:14:23 +00:00
|
|
|
{
|
2022-04-14 12:04:19 +00:00
|
|
|
auto cursor = getCursor(state);
|
2022-04-19 10:09:12 +00:00
|
|
|
auto attrPath = cursor->getAttrPath();
|
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
|
|
|
|
2022-10-26 08:05:27 +00:00
|
|
|
std::string expected = !attrPath.empty() &&
|
|
|
|
(state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
|
|
|
|
? "app" : "derivation";
|
2022-04-19 10:09:12 +00:00
|
|
|
if (type != expected)
|
|
|
|
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expected);
|
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
if (type == "app") {
|
|
|
|
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
|
2021-02-17 16:54:13 +00:00
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
std::vector<StorePathWithOutputs> context2;
|
|
|
|
for (auto & [path, name] : context)
|
2022-03-12 00:28:00 +00:00
|
|
|
context2.push_back({path, {name}});
|
2020-06-29 12:14:23 +00:00
|
|
|
|
2021-05-17 15:49:20 +00:00
|
|
|
return UnresolvedApp{App {
|
|
|
|
.context = std::move(context2),
|
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "derivation") {
|
|
|
|
auto drvPath = cursor->forceDerivation();
|
2022-04-26 12:01:21 +00:00
|
|
|
auto outPath = cursor->getAttr(state.sOutPath)->getString();
|
|
|
|
auto outputName = cursor->getAttr(state.sOutputName)->getString();
|
|
|
|
auto name = cursor->getAttr(state.sName)->getString();
|
2021-10-27 18:18:30 +00:00
|
|
|
auto aPname = cursor->maybeGetAttr("pname");
|
2022-04-20 14:39:47 +00:00
|
|
|
auto aMeta = cursor->maybeGetAttr(state.sMeta);
|
2021-02-17 16:54:13 +00:00
|
|
|
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
|
|
|
|
auto mainProgram =
|
|
|
|
aMainProgram
|
|
|
|
? aMainProgram->getString()
|
2021-10-27 18:18:30 +00:00
|
|
|
: aPname
|
|
|
|
? aPname->getString()
|
2021-02-17 16:54:13 +00:00
|
|
|
: DrvName(name).name;
|
2021-05-17 15:49:20 +00:00
|
|
|
auto program = outPath + "/bin/" + mainProgram;
|
|
|
|
return UnresolvedApp { App {
|
|
|
|
.context = { { drvPath, {outputName} } },
|
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 17:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2022-04-14 12:04:19 +00:00
|
|
|
throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type);
|
2021-05-17 15:49:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:04:47 +00:00
|
|
|
// FIXME: move to libcmd
|
|
|
|
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
|
2021-05-17 15:49:20 +00:00
|
|
|
{
|
|
|
|
auto res = unresolved;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Installable>> installableContext;
|
|
|
|
|
|
|
|
for (auto & ctxElt : unresolved.context)
|
|
|
|
installableContext.push_back(
|
|
|
|
std::make_shared<InstallableDerivedPath>(store, ctxElt.toDerivedPath()));
|
2021-05-17 07:48:51 +00:00
|
|
|
|
2022-03-02 12:54:08 +00:00
|
|
|
auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext);
|
2021-05-17 15:49:20 +00:00
|
|
|
res.program = resolveString(*store, unresolved.program, builtContext);
|
2021-05-18 11:54:05 +00:00
|
|
|
if (!store->isInStore(res.program))
|
2021-05-17 15:49:20 +00:00
|
|
|
throw Error("app program '%s' is not in the Nix store", res.program);
|
|
|
|
|
|
|
|
return res;
|
2020-06-29 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|