1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-28 17:51:15 +02:00

FlakeAlias is implemented

This commit is contained in:
Nick Van den Broeck 2019-03-21 09:30:16 +01:00
parent f39670c631
commit c64f98b883
6 changed files with 129 additions and 46 deletions

View file

@ -127,6 +127,86 @@ void writeLockFile(LockFile lockFile, Path path)
writeFile(path, json.dump(4)); // '4' = indentation in json file
}
Path getUserRegistryPath()
>>>>>>> Fixed dependency resolution
{
FlakeRef flakeRef(json["uri"]);
if (!flakeRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef.to_string());
LockFile::FlakeEntry entry(flakeRef);
auto nonFlakeRequires = json["nonFlakeRequires"];
for (auto i = nonFlakeRequires.begin(); i != nonFlakeRequires.end(); ++i) {
FlakeRef flakeRef(i->value("uri", ""));
if (!flakeRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef.to_string());
entry.nonFlakeEntries.insert_or_assign(i.key(), flakeRef);
}
auto requires = json["requires"];
for (auto i = requires.begin(); i != requires.end(); ++i)
entry.flakeEntries.insert_or_assign(i.key(), readFlakeEntry(*i));
return entry;
}
LockFile readLockFile(const Path & path)
{
LockFile lockFile;
if (!pathExists(path))
return lockFile;
auto json = nlohmann::json::parse(readFile(path));
auto version = json.value("version", 0);
if (version != 1)
throw Error("lock file '%s' has unsupported version %d", path, version);
auto nonFlakeRequires = json["nonFlakeRequires"];
for (auto i = nonFlakeRequires.begin(); i != nonFlakeRequires.end(); ++i) {
FlakeRef flakeRef(i->value("uri", ""));
if (!flakeRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef.to_string());
lockFile.nonFlakeEntries.insert_or_assign(i.key(), flakeRef);
}
auto requires = json["requires"];
for (auto i = requires.begin(); i != requires.end(); ++i)
lockFile.flakeEntries.insert_or_assign(i.key(), readFlakeEntry(*i));
return lockFile;
}
nlohmann::json flakeEntryToJson(LockFile::FlakeEntry & entry)
{
nlohmann::json json;
json["uri"] = entry.ref.to_string();
for (auto & x : entry.nonFlakeEntries)
json["nonFlakeRequires"][x.first]["uri"] = x.second.to_string();
for (auto & x : entry.flakeEntries)
json["requires"][x.first] = flakeEntryToJson(x.second);
return json;
}
void writeLockFile(LockFile lockFile, Path path)
{
nlohmann::json json;
json["version"] = 1;
json["nonFlakeRequires"];
for (auto & x : lockFile.nonFlakeEntries)
json["nonFlakeRequires"][x.first]["uri"] = x.second.to_string();
for (auto & x : lockFile.flakeEntries)
json["requires"][x.first] = flakeEntryToJson(x.second);
createDirs(dirOf(path));
writeFile(path, json.dump(4)); // '4' = indentation in json file
}
Path getUserRegistryPath()
{
return getHome() + "/.config/nix/registry.json";
@ -194,9 +274,9 @@ Value * makeFlakeRegistryValue(EvalState & state)
static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef,
std::vector<std::shared_ptr<FlakeRegistry>> registries)
{
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&flakeRef.data)) {
if (auto refData = std::get_if<FlakeRef::IsAlias>(&flakeRef.data)) {
for (auto registry : registries) {
auto i = registry->entries.find(refData->id);
auto i = registry->entries.find(refData->alias);
if (i != registry->entries.end()) {
auto newRef = FlakeRef(i->second.ref);
if (!newRef.isDirect())
@ -206,7 +286,7 @@ static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef,
return newRef;
}
}
throw Error("cannot find flake '%s' in the flake registry or in the flake lock file", refData->id);
throw Error("cannot find flake with alias '%s' in the flake registry or in the flake lock file", refData->alias);
} else
return flakeRef;
}
@ -342,7 +422,7 @@ Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowe
}
// Get the `NonFlake` corresponding to a `FlakeRef`.
NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeId flakeId)
NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeAlias alias)
{
FlakeSourceInfo sourceInfo = fetchFlake(state, flakeRef);
debug("got non-flake source '%s' with revision %s",
@ -363,7 +443,7 @@ NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeId flake
nonFlake.path = flakePath;
nonFlake.id = flakeId;
nonFlake.alias = alias;
return nonFlake;
}
@ -394,14 +474,14 @@ LockFile::FlakeEntry dependenciesToFlakeEntry(Dependencies & deps)
entry.flakeEntries.insert_or_assign(deps.flake.id, dependenciesToFlakeEntry(deps));
for (NonFlake & nonFlake : deps.nonFlakeDeps)
entry.nonFlakeEntries.insert_or_assign(nonFlake.id, nonFlake.ref);
entry.nonFlakeEntries.insert_or_assign(nonFlake.alias, nonFlake.ref);
return entry;
}
LockFile getLockFile(EvalState & evalState, FlakeRef & flakeRef, bool impureTopRef)
LockFile getLockFile(EvalState & evalState, FlakeRef & flakeRef)
{
Dependencies deps = resolveFlake(evalState, flakeRef, impureTopRef);
Dependencies deps = resolveFlake(evalState, flakeRef, true);
LockFile::FlakeEntry entry = dependenciesToFlakeEntry(deps);
LockFile lockFile;
lockFile.flakeEntries = entry.flakeEntries;
@ -409,17 +489,17 @@ LockFile getLockFile(EvalState & evalState, FlakeRef & flakeRef, bool impureTopR
return lockFile;
}
void updateLockFile(EvalState & state, Path path, bool impureTopRef)
void updateLockFile(EvalState & state, Path path)
{
// 'path' is the path to the local flake repo.
FlakeRef flakeRef = FlakeRef("file://" + path);
if (std::get_if<FlakeRef::IsGit>(&flakeRef.data)) {
LockFile lockFile = getLockFile(state, flakeRef, impureTopRef);
LockFile lockFile = getLockFile(state, flakeRef);
writeLockFile(lockFile, path + "/flake.lock");
} else if (std::get_if<FlakeRef::IsGitHub>(&flakeRef.data)) {
throw UsageError("you can only update local flakes, not flakes on GitHub");
} else {
throw UsageError("you can only update local flakes, not flakes through their FlakeId");
throw UsageError("you can only update local flakes, not flakes through their FlakeAlias");
}
}
@ -456,7 +536,7 @@ Value * makeFlakeValue(EvalState & state, FlakeUri flakeUri, Value & v)
mkInt(*state.allocAttr(*vFlake, state.symbols.create("revCount")), *flake.revCount);
auto vProvides = state.allocAttr(*vFlake, state.symbols.create("provides"));
mkApp(*vProvides, *flake.vProvides, *vResult); // Should this be vResult or vFlake??? Or both!
mkApp(*vProvides, *flake.vProvides, *vResult);
vFlake->attrs->sort();
}

