2017-04-25 10:06:32 +00:00
|
|
|
|
#include "command.hh"
|
2016-02-09 20:34:24 +00:00
|
|
|
|
#include "attr-path.hh"
|
|
|
|
|
#include "common-opts.hh"
|
|
|
|
|
#include "derivations.hh"
|
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
#include "get-drvs.hh"
|
|
|
|
|
#include "store-api.hh"
|
2017-04-25 09:20:37 +00:00
|
|
|
|
#include "shared.hh"
|
|
|
|
|
|
|
|
|
|
#include <regex>
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
Value * InstallablesCommand::getSourceExpr(EvalState & state)
|
2016-08-23 15:11:19 +00:00
|
|
|
|
{
|
2017-04-25 09:20:37 +00:00
|
|
|
|
if (vSourceExpr) return vSourceExpr;
|
|
|
|
|
|
|
|
|
|
vSourceExpr = state.allocValue();
|
2016-08-23 15:11:19 +00:00
|
|
|
|
|
|
|
|
|
if (file != "") {
|
|
|
|
|
Expr * e = state.parseExprFromFile(resolveExprPath(lookupFileArg(state, file)));
|
2017-04-25 09:20:37 +00:00
|
|
|
|
state.eval(e, *vSourceExpr);
|
2016-08-23 15:11:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
/* Construct the installation source from $NIX_PATH. */
|
|
|
|
|
|
|
|
|
|
auto searchPath = state.getSearchPath();
|
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
state.mkAttrs(*vSourceExpr, searchPath.size());
|
2016-08-23 15:11:19 +00:00
|
|
|
|
|
|
|
|
|
std::unordered_set<std::string> seen;
|
|
|
|
|
|
|
|
|
|
for (auto & i : searchPath) {
|
|
|
|
|
if (i.first == "") continue;
|
|
|
|
|
if (seen.count(i.first)) continue;
|
|
|
|
|
seen.insert(i.first);
|
2017-04-25 09:20:37 +00:00
|
|
|
|
#if 0
|
|
|
|
|
auto res = state.resolveSearchPathElem(i);
|
|
|
|
|
if (!res.first) continue;
|
|
|
|
|
if (!pathExists(res.second)) continue;
|
|
|
|
|
mkApp(*state.allocAttr(*vSourceExpr, state.symbols.create(i.first)),
|
2016-08-23 15:11:19 +00:00
|
|
|
|
state.getBuiltin("import"),
|
2017-04-25 09:20:37 +00:00
|
|
|
|
mkString(*state.allocValue(), res.second));
|
|
|
|
|
#endif
|
|
|
|
|
Value * v1 = state.allocValue();
|
|
|
|
|
mkPrimOpApp(*v1, state.getBuiltin("findFile"), state.getBuiltin("nixPath"));
|
|
|
|
|
Value * v2 = state.allocValue();
|
|
|
|
|
mkApp(*v2, *v1, mkString(*state.allocValue(), i.first));
|
|
|
|
|
mkApp(*state.allocAttr(*vSourceExpr, state.symbols.create(i.first)),
|
|
|
|
|
state.getBuiltin("import"), *v2);
|
2016-08-23 15:11:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
vSourceExpr->attrs->sort();
|
2016-08-23 15:11:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
return vSourceExpr;
|
2016-08-23 15:11:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
struct InstallableStoreDrv : Installable
|
|
|
|
|
{
|
|
|
|
|
Path storePath;
|
|
|
|
|
|
|
|
|
|
InstallableStoreDrv(const Path & storePath) : storePath(storePath) { }
|
|
|
|
|
|
|
|
|
|
std::string what() override { return storePath; }
|
|
|
|
|
|
|
|
|
|
PathSet toBuildable() override
|
|
|
|
|
{
|
|
|
|
|
return {storePath};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct InstallableStorePath : Installable
|
2016-02-09 20:34:24 +00:00
|
|
|
|
{
|
2017-04-25 09:20:37 +00:00
|
|
|
|
Path storePath;
|
|
|
|
|
|
|
|
|
|
InstallableStorePath(const Path & storePath) : storePath(storePath) { }
|
|
|
|
|
|
|
|
|
|
std::string what() override { return storePath; }
|
|
|
|
|
|
|
|
|
|
PathSet toBuildable() override
|
|
|
|
|
{
|
|
|
|
|
return {storePath};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct InstallableExpr : Installable
|
|
|
|
|
{
|
2017-04-25 10:06:32 +00:00
|
|
|
|
InstallablesCommand & installables;
|
2017-04-25 09:20:37 +00:00
|
|
|
|
std::string text;
|
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
InstallableExpr(InstallablesCommand & installables, const std::string & text)
|
2017-04-25 09:20:37 +00:00
|
|
|
|
: installables(installables), text(text) { }
|
|
|
|
|
|
|
|
|
|
std::string what() override { return text; }
|
|
|
|
|
|
|
|
|
|
PathSet toBuildable() override
|
|
|
|
|
{
|
|
|
|
|
auto state = installables.getEvalState();
|
|
|
|
|
|
|
|
|
|
auto v = toValue(*state);
|
|
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
|
std::map<string, string> autoArgs_;
|
|
|
|
|
Bindings & autoArgs(*evalAutoArgs(*state, autoArgs_));
|
|
|
|
|
|
|
|
|
|
DrvInfos drvs;
|
|
|
|
|
getDerivations(*state, *v, "", autoArgs, drvs, false);
|
|
|
|
|
|
|
|
|
|
PathSet res;
|
|
|
|
|
|
|
|
|
|
for (auto & i : drvs)
|
|
|
|
|
res.insert(i.queryDrvPath());
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value * toValue(EvalState & state) override
|
|
|
|
|
{
|
|
|
|
|
auto v = state.allocValue();
|
|
|
|
|
state.eval(state.parseExprFromString(text, absPath(".")), *v);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct InstallableAttrPath : Installable
|
|
|
|
|
{
|
2017-04-25 10:06:32 +00:00
|
|
|
|
InstallablesCommand & installables;
|
2017-04-25 09:20:37 +00:00
|
|
|
|
std::string attrPath;
|
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
InstallableAttrPath(InstallablesCommand & installables, const std::string & attrPath)
|
2017-04-25 09:20:37 +00:00
|
|
|
|
: installables(installables), attrPath(attrPath)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
std::string what() override { return attrPath; }
|
|
|
|
|
|
|
|
|
|
PathSet toBuildable() override
|
|
|
|
|
{
|
|
|
|
|
auto state = installables.getEvalState();
|
|
|
|
|
|
|
|
|
|
auto v = toValue(*state);
|
|
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
|
std::map<string, string> autoArgs_;
|
|
|
|
|
Bindings & autoArgs(*evalAutoArgs(*state, autoArgs_));
|
|
|
|
|
|
|
|
|
|
DrvInfos drvs;
|
|
|
|
|
getDerivations(*state, *v, "", autoArgs, drvs, false);
|
|
|
|
|
|
|
|
|
|
PathSet res;
|
|
|
|
|
|
|
|
|
|
for (auto & i : drvs)
|
|
|
|
|
res.insert(i.queryDrvPath());
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value * toValue(EvalState & state) override
|
|
|
|
|
{
|
|
|
|
|
auto source = installables.getSourceExpr(state);
|
|
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
|
std::map<string, string> autoArgs_;
|
|
|
|
|
Bindings & autoArgs(*evalAutoArgs(state, autoArgs_));
|
|
|
|
|
|
|
|
|
|
Value * v = findAlongAttrPath(state, attrPath, autoArgs, *source);
|
|
|
|
|
state.forceValue(*v);
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FIXME: extend
|
|
|
|
|
std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)";
|
|
|
|
|
static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex));
|
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
std::vector<std::shared_ptr<Installable>> InstallablesCommand::parseInstallables(ref<Store> store, Strings ss)
|
2017-04-25 09:20:37 +00:00
|
|
|
|
{
|
|
|
|
|
std::vector<std::shared_ptr<Installable>> result;
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
if (ss.empty() && useDefaultInstallables()) {
|
2017-04-25 13:18:05 +00:00
|
|
|
|
if (file == "")
|
|
|
|
|
file = ".";
|
2017-05-02 13:28:35 +00:00
|
|
|
|
ss = Strings{""};
|
2017-04-25 13:18:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
for (auto & s : ss) {
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
if (s.find("/") != std::string::npos) {
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
auto path = store->toStorePath(store->followLinksToStore(s));
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-04-25 12:09:01 +00:00
|
|
|
|
if (store->isStorePath(path)) {
|
|
|
|
|
if (isDerivation(path))
|
|
|
|
|
result.push_back(std::make_shared<InstallableStoreDrv>(path));
|
|
|
|
|
else
|
|
|
|
|
result.push_back(std::make_shared<InstallableStorePath>(path));
|
2016-02-09 20:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
else if (s.compare(0, 1, "(") == 0)
|
|
|
|
|
result.push_back(std::make_shared<InstallableExpr>(*this, s));
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-05-02 13:28:35 +00:00
|
|
|
|
else if (s == "" || std::regex_match(s, attrPathRegex))
|
|
|
|
|
result.push_back(std::make_shared<InstallableAttrPath>(*this, s));
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
else
|
2017-05-02 13:28:35 +00:00
|
|
|
|
throw UsageError("don't know what to do with argument ‘%s’", s);
|
2017-04-25 09:20:37 +00:00
|
|
|
|
}
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
PathSet InstallablesCommand::buildInstallables(ref<Store> store, bool dryRun)
|
2017-04-25 09:20:37 +00:00
|
|
|
|
{
|
|
|
|
|
PathSet buildables;
|
2016-02-09 20:34:24 +00:00
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
for (auto & i : installables) {
|
|
|
|
|
auto b = i->toBuildable();
|
|
|
|
|
buildables.insert(b.begin(), b.end());
|
2016-02-09 20:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 09:20:37 +00:00
|
|
|
|
printMissing(store, buildables);
|
|
|
|
|
|
|
|
|
|
if (!dryRun)
|
|
|
|
|
store->buildPaths(buildables);
|
|
|
|
|
|
2017-04-25 14:19:22 +00:00
|
|
|
|
PathSet outPaths;
|
|
|
|
|
for (auto & path : buildables)
|
|
|
|
|
if (isDerivation(path)) {
|
|
|
|
|
Derivation drv = store->derivationFromPath(path);
|
|
|
|
|
for (auto & output : drv.outputs)
|
|
|
|
|
outPaths.insert(output.second.path);
|
|
|
|
|
} else
|
|
|
|
|
outPaths.insert(path);
|
|
|
|
|
|
|
|
|
|
return outPaths;
|
2017-04-25 09:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
ref<EvalState> InstallablesCommand::getEvalState()
|
2017-04-25 09:20:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (!evalState)
|
|
|
|
|
evalState = std::make_shared<EvalState>(Strings{}, getStore());
|
|
|
|
|
return ref<EvalState>(evalState);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 10:06:32 +00:00
|
|
|
|
void InstallablesCommand::prepare()
|
2017-04-25 09:20:37 +00:00
|
|
|
|
{
|
|
|
|
|
installables = parseInstallables(getStore(), _installables);
|
2016-02-09 20:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|