1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-20 09:19:36 +01:00

Support modules in toDerivation() and toApp()

This commit is contained in:
Eelco Dolstra 2020-09-03 13:11:22 +02:00
parent 22b8e07f09
commit b7d263b79a
2 changed files with 44 additions and 17 deletions

View file

@ -28,18 +28,29 @@ App Installable::toApp(EvalState & state)
};
}
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();
auto getDerivation = [&](std::shared_ptr<eval_cache::AttrCursor> attr)
{
auto drvPath = attr->forceDerivation();
auto outPath = attr->getAttr(state.sOutPath)->getString();
auto outputName = attr->getAttr(state.sOutputName)->getString();
auto name = attr->getAttr(state.sName)->getString();
return App {
.context = { { drvPath, {outputName} } },
.program = outPath + "/bin/" + DrvName(name).name,
};
};
if (type == "derivation")
return getDerivation(cursor);
if (type == "module") {
// FIXME: define an 'app' option.
auto aDerivation = cursor->findAlongAttrPath(
{state.symbols.create("final"), state.symbols.create("derivation")});
if (aDerivation)
return getDerivation(aDerivation);
}
else
throw Error("attribute '%s' has unsupported type '%s'", attrPath, type);
}

View file

@ -149,7 +149,8 @@ Strings SourceExprCommand::getDefaultFlakeAttrPathPrefixes()
"packages." + settings.thisSystem.get() + ".",
// As a temporary hack until Nixpkgs is properly converted
// to provide a clean 'packages' set, look in 'legacyPackages'.
"legacyPackages." + settings.thisSystem.get() + "."
"legacyPackages." + settings.thisSystem.get() + ".",
"modules.",
};
}
@ -464,12 +465,9 @@ std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableF
auto root = cache->getRoot();
for (auto & attrPath : getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(parseAttrPath(*state, attrPath));
if (!attr) continue;
if (!attr->isDerivation())
throw Error("flake output attribute '%s' is not a derivation", attrPath);
auto getDerivation = [&](std::shared_ptr<eval_cache::AttrCursor> attr) -> std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo>
{
auto drvPath = attr->forceDerivation();
auto drvInfo = DerivationInfo{
@ -479,6 +477,24 @@ std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableF
};
return {attrPath, lockedFlake->flake.lockedRef, std::move(drvInfo)};
};
auto attr = root->findAlongAttrPath(parseAttrPath(*state, attrPath));
if (!attr) continue;
auto aType = attr->maybeGetAttr(state->sType);
if (aType && aType->getString() == "derivation")
return getDerivation(attr);
else if (aType && aType->getString() == "module") {
if (auto aDerivation = attr->findAlongAttrPath(
{state->symbols.create("final"), state->symbols.create("derivation")}))
if (aDerivation)
return getDerivation(aDerivation);
}
throw Error("flake output attribute '%s' is not a derivation", attrPath);
}
throw Error("flake '%s' does not provide attribute %s",