Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
derivations.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/store/path.hh"
5#include "nix/util/types.hh"
6#include "nix/util/hash.hh"
12#include "nix/util/sync.hh"
14#include "nix/util/fun.hh"
15
16#include <boost/unordered/concurrent_flat_map_fwd.hpp>
17#include <variant>
18
19namespace nix {
20
24static constexpr std::string_view drvFeatureBuilderRpcV0 = "builder-rpc-v0";
25
26struct StoreDirConfig;
27class Store;
28
29/* Abstract syntax of derivations. */
30
31namespace derivation {
32
37typedef std::map<StorePath, StringSet> Inputs;
38
39struct Type
40{
45 {
52
53 bool operator==(const InputAddressed &) const = default;
54 auto operator<=>(const InputAddressed &) const = default;
55 };
56
61 {
77 bool fixed;
78
79 bool operator==(const ContentAddressed &) const = default;
80 auto operator<=>(const ContentAddressed &) const = default;
81 };
82
89 struct Impure
90 {
91 bool operator==(const Impure &) const = default;
92 auto operator<=>(const Impure &) const = default;
93 };
94
95 typedef std::variant<InputAddressed, ContentAddressed, Impure> Raw;
96
97 Raw raw;
98
99 bool operator==(const Type &) const = default;
100 auto operator<=>(const Type &) const = default;
101
102 MAKE_WRAPPER_CONSTRUCTOR(Type);
103
107 Type() = delete;
108
113 bool isCA() const;
114
119 bool isFixed() const;
120
127 bool isSandboxed() const;
128
140 bool isImpure() const;
141
147 bool hasKnownOutputPaths() const;
148};
149
150template<typename Inputs, typename Out = Output>
151struct Derivation;
152
161
169
180using ReadDerivation = fun<Full(const StorePath & drvPath)>;
181
187
197
205
206template<typename Inputs, typename Out>
208{
212 Outputs<Out> outputs;
213 Inputs inputs;
214 std::string platform;
218 std::string builder;
219 Strings args;
224 std::optional<StructuredAttrs> structuredAttrs;
225
226 std::string name;
227
228 bool operator==(const Derivation &) const = default;
229
230 bool isBuiltin() const;
231
236
237 static std::string_view nameFromPath(const StorePath & storePath);
238
243 void applyRewrites(const StringMap & rewrites);
244
248 template<typename F>
250 {
251 return {
252 .outputs = outputs,
253 .inputs = f(inputs),
254 .platform = platform,
255 .builder = builder,
256 .args = args,
257 .env = env,
258 .structuredAttrs = structuredAttrs,
259 .name = name,
260 };
261 }
262
267 template<typename F>
269 {
270 Outputs<std::invoke_result_t<F, const Out &>> newOutputs;
271 for (const auto & [name, output] : outputs)
272 newOutputs.insert_or_assign(name, f(output));
273 return {
274 .outputs = std::move(newOutputs),
275 .inputs = inputs,
276 .platform = platform,
277 .builder = builder,
278 .args = args,
279 .env = env,
280 .structuredAttrs = structuredAttrs,
281 .name = name,
282 };
283 }
284};
285
289template<typename Inputs>
290Type type(const Derivation<Inputs, Output> & drv);
291
297template<typename Inputs>
298OutputsAndOptPaths outputsAndOptPaths(const Derivation<Inputs, Output> & drv, const StoreDirConfig & store);
299
309bool hasDynamicDrvDep(const std::set<SingleDerivedPath> & inputs);
310
325void checkInvariants(const Basic & drv, const StoreDirConfig & store);
326void checkInvariants(const Full & drv, Store & store);
327
332void checkInvariants(const Full & drv, const StoreDirConfig & store, ReadDerivation & readDerivation);
333
342template<typename Inputs>
343void checkInvariants(const Derivation<Inputs, Output> & drv, Store & store, const StorePath & drvPath)
344{
345 checkInvariants(drv, store, readInvalid(store), drvPath);
346}
347
352template<typename Inputs>
353void checkInvariants(
354 const Derivation<Inputs, Output> & drv,
355 const StoreDirConfig & store,
356 auto && readDerivation,
357 const StorePath & drvPath);
358
379void fillInOutputPaths(Basic & drv, const StoreDirConfig & store);
380void fillInOutputPaths(Full & drv, Store & store);
381
386void fillInOutputPaths(Full & drv, const StoreDirConfig & store, ReadDerivation & readDerivation);
387
405std::optional<FullInputAddressed>
406fillInOutputPaths(FullDeferred drv, const StoreDirConfig & store, ReadDerivation & readDerivation);
407
429Full parseJsonAndValidate(Store & store, const nlohmann::json & json);
430
431} // namespace derivation
432
433using BasicDerivation = derivation::Basic;
434using Derivation = derivation::Full;
435
441StorePath computeStorePath(const StoreDirConfig & store, const Derivation & drv);
442
448bool isDerivation(std::string_view fileName);
449
457std::string outputPathName(std::string_view drvName, OutputNameView outputName);
458
467std::string hashPlaceholder(const OutputNameView outputName);
468
473constexpr unsigned expectedJsonVersionDerivation = 4;
474
475} // namespace nix
476
477namespace nlohmann {
478template<typename Inputs>
479JSON_IMPL_WITH_XP_FEATURES_INNER(nix::derivation::Derivation<Inputs>);
480} // namespace nlohmann
Definition path.hh:33
Definition store-api.hh:397
Definition fun.hh:19
constexpr unsigned expectedJsonVersionDerivation
Definition derivations.hh:473
Definition derivation-options.cc:339
std::map< std::string, std::pair< Output, std::optional< StorePath > > > OutputsAndOptPaths
Definition output.hh:143
Derivation< std::set< SingleDerivedPath > > Full
Derivation that depends on the outputs of other derivations in addition.
Definition derivations.hh:168
Derivation< StorePathSet > Basic
Derivation that depends only on other store objects.
Definition derivations.hh:160
fun< Full(const StorePath &drvPath)> ReadDerivation
Definition derivations.hh:180
std::map< StorePath, StringSet > Inputs
Definition derivations.hh:37
OutputsAndOptPaths outputsAndOptPaths(const Derivation< Inputs, Output > &drv, const StoreDirConfig &store)
Definition derivations.cc:247
bool hasDynamicDrvDep(const FullInputs &inputs)
Definition full-inputs.cc:68
ReadDerivation readInvalid(Store &store)
Definition derivations.cc:560
Derivation< std::set< SingleDerivedPath >, Output::InputAddressed > FullInputAddressed
Full, but statically known to be input-addressed.
Definition derivations.hh:204
void fillInOutputPaths(Basic &drv, const StoreDirConfig &store)
Definition derivations.cc:565
Derivation< std::set< SingleDerivedPath >, Output::Deferred > FullDeferred
Full, but statically known to have no output paths yet.
Definition derivations.hh:196
Full parseJsonAndValidate(Store &store, const nlohmann::json &json)
Definition derivations.cc:621
void checkInvariants(const Derivation< Inputs, Output > &drv, const StoreDirConfig &store, auto &&readDerivation, const StorePath &drvPath)
Definition derivations.cc:513
Type type(const Derivation< Inputs, Output > &drv)
Definition derivations.cc:167
Definition store-dir-config.hh:29
Definition derivations.hh:208
void applyRewrites(const StringMap &rewrites)
Definition derivations.cc:281
Derivation< std::invoke_result_t< F, const Inputs & >, Out > mapInputs(F f) const
Definition derivations.hh:249
StringSet outputNames() const
Definition derivations.cc:238
Outputs< derivation::Output > outputs
Definition derivations.hh:212
Derivation< Inputs, std::invoke_result_t< F, const Out & > > mapOutputs(F f) const
Definition derivations.hh:268
Definition output.hh:85
Definition derivations.hh:61
bool sandboxed
Definition derivations.hh:65
bool fixed
Definition derivations.hh:77
Definition derivations.hh:90
Definition derivations.hh:45
bool deferred
Definition derivations.hh:51
bool isSandboxed() const
Definition derivations.cc:63
bool isImpure() const
Definition derivations.cc:74
bool isCA() const
Definition derivations.cc:26
bool hasKnownOutputPaths() const
Definition derivations.cc:52
bool isFixed() const
Definition derivations.cc:41
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
StringMap StringPairs
Definition types.hh:32
std::set< std::string, std::less<> > StringSet
Definition types.hh:45