Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
fetchers.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/types.hh"
5#include "nix/util/hash.hh"
9#include "nix/util/url.hh"
10
11#include <memory>
12#include <nlohmann/json_fwd.hpp>
13
14#include "nix/util/ref.hh"
15
16namespace nix {
17class Store;
18class StorePath;
19struct SourceAccessor;
20} // namespace nix
21
22namespace nix::fetchers {
23
24struct InputScheme;
25
26struct Settings;
27
35struct Input
36{
37 friend struct InputScheme;
38
39 std::shared_ptr<InputScheme> scheme; // note: can be null
40 Attrs attrs;
41
45 mutable std::optional<std::optional<std::string>> cachedFingerprint;
46
47public:
53 static Input fromURL(const std::string & url, bool requireTree = true);
54
55 static Input fromURL(const ParsedURL & url, bool requireTree = true);
56
62 static Input fromAttrs(Attrs && attrs);
63
64 ParsedURL toURL() const;
65
66 std::string toURLString(const StringMap & extraQuery = {}) const;
67
68 std::string to_string() const;
69
70 Attrs toAttrs() const;
71
76 bool isDirect() const;
77
83 bool isLocked(const Settings & settings) const;
84
89 std::optional<std::filesystem::path> isRelative() const;
90
101 bool isFinal() const;
102
103 bool operator==(const Input & other) const;
104
105 bool operator<(const Input & other) const
106 {
107 return attrs < other.attrs;
108 }
109
110 bool contains(const Input & other) const;
111
116 std::pair<StorePath, Input> fetchToStore(const Settings & settings, Store & store) const;
117
129 static void checkLocks(Input specified, Input & result);
130
136 std::pair<ref<SourceAccessor>, Input> getAccessor(const Settings & settings, Store & store) const;
137
138private:
139
140 std::pair<ref<SourceAccessor>, Input> getAccessorUnchecked(const Settings & settings, Store & store) const;
141
142public:
143
144 Input applyOverrides(std::optional<std::string> ref, std::optional<Hash> rev) const;
145
146 void clone(const Settings & settings, Store & store, const std::filesystem::path & destDir) const;
147
148 std::optional<std::filesystem::path> getSourcePath() const;
149
154 void putFile(const CanonPath & path, std::string_view contents, std::optional<std::string> commitMsg) const;
155
156 std::string getName() const;
157
158 StorePath computeStorePath(Store & store) const;
159
160 // Convenience functions for common attributes.
161 std::string getType() const;
162 std::optional<Hash> getNarHash() const;
163 std::optional<std::string> getRef() const;
164 std::optional<Hash> getRev() const;
165 std::optional<uint64_t> getRevCount() const;
166 std::optional<time_t> getLastModified() const;
167
176 std::optional<std::string> getFingerprint(Store & store) const;
177};
178
189{
190 virtual ~InputScheme() {}
191
192 virtual std::optional<Input> inputFromURL(const ParsedURL & url, bool requireTree) const = 0;
193
194 virtual std::optional<Input> inputFromAttrs(const Attrs & attrs) const = 0;
195
203 virtual std::string_view schemeName() const = 0;
204
208 virtual std::string schemeDescription() const = 0;
209
210 // TODO remove these defaults
212 {
213 const char * type = "String";
214 bool required = true;
215 const char * doc = "";
216 };
217
225 virtual const std::map<std::string, AttributeInfo> & allowedAttrs() const = 0;
226
227 virtual ParsedURL toURL(const Input & input) const;
228
229 virtual Input applyOverrides(const Input & input, std::optional<std::string> ref, std::optional<Hash> rev) const;
230
231 virtual void
232 clone(const Settings & settings, Store & store, const Input & input, const std::filesystem::path & destDir) const;
233
234 virtual std::optional<std::filesystem::path> getSourcePath(const Input & input) const;
235
236 virtual void putFile(
237 const Input & input,
238 const CanonPath & path,
239 std::string_view contents,
240 std::optional<std::string> commitMsg) const;
241
242 virtual std::pair<ref<SourceAccessor>, Input>
243 getAccessor(const Settings & settings, Store & store, const Input & input) const = 0;
244
248 virtual std::optional<ExperimentalFeature> experimentalFeature() const;
249
250 virtual bool isDirect(const Input & input) const
251 {
252 return true;
253 }
254
255 virtual std::optional<std::string> getFingerprint(Store & store, const Input & input) const
256 {
257 return std::nullopt;
258 }
259
260 virtual bool isLocked(const Settings & settings, const Input & input) const
261 {
262 return false;
263 }
264
265 virtual std::optional<std::filesystem::path> isRelative(const Input & input) const
266 {
267 return std::nullopt;
268 }
269
274 virtual std::optional<ParsedURL> localRepoURL(const std::filesystem::path & path) const
275 {
276 return std::nullopt;
277 }
278
279 virtual std::optional<std::string>
280 getAccessToken(const fetchers::Settings & settings, const std::string & host, const std::string & url) const
281 {
282 return {};
283 }
284};
285
286void registerInputScheme(std::shared_ptr<InputScheme> && fetcher);
287
288using InputSchemeMap = std::map<std::string_view, std::shared_ptr<InputScheme>>;
289
293const InputSchemeMap & getAllInputSchemes();
294
300std::optional<ParsedURL> getLocalRepoURL(const std::filesystem::path & path);
301
303{
304 std::string type = "ssh-ed25519";
305 std::string key;
306
307 auto operator<=>(const PublicKey &) const = default;
308};
309
310std::string publicKeys_to_string(const std::vector<PublicKey> &);
311
312} // namespace nix::fetchers
313
std::map< std::string, Attr > Attrs
Definition attrs.hh:68
Definition canon-path.hh:50
Definition globals.hh:106
Definition path.hh:33
Definition store-api.hh:397
Definition ref.hh:58
Definition url.hh:22
Definition source-accessor.hh:50
Definition fetchers.hh:189
virtual const std::map< std::string, AttributeInfo > & allowedAttrs() const =0
virtual std::optional< ParsedURL > localRepoURL(const std::filesystem::path &path) const
Definition fetchers.hh:274
virtual std::string_view schemeName() const =0
virtual std::optional< ExperimentalFeature > experimentalFeature() const
Definition fetchers.cc:531
virtual std::string schemeDescription() const =0
Definition fetchers.hh:36
bool isDirect() const
Definition fetchers.cc:170
std::optional< std::optional< std::string > > cachedFingerprint
Definition fetchers.hh:45
std::pair< ref< SourceAccessor >, Input > getAccessor(const Settings &settings, Store &store) const
Definition fetchers.cc:301
static Input fromURL(const std::string &url, bool requireTree=true)
Definition fetchers.cc:53
std::optional< std::string > getFingerprint(Store &store) const
Definition fetchers.cc:132
std::optional< std::filesystem::path > isRelative() const
Definition fetchers.cc:185
static Input fromAttrs(Attrs &&attrs)
Definition fetchers.cc:88
bool isLocked(const Settings &settings) const
Definition fetchers.cc:175
static void checkLocks(Input specified, Input &result)
Definition fetchers.cc:244
void putFile(const CanonPath &path, std::string_view contents, std::optional< std::string > commitMsg) const
Definition fetchers.cc:411
bool isFinal() const
Definition fetchers.cc:180
Definition fetchers.hh:303
Definition fetch-settings.hh:27
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26