2019-06-04 21:01:21 +03:00
|
|
|
#include "lockfile.hh"
|
|
|
|
#include "store-api.hh"
|
2020-09-21 19:22:45 +03:00
|
|
|
#include "url-parts.hh"
|
2019-06-04 21:01:21 +03:00
|
|
|
|
2021-08-11 20:35:28 +03:00
|
|
|
#include <iomanip>
|
|
|
|
|
2019-10-21 23:11:21 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2019-06-04 21:01:21 +03:00
|
|
|
namespace nix::flake {
|
|
|
|
|
2020-01-31 20:16:40 +02:00
|
|
|
FlakeRef getFlakeRef(
|
|
|
|
const nlohmann::json & json,
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
const char * attr,
|
|
|
|
const char * info)
|
2020-01-31 20:16:40 +02:00
|
|
|
{
|
2020-05-06 18:48:18 +03:00
|
|
|
auto i = json.find(attr);
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
if (i != json.end()) {
|
2020-10-06 12:16:32 +03:00
|
|
|
auto attrs = fetchers::jsonToAttrs(*i);
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
// FIXME: remove when we drop support for version 5.
|
|
|
|
if (info) {
|
|
|
|
auto j = json.find(info);
|
|
|
|
if (j != json.end()) {
|
2020-10-06 12:16:32 +03:00
|
|
|
for (auto k : fetchers::jsonToAttrs(*j))
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
attrs.insert_or_assign(k.first, k.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FlakeRef::fromAttrs(attrs);
|
|
|
|
}
|
2020-01-31 20:16:40 +02:00
|
|
|
|
2020-05-06 18:48:18 +03:00
|
|
|
throw Error("attribute '%s' missing in lock file", attr);
|
2020-01-31 20:16:40 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02:00
|
|
|
LockedNode::LockedNode(const nlohmann::json & json)
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
: lockedRef(getFlakeRef(json, "locked", "info"))
|
|
|
|
, originalRef(getFlakeRef(json, "original", nullptr))
|
2020-03-09 16:27:49 +02:00
|
|
|
, isFlake(json.find("flake") != json.end() ? (bool) json["flake"] : true)
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
2022-02-24 19:09:00 +02:00
|
|
|
if (!lockedRef.input.isLocked())
|
2020-10-26 17:58:58 +02:00
|
|
|
throw Error("lockfile contains mutable lock '%s'",
|
2020-10-28 16:41:18 +02:00
|
|
|
fetchers::attrsToJSON(lockedRef.input.toAttrs()));
|
2019-06-04 21:01:21 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02:00
|
|
|
StorePath LockedNode::computeStorePath(Store & store) const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
return lockedRef.input.computeStorePath(store);
|
2019-06-04 21:01:21 +03:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:21 +03:00
|
|
|
std::shared_ptr<Node> LockFile::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 23:05:11 +02:00
|
|
|
{
|
2020-06-11 15:40:21 +03:00
|
|
|
auto pos = root;
|
|
|
|
|
|
|
|
if (!pos) return {};
|
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
|
|
|
|
|
|
|
for (auto & elem : path) {
|
2020-06-11 15:40:21 +03:00
|
|
|
if (auto i = get(pos->inputs, elem)) {
|
|
|
|
if (auto node = std::get_if<0>(&*i))
|
|
|
|
pos = *node;
|
|
|
|
else if (auto follows = std::get_if<1>(&*i)) {
|
|
|
|
pos = findInput(*follows);
|
|
|
|
if (!pos) return {};
|
|
|
|
}
|
|
|
|
} else
|
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
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02: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 23:05:11 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02:00
|
|
|
LockFile::LockFile(const nlohmann::json & json, const Path & path)
|
2020-01-30 00:12:58 +02:00
|
|
|
{
|
2020-03-12 23:06:57 +02:00
|
|
|
auto version = json.value("version", 0);
|
2020-06-11 15:40:21 +03:00
|
|
|
if (version < 5 || version > 7)
|
2020-03-12 23:06:57 +02:00
|
|
|
throw Error("lock file '%s' has unsupported version %d", path, version);
|
|
|
|
|
2020-05-06 18:48:18 +03:00
|
|
|
std::unordered_map<std::string, std::shared_ptr<Node>> nodeMap;
|
2020-03-12 23:06:57 +02:00
|
|
|
|
2020-05-06 18:48:18 +03:00
|
|
|
std::function<void(Node & node, const nlohmann::json & jsonNode)> getInputs;
|
2020-03-12 23:06:57 +02:00
|
|
|
|
2020-05-06 18:48:18 +03:00
|
|
|
getInputs = [&](Node & node, const nlohmann::json & jsonNode)
|
|
|
|
{
|
|
|
|
if (jsonNode.find("inputs") == jsonNode.end()) return;
|
|
|
|
for (auto & i : jsonNode["inputs"].items()) {
|
2020-11-19 21:30:41 +02:00
|
|
|
if (i.value().is_array()) { // FIXME: remove, obsolete
|
2020-06-11 15:40:21 +03:00
|
|
|
InputPath path;
|
|
|
|
for (auto & j : i.value())
|
|
|
|
path.push_back(j);
|
|
|
|
node.inputs.insert_or_assign(i.key(), path);
|
|
|
|
} else {
|
|
|
|
std::string inputKey = i.value();
|
|
|
|
auto k = nodeMap.find(inputKey);
|
|
|
|
if (k == nodeMap.end()) {
|
2020-11-19 21:30:41 +02:00
|
|
|
auto nodes = json["nodes"];
|
|
|
|
auto jsonNode2 = nodes.find(inputKey);
|
|
|
|
if (jsonNode2 == nodes.end())
|
|
|
|
throw Error("lock file references missing node '%s'", inputKey);
|
|
|
|
auto input = std::make_shared<LockedNode>(*jsonNode2);
|
2020-06-11 15:40:21 +03:00
|
|
|
k = nodeMap.insert_or_assign(inputKey, input).first;
|
2020-11-19 21:30:41 +02:00
|
|
|
getInputs(*input, *jsonNode2);
|
2020-06-11 15:40:21 +03:00
|
|
|
}
|
|
|
|
if (auto child = std::dynamic_pointer_cast<LockedNode>(k->second))
|
|
|
|
node.inputs.insert_or_assign(i.key(), child);
|
|
|
|
else
|
|
|
|
// FIXME: replace by follows node
|
|
|
|
throw Error("lock file contains cycle to root node");
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
2020-05-06 18:48:18 +03:00
|
|
|
}
|
|
|
|
};
|
2020-03-12 23:06:57 +02:00
|
|
|
|
2020-05-06 18:48:18 +03:00
|
|
|
std::string rootKey = json["root"];
|
|
|
|
nodeMap.insert_or_assign(rootKey, root);
|
|
|
|
getInputs(*root, json["nodes"][rootKey]);
|
2020-06-11 15:40:21 +03:00
|
|
|
|
|
|
|
// FIXME: check that there are no cycles in version >= 7. Cycles
|
|
|
|
// between inputs are only possible using 'follows' indirections.
|
|
|
|
// Once we drop support for version <= 6, we can simplify the code
|
|
|
|
// a bit since we don't need to worry about cycles.
|
2020-01-30 00:12:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 16:41:18 +02:00
|
|
|
nlohmann::json LockFile::toJSON() const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
2020-03-12 23:06:57 +02: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();
|
2020-06-11 15:40:21 +03:00
|
|
|
for (auto & i : node->inputs) {
|
|
|
|
if (auto child = std::get_if<0>(&i.second)) {
|
|
|
|
inputs[i.first] = dumpNode(i.first, *child);
|
|
|
|
} else if (auto follows = std::get_if<1>(&i.second)) {
|
|
|
|
auto arr = nlohmann::json::array();
|
|
|
|
for (auto & x : *follows)
|
|
|
|
arr.push_back(x);
|
|
|
|
inputs[i.first] = std::move(arr);
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 23:06:57 +02:00
|
|
|
n["inputs"] = std::move(inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto lockedNode = std::dynamic_pointer_cast<const LockedNode>(node)) {
|
2020-10-28 16:41:18 +02:00
|
|
|
n["original"] = fetchers::attrsToJSON(lockedNode->originalRef.toAttrs());
|
|
|
|
n["locked"] = fetchers::attrsToJSON(lockedNode->lockedRef.toAttrs());
|
2020-03-12 23:06:57 +02:00
|
|
|
if (!lockedNode->isFlake) n["flake"] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[key] = std::move(n);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
|
|
|
nlohmann::json json;
|
2020-06-11 15:40:21 +03:00
|
|
|
json["version"] = 7;
|
2020-03-12 23:06:57 +02:00
|
|
|
json["root"] = dumpNode("root", root);
|
|
|
|
json["nodes"] = std::move(nodes);
|
|
|
|
|
2019-06-04 21:01:21 +03:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02:00
|
|
|
std::string LockFile::to_string() const
|
2019-06-04 21:01:21 +03:00
|
|
|
{
|
2020-10-28 16:41:18 +02:00
|
|
|
return toJSON().dump(2);
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
2019-06-04 21:01:21 +03:00
|
|
|
|
2020-03-12 23:06:57 +02:00
|
|
|
LockFile LockFile::read(const Path & path)
|
|
|
|
{
|
|
|
|
if (!pathExists(path)) return LockFile();
|
|
|
|
return LockFile(nlohmann::json::parse(readFile(path)), path);
|
2019-06-04 21:01:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator <<(std::ostream & stream, const LockFile & lockFile)
|
|
|
|
{
|
2020-10-28 16:41:18 +02:00
|
|
|
stream << lockFile.toJSON().dump(2);
|
2019-06-04 21:01:21 +03:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockFile::write(const Path & path) const
|
|
|
|
{
|
|
|
|
createDirs(dirOf(path));
|
|
|
|
writeFile(path, fmt("%s\n", *this));
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:06:57 +02: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;
|
2020-06-11 15:40:21 +03:00
|
|
|
for (auto & i : node->inputs)
|
|
|
|
if (auto child = std::get_if<0>(&i.second))
|
|
|
|
visit(*child);
|
2020-03-12 23:06:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
visit(root);
|
|
|
|
|
|
|
|
for (auto & i : nodes) {
|
|
|
|
if (i == root) continue;
|
|
|
|
auto lockedNode = std::dynamic_pointer_cast<const LockedNode>(i);
|
2022-02-24 19:09:00 +02:00
|
|
|
if (lockedNode && !lockedNode->lockedRef.input.isLocked()) return false;
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LockFile::operator ==(const LockFile & other) const
|
|
|
|
{
|
|
|
|
// FIXME: slow
|
2020-10-28 16:41:18 +02:00
|
|
|
return toJSON() == other.toJSON();
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 15:57:57 +02:00
|
|
|
InputPath parseInputPath(std::string_view s)
|
|
|
|
{
|
|
|
|
InputPath path;
|
|
|
|
|
|
|
|
for (auto & elem : tokenizeString<std::vector<std::string>>(s, "/")) {
|
2020-03-30 15:03:28 +03:00
|
|
|
if (!std::regex_match(elem, flakeIdRegex))
|
2020-06-08 17:20:00 +03:00
|
|
|
throw UsageError("invalid flake input path element '%s'", elem);
|
2020-01-29 15:57:57 +02:00
|
|
|
path.push_back(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-06-12 00:34:02 +03:00
|
|
|
std::map<InputPath, Node::Edge> LockFile::getAllInputs() const
|
2020-03-12 23:06:57 +02:00
|
|
|
{
|
2020-06-12 00:34:02 +03:00
|
|
|
std::unordered_set<std::shared_ptr<Node>> done;
|
|
|
|
std::map<InputPath, Node::Edge> res;
|
|
|
|
|
|
|
|
std::function<void(const InputPath & prefix, std::shared_ptr<Node> node)> recurse;
|
|
|
|
|
|
|
|
recurse = [&](const InputPath & prefix, std::shared_ptr<Node> node)
|
|
|
|
{
|
|
|
|
if (!done.insert(node).second) return;
|
|
|
|
|
|
|
|
for (auto &[id, input] : node->inputs) {
|
|
|
|
auto inputPath(prefix);
|
|
|
|
inputPath.push_back(id);
|
|
|
|
res.emplace(inputPath, input);
|
|
|
|
if (auto child = std::get_if<0>(&input))
|
|
|
|
recurse(inputPath, *child);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
recurse({}, root);
|
|
|
|
|
|
|
|
return res;
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 20:35:28 +03:00
|
|
|
static std::string describe(const FlakeRef & flakeRef)
|
|
|
|
{
|
|
|
|
auto s = fmt("'%s'", flakeRef.to_string());
|
|
|
|
|
|
|
|
if (auto lastModified = flakeRef.input.getLastModified())
|
|
|
|
s += fmt(" (%s)", std::put_time(std::gmtime(&*lastModified), "%Y-%m-%d"));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-06-11 23:00:58 +03:00
|
|
|
std::ostream & operator <<(std::ostream & stream, const Node::Edge & edge)
|
|
|
|
{
|
|
|
|
if (auto node = std::get_if<0>(&edge))
|
2021-08-11 20:35:28 +03:00
|
|
|
stream << describe((*node)->lockedRef);
|
2020-06-11 23:00:58 +03:00
|
|
|
else if (auto follows = std::get_if<1>(&edge))
|
|
|
|
stream << fmt("follows '%s'", printInputPath(*follows));
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool equals(const Node::Edge & e1, const Node::Edge & e2)
|
|
|
|
{
|
|
|
|
if (auto n1 = std::get_if<0>(&e1))
|
|
|
|
if (auto n2 = std::get_if<0>(&e2))
|
|
|
|
return (*n1)->lockedRef == (*n2)->lockedRef;
|
|
|
|
if (auto f1 = std::get_if<1>(&e1))
|
|
|
|
if (auto f2 = std::get_if<1>(&e2))
|
|
|
|
return *f1 == *f2;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 00:34:02 +03:00
|
|
|
std::string LockFile::diff(const LockFile & oldLocks, const LockFile & newLocks)
|
2020-03-12 23:06:57 +02:00
|
|
|
{
|
2020-06-12 00:34:02 +03:00
|
|
|
auto oldFlat = oldLocks.getAllInputs();
|
|
|
|
auto newFlat = newLocks.getAllInputs();
|
2020-03-12 23:06:57 +02: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)) {
|
2021-08-11 20:35:28 +03:00
|
|
|
res += fmt("• " ANSI_GREEN "Added input '%s':" ANSI_NORMAL "\n %s\n",
|
|
|
|
printInputPath(j->first), j->second);
|
2020-03-12 23:06:57 +02:00
|
|
|
++j;
|
|
|
|
} else if (i != oldFlat.end() && (j == newFlat.end() || i->first < j->first)) {
|
2021-08-11 20:35:28 +03:00
|
|
|
res += fmt("• " ANSI_RED "Removed input '%s'" ANSI_NORMAL "\n", printInputPath(i->first));
|
2020-03-12 23:06:57 +02:00
|
|
|
++i;
|
|
|
|
} else {
|
2020-06-11 23:00:58 +03:00
|
|
|
if (!equals(i->second, j->second)) {
|
2021-08-11 20:35:28 +03:00
|
|
|
res += fmt("• " ANSI_BOLD "Updated input '%s':" ANSI_NORMAL "\n %s\n → %s\n",
|
2020-06-10 16:20:00 +03:00
|
|
|
printInputPath(i->first),
|
2020-06-11 23:00:58 +03:00
|
|
|
i->second,
|
|
|
|
j->second);
|
2020-03-12 23:06:57 +02:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-06-12 00:34:02 +03:00
|
|
|
void LockFile::check()
|
|
|
|
{
|
|
|
|
auto inputs = getAllInputs();
|
|
|
|
|
|
|
|
for (auto & [inputPath, input] : inputs) {
|
|
|
|
if (auto follows = std::get_if<1>(&input)) {
|
|
|
|
if (!follows->empty() && !get(inputs, *follows))
|
|
|
|
throw Error("input '%s' follows a non-existent input '%s'",
|
|
|
|
printInputPath(inputPath),
|
|
|
|
printInputPath(*follows));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check();
|
|
|
|
|
2020-06-10 16:20:00 +03:00
|
|
|
std::string printInputPath(const InputPath & path)
|
|
|
|
{
|
|
|
|
return concatStringsSep("/", path);
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:01:21 +03:00
|
|
|
}
|