InstallableFlake: Default attr paths cleanup

This removes some duplicated logic, and fixes "nix bundle" parsing its
installable twice.
This commit is contained in:
Eelco Dolstra 2022-02-14 20:39:44 +01:00
parent 744a101a36
commit 023e459777
6 changed files with 32 additions and 27 deletions

View file

@ -545,13 +545,14 @@ InstallableFlake::InstallableFlake(
SourceExprCommand * cmd, SourceExprCommand * cmd,
ref<EvalState> state, ref<EvalState> state,
FlakeRef && flakeRef, FlakeRef && flakeRef,
Strings && attrPaths, std::string_view fragment,
Strings && prefixes, Strings attrPaths,
Strings prefixes,
const flake::LockFlags & lockFlags) const flake::LockFlags & lockFlags)
: InstallableValue(state), : InstallableValue(state),
flakeRef(flakeRef), flakeRef(flakeRef),
attrPaths(attrPaths), attrPaths(fragment == "" ? attrPaths : Strings{(std::string) fragment}),
prefixes(prefixes), prefixes(fragment == "" ? Strings{} : prefixes),
lockFlags(lockFlags) lockFlags(lockFlags)
{ {
if (cmd && cmd->getAutoArgs(*state)->size()) if (cmd && cmd->getAutoArgs(*state)->size())
@ -566,6 +567,8 @@ std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableF
auto root = cache->getRoot(); auto root = cache->getRoot();
for (auto & attrPath : getActualAttrPaths()) { for (auto & attrPath : getActualAttrPaths()) {
debug("trying flake output attribute '%s'", attrPath);
auto attr = root->findAlongAttrPath( auto attr = root->findAlongAttrPath(
parseAttrPath(*state, attrPath), parseAttrPath(*state, attrPath),
true true
@ -708,8 +711,9 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
this, this,
getEvalState(), getEvalState(),
std::move(flakeRef), std::move(flakeRef),
fragment == "" ? getDefaultFlakeAttrPaths() : Strings{fragment}, fragment,
fragment == "" ? Strings{} : getDefaultFlakeAttrPathPrefixes(), getDefaultFlakeAttrPaths(),
getDefaultFlakeAttrPathPrefixes(),
lockFlags)); lockFlags));
continue; continue;
} catch (...) { } catch (...) {

View file

@ -102,8 +102,9 @@ struct InstallableFlake : InstallableValue
SourceExprCommand * cmd, SourceExprCommand * cmd,
ref<EvalState> state, ref<EvalState> state,
FlakeRef && flakeRef, FlakeRef && flakeRef,
Strings && attrPaths, std::string_view fragment,
Strings && prefixes, Strings attrPaths,
Strings prefixes,
const flake::LockFlags & lockFlags); const flake::LockFlags & lockFlags);
std::string what() const override { return flakeRef.to_string() + "#" + *attrPaths.begin(); } std::string what() const override { return flakeRef.to_string() + "#" + *attrPaths.begin(); }

View file

@ -74,21 +74,16 @@ struct CmdBundle : InstallableCommand
{ {
auto evalState = getEvalState(); auto evalState = getEvalState();
auto [progFlakeRef, progName] = parseFlakeRefWithFragment(installable->what(), absPath(".")); auto val = installable->toValue(*evalState).first;
const flake::LockFlags lockFlagsProg{ .writeLockFile = false };
auto programInstallable = InstallableFlake(this,
evalState, std::move(progFlakeRef),
Strings{progName == "" ? "defaultApp" : progName},
Strings(this->getDefaultFlakeAttrPathPrefixes()),
lockFlagsProg);
auto val = programInstallable.toValue(*evalState).first;
auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath(".")); auto [bundlerFlakeRef, bundlerName] = parseFlakeRefWithFragment(bundler, absPath("."));
const flake::LockFlags lockFlags{ .writeLockFile = false }; const flake::LockFlags lockFlags{ .writeLockFile = false };
auto bundler = InstallableFlake(this, InstallableFlake bundler{this,
evalState, std::move(bundlerFlakeRef), evalState, std::move(bundlerFlakeRef), bundlerName,
Strings{bundlerName == "" ? "defaultBundler." + settings.thisSystem.get() : settings.thisSystem.get() + "." + bundlerName, bundlerName}, {"defaultBundler." + settings.thisSystem.get()},
Strings({"","bundlers."}), lockFlags); {"bundlers." + settings.thisSystem.get() + "."},
lockFlags
};
auto vRes = evalState->allocValue(); auto vRes = evalState->allocValue();
evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos); evalState->callFunction(*bundler.toValue(*evalState).first, *val, *vRes, noPos);

View file

@ -498,7 +498,8 @@ struct CmdDevelop : Common, MixEnvironment
this, this,
state, state,
installable->nixpkgsFlakeRef(), installable->nixpkgsFlakeRef(),
Strings{"bashInteractive"}, "bashInteractive",
Strings{},
Strings{"legacyPackages." + settings.thisSystem.get() + "."}, Strings{"legacyPackages." + settings.thisSystem.get() + "."},
nixpkgsLockFlags); nixpkgsLockFlags);

View file

@ -649,12 +649,14 @@ struct CmdFlakeCheck : FlakeCommand
} }
}; };
static Strings defaultTemplateAttrPathsPrefixes{"templates."};
static Strings defaultTemplateAttrPaths = {"defaultTemplate"};
struct CmdFlakeInitCommon : virtual Args, EvalCommand struct CmdFlakeInitCommon : virtual Args, EvalCommand
{ {
std::string templateUrl = "templates"; std::string templateUrl = "templates";
Path destDir; Path destDir;
const Strings attrsPathPrefixes{"templates."};
const LockFlags lockFlags{ .writeLockFile = false }; const LockFlags lockFlags{ .writeLockFile = false };
CmdFlakeInitCommon() CmdFlakeInitCommon()
@ -669,8 +671,8 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
completeFlakeRefWithFragment( completeFlakeRefWithFragment(
getEvalState(), getEvalState(),
lockFlags, lockFlags,
attrsPathPrefixes, defaultTemplateAttrPathsPrefixes,
{"defaultTemplate"}, defaultTemplateAttrPaths,
prefix); prefix);
}} }}
}); });
@ -685,9 +687,10 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
auto [templateFlakeRef, templateName] = parseFlakeRefWithFragment(templateUrl, absPath(".")); auto [templateFlakeRef, templateName] = parseFlakeRefWithFragment(templateUrl, absPath("."));
auto installable = InstallableFlake(nullptr, auto installable = InstallableFlake(nullptr,
evalState, std::move(templateFlakeRef), evalState, std::move(templateFlakeRef), templateName,
Strings{templateName == "" ? "defaultTemplate" : templateName}, defaultTemplateAttrPaths,
Strings(attrsPathPrefixes), lockFlags); defaultTemplateAttrPathsPrefixes,
lockFlags);
auto [cursor, attrPath] = installable.getCursor(*evalState); auto [cursor, attrPath] = installable.getCursor(*evalState);

View file

@ -423,6 +423,7 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
this, this,
getEvalState(), getEvalState(),
FlakeRef(element.source->originalRef), FlakeRef(element.source->originalRef),
"",
{element.source->attrPath}, {element.source->attrPath},
{}, {},
lockFlags); lockFlags);