Skip to content

Commit

Permalink
Merge branch 'store-path-or-ca' of github.com:obsidiansystems/nix int…
Browse files Browse the repository at this point in the history
…o single-ca-drv-build
  • Loading branch information
Ericson2314 committed Aug 7, 2020
2 parents c127ab3 + a1ccec5 commit 5f1aa4b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 60 deletions.
17 changes: 11 additions & 6 deletions perl/lib/Nix/Store.xs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ void setVerbosity(int level)
int isValidPath(char * path)
CODE:
try {
RETVAL = store()->isValidPath(store()->parseStorePath(path));
auto storePath = store()->parseStorePath(path);
RETVAL = store()->isValidPath(storePath);
} catch (Error & e) {
croak("%s", e.what());
}
Expand All @@ -70,7 +71,8 @@ int isValidPath(char * path)
SV * queryReferences(char * path)
PPCODE:
try {
for (auto & i : store()->queryPathInfo(store()->parseStorePath(path))->references)
auto storePath = store()->parseStorePath(path);
for (auto & i : store()->queryPathInfo(storePath)->referencesPossiblyToSelf())
XPUSHs(sv_2mortal(newSVpv(store()->printStorePath(i).c_str(), 0)));
} catch (Error & e) {
croak("%s", e.what());
Expand All @@ -80,7 +82,8 @@ SV * queryReferences(char * path)
SV * queryPathHash(char * path)
PPCODE:
try {
auto s = store()->queryPathInfo(store()->parseStorePath(path))->narHash->to_string(Base32, true);
auto storePath = store()->parseStorePath(path);
auto s = store()->queryPathInfo(storePath)->narHash->to_string(Base32, true);
XPUSHs(sv_2mortal(newSVpv(s.c_str(), 0)));
} catch (Error & e) {
croak("%s", e.what());
Expand All @@ -90,7 +93,8 @@ SV * queryPathHash(char * path)
SV * queryDeriver(char * path)
PPCODE:
try {
auto info = store()->queryPathInfo(store()->parseStorePath(path));
auto storePath = store()->parseStorePath(path);
auto info = store()->queryPathInfo(storePath);
if (!info->deriver) XSRETURN_UNDEF;
XPUSHs(sv_2mortal(newSVpv(store()->printStorePath(*info->deriver).c_str(), 0)));
} catch (Error & e) {
Expand All @@ -101,7 +105,8 @@ SV * queryDeriver(char * path)
SV * queryPathInfo(char * path, int base32)
PPCODE:
try {
auto info = store()->queryPathInfo(store()->parseStorePath(path));
auto storePath = store()->parseStorePath(path);
auto info = store()->queryPathInfo(storePath);
if (!info->deriver)
XPUSHs(&PL_sv_undef);
else
Expand All @@ -111,7 +116,7 @@ SV * queryPathInfo(char * path, int base32)
mXPUSHi(info->registrationTime);
mXPUSHi(info->narSize);
AV * arr = newAV();
for (auto & i : info->references)
for (auto & i : info->referencesPossiblyToSelf())
av_push(arr, newSVpv(store()->printStorePath(i).c_str(), 0));
XPUSHs(sv_2mortal(newRV((SV *) arr)));
} catch (Error & e) {
Expand Down
17 changes: 10 additions & 7 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ Value & AttrCursor::forceValue()
return v;
}

std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name)
std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name, bool forceErrors)
{
if (root->db) {
if (!cachedValue)
Expand All @@ -422,9 +422,12 @@ std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name)
if (attr) {
if (std::get_if<missing_t>(&attr->second))
return nullptr;
else if (std::get_if<failed_t>(&attr->second))
throw EvalError("cached failure of attribute '%s'", getAttrPathStr(name));
else
else if (std::get_if<failed_t>(&attr->second)) {
if (forceErrors)
debug("reevaluating failed cached attribute '%s'");
else
throw CachedEvalError("cached failure of attribute '%s'", getAttrPathStr(name));
} else
return std::make_shared<AttrCursor>(root,
std::make_pair(shared_from_this(), name), nullptr, std::move(attr));
}
Expand Down Expand Up @@ -469,9 +472,9 @@ std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(std::string_view name)
return maybeGetAttr(root->state.symbols.create(name));
}

std::shared_ptr<AttrCursor> AttrCursor::getAttr(Symbol name)
std::shared_ptr<AttrCursor> AttrCursor::getAttr(Symbol name, bool forceErrors)
{
auto p = maybeGetAttr(name);
auto p = maybeGetAttr(name, forceErrors);
if (!p)
throw Error("attribute '%s' does not exist", getAttrPathStr(name));
return p;
Expand Down Expand Up @@ -600,7 +603,7 @@ bool AttrCursor::isDerivation()

StorePath AttrCursor::forceDerivation()
{
auto aDrvPath = getAttr(root->state.sDrvPath);
auto aDrvPath = getAttr(root->state.sDrvPath, true);
auto drvPath = root->state.store->parseStorePath(aDrvPath->getString());
if (!root->state.store->isValidPath(drvPath) && !settings.readOnlyMode) {
/* The eval cache contains 'drvPath', but the actual path has
Expand Down
6 changes: 4 additions & 2 deletions src/libexpr/eval-cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace nix::eval_cache {

MakeError(CachedEvalError, EvalError);

class AttrDb;
class AttrCursor;

Expand Down Expand Up @@ -92,11 +94,11 @@ public:

std::string getAttrPathStr(Symbol name) const;

std::shared_ptr<AttrCursor> maybeGetAttr(Symbol name);
std::shared_ptr<AttrCursor> maybeGetAttr(Symbol name, bool forceErrors = false);

std::shared_ptr<AttrCursor> maybeGetAttr(std::string_view name);

std::shared_ptr<AttrCursor> getAttr(Symbol name);
std::shared_ptr<AttrCursor> getAttr(Symbol name, bool forceErrors = false);

std::shared_ptr<AttrCursor> getAttr(std::string_view name);

Expand Down
3 changes: 3 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ struct EvalSettings : Config

Setting<bool> traceFunctionCalls{this, false, "trace-function-calls",
"Emit log messages for each function entry and exit at the 'vomit' log level (-vvvv)."};

Setting<bool> useEvalCache{this, true, "eval-cache",
"Whether to use the flake evaluation cache."};
};

extern EvalSettings evalSettings;
Expand Down
13 changes: 13 additions & 0 deletions src/libstore/remote-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void WorkerProto<std::string>::write(const Store & store, Sink & out, const std:
out << str;
}


StorePath WorkerProto<StorePath>::read(const Store & store, Source & from)
{
return store.parseStorePath(readString(from));
Expand All @@ -53,6 +54,18 @@ void WorkerProto<StorePathDescriptor>::write(const Store & store, Sink & out, co
}


std::optional<StorePath> WorkerProto<std::optional<StorePath>>::read(const Store & store, Source & from)
{
auto s = readString(from);
return s == "" ? std::optional<StorePath> {} : store.parseStorePath(s);
}

void WorkerProto<std::optional<StorePath>>::write(const Store & store, Sink & out, const std::optional<StorePath> & storePathOpt)
{
out << (storePathOpt ? store.printStorePath(*storePathOpt) : "");
}


/* TODO: Separate these store impls into different files, give them better names */
RemoteStore::RemoteStore(const Params & params)
: Store(params)
Expand Down
34 changes: 18 additions & 16 deletions src/libstore/worker-protocol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,16 @@ struct WorkerProto {
static void write(const Store & store, Sink & out, const T & t);
};

template<>
struct WorkerProto<std::string> {
static std::string read(const Store & store, Source & from);
static void write(const Store & store, Sink & out, const std::string & t);
};

template<>
struct WorkerProto<StorePath> {
static StorePath read(const Store & store, Source & from);
static void write(const Store & store, Sink & out, const StorePath & t);
};
#define MAKE_WORKER_PROTO(T) \
template<> \
struct WorkerProto< T > { \
static T read(const Store & store, Source & from); \
static void write(const Store & store, Sink & out, const T & t); \
}

template<>
struct WorkerProto<StorePathDescriptor> {
static StorePathDescriptor read(const Store & store, Source & from);
static void write(const Store & store, Sink & out, const StorePathDescriptor & t);
};
MAKE_WORKER_PROTO(std::string);
MAKE_WORKER_PROTO(StorePath);
MAKE_WORKER_PROTO(StorePathDescriptor);

template<typename T>
struct WorkerProto<std::set<T>> {
Expand Down Expand Up @@ -164,4 +157,13 @@ struct WorkerProto<std::optional<T>> {

};

/* Specialization which uses and empty string for the empty case, taking
advantage of the fact these types always serialize to non-empty strings.
This is done primarily for backwards compatability, so that T <=
std::optional<T>, where <= is the compatability partial order, T is one of
the types below.
*/
MAKE_WORKER_PROTO(std::optional<StorePath>);
MAKE_WORKER_PROTO(std::optional<ContentAddress>);

}
2 changes: 1 addition & 1 deletion src/nix/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace nix {

App Installable::toApp(EvalState & state)
{
auto [cursor, attrPath] = getCursor(state, true);
auto [cursor, attrPath] = getCursor(state);

auto type = cursor->getAttr("type")->getString();

Expand Down
11 changes: 2 additions & 9 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
Strings{templateName == "" ? "defaultTemplate" : templateName},
Strings(attrsPathPrefixes), lockFlags);

auto [cursor, attrPath] = installable.getCursor(*evalState, true);
auto [cursor, attrPath] = installable.getCursor(*evalState);

auto templateDir = cursor->getAttr("path")->getString();

Expand Down Expand Up @@ -787,7 +787,6 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
struct CmdFlakeShow : FlakeCommand
{
bool showLegacy = false;
bool useEvalCache = true;

CmdFlakeShow()
{
Expand All @@ -796,12 +795,6 @@ struct CmdFlakeShow : FlakeCommand
.description = "show the contents of the 'legacyPackages' output",
.handler = {&showLegacy, true}
});

addFlag({
.longName = "no-eval-cache",
.description = "do not use the flake evaluation cache",
.handler = {[&]() { useEvalCache = false; }}
});
}

std::string description() override
Expand Down Expand Up @@ -939,7 +932,7 @@ struct CmdFlakeShow : FlakeCommand
}
};

auto cache = openEvalCache(*state, flake, useEvalCache);
auto cache = openEvalCache(*state, flake);

visit(*cache->getRoot(), {}, fmt(ANSI_BOLD "%s" ANSI_NORMAL, flake->flake.lockedRef), "");
}
Expand Down
22 changes: 9 additions & 13 deletions src/nix/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ void completeFlakeRefWithFragment(
auto flakeRef = parseFlakeRef(flakeRefS, absPath("."));

auto evalCache = openEvalCache(*evalState,
std::make_shared<flake::LockedFlake>(lockFlake(*evalState, flakeRef, lockFlags)),
true);
std::make_shared<flake::LockedFlake>(lockFlake(*evalState, flakeRef, lockFlags)));

auto root = evalCache->getRoot();

Expand Down Expand Up @@ -273,7 +272,7 @@ Buildable Installable::toBuildable()
}

