2019-06-04 18:01:21 +00:00
|
|
|
#include "lockfile.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
namespace nix::flake {
|
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
AbstractInput::AbstractInput(const nlohmann::json & json)
|
2019-06-04 18:01:21 +00:00
|
|
|
: ref(json["uri"])
|
|
|
|
, narHash(Hash((std::string) json["narHash"]))
|
|
|
|
{
|
|
|
|
if (!ref.isImmutable())
|
|
|
|
throw Error("lockfile contains mutable flakeref '%s'", ref);
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
nlohmann::json AbstractInput::toJson() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
json["uri"] = ref.to_string();
|
|
|
|
json["narHash"] = narHash.to_string(SRI);
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
Path AbstractInput::computeStorePath(Store & store) const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
|
|
|
return store.makeFixedOutputPath(true, narHash, "source");
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
FlakeInput::FlakeInput(const nlohmann::json & json)
|
2019-06-04 18:01:21 +00:00
|
|
|
: FlakeInputs(json)
|
2019-06-04 18:08:13 +00:00
|
|
|
, AbstractInput(json)
|
2019-06-04 18:01:21 +00:00
|
|
|
, id(json["id"])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
nlohmann::json FlakeInput::toJson() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
|
|
|
auto json = FlakeInputs::toJson();
|
2019-06-04 18:08:13 +00:00
|
|
|
json.update(AbstractInput::toJson());
|
2019-06-04 18:01:21 +00:00
|
|
|
json["id"] = id;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlakeInputs::FlakeInputs(const nlohmann::json & json)
|
|
|
|
{
|
2019-06-04 18:08:13 +00:00
|
|
|
for (auto & i : json["nonFlakeInputs"].items())
|
|
|
|
nonFlakeInputs.insert_or_assign(i.key(), NonFlakeInput(i.value()));
|
2019-06-04 18:01:21 +00:00
|
|
|
|
2019-06-04 18:08:13 +00:00
|
|
|
for (auto & i : json["inputs"].items())
|
|
|
|
flakeInputs.insert_or_assign(i.key(), FlakeInput(i.value()));
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json FlakeInputs::toJson() const
|
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
{
|
|
|
|
auto j = nlohmann::json::object();
|
2019-06-04 18:08:13 +00:00
|
|
|
for (auto & i : nonFlakeInputs)
|
2019-06-04 18:01:21 +00:00
|
|
|
j[i.first] = i.second.toJson();
|
|
|
|
json["nonFlakeInputs"] = std::move(j);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto j = nlohmann::json::object();
|
2019-06-04 18:08:13 +00:00
|
|
|
for (auto & i : flakeInputs)
|
2019-06-04 18:01:21 +00:00
|
|
|
j[i.first.to_string()] = i.second.toJson();
|
|
|
|
json["inputs"] = std::move(j);
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json LockFile::toJson() const
|
|
|
|
{
|
|
|
|
auto json = FlakeInputs::toJson();
|
|
|
|
json["version"] = 2;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockFile LockFile::read(const Path & path)
|
|
|
|
{
|
|
|
|
if (pathExists(path)) {
|
|
|
|
auto json = nlohmann::json::parse(readFile(path));
|
|
|
|
|
|
|
|
auto version = json.value("version", 0);
|
|
|
|
if (version != 2)
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|