2019-06-04 21:01:21 +03:00
|
|
|
#include "lockfile.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
2019-10-21 23:11:21 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2019-06-04 21:01:21 +03:00
|
|
|
namespace nix::flake {
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
LockedInput::LockedInput(const nlohmann::json & json)
|
|
|
|
: LockedInputs(json)
|
2019-10-08 17:30:04 +03:00
|
|
|
, ref(json.value("url", json.value("uri", "")))
|
|
|
|
, originalRef(json.value("originalUrl", json.value("originalUri", "")))
|
2019-06-04 21:01:21 +03:00
|
|
|
, narHash(Hash((std::string) json["narHash"]))
|
|
|
|
{
|
|
|
|
if (!ref.isImmutable())
|
|
|
|
throw Error("lockfile contains mutable flakeref '%s'", ref);
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
nlohmann::json LockedInput::toJson() const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
2019-08-30 17:27:51 +03:00
|
|
|
auto json = LockedInputs::toJson();
|
2019-10-08 17:30:04 +03:00
|
|
|
json["url"] = ref.to_string();
|
|
|
|
json["originalUrl"] = originalRef.to_string();
|
2019-06-04 21:01:21 +03:00
|
|
|
json["narHash"] = narHash.to_string(SRI);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
Path LockedInput::computeStorePath(Store & store) const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
|
|
|
return store.makeFixedOutputPath(true, narHash, "source");
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
LockedInputs::LockedInputs(const nlohmann::json & json)
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
2019-06-04 21:08:13 +03:00
|
|
|
for (auto & i : json["inputs"].items())
|
2019-08-30 17:27:51 +03:00
|
|
|
inputs.insert_or_assign(i.key(), LockedInput(i.value()));
|
2019-06-04 21:01:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
nlohmann::json LockedInputs::toJson() const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
{
|
|
|
|
auto j = nlohmann::json::object();
|
2019-08-30 17:27:51 +03:00
|
|
|
for (auto & i : inputs)
|
2019-06-04 21:01:21 +03:00
|
|
|
j[i.first] = i.second.toJson();
|
|
|
|
json["inputs"] = std::move(j);
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:51 +03:00
|
|
|
bool LockedInputs::isDirty() const
|
2019-07-12 14:29:54 +03:00
|
|
|
{
|
2019-08-30 17:27:51 +03:00
|
|
|
for (auto & i : inputs)
|
2019-07-12 14:29:54 +03:00
|
|
|
if (i.second.ref.isDirty() || i.second.isDirty()) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:01:21 +03:00
|
|
|
nlohmann::json LockFile::toJson() const
|
|
|
|
{
|
2019-08-30 17:27:51 +03:00
|
|
|
auto json = LockedInputs::toJson();
|
2019-09-18 22:17:27 +03:00
|
|
|
json["version"] = 3;
|
2019-06-04 21:01:21 +03:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockFile LockFile::read(const Path & path)
|
|
|
|
{
|
|
|
|
if (pathExists(path)) {
|
|
|
|
auto json = nlohmann::json::parse(readFile(path));
|
|
|
|
|
|
|
|
auto version = json.value("version", 0);
|
2019-09-18 22:17:27 +03:00
|
|
|
if (version != 3)
|
2019-06-04 21:01:21 +03:00
|
|
|
throw Error("lock file '%s' has unsupported version %d", path, version);
|
|
|
|
|
|
|
|
return LockFile(json);
|
|
|
|
} else
|
|
|
|
return LockFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator <<(std::ostream & stream, const LockFile & lockFile)
|
|
|
|
{
|
|
|
|
stream << lockFile.toJson().dump(4); // '4' = indentation in json file
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockFile::write(const Path & path) const
|
|
|
|
{
|
|
|
|
createDirs(dirOf(path));
|
|
|
|
writeFile(path, fmt("%s\n", *this));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|