Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
fetchers.hh
Go to the documentation of this file.
1#pragma once
3
7#include "lix/libutil/hash.hh"
11#include "lix/libutil/url.hh"
12#include "lix/libutil/ref.hh"
14
15#include <kj/async.h>
16#include <memory>
17
18namespace nix { class Store; }
19
20namespace nix::fetchers {
21
22struct Tree
23{
24 Path actualPath;
25 StorePath storePath;
26};
27
28struct InputScheme;
29
38struct Input
39{
40 friend struct InputScheme;
41
42 std::shared_ptr<InputScheme> scheme; // note: can be null
43 Attrs attrs;
44 bool locked = false;
45 bool direct = true;
46
50 std::optional<Path> parent;
51
52public:
53 static Input fromURL(const std::string & url, bool requireTree = true);
54
55 static Input fromURL(const ParsedURL & url, bool requireTree = true);
56
57 static Input fromAttrs(Attrs && attrs);
58
59 ParsedURL toURL() const;
60
61 std::string toURLString(const std::map<std::string, std::string> & extraQuery = {}) const;
62
63 std::string to_string() const;
64
65 Attrs toAttrs() const;
66
71 bool isDirect() const { return direct; }
72
77 bool isLocked() const { return locked; }
78
86 bool hasAllInfo() const;
87
88 bool operator ==(const Input & other) const;
89
90 bool contains(const Input & other) const;
91
96 kj::Promise<Result<std::pair<Tree, Input>>> fetch(ref<Store> store) const;
97
98 Input applyOverrides(
99 std::optional<std::string> ref,
100 std::optional<Hash> rev) const;
101
102 void clone(const Path & destDir) const;
103
104 std::optional<Path> getSourcePath() const;
105
110 void putFile(
111 const CanonPath & path,
112 std::string_view contents,
113 std::optional<std::string> commitMsg) const;
114
115 std::string getName() const;
116
117 StorePath computeStorePath(Store & store) const;
118
119 // Convenience functions for common attributes.
120 std::string getType() const;
121 std::optional<Hash> getNarHash() const;
122 std::optional<std::string> getRef() const;
123 std::optional<Hash> getRev() const;
124 std::optional<uint64_t> getRevCount() const;
125 std::optional<time_t> getLastModified() const;
126};
127
128
139{
140 virtual ~InputScheme()
141 { }
142
143 virtual std::optional<Input> inputFromURL(const ParsedURL & url, bool requireTree) const = 0;
144
145 virtual std::optional<Input> inputFromAttrs(const Attrs & attrs) const = 0;
146
147 virtual ParsedURL toURL(const Input & input) const;
148
149 virtual bool hasAllInfo(const Input & input) const = 0;
150
151 virtual Input applyOverrides(
152 const Input & input,
153 std::optional<std::string> ref,
154 std::optional<Hash> rev) const;
155
156 virtual void clone(const Input & input, const Path & destDir) const;
157
158 virtual std::optional<Path> getSourcePath(const Input & input) const;
159
160 virtual void putFile(
161 const Input & input,
162 const CanonPath & path,
163 std::string_view contents,
164 std::optional<std::string> commitMsg) const;
165
166 virtual kj::Promise<Result<std::pair<StorePath, Input>>>
167 fetch(ref<Store> store, const Input & input) = 0;
168
169 /*
170 * By default, libfetchers considers inputs as locked if a `rev`
171 * is specified. This however doesn't make any sense for `path` inputs,
172 * so schemes can indicate that a `rev` on its own is not sufficient.
173 */
174 virtual bool isLockedByRev() const { return true; }
175
176protected:
177 void emplaceURLQueryIntoAttrs(
178 const ParsedURL & parsedURL,
179 Attrs & attrs,
180 const StringSet & numericParams,
181 const StringSet & booleanParams) const
182 {
183 for (auto &[name, value] : parsedURL.query) {
184 if (name == "url") {
185 throw BadURL(
186 "URL '%s' must not override url via query param!",
187 parsedURL.to_string()
188 );
189 } else if (numericParams.count(name) != 0) {
190 if (auto n = string2Int<uint64_t>(value)) {
191 attrs.insert_or_assign(name, *n);
192 } else {
193 throw BadURL(
194 "URL '%s' has non-numeric parameter '%s'",
195 parsedURL.to_string(),
196 name
197 );
198 }
199 } else if (booleanParams.count(name) != 0) {
200 attrs.emplace(name, Explicit<bool> { value == "1" });
201 } else {
202 attrs.emplace(name, value);
203 }
204 }
205 }
206};
207
208void registerInputScheme(std::shared_ptr<InputScheme> && fetcher);
209
210void initLibFetchers();
211
213{
214 StorePath storePath;
215 std::string etag;
216 std::string effectiveUrl;
217 std::optional<std::string> immutableUrl;
218};
219
220kj::Promise<Result<DownloadFileResult>> downloadFile(
221 ref<Store> store,
222 const std::string & url,
223 const std::string & name,
224 bool locked,
225 Headers headers = {},
227
229{
230 Tree tree;
231 time_t lastModified;
232 std::optional<std::string> immutableUrl;
233};
234
235kj::Promise<Result<DownloadTarballResult>> downloadTarball(
236 ref<Store> store,
237 const std::string & url,
238 const std::string & name,
239 bool locked,
240 const Headers & headers = {});
241
242}
Definition canon-path.hh:28
Definition path.hh:21
Definition store-api.hh:195
Definition ref.hh:19
FileIngestionMethod
Definition content-address.hh:38
@ Flat
Definition content-address.hh:42
Definition types.hh:40
Definition url.hh:10
Definition fetchers.hh:213
Definition fetchers.hh:229
Definition fetchers.hh:139
Definition fetchers.hh:39
bool isDirect() const
Definition fetchers.hh:71
bool isLocked() const
Definition fetchers.hh:77
std::optional< Path > parent
Definition fetchers.hh:50
bool hasAllInfo() const
Definition fetchers.cc:105
void putFile(const CanonPath &path, std::string_view contents, std::optional< std::string > commitMsg) const
Definition fetchers.cc:234
kj::Promise< Result< std::pair< Tree, Input > > > fetch(ref< Store > store) const
Definition fetchers.cc:125
Definition fetchers.hh:23
std::string Path
Definition types.hh:28