std::vector<std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>>
Installable::getCursors(EvalState & state, bool useEvalCache)
Installable::getCursors(EvalState & state)
{
auto evalCache =
std::make_shared<nix::eval_cache::EvalCache>(std::nullopt, state,
Expand All @@ -282,9 +281,9 @@ Installable::getCursors(EvalState & state, bool useEvalCache)
}

std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>
Installable::getCursor(EvalState & state, bool useEvalCache)
Installable::getCursor(EvalState & state)
{
auto cursors = getCursors(state, useEvalCache);
auto cursors = getCursors(state);
if (cursors.empty())
throw Error("cannot find flake attribute '%s'", what());
return cursors[0];
Expand Down Expand Up @@ -420,12 +419,11 @@ Value * InstallableFlake::getFlakeOutputs(EvalState & state, const flake::Locked

ref<eval_cache::EvalCache> openEvalCache(
EvalState & state,
std::shared_ptr<flake::LockedFlake> lockedFlake,
bool useEvalCache)
std::shared_ptr<flake::LockedFlake> lockedFlake)
{
auto fingerprint = lockedFlake->getFingerprint(*state.store);
return make_ref<nix::eval_cache::EvalCache>(
useEvalCache && evalSettings.pureEval
evalSettings.useEvalCache && evalSettings.pureEval
? std::optional { std::cref(fingerprint) }
: std::nullopt,
state,
Expand Down Expand Up @@ -460,10 +458,9 @@ static std::string showAttrPaths(const std::vector<std::string> & paths)

std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableFlake::toDerivation()
{

auto lockedFlake = getLockedFlake();

auto cache = openEvalCache(*state, lockedFlake, true);
auto cache = openEvalCache(*state, lockedFlake);
auto root = cache->getRoot();

for (auto & attrPath : getActualAttrPaths()) {
Expand Down Expand Up @@ -517,11 +514,10 @@ std::pair<Value *, Pos> InstallableFlake::toValue(EvalState & state)
}

std::vector<std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>>
InstallableFlake::getCursors(EvalState & state, bool useEvalCache)
InstallableFlake::getCursors(EvalState & state)
{
auto evalCache = openEvalCache(state,
std::make_shared<flake::LockedFlake>(lockFlake(state, flakeRef, lockFlags)),
useEvalCache);
std::make_shared<flake::LockedFlake>(lockFlake(state, flakeRef, lockFlags)));

auto root = evalCache->getRoot();

Expand Down
9 changes: 4 additions & 5 deletions src/nix/installables.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ struct Installable
}

virtual std::vector<std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>>
getCursors(EvalState & state, bool useEvalCache);
getCursors(EvalState & state);

std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>
getCursor(EvalState & state, bool useEvalCache);
getCursor(EvalState & state);

virtual FlakeRef nixpkgsFlakeRef() const
{
Expand Down Expand Up @@ -118,7 +118,7 @@ struct InstallableFlake : InstallableValue
std::pair<Value *, Pos> toValue(EvalState & state) override;

std::vector<std::pair<std::shared_ptr<eval_cache::AttrCursor>, std::string>>
getCursors(EvalState & state, bool useEvalCache) override;
getCursors(EvalState & state) override;

std::shared_ptr<flake::LockedFlake> getLockedFlake() const;

Expand All @@ -127,7 +127,6 @@ struct InstallableFlake : InstallableValue

ref<eval_cache::EvalCache> openEvalCache(
EvalState & state,
std::shared_ptr<flake::LockedFlake> lockedFlake,
bool useEvalCache);
std::shared_ptr<flake::LockedFlake> lockedFlake);

}
2 changes: 1 addition & 1 deletion src/nix/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct CmdSearch : InstallableCommand, MixJSON
}
};

for (auto & [cursor, prefix] : installable->getCursors(*state, true))
for (auto & [cursor, prefix] : installable->getCursors(*state))
visit(*cursor, parseAttrPath(*state, prefix));

if (!json && !results)
Expand Down

0 comments on commit 5f1aa4b

Please sign in to comment.