2019-06-04 18:01:21 +00:00
|
|
|
#include "lockfile.hh"
|
|
|
|
#include "store-api.hh"
|
2020-09-21 16:22:45 +00:00
|
|
|
#include "url-parts.hh"
|
2019-06-04 18:01:21 +00:00
|
|
|
|
2021-08-11 17:35:28 +00:00
|
|
|
#include <iomanip>
|
|
|
|
|
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 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-29 22:44:11 +00:00
|
|
|
const char * attr,
|
|
|
|
const char * info)
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
2020-05-06 15:48:18 +00: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-29 22:44:11 +00:00
|
|
|
if (i != json.end()) {
|
2020-10-06 09:16:32 +00: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-29 22:44:11 +00:00
|
|
|
// FIXME: remove when we drop support for version 5.
|
|
|
|
if (info) {
|
|
|
|
auto j = json.find(info);
|
|
|
|
if (j != json.end()) {
|
2020-10-06 09:16:32 +00: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-29 22:44:11 +00:00
|
|
|
attrs.insert_or_assign(k.first, k.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FlakeRef::fromAttrs(attrs);
|
|
|
|
}
|
2020-01-31 18:16:40 +00:00
|
|
|
|
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)
|
2022-12-07 11:58:58 +00:00
|
|
|
: lockedRef(getFlakeRef(json, "locked", "info")) // FIXME: remove "info"
|
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-29 22:44:11 +00:00
|
|
|
, originalRef(getFlakeRef(json, "original", nullptr))
|
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
|
|
|
{
|
2022-02-24 17:09:00 +00:00
|
|
|
if (!lockedRef.input.isLocked())
|
2022-09-06 17:20:31 +00:00
|
|
|
throw Error("lock file contains mutable lock '%s'",
|
2020-10-28 14:41:18 +00:00
|
|
|
fetchers::attrsToJSON(lockedRef.input.toAttrs()));
|
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
|
|
|
{
|
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-29 22:44:11 +00:00
|
|
|
return lockedRef.input.computeStorePath(store);
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:40:21 +00: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 21:05:11 +00:00
|
|
|
{
|
2020-06-11 12:40:21 +00:00
|
|
|
auto pos = root;
|
|
|
|
|
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) {
|
2020-06-11 12:40:21 +00: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)) {
|
2022-12-07 11:58:58 +00:00
|
|
|
if (auto p = findInput(*follows))
|
|
|
|
pos = ref(p);
|
|
|
|
else
|
|
|
|
return {};
|
2020-06-11 12:40:21 +00:00
|
|
|
}
|
|
|
|
} 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 21:05:11 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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-06-11 12:40:21 +00:00
|
|
|
if (version < 5 || version > 7)
|
2020-03-12 21:06:57 +00:00
|
|
|
throw Error("lock file '%s' has unsupported version %d", path, version);
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
std::map<std::string, ref<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()) {
|
2020-11-19 19:30:41 +00:00
|
|
|
if (i.value().is_array()) { // FIXME: remove, obsolete
|
2020-06-11 12:40:21 +00: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 19:30:41 +00:00
|
|
|
auto nodes = json["nodes"];
|
|
|
|
auto jsonNode2 = nodes.find(inputKey);
|
|
|
|
if (jsonNode2 == nodes.end())
|
|
|
|
throw Error("lock file references missing node '%s'", inputKey);
|
2022-12-07 11:58:58 +00:00
|
|
|
auto input = make_ref<LockedNode>(*jsonNode2);
|
2020-06-11 12:40:21 +00:00
|
|
|
k = nodeMap.insert_or_assign(inputKey, input).first;
|
2020-11-19 19:30:41 +00:00
|
|
|
getInputs(*input, *jsonNode2);
|
2020-06-11 12:40:21 +00:00
|
|
|
}
|
2022-12-07 11:58:58 +00:00
|
|
|
if (auto child = k->second.dynamic_pointer_cast<LockedNode>())
|
|
|
|
node.inputs.insert_or_assign(i.key(), ref(child));
|
2020-06-11 12:40:21 +00:00
|
|
|
else
|
|
|
|
// FIXME: replace by follows node
|
|
|
|
throw Error("lock file contains cycle to root node");
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
2020-05-06 15:48:18 +00:00
|
|
|
}
|
|
|
|
};
|
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-06-11 12:40:21 +00: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-29 22:12:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:41:18 +00:00
|
|
|
nlohmann::json LockFile::toJSON() const
|
2019-06-04 18:01:21 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
std::function<std::string(const std::string & key, ref<const Node> node)> dumpNode;
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
dumpNode = [&](std::string key, ref<const Node> node) -> std::string
|
2020-03-12 21:06:57 +00:00
|
|
|
{
|
|
|
|
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 12:40:21 +00: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 21:06:57 +00:00
|
|
|
n["inputs"] = std::move(inputs);
|
|
|
|
}
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
if (auto lockedNode = node.dynamic_pointer_cast<const LockedNode>()) {
|
2020-10-28 14:41:18 +00:00
|
|
|
n["original"] = fetchers::attrsToJSON(lockedNode->originalRef.toAttrs());
|
|
|
|
n["locked"] = fetchers::attrsToJSON(lockedNode->lockedRef.toAttrs());
|
2022-12-07 11:58:58 +00:00
|
|
|
if (!lockedNode->isFlake)
|
|
|
|
n["flake"] = false;
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes[key] = std::move(n);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
|
|
|
nlohmann::json json;
|
2020-06-11 12:40:21 +00:00
|
|
|
json["version"] = 7;
|
2020-03-12 21:06:57 +00:00
|
|
|
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-10-28 14:41:18 +00:00
|
|
|
return toJSON().dump(2);
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
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-10-28 14:41:18 +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));
|
|
|
|
}
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
std::optional<FlakeRef> LockFile::isUnlocked() const
|
2020-03-12 21:06:57 +00:00
|
|
|
{
|
2022-12-07 11:58:58 +00:00
|
|
|
std::set<ref<const Node>> nodes;
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
std::function<void(ref<const Node> node)> visit;
|
2020-03-12 21:06:57 +00:00
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
visit = [&](ref<const Node> node)
|
2020-03-12 21:06:57 +00:00
|
|
|
{
|
|
|
|
if (!nodes.insert(node).second) return;
|
2020-06-11 12:40:21 +00:00
|
|
|
for (auto & i : node->inputs)
|
|
|
|
if (auto child = std::get_if<0>(&i.second))
|
|
|
|
visit(*child);
|
2020-03-12 21:06:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
visit(root);
|
|
|
|
|
|
|
|
for (auto & i : nodes) {
|
2023-02-01 19:34:44 +00:00
|
|
|
if (i == ref<const Node>(root)) continue;
|
2022-12-07 11:58:58 +00:00
|
|
|
auto node = i.dynamic_pointer_cast<const LockedNode>();
|
|
|
|
if (node && !node->lockedRef.input.isLocked())
|
|
|
|
return node->lockedRef;
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
return {};
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LockFile::operator ==(const LockFile & other) const
|
|
|
|
{
|
|
|
|
// FIXME: slow
|
2020-10-28 14:41:18 +00:00
|
|
|
return toJSON() == other.toJSON();
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
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-06-08 14:20:00 +00:00
|
|
|
throw UsageError("invalid flake input path element '%s'", elem);
|
2020-01-29 13:57:57 +00:00
|
|
|
path.push_back(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-06-11 21:34:02 +00:00
|
|
|
std::map<InputPath, Node::Edge> LockFile::getAllInputs() const
|
2020-03-12 21:06:57 +00:00
|
|
|
{
|
2022-12-07 11:58:58 +00:00
|
|
|
std::set<ref<Node>> done;
|
2020-06-11 21:34:02 +00:00
|
|
|
std::map<InputPath, Node::Edge> res;
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
std::function<void(const InputPath & prefix, ref<Node> node)> recurse;
|
2020-06-11 21:34:02 +00:00
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
recurse = [&](const InputPath & prefix, ref<Node> node)
|
2020-06-11 21:34:02 +00:00
|
|
|
{
|
|
|
|
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 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:35:28 +00: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 20:00:58 +00:00
|
|
|
std::ostream & operator <<(std::ostream & stream, const Node::Edge & edge)
|
|
|
|
{
|
|
|
|
if (auto node = std::get_if<0>(&edge))
|
2021-08-11 17:35:28 +00:00
|
|
|
stream << describe((*node)->lockedRef);
|
2020-06-11 20:00:58 +00: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-11 21:34:02 +00:00
|
|
|
std::string LockFile::diff(const LockFile & oldLocks, const LockFile & newLocks)
|
2020-03-12 21:06:57 +00:00
|
|
|
{
|
2020-06-11 21:34:02 +00:00
|
|
|
auto oldFlat = oldLocks.getAllInputs();
|
|
|
|
auto newFlat = newLocks.getAllInputs();
|
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)) {
|
2021-08-11 17:35:28 +00:00
|
|
|
res += fmt("• " ANSI_GREEN "Added input '%s':" ANSI_NORMAL "\n %s\n",
|
|
|
|
printInputPath(j->first), j->second);
|
2020-03-12 21:06:57 +00:00
|
|
|
++j;
|
|
|
|
} else if (i != oldFlat.end() && (j == newFlat.end() || i->first < j->first)) {
|
2021-08-11 17:35:28 +00:00
|
|
|
res += fmt("• " ANSI_RED "Removed input '%s'" ANSI_NORMAL "\n", printInputPath(i->first));
|
2020-03-12 21:06:57 +00:00
|
|
|
++i;
|
|
|
|
} else {
|
2020-06-11 20:00:58 +00:00
|
|
|
if (!equals(i->second, j->second)) {
|
2021-08-11 17:35:28 +00:00
|
|
|
res += fmt("• " ANSI_BOLD "Updated input '%s':" ANSI_NORMAL "\n %s\n → %s\n",
|
2020-06-10 13:20:00 +00:00
|
|
|
printInputPath(i->first),
|
2020-06-11 20:00:58 +00:00
|
|
|
i->second,
|
|
|
|
j->second);
|
2020-03-12 21:06:57 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-06-11 21:34:02 +00:00
|
|
|
void LockFile::check()
|
|
|
|
{
|
|
|
|
auto inputs = getAllInputs();
|
|
|
|
|
|
|
|
for (auto & [inputPath, input] : inputs) {
|
|
|
|
if (auto follows = std::get_if<1>(&input)) {
|
2022-09-01 13:26:19 +00:00
|
|
|
if (!follows->empty() && !get(inputs, *follows))
|
2020-06-11 21:34:02 +00:00
|
|
|
throw Error("input '%s' follows a non-existent input '%s'",
|
|
|
|
printInputPath(inputPath),
|
|
|
|
printInputPath(*follows));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check();
|
|
|
|
|
2020-06-10 13:20:00 +00:00
|
|
|
std::string printInputPath(const InputPath & path)
|
|
|
|
{
|
|
|
|
return concatStringsSep("/", path);
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:01:21 +00:00
|
|
|
}
|