From 9f4cfbb2e70e074fad8f03fdb26523387e2a5456 Mon Sep 17 00:00:00 2001 From: Matthew Kenigsberg Date: Sat, 9 May 2020 09:35:33 -0600 Subject: [PATCH] Refactor installables InstallableValue has children InstallableFlake and InstallableAttrPath, but InstallableFlake was overriding toDerivations, and usage was changed so that InstallableFlake didn't need cmd. So these changes were made: InstallableValue::toDerivations() -> InstalllableAttrPath::toDerivations() InstallableValue::cmd -> InstallableAttrPath::cmd InstallableValue uses state instead of cmd toBuildables() and toDerivations() were made abstract --- src/nix/installables.cc | 26 ++++++++++++-------------- src/nix/installables.hh | 20 +++++++++----------- src/nix/profile.cc | 2 +- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/nix/installables.cc b/src/nix/installables.cc index 16e03875c..5267a37a0 100644 --- a/src/nix/installables.cc +++ b/src/nix/installables.cc @@ -179,10 +179,8 @@ struct InstallableStorePath : Installable } }; -std::vector InstallableValue::toDerivations() +std::vector InstallableAttrPath::toDerivations() { - auto state = cmd.getEvalState(); - auto v = toValue(*state).first; Bindings & autoArgs = *cmd.getAutoArgs(*state); @@ -204,8 +202,6 @@ std::vector InstallableValue::toDerivations() Buildables InstallableValue::toBuildables() { - auto state = cmd.getEvalState(); - Buildables res; StorePathSet drvPaths; @@ -239,11 +235,12 @@ Buildables InstallableValue::toBuildables() struct InstallableAttrPath : InstallableValue { + SourceExprCommand & cmd; RootValue v; std::string attrPath; - InstallableAttrPath(SourceExprCommand & cmd, Value * v, const std::string & attrPath) - : InstallableValue(cmd), v(allocRootValue(v)), attrPath(attrPath) + InstallableAttrPath(ref state, SourceExprCommand & cmd, Value * v, const std::string & attrPath) + : InstallableValue(state), cmd(cmd), v(allocRootValue(v)), attrPath(attrPath) { } std::string what() override { return attrPath; } @@ -254,6 +251,8 @@ struct InstallableAttrPath : InstallableValue state.forceValue(*vRes); return {vRes, pos}; } + + virtual std::vector toDerivations() override; }; std::vector InstallableFlake::getActualAttrPaths() @@ -313,10 +312,9 @@ ref openEvalCache( std::tuple InstallableFlake::toDerivation() { - auto state = cmd.getEvalState(); auto lockedFlake = std::make_shared( - lockFlake(*state, flakeRef, cmd.lockFlags)); + lockFlake(*state, flakeRef, lockFlags)); auto cache = openEvalCache(*state, lockedFlake, true); auto root = cache->getRoot(); @@ -362,7 +360,7 @@ std::vector InstallableFlake::toDerivations() std::pair InstallableFlake::toValue(EvalState & state) { - auto lockedFlake = lockFlake(state, flakeRef, cmd.lockFlags); + auto lockedFlake = lockFlake(state, flakeRef, lockFlags); auto vOutputs = getFlakeOutputs(state, lockedFlake); @@ -385,7 +383,7 @@ std::vector, std::string>> InstallableFlake::getCursor(EvalState & state, bool useEvalCache) { auto evalCache = openEvalCache(state, - std::make_shared(lockFlake(state, flakeRef, cmd.lockFlags)), + std::make_shared(lockFlake(state, flakeRef, lockFlags)), useEvalCache); auto root = evalCache->getRoot(); @@ -423,7 +421,7 @@ std::vector> SourceExprCommand::parseInstallables( } for (auto & s : ss) - result.push_back(std::make_shared(*this, vFile, s == "." ? "" : s)); + result.push_back(std::make_shared(state, *this, vFile, s == "." ? "" : s)); } else { @@ -433,9 +431,9 @@ std::vector> SourceExprCommand::parseInstallables( try { auto [flakeRef, fragment] = parseFlakeRefWithFragment(s, absPath(".")); result.push_back(std::make_shared( - *this, std::move(flakeRef), + getEvalState(), std::move(flakeRef), fragment == "" ? getDefaultFlakeAttrPaths() : Strings{fragment}, - getDefaultFlakeAttrPathPrefixes())); + getDefaultFlakeAttrPathPrefixes(), lockFlags)); continue; } catch (...) { ex = std::current_exception(); diff --git a/src/nix/installables.hh b/src/nix/installables.hh index c9e277a51..531720de6 100644 --- a/src/nix/installables.hh +++ b/src/nix/installables.hh @@ -37,10 +37,7 @@ struct Installable virtual std::string what() = 0; - virtual Buildables toBuildables() - { - throw Error("argument '%s' cannot be built", what()); - } + virtual Buildables toBuildables() = 0; Buildable toBuildable(); @@ -64,9 +61,9 @@ struct Installable struct InstallableValue : Installable { - SourceExprCommand & cmd; + ref state; - InstallableValue(SourceExprCommand & cmd) : cmd(cmd) { } + InstallableValue(ref state) : state(state) {} struct DerivationInfo { @@ -75,7 +72,7 @@ struct InstallableValue : Installable std::string outputName; }; - virtual std::vector toDerivations(); + virtual std::vector toDerivations() = 0; Buildables toBuildables() override; }; @@ -85,11 +82,12 @@ struct InstallableFlake : InstallableValue FlakeRef flakeRef; Strings attrPaths; Strings prefixes; + const flake::LockFlags & lockFlags; - InstallableFlake(SourceExprCommand & cmd, FlakeRef && flakeRef, - Strings && attrPaths, Strings && prefixes) - : InstallableValue(cmd), flakeRef(flakeRef), attrPaths(attrPaths), - prefixes(prefixes) + InstallableFlake(ref state, FlakeRef && flakeRef, + Strings && attrPaths, Strings && prefixes, const flake::LockFlags & lockFlags) + : InstallableValue(state), flakeRef(flakeRef), attrPaths(attrPaths), + prefixes(prefixes), lockFlags(lockFlags) { } std::string what() override { return flakeRef.to_string() + "#" + *attrPaths.begin(); } diff --git a/src/nix/profile.cc b/src/nix/profile.cc index 5aaf5234c..cc239052d 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -336,7 +336,7 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf Activity act(*logger, lvlChatty, actUnknown, fmt("checking '%s' for updates", element.source->attrPath)); - InstallableFlake installable(*this, FlakeRef(element.source->originalRef), {element.source->attrPath}, {}); + InstallableFlake installable(getEvalState(), FlakeRef(element.source->originalRef), {element.source->attrPath}, {}, lockFlags); auto [attrPath, resolvedRef, drv] = installable.toDerivation();