Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
worker-protocol.hh
Go to the documentation of this file.
1#pragma once
3
4#include <algorithm>
5#include <chrono>
6#include <compare>
7
10
11namespace nix {
12
13#define WORKER_MAGIC_1 0x6e697863
14#define WORKER_MAGIC_2 0x6478696f
15
16/* Note: you generally shouldn't change the protocol version. Define a
17 new `WorkerProto::Feature` instead. */
18#define PROTOCOL_VERSION (1 << 8 | 39)
19#define MINIMUM_PROTOCOL_VERSION (1 << 8 | 18)
20#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00)
21#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff)
22#define STDERR_NEXT 0x6f6c6d67
23#define STDERR_READ 0x64617461 // data needed from source
24#define STDERR_WRITE 0x64617416 // data for sink
25#define STDERR_LAST 0x616c7473
26#define STDERR_ERROR 0x63787470
27#define STDERR_START_ACTIVITY 0x53545254
28#define STDERR_STOP_ACTIVITY 0x53544f50
29#define STDERR_RESULT 0x52534c54
30
31struct StoreDirConfig;
32struct Source;
33
34// items being serialised
35struct DerivedPath;
36struct BuildResult;
37struct KeyedBuildResult;
38struct ValidPathInfo;
40struct DrvOutput;
42struct Realisation;
43enum BuildMode : uint8_t;
44enum TrustedFlag : bool;
45enum class GCAction;
46
54{
58 enum struct Op : uint64_t;
59
69 struct Version
70 {
71 struct Number
72 {
73 unsigned int major;
74 uint8_t minor;
75
76 constexpr auto operator<=>(const Number &) const = default;
77
82 constexpr unsigned int toWire() const
83 {
84 return (major << 8) | minor;
85 }
86
90 static constexpr Number fromWire(unsigned int wire)
91 {
92 return {
93 .major = (wire & 0xff00) >> 8,
94 .minor = static_cast<uint8_t>(wire & 0x00ff),
95 };
96 };
97 } number;
98
99 using Feature = std::string;
100 using FeatureSet = std::set<Feature, std::less<>>;
101
102 FeatureSet features = {};
103
104 bool operator==(const Version &) const = default;
105
111 std::partial_ordering operator<=>(const Version & other) const;
112 };
113
118 static const Version latest;
119
120 static const Version minimum;
121
126 static const Version builderRpcV0;
127
132 static constexpr std::string_view featureRealisationWithPath = "realisation-with-path-not-hash";
133
137 static constexpr std::string_view featureDeleteDeadSpecificReferrers = "delete-dead-specific-referrers";
138
142 static constexpr std::string_view featureDisableSetOptions = "disable-set-options";
143
147 static constexpr std::string_view featureAddToStoreScanning = "add-to-store-scanning";
148
152 static constexpr std::string_view featureSubmitOutput = "submit-output";
153
154 /*
155 * Feature for the addTempRoots() operation.
156 */
157 static constexpr std::string_view featureAddTempRoots = "addTempRoots";
158
163 struct ReadConn
164 {
165 Source & from;
166 const Version & version;
167 };
168
174 {
175 Sink & to;
176 const Version & version;
177 };
178
184 struct BasicConnection;
187
191 struct ClientHandshakeInfo;
192
199 template<typename T>
200 struct Serialise;
201 // This is the definition of `Serialise` we *want* to put here, but
202 // do not do so.
203 //
204 // The problem is that if we do so, C++ will think we have
205 // seralisers for *all* types. We don't, of course, but that won't
206 // cause an error until link time. That makes for long debug cycles
207 // when there is a missing serialiser.
208 //
209 // By not defining it globally, and instead letting individual
210 // serialisers specialise the type, we get back the compile-time
211 // errors we would like. When no serialiser exists, C++ sees an
212 // abstract "incomplete" type with no definition, and any attempt to
213 // use `to` or `from` static methods is a compile-time error because
214 // they don't exist on an incomplete type.
215 //
216 // This makes for a quicker debug cycle, as desired.
217#if 0
218 {
219 static T read(const StoreDirConfig & store, ReadConn conn);
220 static void write(const StoreDirConfig & store, WriteConn conn, const T & t);
221 };
222#endif
223
228 template<typename T>
229 static void write(const StoreDirConfig & store, WriteConn conn, const T & t)
230 {
231 WorkerProto::Serialise<T>::write(store, conn, t);
232 }
233};
234
235enum struct WorkerProto::Op : uint64_t {
236 IsValidPath = 1,
237 // HasSubstitutes = 3, // removed
238 // QueryPathHash = 4, // removed
239 // QueryReferences = 5, // removed
240 QueryReferrers = 6,
241 AddToStore = 7,
242 AddTextToStore = 8, // obsolete since 1.25, Nix 3.0. Use WorkerProto::Op::AddToStore
243 BuildPaths = 9,
244 EnsurePath = 10,
245 AddTempRoot = 11,
246 AddIndirectRoot = 12,
247 SyncWithGC = 13,
248 FindRoots = 14,
249 // ExportPath = 16, // removed
250 QueryDeriver = 18, // obsolete
251 SetOptions = 19,
252 CollectGarbage = 20,
253 QuerySubstitutablePathInfo = 21,
254 QueryDerivationOutputs = 22, // obsolete
255 QueryAllValidPaths = 23,
256 // QueryFailedPaths = 24, // removed
257 // ClearFailedPaths = 25, // removed
258 QueryPathInfo = 26,
259 // ImportPaths = 27, // removed
260 QueryDerivationOutputNames = 28, // obsolete
261 QueryPathFromHashPart = 29,
262 QuerySubstitutablePathInfos = 30,
263 QueryValidPaths = 31,
264 QuerySubstitutablePaths = 32,
265 QueryValidDerivers = 33,
266 OptimiseStore = 34,
267 VerifyStore = 35,
268 BuildDerivation = 36,
269 AddSignatures = 37,
270 NarFromPath = 38,
271 AddToStoreNar = 39,
272 QueryMissing = 40,
273 QueryDerivationOutputMap = 41,
274 RegisterDrvOutput = 42,
275 QueryRealisation = 43,
276 AddMultipleToStore = 44,
277 AddBuildLog = 45,
278 BuildPathsWithResults = 46,
279 AddPermRoot = 47,
280 // QueryActiveBuilds = 48, // reserved for https://github.com/NixOS/nix/pull/15979
281 AddTempRoots = 49,
282 // QueryPathInfos = 50, // reserved for https://github.com/DeterminateSystems/nix-src/pull/539
283 SubmitOutput = 1000, // Only used within derivations with feature
284 AddToStoreScanning = 1001,
285};
286
288{
295 std::optional<std::string> daemonNixVersion;
296
308 std::optional<TrustedFlag> remoteTrustsUs;
309
310 bool operator==(const ClientHandshakeInfo &) const = default;
311};
312
319inline Sink & operator<<(Sink & sink, WorkerProto::Op op)
320{
321 return sink << static_cast<uint64_t>(op);
322}
323
329inline std::ostream & operator<<(std::ostream & s, WorkerProto::Op op)
330{
331 return s << static_cast<uint64_t>(op);
332}
333
344#define DECLARE_WORKER_SERIALISER(T) \
345 struct WorkerProto::Serialise<T> \
346 { \
347 static T read(const StoreDirConfig & store, WorkerProto::ReadConn conn); \
348 static void write(const StoreDirConfig & store, WorkerProto::WriteConn conn, const T & t); \
349 };
350
351template<>
353template<>
355template<>
357template<>
359template<>
361template<>
363template<>
365template<>
367template<>
369template<>
370DECLARE_WORKER_SERIALISER(std::optional<UnkeyedRealisation>);
371template<>
373template<>
375template<>
376DECLARE_WORKER_SERIALISER(std::optional<TrustedFlag>);
377template<>
378DECLARE_WORKER_SERIALISER(std::optional<std::chrono::microseconds>);
379template<>
381
382template<>
384
385template<>
387
388template<typename T>
390#define COMMA_ ,
391template<typename T, typename Compare>
392DECLARE_WORKER_SERIALISER(std::set<T COMMA_ Compare>);
393template<typename... Ts>
394DECLARE_WORKER_SERIALISER(std::tuple<Ts...>);
395
396template<typename K, typename V, typename Compare>
397DECLARE_WORKER_SERIALISER(std::map<K COMMA_ V COMMA_ Compare>);
398#undef COMMA_
399
400} // namespace nix
size_t read(Descriptor fd, std::span< std::byte > buffer)
Definition file-descriptor.cc:30
GCAction
Definition gc-store.hh:30
Definition build-result.hh:123
Definition derived-path.hh:238
Definition realisation.hh:26
Definition gc-store.hh:46
std::variant< WholeStore, SpecificPaths > GCPaths
Definition gc-store.hh:68
Definition build-result.hh:204
Definition realisation.hh:103
Definition derived-path.hh:105
Definition serialise.hh:18
Definition serialise.hh:95
Definition store-dir-config.hh:29
Definition realisation.hh:62
Definition path-info.hh:58
Definition path-info.hh:159
Definition worker-protocol-connection.hh:60
Definition worker-protocol-connection.hh:10
Definition worker-protocol-connection.hh:126
Definition worker-protocol.hh:288
std::optional< std::string > daemonNixVersion
Definition worker-protocol.hh:295
std::optional< TrustedFlag > remoteTrustsUs
Definition worker-protocol.hh:308
Definition worker-protocol.hh:164
Definition worker-protocol-impl.hh:47
Definition worker-protocol.hh:72
static constexpr Number fromWire(unsigned int wire)
Definition worker-protocol.hh:90
constexpr unsigned int toWire() const
Definition worker-protocol.hh:82
Definition worker-protocol.hh:70
Definition worker-protocol.hh:174
Definition worker-protocol.hh:54
static const Version builderRpcV0
Definition worker-protocol.hh:126
static const Version latest
Definition worker-protocol.hh:118
static constexpr std::string_view featureRealisationWithPath
Definition worker-protocol.hh:132
static constexpr std::string_view featureSubmitOutput
Definition worker-protocol.hh:152
static constexpr std::string_view featureAddToStoreScanning
Definition worker-protocol.hh:147
static constexpr std::string_view featureDisableSetOptions
Definition worker-protocol.hh:142
static constexpr std::string_view featureDeleteDeadSpecificReferrers
Definition worker-protocol.hh:137
static void write(const StoreDirConfig &store, WriteConn conn, const T &t)
Definition worker-protocol.hh:229
#define DECLARE_WORKER_SERIALISER(T)
Definition worker-protocol.hh:344