nix-super/src/libexpr/flake/lockfile.cc

125 lines
3 KiB
C++
Raw Normal View History

#include "lockfile.hh"
#include "store-api.hh"
#include "fetchers/regex.hh"
#include <nlohmann/json.hpp>
namespace nix::flake {
LockedInput::LockedInput(const nlohmann::json & json)
: LockedInputs(json)
, ref(parseFlakeRef(json.value("url", json.value("uri", ""))))
, originalRef(parseFlakeRef(json.value("originalUrl", json.value("originalUri", ""))))
, narHash(Hash((std::string) json["narHash"]))
{
if (!ref.isImmutable())
throw Error("lockfile contains mutable flakeref '%s'", ref);
}
nlohmann::json LockedInput::toJson() const
{
auto json = LockedInputs::toJson();
2019-10-08 17:30:04 +03:00
json["url"] = ref.to_string();
json["originalUrl"] = originalRef.to_string();
json["narHash"] = narHash.to_string(SRI);
return json;
}
Path LockedInput::computeStorePath(Store & store) const
{
return store.printStorePath(store.makeFixedOutputPath(true, narHash, "source"));
}
LockedInputs::LockedInputs(const nlohmann::json & json)
{
for (auto & i : json["inputs"].items())
inputs.insert_or_assign(i.key(), LockedInput(i.value()));
}
nlohmann::json LockedInputs::toJson() const
{
nlohmann::json json;
{
auto j = nlohmann::json::object();
for (auto & i : inputs)
j[i.first] = i.second.toJson();
json["inputs"] = std::move(j);
}
return json;
}
bool LockedInputs::isImmutable() const
{
for (auto & i : inputs)
if (!i.second.ref.isImmutable() || !i.second.isImmutable()) return false;
return true;
}
Respect lock files of inputs + fine-grained lock file control When computing a lock file, we now respect the lock files of flake inputs. This is important for usability / reproducibility. For example, the 'nixops' flake depends on the 'nixops-aws' and 'nixops-hetzner' repositories. So when the 'nixops' flake is used in another flake, we want the versions of 'nixops-aws' and 'nixops-hetzner' locked by the the 'nixops' flake because those presumably have been tested. This can lead to a proliferation of versions of flakes like 'nixpkgs' (since every flake's lock file could depend on a different version of 'nixpkgs'). This is not a major issue when using Nixpkgs overlays or NixOS modules, since then the top-level flake composes those overlays/modules into *its* version of Nixpkgs and all other versions are ignored. Lock file computation has been made a bit more lazy so it won't try to fetch all those versions of 'nixpkgs'. However, in case it's necessary to minimize flake versions, there now are two input attributes that allow this. First, you can copy an input from another flake, as follows: inputs.nixpkgs.follows = "dwarffs/nixpkgs"; This states that the calling flake's 'nixpkgs' input shall be the same as the 'nixpkgs' input of the 'dwarffs' input. Second, you can override inputs of inputs: inputs.nixpkgs.url = github:edolstra/nixpkgs/<hash>; inputs.nixops.inputs.nixpkgs.url = github:edolstra/nixpkgs/<hash>; or equivalently, using 'follows': inputs.nixpkgs.url = github:edolstra/nixpkgs/<hash>; inputs.nixops.inputs.nixpkgs.follows = "nixpkgs"; This states that the 'nixpkgs' input of the 'nixops' input shall be the same as the calling flake's 'nixpkgs' input. Finally, at '-v' Nix now prints the changes to the lock file, e.g. $ nix flake update ~/Misc/eelco-configurations/hagbard inputs of flake 'git+file:///home/eelco/Misc/eelco-configurations?subdir=hagbard' changed: updated 'nixpkgs': 'github:edolstra/nixpkgs/7845bf5f4b3013df1cf036e9c9c3a55a30331db9' -> 'github:edolstra/nixpkgs/03f3def66a104a221aac8b751eeb7075374848fd' removed 'nixops' removed 'nixops/nixops-aws' removed 'nixops/nixops-hetzner' removed 'nixops/nixpkgs'
2020-01-24 23:05:11 +02:00
std::optional<LockedInput *> LockedInputs::findInput(const InputPath & path)
{
assert(!path.empty());
LockedInputs * pos = this;
for (auto & elem : path) {
auto i = pos->inputs.find(elem);
if (i == pos->inputs.end())
return {};
pos = &i->second;
}
return (LockedInput *) pos;
}
nlohmann::json LockFile::toJson() const
{
auto json = LockedInputs::toJson();
json["version"] = 3;
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 != 3)
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));
}
InputPath parseInputPath(std::string_view s)
{
InputPath path;
for (auto & elem : tokenizeString<std::vector<std::string>>(s, "/")) {
if (!std::regex_match(elem, fetchers::flakeIdRegex))
throw Error("invalid flake input path element '%s'", elem);
path.push_back(elem);
}
if (path.empty())
throw Error("flake input path is empty");
return path;
}
}