mirror of
https://github.com/NixOS/nix.git
synced 2025-12-01 06:31:00 +01:00
Merge pull request #14203 from roberth/move-eval-cache-open-to-libflake
Move eval cache open to libflake
This commit is contained in:
commit
9c8480becb
6 changed files with 54 additions and 49 deletions
|
|
@ -69,7 +69,7 @@ struct InstallableFlake : InstallableValue
|
||||||
*/
|
*/
|
||||||
std::vector<ref<eval_cache::AttrCursor>> getCursors(EvalState & state) override;
|
std::vector<ref<eval_cache::AttrCursor>> getCursors(EvalState & state) override;
|
||||||
|
|
||||||
std::shared_ptr<flake::LockedFlake> getLockedFlake() const;
|
ref<flake::LockedFlake> getLockedFlake() const;
|
||||||
|
|
||||||
FlakeRef nixpkgsFlakeRef() const;
|
FlakeRef nixpkgsFlakeRef() const;
|
||||||
};
|
};
|
||||||
|
|
@ -87,6 +87,4 @@ static inline FlakeRef defaultNixpkgsFlakeRef()
|
||||||
return FlakeRef::fromAttrs(fetchSettings, {{"type", "indirect"}, {"id", "nixpkgs"}});
|
return FlakeRef::fromAttrs(fetchSettings, {{"type", "indirect"}, {"id", "nixpkgs"}});
|
||||||
}
|
}
|
||||||
|
|
||||||
ref<eval_cache::EvalCache> openEvalCache(EvalState & state, std::shared_ptr<flake::LockedFlake> lockedFlake);
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
|
||||||
|
|
@ -185,16 +185,16 @@ std::vector<ref<eval_cache::AttrCursor>> InstallableFlake::getCursors(EvalState
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<flake::LockedFlake> InstallableFlake::getLockedFlake() const
|
ref<flake::LockedFlake> InstallableFlake::getLockedFlake() const
|
||||||
{
|
{
|
||||||
if (!_lockedFlake) {
|
if (!_lockedFlake) {
|
||||||
flake::LockFlags lockFlagsApplyConfig = lockFlags;
|
flake::LockFlags lockFlagsApplyConfig = lockFlags;
|
||||||
// FIXME why this side effect?
|
// FIXME why this side effect?
|
||||||
lockFlagsApplyConfig.applyNixConfig = true;
|
lockFlagsApplyConfig.applyNixConfig = true;
|
||||||
_lockedFlake =
|
_lockedFlake = make_ref<flake::LockedFlake>(lockFlake(flakeSettings, *state, flakeRef, lockFlagsApplyConfig));
|
||||||
std::make_shared<flake::LockedFlake>(lockFlake(flakeSettings, *state, flakeRef, lockFlagsApplyConfig));
|
|
||||||
}
|
}
|
||||||
return _lockedFlake;
|
// _lockedFlake is now non-null but still just a shared_ptr
|
||||||
|
return ref<flake::LockedFlake>(_lockedFlake);
|
||||||
}
|
}
|
||||||
|
|
||||||
FlakeRef InstallableFlake::nixpkgsFlakeRef() const
|
FlakeRef InstallableFlake::nixpkgsFlakeRef() const
|
||||||
|
|
|
||||||
|
|
@ -342,8 +342,7 @@ void completeFlakeRefWithFragment(
|
||||||
parseFlakeRef(fetchSettings, expandTilde(flakeRefS), std::filesystem::current_path().string());
|
parseFlakeRef(fetchSettings, expandTilde(flakeRefS), std::filesystem::current_path().string());
|
||||||
|
|
||||||
auto evalCache = openEvalCache(
|
auto evalCache = openEvalCache(
|
||||||
*evalState,
|
*evalState, make_ref<flake::LockedFlake>(lockFlake(flakeSettings, *evalState, flakeRef, lockFlags)));
|
||||||
std::make_shared<flake::LockedFlake>(lockFlake(flakeSettings, *evalState, flakeRef, lockFlags)));
|
|
||||||
|
|
||||||
auto root = evalCache->getRoot();
|
auto root = evalCache->getRoot();
|
||||||
|
|
||||||
|
|
@ -443,42 +442,6 @@ static StorePath getDeriver(ref<Store> store, const Installable & i, const Store
|
||||||
return *derivers.begin();
|
return *derivers.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
ref<eval_cache::EvalCache> openEvalCache(EvalState & state, std::shared_ptr<flake::LockedFlake> lockedFlake)
|
|
||||||
{
|
|
||||||
auto fingerprint = evalSettings.useEvalCache && evalSettings.pureEval
|
|
||||||
? lockedFlake->getFingerprint(state.store, state.fetchSettings)
|
|
||||||
: std::nullopt;
|
|
||||||
auto rootLoader = [&state, lockedFlake]() {
|
|
||||||
/* For testing whether the evaluation cache is
|
|
||||||
complete. */
|
|
||||||
if (getEnv("NIX_ALLOW_EVAL").value_or("1") == "0")
|
|
||||||
throw Error("not everything is cached, but evaluation is not allowed");
|
|
||||||
|
|
||||||
auto vFlake = state.allocValue();
|
|
||||||
flake::callFlake(state, *lockedFlake, *vFlake);
|
|
||||||
|
|
||||||
state.forceAttrs(*vFlake, noPos, "while parsing cached flake data");
|
|
||||||
|
|
||||||
auto aOutputs = vFlake->attrs()->get(state.symbols.create("outputs"));
|
|
||||||
assert(aOutputs);
|
|
||||||
|
|
||||||
return aOutputs->value;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (fingerprint) {
|
|
||||||
auto search = state.evalCaches.find(fingerprint.value());
|
|
||||||
if (search == state.evalCaches.end()) {
|
|
||||||
search =
|
|
||||||
state.evalCaches
|
|
||||||
.emplace(fingerprint.value(), make_ref<nix::eval_cache::EvalCache>(fingerprint, state, rootLoader))
|
|
||||||
.first;
|
|
||||||
}
|
|
||||||
return search->second;
|
|
||||||
} else {
|
|
||||||
return make_ref<nix::eval_cache::EvalCache>(std::nullopt, state, rootLoader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Installables SourceExprCommand::parseInstallables(ref<Store> store, std::vector<std::string> ss)
|
Installables SourceExprCommand::parseInstallables(ref<Store> store, std::vector<std::string> ss)
|
||||||
{
|
{
|
||||||
Installables result;
|
Installables result;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
#include "nix/util/terminal.hh"
|
#include "nix/util/terminal.hh"
|
||||||
|
#include "nix/util/ref.hh"
|
||||||
|
#include "nix/util/environment-variables.hh"
|
||||||
#include "nix/flake/flake.hh"
|
#include "nix/flake/flake.hh"
|
||||||
#include "nix/expr/eval.hh"
|
#include "nix/expr/eval.hh"
|
||||||
|
#include "nix/expr/eval-cache.hh"
|
||||||
#include "nix/expr/eval-settings.hh"
|
#include "nix/expr/eval-settings.hh"
|
||||||
#include "nix/flake/lockfile.hh"
|
#include "nix/flake/lockfile.hh"
|
||||||
#include "nix/expr/primops.hh"
|
#include "nix/expr/primops.hh"
|
||||||
|
|
@ -924,8 +927,6 @@ void callFlake(EvalState & state, const LockedFlake & lockedFlake, Value & vRes)
|
||||||
state.callFunction(*vCallFlake, args, vRes, noPos);
|
state.callFunction(*vCallFlake, args, vRes, noPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace flake
|
|
||||||
|
|
||||||
std::optional<Fingerprint> LockedFlake::getFingerprint(ref<Store> store, const fetchers::Settings & fetchSettings) const
|
std::optional<Fingerprint> LockedFlake::getFingerprint(ref<Store> store, const fetchers::Settings & fetchSettings) const
|
||||||
{
|
{
|
||||||
if (lockFile.isUnlocked(fetchSettings))
|
if (lockFile.isUnlocked(fetchSettings))
|
||||||
|
|
@ -953,4 +954,41 @@ std::optional<Fingerprint> LockedFlake::getFingerprint(ref<Store> store, const f
|
||||||
|
|
||||||
Flake::~Flake() {}
|
Flake::~Flake() {}
|
||||||
|
|
||||||
|
ref<eval_cache::EvalCache> openEvalCache(EvalState & state, ref<const LockedFlake> lockedFlake)
|
||||||
|
{
|
||||||
|
auto fingerprint = state.settings.useEvalCache && state.settings.pureEval
|
||||||
|
? lockedFlake->getFingerprint(state.store, state.fetchSettings)
|
||||||
|
: std::nullopt;
|
||||||
|
auto rootLoader = [&state, lockedFlake]() {
|
||||||
|
/* For testing whether the evaluation cache is
|
||||||
|
complete. */
|
||||||
|
if (getEnv("NIX_ALLOW_EVAL").value_or("1") == "0")
|
||||||
|
throw Error("not everything is cached, but evaluation is not allowed");
|
||||||
|
|
||||||
|
auto vFlake = state.allocValue();
|
||||||
|
callFlake(state, *lockedFlake, *vFlake);
|
||||||
|
|
||||||
|
state.forceAttrs(*vFlake, noPos, "while parsing cached flake data");
|
||||||
|
|
||||||
|
auto aOutputs = vFlake->attrs()->get(state.symbols.create("outputs"));
|
||||||
|
assert(aOutputs);
|
||||||
|
|
||||||
|
return aOutputs->value;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (fingerprint) {
|
||||||
|
auto search = state.evalCaches.find(fingerprint.value());
|
||||||
|
if (search == state.evalCaches.end()) {
|
||||||
|
search = state.evalCaches
|
||||||
|
.emplace(fingerprint.value(), make_ref<eval_cache::EvalCache>(fingerprint, state, rootLoader))
|
||||||
|
.first;
|
||||||
|
}
|
||||||
|
return search->second;
|
||||||
|
} else {
|
||||||
|
return make_ref<eval_cache::EvalCache>(std::nullopt, state, rootLoader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace flake
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include "nix/flake/flakeref.hh"
|
#include "nix/flake/flakeref.hh"
|
||||||
#include "nix/flake/lockfile.hh"
|
#include "nix/flake/lockfile.hh"
|
||||||
#include "nix/expr/value.hh"
|
#include "nix/expr/value.hh"
|
||||||
|
#include "nix/expr/eval-cache.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
|
@ -218,6 +219,11 @@ lockFlake(const Settings & settings, EvalState & state, const FlakeRef & flakeRe
|
||||||
|
|
||||||
void callFlake(EvalState & state, const LockedFlake & lockedFlake, Value & v);
|
void callFlake(EvalState & state, const LockedFlake & lockedFlake, Value & v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open an evaluation cache for a flake.
|
||||||
|
*/
|
||||||
|
ref<eval_cache::EvalCache> openEvalCache(EvalState & state, ref<const LockedFlake> lockedFlake);
|
||||||
|
|
||||||
} // namespace flake
|
} // namespace flake
|
||||||
|
|
||||||
void emitTreeAttrs(
|
void emitTreeAttrs(
|
||||||
|
|
|
||||||
|
|
@ -1155,7 +1155,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|
||||||
evalSettings.enableImportFromDerivation.setDefault(false);
|
evalSettings.enableImportFromDerivation.setDefault(false);
|
||||||
|
|
||||||
auto state = getEvalState();
|
auto state = getEvalState();
|
||||||
auto flake = std::make_shared<LockedFlake>(lockFlake());
|
auto flake = make_ref<LockedFlake>(lockFlake());
|
||||||
auto localSystem = std::string(settings.thisSystem.get());
|
auto localSystem = std::string(settings.thisSystem.get());
|
||||||
|
|
||||||
std::function<bool(eval_cache::AttrCursor & visitor, const std::vector<Symbol> & attrPath, const Symbol & attr)>
|
std::function<bool(eval_cache::AttrCursor & visitor, const std::vector<Symbol> & attrPath, const Symbol & attr)>
|
||||||
|
|
@ -1443,7 +1443,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|
||||||
return j;
|
return j;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto cache = openEvalCache(*state, flake);
|
auto cache = openEvalCache(*state, ref<flake::LockedFlake>(flake));
|
||||||
|
|
||||||
auto j = visit(*cache->getRoot(), {}, fmt(ANSI_BOLD "%s" ANSI_NORMAL, flake->flake.lockedRef), "");
|
auto j = visit(*cache->getRoot(), {}, fmt(ANSI_BOLD "%s" ANSI_NORMAL, flake->flake.lockedRef), "");
|
||||||
if (json)
|
if (json)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue