Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
lockfile.hh
Go to the documentation of this file.
1#pragma once
3
5
6#include <nlohmann/json_fwd.hpp>
7
8namespace nix {
9class Store;
10class StorePath;
11} // namespace nix
12
13namespace nix::flake {
14
15typedef std::vector<FlakeId> InputAttrPath;
16
24class NonEmptyInputAttrPath
25{
26 InputAttrPath path;
27
28 explicit NonEmptyInputAttrPath(InputAttrPath && p)
29 : path(std::move(p))
30 {
31 assert(!path.empty());
32 }
33
34public:
39 static std::optional<NonEmptyInputAttrPath> parse(std::string_view s);
40
45 static std::optional<NonEmptyInputAttrPath> make(InputAttrPath path);
46
51 static NonEmptyInputAttrPath append(const InputAttrPath & prefix, const FlakeId & element)
52 {
53 InputAttrPath path = prefix;
54 path.push_back(element);
55 return NonEmptyInputAttrPath{std::move(path)};
56 }
57
58 const InputAttrPath & get() const
59 {
60 return path;
61 }
62
63 operator const InputAttrPath &() const
64 {
65 return path;
66 }
67
72 const FlakeId & inputName() const
73 {
74 return path.back();
75 }
76
81 InputAttrPath parent() const
82 {
83 InputAttrPath result = path;
84 result.pop_back();
85 return result;
86 }
87
88 auto operator<=>(const NonEmptyInputAttrPath & other) const = default;
89};
90
91struct LockedNode;
92
98struct Node : std::enable_shared_from_this<Node>
99{
100 typedef std::variant<ref<LockedNode>, InputAttrPath> Edge;
101
102 std::map<FlakeId, Edge> inputs;
103
104 virtual ~Node() {}
105};
106
110struct LockedNode : Node
111{
112 FlakeRef lockedRef, originalRef;
113 bool isFlake = true;
114
115 /* The node relative to which relative source paths
116 (e.g. 'path:../foo') are interpreted. */
117 std::optional<InputAttrPath> parentInputAttrPath;
118
119 LockedNode(
120 const FlakeRef & lockedRef,
121 const FlakeRef & originalRef,
122 bool isFlake = true,
123 std::optional<InputAttrPath> parentInputAttrPath = {})
124 : lockedRef(std::move(lockedRef))
125 , originalRef(std::move(originalRef))
126 , isFlake(isFlake)
127 , parentInputAttrPath(std::move(parentInputAttrPath))
128 {
129 }
130
131 LockedNode(const fetchers::Settings & fetchSettings, const nlohmann::json & json);
132
133 StorePath computeStorePath(Store & store) const;
134};
135
136struct LockFile
137{
138 ref<Node> root = make_ref<Node>();
139
140 LockFile() {};
141 LockFile(const fetchers::Settings & fetchSettings, std::string_view contents, std::string_view path);
142
143 typedef std::map<ref<const Node>, std::string> KeyMap;
144
145 std::pair<nlohmann::json, KeyMap> toJSON() const;
146
147 std::pair<std::string, KeyMap> to_string() const;
148
153 std::optional<FlakeRef> isUnlocked(const fetchers::Settings & fetchSettings) const;
154
155 bool operator==(const LockFile & other) const;
156
157 std::shared_ptr<Node> findInput(const InputAttrPath & path);
158
159 std::map<InputAttrPath, Node::Edge> getAllInputs() const;
160
161 static std::string diff(const LockFile & oldLocks, const LockFile & newLocks);
162
166 void check();
167};
168
169std::ostream & operator<<(std::ostream & stream, const LockFile & lockFile);
170
171InputAttrPath parseInputAttrPath(std::string_view s);
172
173std::string printInputAttrPath(const InputAttrPath & path);
174
175} // namespace nix::flake
Definition path.hh:33
Definition store-api.hh:397
Definition lockfile.hh:25
InputAttrPath parent() const
Definition lockfile.hh:81
const FlakeId & inputName() const
Definition lockfile.hh:72
static std::optional< NonEmptyInputAttrPath > make(InputAttrPath path)
Definition lockfile.cc:327
static std::optional< NonEmptyInputAttrPath > parse(std::string_view s)
Definition lockfile.cc:321
static NonEmptyInputAttrPath append(const InputAttrPath &prefix, const FlakeId &element)
Definition lockfile.hh:51
Definition ref.hh:58
Definition flakeref.hh:44
Definition fetch-settings.hh:27
Definition lockfile.hh:137
std::optional< FlakeRef > isUnlocked(const fetchers::Settings &fetchSettings) const
Definition lockfile.cc:270
void check()
Definition lockfile.cc:418
Definition lockfile.hh:99
const T * get(const std::optional< T > &opt)
Definition util.hh:286