2019-06-04 18:01:21 +00:00
|
|
|
#include "lockfile.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
2019-10-21 20:11:21 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
namespace nix::flake {
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
LockedInput::LockedInput(const nlohmann::json & json)
|
|
|
|
: LockedInputs(json)
|
2019-10-08 14:30:04 +00:00
|
|
|
, ref(json.value("url", json.value("uri", "")))
|
|
|
|
, originalRef(json.value("originalUrl", json.value("originalUri", "")))
|
2019-06-04 18:01:21 +00:00
|
|
|
, narHash(Hash((std::string) json["narHash"]))
|
|
|
|
{
|
|
|
|
if (!ref.isImmutable())
|
|
|
|
throw Error("lockfile contains mutable flakeref '%s'", ref);
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
nlohmann::json LockedInput::toJson() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2019-08-30 14:27:51 +00:00
|
|
|
auto json = LockedInputs::toJson();
|
2019-10-08 14:30:04 +00:00
|
|
|
json["url"] = ref.to_string();
|
|
|
|
json["originalUrl"] = originalRef.to_string();
|
2019-06-04 18:01:21 +00:00
|
|
|
json["narHash"] = narHash.to_string(SRI);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
Path LockedInput::computeStorePath(Store & store) const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2019-12-11 13:53:30 +00:00
|
|
|
return store.printStorePath(store.makeFixedOutputPath(true, narHash, "source"));
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
LockedInputs::LockedInputs(const nlohmann::json & json)
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2019-06-04 18:08:13 +00:00
|
|
|
for (auto & i : json["inputs"].items())
|
2019-08-30 14:27:51 +00:00
|
|
|
inputs.insert_or_assign(i.key(), LockedInput(i.value()));
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
nlohmann::json LockedInputs::toJson() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
{
|
|
|
|
auto j = nlohmann::json::object();
|
2019-08-30 14:27:51 +00:00
|
|
|
for (auto & i : inputs)
|
2019-06-04 18:01:21 +00:00
|
|
|
j[i.first] = i.second.toJson();
|
|
|
|
json["inputs"] = std::move(j);
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:27:51 +00:00
|
|
|
bool LockedInputs::isDirty() const
|
2019-07-12 11:29:54 +00:00
|
|
|
{
|
2019-08-30 14:27:51 +00:00
|
|
|
for (auto & i : inputs)
|
2019-07-12 11:29:54 +00:00
|
|
|
if (i.second.ref.isDirty() || i.second.isDirty()) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
nlohmann::json LockFile::toJson() const
|
|
|
|
{
|
2019-08-30 14:27:51 +00:00
|
|
|
auto json = LockedInputs::toJson();
|
2019-09-18 19:17:27 +00:00
|
|
|
json["version"] = 3;
|
2019-06-04 18:01:21 +00: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 19:17:27 +00:00
|
|
|
if (version != 3)
|
2019-06-04 18:01:21 +00: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|