2020-06-29 15:14:23 +03:00
|
|
|
#include "installables.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval-inline.hh"
|
2020-06-29 20:08:50 +03:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "names.hh"
|
2021-05-17 10:48:51 +03:00
|
|
|
#include "command.hh"
|
2022-03-18 00:29:15 +02:00
|
|
|
#include "derivations.hh"
|
2020-06-29 15:14:23 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-05-17 10:48:51 +03:00
|
|
|
struct InstallableDerivedPath : Installable
|
|
|
|
{
|
|
|
|
ref<Store> store;
|
|
|
|
const DerivedPath derivedPath;
|
|
|
|
|
|
|
|
InstallableDerivedPath(ref<Store> store, const DerivedPath & derivedPath)
|
|
|
|
: store(store)
|
|
|
|
, derivedPath(derivedPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-18 18:28:18 +02:00
|
|
|
std::string what() const override { return derivedPath.to_string(*store); }
|
2021-05-17 10:48:51 +03:00
|
|
|
|
2022-12-15 23:09:32 +02:00
|
|
|
DerivedPathsWithInfo toDerivedPaths() override
|
2021-05-17 10:48:51 +03:00
|
|
|
{
|
2022-12-15 23:09:32 +02:00
|
|
|
return {{derivedPath}};
|
2021-05-17 10:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<StorePath> getStorePath() override
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the rewrites that are needed to resolve a string whose context is
|
2022-04-19 21:48:13 +03:00
|
|
|
* included in `dependencies`.
|
2021-05-17 10:48:51 +03:00
|
|
|
*/
|
2022-11-21 11:49:01 +02:00
|
|
|
StringPairs resolveRewrites(
|
|
|
|
Store & store,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 10:48:51 +03:00
|
|
|
{
|
|
|
|
StringPairs res;
|
|
|
|
for (auto & dep : dependencies)
|
2022-11-21 11:49:01 +02:00
|
|
|
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep.path))
|
2021-05-17 10:48:51 +03:00
|
|
|
for (auto & [ outputName, outputPath ] : drvDep->outputs)
|
|
|
|
res.emplace(
|
|
|
|
downstreamPlaceholder(store, drvDep->drvPath, outputName),
|
|
|
|
store.printStorePath(outputPath)
|
|
|
|
);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 21:48:13 +03:00
|
|
|
* Resolve the given string assuming the given context.
|
2021-05-17 10:48:51 +03:00
|
|
|
*/
|
2022-11-21 11:49:01 +02:00
|
|
|
std::string resolveString(
|
|
|
|
Store & store,
|
|
|
|
const std::string & toResolve,
|
|
|
|
const std::vector<BuiltPathWithResult> & dependencies)
|
2021-05-17 10:48:51 +03:00
|
|
|
{
|
|
|
|
auto rewrites = resolveRewrites(store, dependencies);
|
|
|
|
return rewriteStrings(toResolve, rewrites);
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
UnresolvedApp Installable::toApp(EvalState & state)
|
2020-06-29 15:14:23 +03:00
|
|
|
{
|
2022-04-14 15:04:19 +03:00
|
|
|
auto cursor = getCursor(state);
|
2022-04-19 13:09:12 +03:00
|
|
|
auto attrPath = cursor->getAttrPath();
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2020-06-29 20:08:50 +03:00
|
|
|
auto type = cursor->getAttr("type")->getString();
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2022-10-26 11:05:27 +03:00
|
|
|
std::string expected = !attrPath.empty() &&
|
|
|
|
(state.symbols[attrPath[0]] == "apps" || state.symbols[attrPath[0]] == "defaultApp")
|
|
|
|
? "app" : "derivation";
|
2022-04-19 13:09:12 +03:00
|
|
|
if (type != expected)
|
|
|
|
throw Error("attribute '%s' should have type '%s'", cursor->getAttrPathStr(), expected);
|
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
if (type == "app") {
|
|
|
|
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
|
2021-02-17 18:54:13 +02:00
|
|
|
|
2023-01-10 18:27:19 +02:00
|
|
|
std::vector<DerivedPath> context2;
|
2023-01-03 18:44:59 +02:00
|
|
|
for (auto & c : context) {
|
|
|
|
context2.emplace_back(std::visit(overloaded {
|
|
|
|
[&](const NixStringContextElem::DrvDeep & d) -> DerivedPath {
|
|
|
|
/* We want all outputs of the drv */
|
|
|
|
return DerivedPath::Built {
|
|
|
|
.drvPath = d.drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::All {},
|
2023-01-03 18:44:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Built & b) -> DerivedPath {
|
|
|
|
return DerivedPath::Built {
|
|
|
|
.drvPath = b.drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::Names { b.output },
|
2023-01-03 18:44:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Opaque & o) -> DerivedPath {
|
|
|
|
return DerivedPath::Opaque {
|
|
|
|
.path = o.path,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}, c.raw()));
|
2023-01-10 18:27:19 +02:00
|
|
|
}
|
2020-06-29 15:14:23 +03:00
|
|
|
|
2021-05-17 18:49:20 +03:00
|
|
|
return UnresolvedApp{App {
|
|
|
|
.context = std::move(context2),
|
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 20:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (type == "derivation") {
|
|
|
|
auto drvPath = cursor->forceDerivation();
|
2022-04-26 15:01:21 +03: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 21:18:30 +03:00
|
|
|
auto aPname = cursor->maybeGetAttr("pname");
|
2022-04-20 17:39:47 +03:00
|
|
|
auto aMeta = cursor->maybeGetAttr(state.sMeta);
|
2021-02-17 18:54:13 +02:00
|
|
|
auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
|
|
|
|
auto mainProgram =
|
|
|
|
aMainProgram
|
|
|
|
? aMainProgram->getString()
|
2021-10-27 21:18:30 +03:00
|
|
|
: aPname
|
|
|
|
? aPname->getString()
|
2021-02-17 18:54:13 +02:00
|
|
|
: DrvName(name).name;
|
2021-05-17 18:49:20 +03:00
|
|
|
auto program = outPath + "/bin/" + mainProgram;
|
|
|
|
return UnresolvedApp { App {
|
2023-01-10 18:27:19 +02:00
|
|
|
.context = { DerivedPath::Built {
|
|
|
|
.drvPath = drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
.outputs = OutputsSpec::Names { outputName },
|
2023-01-10 18:27:19 +02:00
|
|
|
} },
|
2021-05-17 18:49:20 +03:00
|
|
|
.program = program,
|
|
|
|
}};
|
2020-06-29 20:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2022-04-14 15:04:19 +03:00
|
|
|
throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type);
|
2021-05-17 18:49:20 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 17:04:47 +03:00
|
|
|
// FIXME: move to libcmd
|
|
|
|
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
|
2021-05-17 18:49:20 +03:00
|
|
|
{
|
|
|
|
auto res = unresolved;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Installable>> installableContext;
|
|
|
|
|
|
|
|
for (auto & ctxElt : unresolved.context)
|
|
|
|
installableContext.push_back(
|
2023-01-10 18:27:19 +02:00
|
|
|
std::make_shared<InstallableDerivedPath>(store, ctxElt));
|
2021-05-17 10:48:51 +03:00
|
|
|
|
2022-03-02 14:54:08 +02:00
|
|
|
auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext);
|
2021-05-17 18:49:20 +03:00
|
|
|
res.program = resolveString(*store, unresolved.program, builtContext);
|
2021-05-18 14:54:05 +03:00
|
|
|
if (!store->isInStore(res.program))
|
2021-05-17 18:49:20 +03:00
|
|
|
throw Error("app program '%s' is not in the Nix store", res.program);
|
|
|
|
|
|
|
|
return res;
|
2020-06-29 15:14:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|