Merge remote-tracking branch 'tweag/flake-registries' into flakes

This commit is contained in:
Eelco Dolstra 2019-03-27 14:40:36 +01:00
commit c101b29133
4 changed files with 73 additions and 47 deletions

View file

@ -316,10 +316,10 @@ private:
public: public:
const FlakeRegistry & getFlakeRegistry(); const std::vector<std::shared_ptr<FlakeRegistry>> getFlakeRegistries();
private: private:
std::unique_ptr<FlakeRegistry> _flakeRegistry; std::shared_ptr<FlakeRegistry> _flakeRegistry;
std::once_flag _flakeRegistryInit; std::once_flag _flakeRegistryInit;
}; };

View file

@ -12,16 +12,11 @@
namespace nix { namespace nix {
Path getUserRegistryPath()
{
return getHome() + "/.config/nix/registry.json";
}
/* Read the registry or a lock file. (Currently they have an identical /* Read the registry or a lock file. (Currently they have an identical
format. */ format. */
std::unique_ptr<FlakeRegistry> readRegistry(const Path & path) std::shared_ptr<FlakeRegistry> readRegistry(const Path & path)
{ {
auto registry = std::make_unique<FlakeRegistry>(); auto registry = std::make_shared<FlakeRegistry>();
try { try {
auto json = nlohmann::json::parse(readFile(path)); auto json = nlohmann::json::parse(readFile(path));
@ -55,37 +50,71 @@ void writeRegistry(FlakeRegistry registry, Path path)
writeFile(path, json.dump(4)); // The '4' is the number of spaces used in the indentation in the json file. writeFile(path, json.dump(4)); // The '4' is the number of spaces used in the indentation in the json file.
} }
const FlakeRegistry & EvalState::getFlakeRegistry() Path getUserRegistryPath()
{ {
std::call_once(_flakeRegistryInit, [&]() return getHome() + "/.config/nix/registry.json";
{ }
#if 0
auto registryUri = "file:///home/eelco/Dev/gists/nix-flakes/registry.json";
auto registryFile = getDownloader()->download(DownloadRequest(registryUri)); std::shared_ptr<FlakeRegistry> getGlobalRegistry()
#endif {
// TODO: Make a global registry, and return it here.
return std::make_shared<FlakeRegistry>();
}
auto registryFile = settings.nixDataDir + "/nix/flake-registry.json"; std::shared_ptr<FlakeRegistry> getUserRegistry()
{
return readRegistry(getUserRegistryPath());
}
_flakeRegistry = readRegistry(registryFile); // Project-specific registry saved in flake-registry.json.
}); std::shared_ptr<FlakeRegistry> getLocalRegistry()
{
Path registryFile = settings.nixDataDir + "/nix/flake-registry.json";
return readRegistry(registryFile);
}
return *_flakeRegistry; std::shared_ptr<FlakeRegistry> getFlagRegistry()
{
return std::make_shared<FlakeRegistry>();
// TODO: Implement this once the right flags are implemented.
}
// This always returns a vector with globalReg, userReg, localReg, flakeReg.
// If one of them doesn't exist, the registry is left empty but does exist.
const std::vector<std::shared_ptr<FlakeRegistry>> EvalState::getFlakeRegistries()
{
std::vector<std::shared_ptr<FlakeRegistry>> registries;
if (!evalSettings.pureEval) {
registries.push_back(std::make_shared<FlakeRegistry>()); // global
registries.push_back(std::make_shared<FlakeRegistry>()); // user
registries.push_back(std::make_shared<FlakeRegistry>()); // local
} else {
registries.push_back(getGlobalRegistry());
registries.push_back(getUserRegistry());
registries.push_back(getLocalRegistry());
}
registries.push_back(getFlagRegistry());
return registries;
} }
Value * makeFlakeRegistryValue(EvalState & state) Value * makeFlakeRegistryValue(EvalState & state)
{ {
auto v = state.allocValue(); auto v = state.allocValue();
auto registry = state.getFlakeRegistry(); auto registries = state.getFlakeRegistries();
state.mkAttrs(*v, registry.entries.size()); int size = 0;
for (auto registry : registries)
size += registry->entries.size();
state.mkAttrs(*v, size);
for (auto & entry : registry.entries) { for (auto & registry : registries) {
auto vEntry = state.allocAttr(*v, entry.first); for (auto & entry : registry->entries) {
state.mkAttrs(*vEntry, 2); auto vEntry = state.allocAttr(*v, entry.first);
mkString(*state.allocAttr(*vEntry, state.symbols.create("uri")), entry.second.ref.to_string()); state.mkAttrs(*vEntry, 2);
vEntry->attrs->sort(); mkString(*state.allocAttr(*vEntry, state.symbols.create("uri")), entry.second.ref.to_string());
vEntry->attrs->sort();
}
} }
v->attrs->sort(); v->attrs->sort();
@ -94,7 +123,7 @@ Value * makeFlakeRegistryValue(EvalState & state)
} }
static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef, static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef,
std::vector<const FlakeRegistry *> registries) std::vector<std::shared_ptr<FlakeRegistry>> registries)
{ {
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&flakeRef.data)) { if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&flakeRef.data)) {
for (auto registry : registries) { for (auto registry : registries) {
@ -122,13 +151,7 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
FlakeRef directFlakeRef = FlakeRef(flakeRef); FlakeRef directFlakeRef = FlakeRef(flakeRef);
if (!flakeRef.isDirect()) if (!flakeRef.isDirect())
{ {
std::vector<const FlakeRegistry *> registries; directFlakeRef = lookupFlake(state, flakeRef, state.getFlakeRegistries());
// 'pureEval' is a setting which cannot be changed in `nix flake`,
// but without flagging it off, we can't use any FlakeIds.
// if (!evalSettings.pureEval) {
registries.push_back(&state.getFlakeRegistry());
// }
directFlakeRef = lookupFlake(state, flakeRef, registries);
} }
assert(directFlakeRef.isDirect()); assert(directFlakeRef.isDirect());
// NOTE FROM NICK: I don't see why one wouldn't fetch FlakeId flakes.. // NOTE FROM NICK: I don't see why one wouldn't fetch FlakeId flakes..
@ -251,11 +274,8 @@ static std::tuple<FlakeId, std::map<FlakeId, Flake>> resolveFlake(EvalState & st
std::optional<FlakeId> topFlakeId; /// FIXME: ambiguous std::optional<FlakeId> topFlakeId; /// FIXME: ambiguous
todo.push({topRef, true}); todo.push({topRef, true});
std::vector<const FlakeRegistry *> registries; std::vector<std::shared_ptr<FlakeRegistry>> registries = state.getFlakeRegistries();
FlakeRegistry localRegistry; std::shared_ptr<FlakeRegistry> localRegistry = registries.at(2);
registries.push_back(&localRegistry);
if (!evalSettings.pureEval)
registries.push_back(&state.getFlakeRegistry());
while (!todo.empty()) { while (!todo.empty()) {
auto [flakeRef, toplevel] = todo.front(); auto [flakeRef, toplevel] = todo.front();
@ -264,6 +284,7 @@ static std::tuple<FlakeId, std::map<FlakeId, Flake>> resolveFlake(EvalState & st
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&flakeRef.data)) { if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&flakeRef.data)) {
if (done.count(refData->id)) continue; // optimization if (done.count(refData->id)) continue; // optimization
flakeRef = lookupFlake(state, flakeRef, registries); flakeRef = lookupFlake(state, flakeRef, registries);
// This is why we need the `registries`.
} }
if (evalSettings.pureEval && !flakeRef.isImmutable() && (!toplevel || !impureTopRef)) if (evalSettings.pureEval && !flakeRef.isImmutable() && (!toplevel || !impureTopRef))
@ -278,10 +299,13 @@ static std::tuple<FlakeId, std::map<FlakeId, Flake>> resolveFlake(EvalState & st
for (auto & require : flake.requires) for (auto & require : flake.requires)
todo.push({require, false}); todo.push({require, false});
// The following piece of code basically adds the FlakeRefs from
// the lockfiles of dependencies to the localRegistry. This is used
// to resolve future `FlakeId`s, in `lookupFlake` a bit above this.
if (flake.lockFile) if (flake.lockFile)
for (auto & entry : flake.lockFile->entries) { for (auto & entry : flake.lockFile->entries) {
if (localRegistry.entries.count(entry.first)) continue; if (localRegistry->entries.count(entry.first)) continue;
localRegistry.entries.emplace(entry.first, entry.second); localRegistry->entries.emplace(entry.first, entry.second);
} }
done.emplace(flake.id, std::move(flake)); done.emplace(flake.id, std::move(flake));

View file

@ -25,7 +25,7 @@ Value * makeFlakeRegistryValue(EvalState & state);
Value * makeFlakeValue(EvalState & state, std::string flakeUri, Value & v); Value * makeFlakeValue(EvalState & state, std::string flakeUri, Value & v);
std::unique_ptr<FlakeRegistry> readRegistry(const Path &); std::shared_ptr<FlakeRegistry> readRegistry(const Path &);
void writeRegistry(FlakeRegistry, Path); void writeRegistry(FlakeRegistry, Path);
@ -36,7 +36,7 @@ struct Flake
std::string description; std::string description;
Path path; Path path;
std::vector<FlakeRef> requires; std::vector<FlakeRef> requires;
std::unique_ptr<FlakeRegistry> lockFile; std::shared_ptr<FlakeRegistry> lockFile;
Value * vProvides; // FIXME: gc Value * vProvides; // FIXME: gc
// commit hash // commit hash
// date // date

View file

@ -24,12 +24,14 @@ struct CmdFlakeList : StoreCommand, MixEvalArgs
{ {
auto evalState = std::make_shared<EvalState>(searchPath, store); auto evalState = std::make_shared<EvalState>(searchPath, store);
auto registry = evalState->getFlakeRegistry(); auto registries = evalState->getFlakeRegistries();
stopProgressBar(); stopProgressBar();
for (auto & entry : registry.entries) { for (auto & registry : registries) {
std::cout << entry.first << " " << entry.second.ref.to_string() << "\n"; for (auto & entry : registry->entries) {
std::cout << entry.first << " " << entry.second.ref.to_string() << "\n";
}
} }
} }
}; };