View file

@ -16,7 +16,7 @@ struct FlakeRegistry
Entry(const FlakeRef & flakeRef) : ref(flakeRef) {};
Entry operator=(const Entry & entry) { return Entry(entry.ref); }
};
std::map<FlakeId, Entry> entries;
std::map<FlakeAlias, Entry> entries;
};
struct LockFile
@ -52,7 +52,7 @@ struct Flake
std::optional<uint64_t> revCount;
std::vector<FlakeRef> requires;
LockFile lockFile;
std::map<FlakeId, FlakeRef> nonFlakeRequires;
std::map<FlakeAlias, FlakeRef> nonFlakeRequires;
Value * vProvides; // FIXME: gc
// date
// content hash
@ -61,7 +61,7 @@ struct Flake
struct NonFlake
{
FlakeId id;
FlakeAlias alias;
FlakeRef ref;
Path path;
// date
@ -81,5 +81,7 @@ struct Dependencies
Dependencies resolveFlake(EvalState &, const FlakeRef &, bool impureTopRef, bool isTopFlake);
void updateLockFile(EvalState &, Path path, bool impureTopRef);
FlakeRegistry updateLockFile(EvalState &, Flake &);
void updateLockFile(EvalState &, Path path);
}

View file

@ -19,7 +19,7 @@ const static std::string revOrRefRegex = "(?:(" + revRegexS + ")|(" + refRegex +
// "master/e72daba8250068216d79d2aeef40d4d95aff6666").
const static std::string refAndOrRevRegex = "(?:(" + revRegexS + ")|(?:(" + refRegex + ")(?:/(" + revRegexS + "))?))";
const static std::string flakeId = "[a-zA-Z][a-zA-Z0-9_-]*";
const static std::string flakeAlias = "[a-zA-Z][a-zA-Z0-9_-]*";
// GitHub references.
const static std::string ownerRegex = "[a-zA-Z][a-zA-Z0-9_-]*";
@ -37,7 +37,7 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
// FIXME: could combine this into one regex.
static std::regex flakeRegex(
"(?:flake:)?(" + flakeId + ")(?:/(?:" + refAndOrRevRegex + "))?",
"(?:flake:)?(" + flakeAlias + ")(?:/(?:" + refAndOrRevRegex + "))?",
std::regex::ECMAScript);
static std::regex githubRegex(
@ -55,8 +55,8 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
std::cmatch match;
if (std::regex_match(uri.c_str(), match, flakeRegex)) {
IsFlakeId d;
d.id = match[1];
IsAlias d;
d.alias = match[1];
if (match[2].matched)
rev = Hash(match[2], htSHA1);
else if (match[3].matched) {
@ -119,8 +119,8 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
std::string FlakeRef::to_string() const
{
std::string string;
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&data))
string = "flake:" + refData->id;
if (auto refData = std::get_if<FlakeRef::IsAlias>(&data))
string = "flake:" + refData->alias;
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
assert(!ref || !rev);
@ -132,9 +132,8 @@ std::string FlakeRef::to_string() const
string = refData->uri;
}
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) {
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data))
return refData->path;
}
else abort();

View file

@ -98,15 +98,17 @@ namespace nix {
*/
typedef std::string FlakeId;
typedef std::string FlakeAlias;
typedef std::string FlakeUri;
struct FlakeRef
{
std::optional<std::string> ref;
std::optional<Hash> rev;
struct IsFlakeId
struct IsAlias
{
FlakeId id;
FlakeAlias alias;
};
struct IsGitHub
@ -150,7 +152,7 @@ struct FlakeRef
a flake ID, which requires a lookup in the flake registry. */
bool isDirect() const
{
return !std::get_if<FlakeRef::IsFlakeId>(&data);
return !std::get_if<FlakeRef::IsAlias>(&data);
}
/* Check whether this is an "immutable" flake reference, that is,