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 {
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
FlakeRef flakeRefFromJson(const nlohmann::json & json)
|
|
|
|
{
|
2020-02-06 13:27:31 +00:00
|
|
|
return FlakeRef::fromAttrs(jsonToAttrs(json));
|
2020-01-31 18:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FlakeRef getFlakeRef(
|
|
|
|
const nlohmann::json & json,
|
2020-05-06 15:48:18 +00:00
|
|
|
const char * attr)
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
2020-05-06 15:48:18 +00:00
|
|
|
auto i = json.find(attr);
|
2020-01-31 18:16:40 +00:00
|
|
|
if (i != json.end())
|
|
|
|
return flakeRefFromJson(*i);
|
|
|
|
|
2020-05-06 15:48:18 +00:00
|
|
|
throw Error("attribute '%s' missing in lock file", attr);
|
2020-01-31 18:16:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
LockedNode::LockedNode(const nlohmann::json & json)
|
2020-05-06 15:48:18 +00:00
|
|
|
: lockedRef(getFlakeRef(json, "locked"))
|
|
|
|
, originalRef(getFlakeRef(json, "original"))
|
2020-04-02 16:26:39 +00:00
|
|
|
, info(TreeInfo::fromJson(json))
|
2020-03-09 14:27:49 +00:00
|
|
|
, isFlake(json.find("flake") != json.end() ? (bool) json["flake"] : true)
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2020-02-02 10:31:58 +00:00
|
|
|
if (!lockedRef.input->isImmutable())
|
2020-02-01 23:05:53 +00:00
|
|
|
throw Error("lockfile contains mutable flakeref '%s'", lockedRef);
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
StorePath LockedNode::computeStorePath(Store & store) const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2020-02-02 11:29:53 +00:00
|
|
|
return info.computeStorePath(store);
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
std::shared_ptr<Node> Node::findInput(const InputPath & path)
|
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 21:05:11 +00:00
|
|
|
{
|
2020-03-12 21:06:57 +00:00
|
|
|
auto pos = shared_from_this();
|
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 21:05:11 +00:00
|
|
|
|
|
|
|
for (auto & elem : path) {
|
|
|
|
auto i = pos->inputs.find(elem);
|
|
|
|
if (i == pos->inputs.end())
|
|
|
|
return {};
|
2020-03-12 21:06:57 +00:00
|
|
|
pos = i->second;
|
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 21:05:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
return pos;
|
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 21:05:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
LockFile::LockFile(const nlohmann::json & json, const Path & path)
|
2020-01-29 22:12:58 +00:00
|
|
|
{
|
2020-03-12 21:06:57 +00:00
|
|
|
auto version = json.value("version", 0);
|
2020-05-06 15:48:18 +00:00
|
|
|
if (version != 5)
|
2020-03-12 21:06:57 +00:00
|
|
|
throw Error("lock file '%s' has unsupported version %d", path, version);
|
|
|
|
|
2020-05-06 15:48:18 +00:00
|
|
|
std::unordered_map<std::string, std::shared_ptr<Node>> nodeMap;
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2020-05-06 15:48:18 +00:00
|
|
|
std::function<void(Node & node, const nlohmann::json & jsonNode)> getInputs;
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2020-05-06 15:48:18 +00:00
|
|
|
getInputs = [&](Node & node, const nlohmann::json & jsonNode)
|
|
|
|
{
|
|
|
|
if (jsonNode.find("inputs") == jsonNode.end()) return;
|
|
|
|
for (auto & i : jsonNode["inputs"].items()) {
|
|
|
|
std::string inputKey = i.value();
|
|
|
|
auto k = nodeMap.find(inputKey);
|
|
|
|
if (k == nodeMap.end()) {
|
|
|
|
auto jsonNode2 = json["nodes"][inputKey];
|
|
|
|
auto input = std::make_shared<LockedNode>(jsonNode2);
|
|
|
|
k = nodeMap.insert_or_assign(inputKey, input).first;
|
|
|
|
getInputs(*input, jsonNode2);
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
2020-05-06 15:48:18 +00:00
|
|
|
node.inputs.insert_or_assign(i.key(), k->second);
|
|
|
|
}
|
|
|
|
};
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2020-05-06 15:48:18 +00:00
|
|
|
std::string rootKey = json["root"];
|
|
|
|
nodeMap.insert_or_assign(rootKey, root);
|
|
|
|
getInputs(*root, json["nodes"][rootKey]);
|
2020-01-29 22:12:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
nlohmann::json LockFile::toJson() const
|
|
|
|
{
|
2020-03-12 21:06:57 +00:00
|
|
|
nlohmann::json nodes;
|
|
|
|
std::unordered_map<std::shared_ptr<const Node>, std::string> nodeKeys;
|
|
|
|
std::unordered_set<std::string> keys;
|
|
|
|
|
|
|
|
std::function<std::string(const std::string & key, std::shared_ptr<const Node> node)> dumpNode;
|
|
|
|
|
|
|
|
dumpNode = [&](std::string key, std::shared_ptr<const Node> node) -> std::string
|
|
|
|
{
|
|
|
|
auto k = nodeKeys.find(node);
|
|
|
|
if (k != nodeKeys.end())
|
|
|
|
return k->second;
|
|
|
|
|
|
|
|
if (!keys.insert(key).second) {
|
|
|
|
for (int n = 2; ; ++n) {
|
|
|
|
auto k = fmt("%s_%d", key, n);
|
|
|
|
if (keys.insert(k).second) {
|
|
|
|
key = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKeys.insert_or_assign(node, key);
|
|
|
|
|
|
|
|
auto n = nlohmann::json::object();
|
|
|
|
|
|
|
|
if (!node->inputs.empty()) {
|
|
|
|
auto inputs = nlohmann::json::object();
|
|
|
|
for (auto & i : node->inputs)
|
|
|
|
inputs[i.first] = dumpNode(i.first, i.second);
|
|
|
|
n["inputs"] = std::move(inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto lockedNode = std::dynamic_pointer_cast<const LockedNode>(node)) {
|
|
|
|
n["original"] = fetchers::attrsToJson(lockedNode->originalRef.toAttrs());
|
|
|
|
n["locked"] = fetchers::attrsToJson(lockedNode->lockedRef.toAttrs());
|
2020-04-02 09:51:34 +00:00
|
|
|
n["info"] = lockedNode->info.toJson();
|
2020-03-12 21:06:57 +00:00
|
|
|
if (!lockedNode->isFlake) n["flake"] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[key] = std::move(n);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
json["version"] = 5;
|
|
|
|
json["root"] = dumpNode("root", root);
|
|
|
|
json["nodes"] = std::move(nodes);
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
std::string LockFile::to_string() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
2020-03-12 21:06:57 +00:00
|
|
|
return toJson().dump(2);
|
|
|
|
}
|
2019-06-04 18:01:21 +00:00
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
LockFile LockFile::read(const Path & path)
|
|
|
|
{
|
|
|
|
if (!pathExists(path)) return LockFile();
|
|
|
|
return LockFile(nlohmann::json::parse(readFile(path)), path);
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator <<(std::ostream & stream, const LockFile & lockFile)
|
|
|
|
{
|
2020-01-31 18:16:40 +00:00
|
|
|
stream << lockFile.toJson().dump(2);
|
2019-06-04 18:01:21 +00:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockFile::write(const Path & path) const
|
|
|
|
{
|
|
|
|
createDirs(dirOf(path));
|
|
|
|
writeFile(path, fmt("%s\n", *this));
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
bool LockFile::isImmutable() const
|
|
|
|
{
|
|
|
|
std::unordered_set<std::shared_ptr<const Node>> nodes;
|
|
|
|
|
|
|
|
std::function<void(std::shared_ptr<const Node> node)> visit;
|
|
|
|
|
|
|
|
visit = [&](std::shared_ptr<const Node> node)
|
|
|
|
{
|
|
|
|
if (!nodes.insert(node).second) return;
|
|
|
|
for (auto & i : node->inputs) visit(i.second);
|
|
|
|
};
|
|
|
|
|
|
|
|
visit(root);
|
|
|
|
|
|
|
|
for (auto & i : nodes) {
|
|
|
|
if (i == root) continue;
|
|
|
|
auto lockedNode = std::dynamic_pointer_cast<const LockedNode>(i);
|
|
|
|
if (lockedNode && !lockedNode->lockedRef.input->isImmutable()) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LockFile::operator ==(const LockFile & other) const
|
|
|
|
{
|
|
|
|
// FIXME: slow
|
|
|
|
return toJson() == other.toJson();
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:57:57 +00:00
|
|
|
InputPath parseInputPath(std::string_view s)
|
|
|
|
{
|
|
|
|
InputPath path;
|
|
|
|
|
|
|
|
for (auto & elem : tokenizeString<std::vector<std::string>>(s, "/")) {
|
2020-03-30 12:03:28 +00:00
|
|
|
if (!std::regex_match(elem, flakeIdRegex))
|
2020-01-29 13:57:57 +00:00
|
|
|
throw Error("invalid flake input path element '%s'", elem);
|
|
|
|
path.push_back(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
static void flattenLockFile(
|
|
|
|
std::shared_ptr<const Node> node,
|
|
|
|
const InputPath & prefix,
|
2020-03-27 15:15:50 +00:00
|
|
|
std::unordered_set<std::shared_ptr<const Node>> & done,
|
2020-03-12 21:06:57 +00:00
|
|
|
std::map<InputPath, std::shared_ptr<const LockedNode>> & res)
|
|
|
|
{
|
2020-03-27 15:15:50 +00:00
|
|
|
if (!done.insert(node).second) return;
|
|
|
|
|
2020-03-12 21:06:57 +00:00
|
|
|
for (auto &[id, input] : node->inputs) {
|
|
|
|
auto inputPath(prefix);
|
|
|
|
inputPath.push_back(id);
|
|
|
|
if (auto lockedInput = std::dynamic_pointer_cast<const LockedNode>(input))
|
|
|
|
res.emplace(inputPath, lockedInput);
|
2020-03-27 15:15:50 +00:00
|
|
|
flattenLockFile(input, inputPath, done, res);
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string diffLockFiles(const LockFile & oldLocks, const LockFile & newLocks)
|
|
|
|
{
|
2020-03-27 15:15:50 +00:00
|
|
|
std::unordered_set<std::shared_ptr<const Node>> done;
|
2020-03-12 21:06:57 +00:00
|
|
|
std::map<InputPath, std::shared_ptr<const LockedNode>> oldFlat, newFlat;
|
2020-03-27 15:15:50 +00:00
|
|
|
flattenLockFile(oldLocks.root, {}, done, oldFlat);
|
|
|
|
done.clear();
|
|
|
|
flattenLockFile(newLocks.root, {}, done, newFlat);
|
2020-03-12 21:06:57 +00:00
|
|
|
|
|
|
|
auto i = oldFlat.begin();
|
|
|
|
auto j = newFlat.begin();
|
|
|
|
std::string res;
|
|
|
|
|
|
|
|
while (i != oldFlat.end() || j != newFlat.end()) {
|
|
|
|
if (j != newFlat.end() && (i == oldFlat.end() || i->first > j->first)) {
|
|
|
|
res += fmt("* Added '%s': '%s'\n", concatStringsSep("/", j->first), j->second->lockedRef);
|
|
|
|
++j;
|
|
|
|
} else if (i != oldFlat.end() && (j == newFlat.end() || i->first < j->first)) {
|
|
|
|
res += fmt("* Removed '%s'\n", concatStringsSep("/", i->first));
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
if (!(i->second->lockedRef == j->second->lockedRef)) {
|
|
|
|
assert(i->second->lockedRef.to_string() != j->second->lockedRef.to_string());
|
|
|
|
res += fmt("* Updated '%s': '%s' -> '%s'\n",
|
|
|
|
concatStringsSep("/", i->first),
|
|
|
|
i->second->lockedRef,
|
|
|
|
j->second->lockedRef);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|