Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
environment.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/resolver/environment.hh
4 *
5 * @brief A collection of files associated with an environment.
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <memory>
13#include <optional>
14#include <string_view>
15#include <utility>
16#include <vector>
17
18#include <nix/ref.hh>
19
22#include "flox/core/types.hh"
23#include "flox/pkgdb/input.hh"
25#include "flox/registry.hh"
29
30
31/* -------------------------------------------------------------------------- */
32
33/* Forward Declarations */
34
35namespace flox {
36
37namespace pkgdb {
38class PkgDbReadOnly;
39} // namespace pkgdb
40
41namespace resolver {
42struct ManifestDescriptor;
43} // namespace resolver
44
45
46} // namespace flox
47
48
49/* -------------------------------------------------------------------------- */
50
51namespace flox::resolver {
52
53/* -------------------------------------------------------------------------- */
54
55FLOX_DEFINE_EXCEPTION( ResolutionFailureException,
57 "resolution failure" );
58
59
60/* -------------------------------------------------------------------------- */
61
68using ResolutionFailure = std::vector<std::pair<InstallID, std::string>>;
69
81using ResolutionResult = std::variant<ResolutionFailure, SystemPackages>;
82
83
84/* -------------------------------------------------------------------------- */
85
97{
98
99private:
100
101 /* From `NixStoreMixin':
102 * std::shared_ptr<nix::Store> store
103 */
104
106 std::optional<GlobalManifest> globalManifest;
107
110
112 std::optional<Lockfile> oldLockfile;
113
114
122 using Upgrades = std::variant<bool, std::vector<InstallID>>;
125
127 std::optional<LockfileRaw> lockfileRaw;
128
129 std::optional<RegistryRaw> combinedRegistryRaw;
130
131 std::optional<Options> combinedOptions;
132
133 std::optional<pkgdb::PkgQueryArgs> combinedBaseQueryArgs;
134
136 std::optional<RegistryRaw> lockedRegistry;
137
138 std::shared_ptr<Registry<pkgdb::PkgDbInputFactory>> dbs;
139
140
141 static LockedPackageRaw
142 lockPackage( const LockedInputRaw & input,
144 pkgdb::row_id row,
145 unsigned priority );
146
147 static inline LockedPackageRaw
148 lockPackage( const pkgdb::PkgDbInput & input,
149 pkgdb::row_id row,
150 unsigned priority )
151 {
152 return lockPackage( LockedInputRaw( input ),
153 *input.getDbReadOnly(),
154 row,
155 priority );
156 }
157
162 [[nodiscard]] std::vector<InstallDescriptors>
163 getUnlockedGroups( const System & system );
164
166 [[nodiscard]] std::vector<InstallDescriptors>
167 getLockedGroups( const System & system );
168
178 [[nodiscard]] const Options &
180
182 [[nodiscard]] std::optional<pkgdb::row_id>
183 tryResolveDescriptorIn( const ManifestDescriptor & descriptor,
184 const pkgdb::PkgDbInput & input,
185 const System & system );
186
197 [[nodiscard]] ResolutionResult
198 tryResolveGroup( const InstallDescriptors & group, const System & system );
199
206 [[nodiscard]] std::variant<InstallID, SystemPackages>
208 const pkgdb::PkgDbInput & input,
209 const System & system );
210
220 void
221 lockSystem( const System & system );
222
223
224protected:
225
250 [[nodiscard]] std::optional<LockedInputRaw>
251 getGroupInput( const InstallDescriptors & group,
252 const Lockfile & oldLockfile,
253 const System & system ) const;
254
264 [[nodiscard]] bool
265 groupIsLocked( const InstallDescriptors & group,
266 const Lockfile & oldLockfile,
267 const System & system ) const;
268
269
270public:
271
272 Environment( std::optional<GlobalManifest> globalManifest,
274 std::optional<Lockfile> oldLockfile,
275 Upgrades upgrades = false )
276 : globalManifest( std::move( globalManifest ) )
277 , manifest( std::move( manifest ) )
278 , oldLockfile( std::move( oldLockfile ) )
279 , upgrades( std::move( upgrades ) )
280 {}
281
282 explicit Environment( EnvironmentManifest manifest,
283 std::optional<Lockfile> oldLockfile = std::nullopt )
284 : globalManifest( std::nullopt )
285 , manifest( std::move( manifest ) )
286 , oldLockfile( std::move( oldLockfile ) )
287 {}
288
289 [[nodiscard]] const std::optional<GlobalManifest> &
290 getGlobalManifest() const
291 {
292 return this->globalManifest;
293 }
294
295 [[nodiscard]] std::optional<GlobalManifestRaw>
296 getGlobalManifestRaw() const
297 {
298 const auto & global = this->getGlobalManifest();
299 if ( ! global.has_value() ) { return std::nullopt; }
300 return global->getManifestRaw();
301 }
302
303 [[nodiscard]] const EnvironmentManifest &
304 getManifest() const
305 {
306 return this->manifest;
307 }
308
309 [[nodiscard]] const ManifestRaw &
310 getManifestRaw() const
311 {
312 return this->getManifest().getManifestRaw();
313 }
314
316 [[nodiscard]] std::optional<ManifestRaw>
317 getOldManifestRaw() const;
318
319 [[nodiscard]] std::optional<Lockfile>
320 getOldLockfile() const
321 {
322 return this->oldLockfile;
323 }
324
334 [[nodiscard]] RegistryRaw &
336
341 [[nodiscard]] const pkgdb::PkgQueryArgs &
343
345 [[nodiscard]] std::vector<System>
347 {
348 return this->getManifest().getSystems();
349 }
350
352 [[nodiscard]] nix::ref<Registry<pkgdb::PkgDbInputFactory>>
354
355 // TODO: (Question) Should we lock the combined options and fill registry
356 // `default` fields in inputs?
358 [[nodiscard]] Lockfile
360
361
362}; /* End class `Environment' */
363
364
365/* -------------------------------------------------------------------------- */
366
367} // namespace flox::resolver
368
369
370/* -------------------------------------------------------------------------- *
371 *
372 *
373 *
374 * ========================================================================== */
Mixin which provides a lazy handle to a nix store connection.
Definition: nix-state.hh:59
A RegistryInput that opens a PkgDb associated with a flake.
Definition: input.hh:55
nix::ref< PkgDbReadOnly > getDbReadOnly() const
Definition: input.hh:160
A SQLite3 database used to cache derivation/package information about a single locked flake.
Definition: read.hh:135
A collection of data associated with an environment and its state.
Definition: environment.hh:97
Lockfile createLockfile()
Create a new lockfile from manifest.
Definition: environment.cc:692
nix::ref< Registry< pkgdb::PkgDbInputFactory > > getPkgDbRegistry()
Lazily initialize and get the combined registry's DBs.
Definition: environment.cc:94
std::optional< ManifestRaw > getOldManifestRaw() const
Get the old manifest from oldLockfile if it exists.
Definition: environment.cc:116
std::vector< System > getSystems() const
Get the set of supported systems.
Definition: environment.hh:346
const pkgdb::PkgQueryArgs & getCombinedBaseQueryArgs()
Get a base set of flox::pkgdb::PkgQueryArgs from combined options.
Definition: environment.cc:320
std::optional< LockedInputRaw > getGroupInput(const InstallDescriptors &group, const Lockfile &oldLockfile, const System &system) const
Get locked input from a lockfile to try to use to resolve a group of packages.
Definition: environment.cc:387
const Options & getCombinedOptions()
Get a merged form of oldLockfile or globalManifest ( if available ) and manifest options.
Definition: environment.cc:288
EnvironmentManifest manifest
Definition: environment.hh:109
ResolutionResult tryResolveGroup(const InstallDescriptors &group, const System &system)
Try to resolve a group of descriptors.
Definition: environment.cc:463
std::optional< GlobalManifest > globalManifest
Definition: environment.hh:106
std::variant< bool, std::vector< InstallID > > Upgrades
Indicator for lockfile upgrade operations.
Definition: environment.hh:122
bool groupIsLocked(const InstallDescriptors &group, const Lockfile &oldLockfile, const System &system) const
Check if lock from @ oldLockfile can be reused for a group.
Definition: environment.cc:146
std::variant< InstallID, SystemPackages > tryResolveGroupIn(const InstallDescriptors &group, const pkgdb::PkgDbInput &input, const System &system)
Try to resolve a group of descriptors in a given package database.
Definition: environment.cc:550
std::optional< pkgdb::row_id > tryResolveDescriptorIn(const ManifestDescriptor &descriptor, const pkgdb::PkgDbInput &input, const System &system)
Try to resolve a descriptor in a given package database.
Definition: environment.cc:334
RegistryRaw & getCombinedRegistryRaw()
Get a merged form of oldLockfile ( if available ), globalManifest ( if available ) and manifest regis...
Definition: environment.cc:52
std::optional< LockfileRaw > lockfileRaw
Definition: environment.hh:127
void lockSystem(const System &system)
Lock all descriptors for a given system. This is a helper function of flox::resolver::Environment::cr...
Definition: environment.cc:603
Upgrades upgrades
Definition: environment.hh:124
std::vector< InstallDescriptors > getUnlockedGroups(const System &system)
Get groups that need to be locked as opposed to reusing locks from oldLockfile.
Definition: environment.cc:240
std::vector< InstallDescriptors > getLockedGroups(const System &system)
Get groups with locks that can be reused from oldLockfile.
Definition: environment.cc:263
std::optional< Lockfile > oldLockfile
Definition: environment.hh:112
std::optional< RegistryRaw > lockedRegistry
Definition: environment.hh:136
A locked representation of an environment.
Definition: lockfile.hh:238
std::vector< System > getSystems() const
Get the list of systems requested by the manifest.
Definition: manifest.hh:170
std::variant< ResolutionFailure, SystemPackages > ResolutionResult
Either a set of resolved packages ( for a given system ) or a memo indicating that resolution failed ...
Definition: environment.hh:81
std::vector< std::pair< InstallID, std::string > > ResolutionFailure
A pair of install ID and locked flake URLs used to record failed resolution attempts for a given desc...
Definition: environment.hh:68
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.
A lockfile representing a resolved environment.
An abstract description of an environment in its unresolved state. This representation is intended fo...
An abstract description of an environment in its unresolved state.
std::unordered_map< InstallID, ManifestDescriptor > InstallDescriptors
A map of install IDs to manifest descriptors.
Definition: manifest.hh:259
uint64_t row_id
Definition: pkg-query.hh:48
Interfaces for use by flox.
Definition: command.cc:29
@ EC_RESOLUTION_FAILURE
Definition: exceptions.hh:77
std::string System
A system pair indicating architecture and platform.
Definition: types.hh:62
Manages a nix runtime state blob with associated helpers.
Interfaces for constructing complex ‘Packages’ queries.
A set of user inputs used to set input preferences during search and resolution.
A locked package's installable URI.
Definition: lockfile.hh:125
A set of user defined requirements describing a package/dependency.
Definition: descriptor.hh:221
A set of options that apply to an entire environment.
Definition: manifest-raw.hh:73
Miscellaneous typedefs and aliases.