Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
lockfile.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/resolver/lockfile.hh
4 *
5 * @brief A lockfile representing a resolved environment.
6 *
7 * This lockfile is processed by `mkEnv` to realize an environment.
8 *
9 *
10 * -------------------------------------------------------------------------- */
11
12#pragma once
13
14#include <unordered_map>
15
16#include <nlohmann/json.hpp>
17
19#include "flox/core/types.hh"
20#include "flox/pkgdb/input.hh"
21#include "flox/pkgdb/read.hh"
22#include "flox/registry.hh"
24
25
26/* -------------------------------------------------------------------------- */
27
28namespace flox::resolver {
29
30/* -------------------------------------------------------------------------- */
31
39 "invalid lockfile" )
43/* -------------------------------------------------------------------------- */
44
45struct LockedInputRaw
46{
47
48 pkgdb::Fingerprint fingerprint;
49 std::string url;
51 nlohmann::json attrs;
52
53 ~LockedInputRaw() = default;
54 LockedInputRaw() : fingerprint( nix::htSHA256 ) {}
55 LockedInputRaw( const LockedInputRaw & ) = default;
56 LockedInputRaw( LockedInputRaw && ) = default;
57
58 LockedInputRaw &
59 operator=( const LockedInputRaw & )
60 = default;
61 LockedInputRaw &
62 operator=( LockedInputRaw && )
63 = default;
64
65 explicit LockedInputRaw( const pkgdb::PkgDbReadOnly & pdb )
66 : fingerprint( pdb.fingerprint )
67 , url( pdb.lockedRef.string )
68 , attrs( pdb.lockedRef.attrs )
69 {}
70
71 explicit LockedInputRaw( const pkgdb::PkgDbInput & input )
72 : LockedInputRaw( *input.getDbReadOnly() )
73 {}
74
75 explicit operator nix::FlakeRef() const
76 {
77 return nix::FlakeRef::fromAttrs(
78 nix::fetchers::jsonToAttrs( this->attrs ) );
79 }
80
81 explicit operator RegistryInput() const
82 {
83 return RegistryInput( static_cast<nix::FlakeRef>( *this ) );
84 }
85
86 [[nodiscard]] bool
87 operator==( const LockedInputRaw & other ) const
88 {
89 return ( this->fingerprint == other.fingerprint )
90 && ( this->url == other.url ) && ( this->attrs == other.attrs );
91 }
92
93 [[nodiscard]] bool
94 operator!=( const LockedInputRaw & other ) const
95 {
96 return ! ( ( *this ) == other );
97 }
98
99
100}; /* End struct `LockedInputRaw' */
101
102
103/* -------------------------------------------------------------------------- */
104
106void
107from_json( const nlohmann::json & jfrom, LockedInputRaw & raw );
108
110void
111to_json( nlohmann::json & jto, const LockedInputRaw & raw );
112
113
114/* -------------------------------------------------------------------------- */
115
117std::ostream &
118operator<<( std::ostream & oss, const LockedInputRaw & raw );
119
120
121/* -------------------------------------------------------------------------- */
122
125{
126
127 LockedInputRaw input;
128 AttrPath attrPath;
129 unsigned priority;
130 nlohmann::json info; /* pname, version, license */
131
132 [[nodiscard]] bool
133 operator==( const LockedPackageRaw & other ) const
134 {
135 return ( this->input == other.input )
136 && ( this->attrPath == other.attrPath )
137 && ( this->priority == other.priority )
138 && ( this->info == other.info );
139 }
140
141 [[nodiscard]] bool
142 operator!=( const LockedPackageRaw & other ) const
143 {
144 return ! ( ( *this ) == other );
145 }
146
147
148}; /* End struct `LockedPackageRaw' */
149
150
151/* -------------------------------------------------------------------------- */
152
154void
155from_json( const nlohmann::json & jfrom, LockedPackageRaw & raw );
156
158void
159to_json( nlohmann::json & jto, const LockedPackageRaw & raw );
160
161
162/* -------------------------------------------------------------------------- */
163
165std::ostream &
166operator<<( std::ostream & oss, const LockedPackageRaw & raw );
167
168
169/* -------------------------------------------------------------------------- */
170
171using SystemPackages
172 = std::unordered_map<InstallID, std::optional<LockedPackageRaw>>;
173
181{
182
183 ManifestRaw manifest;
184 RegistryRaw registry;
185 std::unordered_map<System, SystemPackages> packages;
186 unsigned lockfileVersion = 0;
187
188
189 ~LockfileRaw() = default;
190 LockfileRaw() = default;
191 LockfileRaw( const LockfileRaw & ) = default;
192 LockfileRaw( LockfileRaw && ) = default;
194 operator=( const LockfileRaw & )
195 = default;
197 operator=( LockfileRaw && )
198 = default;
199
207 void
208 check() const;
209
211 void
212 clear();
213
214
215}; /* End struct `LockfileRaw' */
216
217
218/* -------------------------------------------------------------------------- */
219
221void
222from_json( const nlohmann::json & jfrom, LockfileRaw & raw );
223
225void
226to_json( nlohmann::json & jto, const LockfileRaw & raw );
227
228
229/* -------------------------------------------------------------------------- */
230
238{
239
240private:
241
244
252
253
260 void
261 checkGroups() const;
262
275 void
276 check() const;
277
282 void
283 init();
284
285
286public:
287
288 ~Lockfile() = default;
289 Lockfile() = default;
290 Lockfile( const Lockfile & ) = default;
291 Lockfile( Lockfile && ) = default;
292
293 explicit Lockfile( LockfileRaw raw ) : lockfileRaw( std::move( raw ) )
294 {
295 this->init();
296 }
297
298 explicit Lockfile( std::filesystem::path lockfilePath );
299
300 Lockfile &
301 operator=( const Lockfile & )
302 = default;
303
304 Lockfile &
305 operator=( Lockfile && )
306 = default;
307
309 [[nodiscard]] const LockfileRaw &
311 {
312 return this->lockfileRaw;
313 }
314
316 [[nodiscard]] const ManifestRaw &
318 {
319 return this->getLockfileRaw().manifest;
320 }
321
323 [[nodiscard]] const RegistryRaw &
325 {
326 return this->getLockfileRaw().registry;
327 }
328
330 [[nodiscard]] const EnvironmentManifest &
332 {
333 return this->manifest;
334 }
335
337 [[nodiscard]] const std::unordered_map<InstallID, ManifestDescriptor> &
339 {
340 return this->getManifest().getDescriptors();
341 }
342
349 [[nodiscard]] const RegistryRaw &
351 {
352 return this->packagesRegistryRaw;
353 }
354
362 std::size_t
364
365
366}; /* End class `Lockfile' */
367
368
369/* -------------------------------------------------------------------------- */
370
371} // namespace flox::resolver
372
373
374/* -------------------------------------------------------------------------- *
375 *
376 *
377 *
378 * ========================================================================== */
const InstallDescriptors & getDescriptors() const
Get descriptors from the manifest's ‘install’ field.
Definition: manifest.hh:384
An exception thrown when a lockfile is invalid.
A locked representation of an environment.
Definition: lockfile.hh:238
const LockfileRaw & getLockfileRaw() const
Get the raw representation of the lockfile.
Definition: lockfile.hh:310
LockfileRaw lockfileRaw
Definition: lockfile.hh:243
RegistryRaw packagesRegistryRaw
Definition: lockfile.hh:251
EnvironmentManifest manifest
Definition: lockfile.hh:249
void checkGroups() const
Check the lockfile's packages.** locked inputs align with the requested groups in manifest....
Definition: lockfile.cc:41
const EnvironmentManifest & getManifest() const
Get old manifest.
Definition: lockfile.hh:331
void init()
Initialize manifest and packagesRegistryRaw from lockfileRaw.
Definition: lockfile.cc:126
const RegistryRaw & getPackagesRegistryRaw() const
Get the packagesRegistryRaw, containing all inputs used by packages.** members of the lockfile.
Definition: lockfile.hh:350
const RegistryRaw & getRegistryRaw() const
Get the locked registry from the raw lockfile.
Definition: lockfile.hh:324
std::size_t removeUnusedInputs()
Drop any registry.inputs and registry.priority members that are not explicitly declared in the manife...
Definition: lockfile.cc:442
const ManifestRaw & getManifestRaw() const
Get the original manifest used to create the lockfile.
Definition: lockfile.hh:317
const std::unordered_map< InstallID, ManifestDescriptor > & getDescriptors() const
Get old descriptors.
Definition: lockfile.hh:338
void check() const
Check the lockfile's validity, throwing an exception for invalid contents.
Definition: lockfile.cc:102
Definitions of various std::exception children used for throwing errors with nice messages and typed ...
#define FLOX_DEFINE_EXCEPTION(NAME, ERROR_CODE, CATEGORY_MSG)
Generate a class definition with an error code and category message.
Definition: exceptions.hh:206
A RegistryInput that opens a PkgDb associated with a flake.
An abstract description of an environment in its unresolved state.
nix::flake::Fingerprint Fingerprint
Definition: read.hh:93
std::ostream & operator<<(std::ostream &oss, const SqlVersions &versions)
Emit version information to an output stream.
Definition: read.cc:44
@ EC_INVALID_LOCKFILE
Definition: exceptions.hh:73
void from_json(const nlohmann::json &jfrom, RawPackage &pkg)
Convert a JSON object to a flox::RawPackage.
Definition: raw-package.cc:27
void to_json(nlohmann::json &jto, const FloxException &err)
Convert a flox::FloxException to a JSON object.
Definition: exceptions.cc:27
std::vector< std::string > AttrPath
A list of key names addressing a location in a nested JSON-like object.
Definition: types.hh:33
Interfaces for reading a SQLite3 package set database.
A set of user inputs used to set input preferences during search and resolution.
A set of user inputs used to set input preferences during search and resolution.
Definition: registry.hh:297
A locked package's installable URI.
Definition: lockfile.hh:125
An environment lockfile in its raw form.
Definition: lockfile.hh:181
void check() const
Check the lockfile for validity, throw and exception if it is invalid.
Definition: lockfile.cc:27
void clear()
Reset to default/empty state.
Definition: lockfile.cc:335
A raw description of an environment to be read from a file.
Definition: manifest-raw.hh:264
Miscellaneous typedefs and aliases.