Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
input.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/pkgdb/input.hh
4 *
5 * @brief A @a RegistryInput that opens a @a PkgDb associated with a flake.
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <filesystem>
13#include <memory>
14#include <nlohmann/json_fwd.hpp>
15#include <optional>
16#include <string>
17#include <string_view>
18#include <type_traits>
19#include <utility>
20#include <vector>
21
22#include <nix/flake/flake.hh>
23#include <nix/flake/flakeref.hh>
24#include <nix/ref.hh>
25
27#include "flox/core/types.hh"
28#include "flox/flox-flake.hh"
30#include "flox/pkgdb/read.hh"
31#include "flox/registry.hh"
32
33
34/* -------------------------------------------------------------------------- */
35
36/* Forward declare */
37namespace nix {
38class Store;
39}
40
41
42/* -------------------------------------------------------------------------- */
43
44namespace flox::pkgdb {
45
46/* -------------------------------------------------------------------------- */
47
48/* Forward declare */
49class PkgDb;
50
51/* -------------------------------------------------------------------------- */
52
55{
56
57private:
58
59 /* Provided by `FloxFlakeInput':
60 * nix::ref<nix::FlakeRef> flakeRef
61 * nix::ref<nix::Store> store
62 * std::shared_ptr<FloxFlake> flake
63 * std::optional<std::vector<Subtree>> enabledSubtrees
64 */
65
67 std::filesystem::path dbPath;
68
73 std::shared_ptr<PkgDbReadOnly> dbRO;
74
79 std::shared_ptr<PkgDb> dbRW;
80
82 std::optional<std::string> name;
83
84
98 void
99 init();
100
101
102public:
103
109 {};
110
111
122 PkgDbInput( nix::ref<nix::Store> & store,
123 const RegistryInput & input,
124 std::filesystem::path dbPath,
125 const db_path_tag & /* unused */
126 ,
127 const std::string & name = "" )
128 : FloxFlakeInput( store, input )
129 , dbPath( std::move( dbPath ) )
130 , name( name.empty() ? std::nullopt : std::make_optional( name ) )
131 {
132 this->init();
133 }
134
144 PkgDbInput( nix::ref<nix::Store> & store,
145 const RegistryInput & input,
146 const std::filesystem::path & cacheDir = getPkgDbCachedir(),
147 const std::string & name = "" )
148 : FloxFlakeInput( store, input )
149 , dbPath( genPkgDbName( this->getFlake()->lockedFlake.getFingerprint(),
150 cacheDir ) )
151 , name( name.empty() ? std::nullopt : std::make_optional( name ) )
152 {
153 this->init();
154 }
155
159 [[nodiscard]] nix::ref<PkgDbReadOnly>
161 {
162 return static_cast<nix::ref<PkgDbReadOnly>>( this->dbRO );
163 }
164
169 [[nodiscard]] nix::ref<PkgDb>
171
173 void
175 {
176 this->dbRW = nullptr;
177 }
178
180 [[nodiscard]] std::filesystem::path
181 getDbPath() const
182 {
183 return this->dbPath;
184 }
185
197 void
198 scrapePrefix( const flox::AttrPath & prefix );
199
205 void
206 scrapeSystems( const std::vector<System> & systems );
207
209 void
210 setName( std::string_view name )
211 {
212 this->name = name;
213 }
214
219 [[nodiscard]] std::string
221 {
222 return this->name.value_or(
223 this->getFlake()->lockedFlake.flake.lockedRef.to_string() );
224 }
225
227 [[nodiscard]] nlohmann::json
228 getRowJSON( row_id row );
229
230
231}; /* End struct `PkgDbInput' */
232
233
234/* -------------------------------------------------------------------------- */
235
238{
239
240private:
241
242 nix::ref<nix::Store> store;
243 std::filesystem::path cacheDir;
246public:
247
248 using input_type = PkgDbInput;
249
251 explicit PkgDbInputFactory( nix::ref<nix::Store> & store,
252 std::filesystem::path cacheDir
253 = getPkgDbCachedir() )
254 : store( store ), cacheDir( std::move( cacheDir ) )
255 {}
256
258 [[nodiscard]] std::shared_ptr<PkgDbInput>
259 mkInput( const std::string & name, const RegistryInput & input )
260 {
261 return std::make_shared<PkgDbInput>( this->store,
262 input,
263 this->cacheDir,
264 name );
265 }
266
267
268}; /* End class `PkgDbInputFactory' */
269
270
272
273
274/* -------------------------------------------------------------------------- */
275
282class PkgDbRegistryMixin : virtual protected NixStoreMixin
283{
284
285private:
286
287 /* From `NixStoreMixin':
288 * std::shared_ptr<nix::Store> store
289 */
290
291 std::shared_ptr<Registry<PkgDbInputFactory>> registry;
292
293 // TODO: Implement
295 bool force = false;
296
297
298protected:
299
300 /* From `NixStoreMixin':
301 * nix::ref<nix::Store> getStore()
302 */
303
304
306 void
307 initRegistry();
308
315 void
317
319 [[nodiscard]] virtual RegistryRaw
321 = 0;
322
324 [[nodiscard]] virtual const std::vector<System> &
326 = 0;
327
328
329public:
330
336 [[nodiscard]] nix::ref<Registry<PkgDbInputFactory>>
338
340 [[nodiscard]] bool
342 {
343 return this->force;
344 }
345
347 void
348 setPkgDbForced( bool force )
349 {
350 this->force = force;
351 }
352
353
354}; /* End class `PkgDbRegistryMixin' */
355
356
357/* -------------------------------------------------------------------------- */
358
359
360} // namespace flox::pkgdb
361
362
363/* -------------------------------------------------------------------------- *
364 *
365 *
366 *
367 * ========================================================================== */
A simple flox::RegistryInput that opens a nix evaluator for a flake.
Definition: registry.hh:623
nix::ref< FloxFlake > getFlake()
Get a handle for a flake with a nix evaluator.
Definition: registry.cc:260
nix::ref< nix::Store > store
Definition: registry.hh:633
Mixin which provides a lazy handle to a nix store connection.
Definition: nix-state.hh:59
Factory for PkgDbInput.
Definition: input.hh:238
PkgDbInputFactory(nix::ref< nix::Store > &store, std::filesystem::path cacheDir=getPkgDbCachedir())
Construct a factory using a nix evaluator.
Definition: input.hh:251
std::shared_ptr< PkgDbInput > mkInput(const std::string &name, const RegistryInput &input)
Construct an input from a RegistryInput.
Definition: input.hh:259
nix::ref< nix::Store > store
Definition: input.hh:242
std::filesystem::path cacheDir
Definition: input.hh:243
A RegistryInput that opens a PkgDb associated with a flake.
Definition: input.hh:55
nlohmann::json getRowJSON(row_id row)
Get a JSON representation of a row in the database.
Definition: input.cc:177
std::shared_ptr< PkgDbReadOnly > dbRO
Definition: input.hh:73
void closeDbReadWrite()
Close the read/write database connection if it is open.
Definition: input.hh:174
PkgDbInput(nix::ref< nix::Store > &store, const RegistryInput &input, std::filesystem::path dbPath, const db_path_tag &, const std::string &name="")
Construct a PkgDbInput from a RegistryInput and a path to the database.
Definition: input.hh:122
nix::ref< PkgDbReadOnly > getDbReadOnly() const
Definition: input.hh:160
std::shared_ptr< PkgDb > dbRW
Definition: input.hh:79
std::filesystem::path dbPath
Definition: input.hh:67
void scrapeSystems(const std::vector< System > &systems)
Scrape all prefixes indicated by InputPreferences for systems.
Definition: input.cc:157
void setName(std::string_view name)
Add/set a shortname for this input.
Definition: input.hh:210
std::optional< std::string > name
Definition: input.hh:82
PkgDbInput(nix::ref< nix::Store > &store, const RegistryInput &input, const std::filesystem::path &cacheDir=getPkgDbCachedir(), const std::string &name="")
Construct a PkgDbInput from a RegistryInput and a path to the directory where the database should be ...
Definition: input.hh:144
std::filesystem::path getDbPath() const
Definition: input.hh:181
std::string getNameOrURL()
Get an identifier for this input.
Definition: input.hh:220
nix::ref< PkgDb > getDbReadWrite()
Open a read/write database connection if one is not open, and return a handle.
Definition: input.cc:94
void init()
Prepare database handles for use.
Definition: input.cc:44
void scrapePrefix(const flox::AttrPath &prefix)
Ensure that an attribute path prefix has been scraped.
Definition: input.cc:108
Provides a registry of PkgDb managers.
Definition: input.hh:283
nix::ref< Registry< PkgDbInputFactory > > getPkgDbRegistry()
Get the set of package databases to resolve in.
Definition: input.cc:219
void setPkgDbForced(bool force)
Set whether DBs will be regenerated from scratch.
Definition: input.hh:348
bool force
Definition: input.hh:295
virtual const std::vector< System > & getSystems()=0
void initRegistry()
Initialize registry member from params.registry.
Definition: input.cc:189
virtual RegistryRaw getRegistryRaw()=0
bool isPkgDbForced() const
Whether DBs will be regenerated from scratch.
Definition: input.hh:341
void scrapeIfNeeded()
Lazily perform scraping on input flakes.
Definition: input.cc:205
Restricts types to those which can construct flox::RegistryInput values.
Definition: registry.hh:223
Defines a convenience wrapper that provides various operations on a flake.
Interfaces for caching package metadata in SQLite3 databases.
Definition: command.cc:44
std::filesystem::path genPkgDbName(const Fingerprint &fingerprint, const std::filesystem::path &cacheDir)
Get an absolute path to the ‘PkgDb’ for a given fingerprint hash.
Definition: read.cc:76
std::filesystem::path getPkgDbCachedir()
Get the default pkgdb cache directory to save databases.
Definition: read.cc:53
uint64_t row_id
Definition: pkg-query.hh:48
std::vector< std::string > AttrPath
A list of key names addressing a location in a nested JSON-like object.
Definition: types.hh:33
Manages a nix runtime state blob with associated helpers.
Interfaces for constructing complex ‘Packages’ queries.
Interfaces for reading a SQLite3 package set database.
A set of user inputs used to set input preferences during search and resolution.
Preferences associated with a named registry input.
Definition: registry.hh:135
A set of user inputs used to set input preferences during search and resolution.
Definition: registry.hh:297
Tag used to disambiguate construction with database path and cache directory path.
Definition: input.hh:109
Miscellaneous typedefs and aliases.