Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
flake-package.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/flake-package.hh
4 *
5 * @brief Provides a @a flox::Package implementation which are pulled from
6 * evaluation of a `nix` flake.
7 *
8 *
9 * -------------------------------------------------------------------------- */
10
11#pragma once
12
13#include <memory>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19#include <nix/eval-cache.hh>
20#include <nix/names.hh>
21#include <nix/ref.hh>
22#include <nix/symbol-table.hh>
23
25#include "flox/core/types.hh"
26#include "flox/package.hh"
27
28
29/* -------------------------------------------------------------------------- */
30
31namespace flox {
32
33/* -------------------------------------------------------------------------- */
34
35/* Forward declare a friend. */
36namespace pkgdb {
37class PkgDb;
38}
39
40/* -------------------------------------------------------------------------- */
41
46class FlakePackage : public Package
47{
48
49public:
50
51 friend class pkgdb::PkgDb;
52
53private:
54
55 Cursor _cursor;
56 AttrPath _pathS;
57
58 bool _hasMetaAttr = false;
59 bool _hasPnameAttr = false;
60 bool _hasVersionAttr = false;
61
62 std::string _fullName;
63 std::string _pname;
64 std::string _version;
65 std::optional<std::string> _semver;
66 System _system;
67 Subtree _subtree;
68 std::optional<std::string> _license;
69
70
71 void
72 init( bool checkDrv = true );
73
74
75 /* --------------------------------------------------------------------------
76 */
77
78public:
79
80 virtual ~FlakePackage() = default;
81
82 FlakePackage( Cursor cursor, const AttrPath & path, bool checkDrv = true )
83 : _cursor( cursor )
84 , _pathS( path )
85 , _fullName( cursor->getAttr( "name" )->getString() )
86 {
87 {
88 nix::DrvName dname( this->_fullName );
89 this->_pname = dname.name;
90 this->_version = dname.version;
91 }
92 this->init( checkDrv );
93 }
94
95
96 FlakePackage( Cursor cursor, nix::SymbolTable * symtab, bool checkDrv = true )
97 : _cursor( cursor ), _fullName( cursor->getAttr( "name" )->getString() )
98 {
99 {
100 nix::DrvName dname( this->_fullName );
101 this->_pname = dname.name;
102 this->_version = dname.version;
103 }
104 for ( auto & p : symtab->resolve( cursor->getAttrPath() ) )
105 {
106 this->_pathS.push_back( p );
107 }
108 this->init( checkDrv );
109 }
110
111
112 /* --------------------------------------------------------------------------
113 */
114
115 std::vector<std::string>
116 getOutputsToInstall() const override;
117 std::optional<bool>
118 isBroken() const override;
119 std::optional<bool>
120 isUnfree() const override;
121
123 getPathStrs() const override
124 {
125 return this->_pathS;
126 }
127 std::string
128 getFullName() const override
129 {
130 return this->_fullName;
131 }
132 std::string
133 getPname() const override
134 {
135 return this->_pname;
136 }
137 Cursor
138 getCursor() const
139 {
140 return this->_cursor;
141 }
142 Subtree
143 getSubtreeType() const override
144 {
145 return this->_subtree;
146 }
147
148 nix::DrvName
149 getParsedDrvName() const override
150 {
151 return nix::DrvName( this->_fullName );
152 }
153
154 std::optional<std::string>
155 getVersion() const override
156 {
157 if ( this->_version.empty() ) { return std::nullopt; }
158 else { return this->_version; }
159 }
160
161 std::optional<std::string>
162 getSemver() const override
163 {
164 return this->_semver;
165 }
166
167 std::optional<std::string>
168 getLicense() const override
169 {
170 if ( this->_license.has_value() ) { return this->_license; }
171 else { return std::nullopt; }
172 }
173
174 std::vector<std::string>
175 getOutputs() const override
176 {
177 MaybeCursor o = this->_cursor->maybeGetAttr( "outputs" );
178 if ( o == nullptr ) { return { "out" }; }
179 else { return o->getListOfStrings(); }
180 }
181
182 std::optional<std::string>
183 getDescription() const override
184 {
185 if ( ! this->_hasMetaAttr ) { return std::nullopt; }
187 = this->_cursor->getAttr( "meta" )->maybeGetAttr( "description" );
188 if ( l == nullptr ) { return std::nullopt; }
189 try
190 {
191 return l->getString();
192 }
193 catch ( ... )
194 {
195 return std::nullopt;
196 }
197 }
198
199
200}; /* End class `FlakePackage' */
201
202
203/* -------------------------------------------------------------------------- */
204
213 "error initializing FlakePackage" )
217/* -------------------------------------------------------------------------- */
218
219} // namespace flox
220
221
222/* -------------------------------------------------------------------------- *
223 *
224 *
225 *
226 * ========================================================================== */
A flox::Package implementation which are pulled from evaluation of a nix flake.
Definition: flake-package.hh:47
std::string getPname() const override
Definition: flake-package.hh:133
std::optional< std::string > getLicense() const override
Definition: flake-package.hh:168
std::vector< std::string > getOutputsToInstall() const override
Definition: flake-package.cc:108
Subtree getSubtreeType() const override
Definition: flake-package.hh:143
std::optional< bool > isBroken() const override
Definition: flake-package.cc:129
std::optional< std::string > getVersion() const override
Definition: flake-package.hh:155
std::optional< std::string > getSemver() const override
Definition: flake-package.hh:162
std::optional< std::string > getDescription() const override
Definition: flake-package.hh:183
std::vector< std::string > getOutputs() const override
Definition: flake-package.hh:175
nix::DrvName getParsedDrvName() const override
Definition: flake-package.hh:149
AttrPath getPathStrs() const override
Definition: flake-package.hh:123
std::optional< bool > isUnfree() const override
Definition: flake-package.cc:146
std::string getFullName() const override
Definition: flake-package.hh:128
An exception thrown when initializing a flox::FlakePackage.
Abstract representation of a "package", analogous to a Nix ‘derivation’.
Definition: package.hh:44
A SQLite3 database used to cache derivation/package information about a single locked flake.
Definition: write.hh:37
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
Interfaces for use by flox.
Definition: command.cc:29
std::shared_ptr< nix::eval_cache::AttrCursor > MaybeCursor
A std::shared_ptr<nix::eval_cache::AttrCursor> which may be nullptr.
Definition: types.hh:47
@ EC_PACKAGE_INIT
Definition: exceptions.hh:55
nix::ref< nix::eval_cache::AttrCursor > Cursor
A non-nullptr std::shared_ptr<nix::eval_cache::AttrCursor>.
Definition: types.hh:50
std::string System
A system pair indicating architecture and platform.
Definition: types.hh:62
std::vector< std::string > AttrPath
A list of key names addressing a location in a nested JSON-like object.
Definition: types.hh:33
Abstract representation of a package.
Miscellaneous typedefs and aliases.