Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
command.hh
Go to the documentation of this file.
1#pragma once
3
5#include "nix/util/args.hh"
7#include "nix/store/path.hh"
10
11#include <optional>
12
13namespace nix {
14
15class EvalState;
16struct Pos;
17class Store;
18struct LocalFSStore;
19
20static constexpr Command::Category catHelp = -1;
21static constexpr Command::Category catSecondary = 100;
22static constexpr Command::Category catUtility = 101;
23static constexpr Command::Category catNixInstallation = 102;
24
25static constexpr auto installablesCategory =
26 "Options that change the interpretation of [installables](@docroot@/command-ref/new-cli/nix.md#installables)";
27
28struct NixMultiCommand : MultiCommand, virtual Command
29{
30 nlohmann::json toJSON() override;
31
32 using MultiCommand::MultiCommand;
33
34 virtual void run() override;
35};
36
37// For the overloaded run methods
38#pragma GCC diagnostic ignored "-Woverloaded-virtual"
39
43struct StoreConfigCommand : virtual Command
44{
45 StoreConfigCommand();
46 void run() override;
47
52
53 virtual ref<StoreConfig> createStoreConfig();
57 virtual void run(ref<StoreConfig>) = 0;
58
59private:
60 std::shared_ptr<StoreConfig> _storeConfig;
61};
62
66struct StoreCommand : virtual StoreConfigCommand
67{
68 StoreCommand();
69 void run(ref<StoreConfig>) override;
70
75
76 ref<Store> createStore();
80 virtual void run(ref<Store>) = 0;
81
82private:
83 std::shared_ptr<Store> _store;
84};
85
90struct CopyCommand : virtual StoreCommand
91{
92 std::optional<StoreReference> srcUri, dstUri;
93
94 CopyCommand();
95
96 ref<StoreConfig> createStoreConfig() override;
97
98 ref<Store> getDstStore();
99};
100
104struct EvalCommand : virtual StoreCommand, MixEvalArgs
105{
106 bool startReplOnEvalErrors = false;
107 bool ignoreExceptionsDuringTry = false;
108
109 EvalCommand();
110
111 ~EvalCommand();
112
113 ref<Store> getEvalStore();
114
115 ref<EvalState> getEvalState();
116
117private:
118 std::shared_ptr<Store> evalStore;
119
120 std::shared_ptr<EvalState> evalState;
121};
122
127struct MixFlakeOptions : virtual Args, EvalCommand
128{
129 flake::LockFlags lockFlags;
130
131 MixFlakeOptions();
132
141 virtual std::vector<FlakeRef> getFlakeRefsForCompletion()
142 {
143 return {};
144 }
145};
146
147struct SourceExprCommand : virtual Args, MixFlakeOptions
148{
149 std::optional<std::filesystem::path> file;
150 std::optional<std::string> expr;
151
152 SourceExprCommand();
153
154 Installables parseInstallables(ref<Store> store, std::vector<std::string> ss);
155
156 ref<Installable> parseInstallable(ref<Store> store, const std::string & installable);
157
158 virtual Strings getDefaultFlakeAttrPaths();
159
160 virtual Strings getDefaultFlakeAttrPathPrefixes();
161
165 void completeInstallable(AddCompletions & completions, std::string_view prefix);
166
172};
173
180struct MixReadOnlyOption : virtual Args
181{
182 MixReadOnlyOption();
183};
184
191struct RawInstallablesCommand : virtual Args, SourceExprCommand
192{
193 RawInstallablesCommand();
194
195 virtual void run(ref<Store> store, std::vector<std::string> && rawInstallables) = 0;
196
197 void run(ref<Store> store) override;
198
199 // FIXME make const after `CmdRepl`'s override is fixed up
200 virtual void applyDefaultInstallables(std::vector<std::string> & rawInstallables);
201
202 bool readFromStdIn = false;
203
204 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
205
206private:
207
208 std::vector<std::string> rawInstallables;
209};
210
215struct InstallablesCommand : RawInstallablesCommand
216{
217 virtual void run(ref<Store> store, Installables && installables) = 0;
218
219 void run(ref<Store> store, std::vector<std::string> && rawInstallables) override;
220};
221
225struct InstallableCommand : virtual Args, SourceExprCommand
226{
227 InstallableCommand();
228
229 virtual void run(ref<Store> store, ref<Installable> installable) = 0;
230
231 void run(ref<Store> store) override;
232
233 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
234
235private:
236
237 std::string _installable{"."};
238};
239
240struct MixOperateOnOptions : virtual Args
241{
242 OperateOn operateOn = OperateOn::Output;
243
244 MixOperateOnOptions();
245};
246
253struct BuiltPathsCommand : InstallablesCommand, virtual MixOperateOnOptions
254{
255private:
256
257 bool recursive = false;
258 bool all = false;
259
260protected:
261
262 Realise realiseMode = Realise::Derivation;
263
264public:
265
266 BuiltPathsCommand(bool recursive = false);
267
268 virtual void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) = 0;
269
270 void run(ref<Store> store, Installables && installables) override;
271
272 void applyDefaultInstallables(std::vector<std::string> & rawInstallables) override;
273};
274
275struct StorePathsCommand : public BuiltPathsCommand
276{
277 StorePathsCommand(bool recursive = false);
278
279 virtual void run(ref<Store> store, StorePaths && storePaths) = 0;
280
281 void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) override;
282};
283
287struct StorePathCommand : public StorePathsCommand
288{
289 virtual void run(ref<Store> store, const StorePath & storePath) = 0;
290
291 void run(ref<Store> store, StorePaths && storePaths) override;
292};
293
297struct RegisterCommand
298{
299 typedef std::map<std::vector<std::string>, fun<ref<Command>()>> Commands;
300
301 static Commands & commands();
302
303 RegisterCommand(std::vector<std::string> && name, fun<ref<Command>()> command)
304 {
305 commands().emplace(name, command);
306 }
307
308 static nix::Commands getCommandsFor(const std::vector<std::string> & prefix);
309};
310
311template<class T>
312static RegisterCommand registerCommand(const std::string & name)
313{
314 return RegisterCommand({name}, []() { return make_ref<T>(); });
315}
316
317template<class T>
318static RegisterCommand registerCommand2(std::vector<std::string> && name)
319{
320 return RegisterCommand(std::move(name), []() { return make_ref<T>(); });
321}
322
323struct MixProfile : virtual StoreCommand
324{
325 std::optional<std::filesystem::path> profile;
326
327 MixProfile();
328
329 /* If 'profile' is set, make it point at 'storePath'. */
330 void updateProfile(Store & store, const StorePath & storePath);
331
332 /* If 'profile' is set, make it point at the store path produced
333 by 'buildables'. */
334 void updateProfile(Store & store, const BuiltPaths & buildables);
335};
336
337struct MixDefaultProfile : MixProfile
338{
339 MixDefaultProfile();
340};
341
342struct MixEnvironment : virtual Args
343{
344
345 StringSet keepVars;
346 StringSet unsetVars;
347 StringMap setVars;
348 bool ignoreEnvironment;
349
350 MixEnvironment();
351
352 /***
353 * Modify global environ based on `ignoreEnvironment`, `keep`,
354 * `unset`, and `added`. It's expected that exec will be called
355 * before this class goes out of scope, otherwise `environ` will
356 * become invalid.
357 */
358 void setEnviron();
359};
360
361struct MixNoCheckSigs : virtual Args
362{
363 CheckSigsFlag checkSigs = CheckSigs;
364
365 MixNoCheckSigs()
366 {
367 addFlag({
368 .longName = "no-check-sigs",
369 .description = "Do not require that paths are signed by trusted keys.",
370 .handler = {&checkSigs, NoCheckSigs},
371 });
372 }
373};
374
375void completeFlakeInputAttrPath(
376 AddCompletions & completions,
377 ref<EvalState> evalState,
378 const std::vector<FlakeRef> & flakeRefs,
379 std::string_view prefix);
380
381void completeFlakeRef(AddCompletions & completions, ref<Store> store, std::string_view prefix);
382
383void completeFlakeRefWithFragment(
384 AddCompletions & completions,
385 ref<EvalState> evalState,
386 flake::LockFlags lockFlags,
387 Strings attrPathPrefixes,
388 const Strings & defaultFlakeAttrPaths,
389 std::string_view prefix);
390
391std::string showVersions(const StringSet & versions);
392
393void printClosureDiff(
394 ref<Store> store, const StorePath & beforePath, const StorePath & afterPath, std::string_view indent);
395
400void createOutLinks(const std::filesystem::path & outLink, const BuiltPaths & buildables, LocalFSStore & store);
401
403struct MixOutLinkBase : virtual Args
404{
406 std::optional<std::filesystem::path> outLink = std::nullopt;
407
408 MixOutLinkBase(const std::optional<std::filesystem::path> & defaultOutLink)
409 : outLink(defaultOutLink)
410 {
411 }
412
414 void createOutLinksMaybe(const BuiltPaths & paths, ref<Store> & store);
416 void createOutLinksMaybe(const std::vector<BuiltPathWithResult> & buildables, ref<Store> & store);
417};
418
420struct MixOutLinkByDefault : MixOutLinkBase, virtual Args
421{
422 MixOutLinkByDefault()
423 : MixOutLinkBase("result")
424 {
425 addFlag({
426 .longName = "out-link",
427 .shortName = 'o',
428 .description = "Use *path* as prefix for the symlinks to the build results. It defaults to `result`.",
429 .labels = {"path"},
430 .handler = {&outLink},
431 .completer = completePath,
432 });
433
434 addFlag({
435 .longName = "no-link",
436 .description = "Do not create symlinks to the build results.",
437 .handler = {[&] { outLink = std::nullopt; }},
438 });
439 }
440};
441
442struct MixPrintOutPaths : virtual Args
443{
444 bool printOutputPaths = false;
445
446 MixPrintOutPaths()
447 {
448 addFlag({
449 .longName = "print-out-paths",
450 .description = "Print the resulting output paths",
451 .handler = {&printOutputPaths, true},
452 });
453 }
454
455 void printOutPathsMaybe(const BuiltPaths & paths, ref<Store> store);
456};
457
458} // namespace nix
Definition args.hh:466
Definition args.hh:29
std::function< CompleterFun > CompleterClosure
Definition args.hh:186
Definition eval.hh:420
Definition path.hh:33
Definition store-api.hh:397
Definition fun.hh:19
Definition ref.hh:58
OperateOn
Definition installables.hh:44
@ Output
Definition installables.hh:48
Realise
Definition installables.hh:17
@ Derivation
Definition installables.hh:29
std::vector< ref< Installable > > Installables
Definition installables.hh:103
Definition args.hh:367
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:762
Definition command.hh:216
Definition local-fs-store.hh:91
virtual std::vector< FlakeRef > getFlakeRefsForCompletion()
Definition command.hh:141
void createOutLinksMaybe(const BuiltPaths &paths, ref< Store > &store)
Definition command.cc:446
std::optional< std::filesystem::path > outLink
Definition command.hh:406
Definition command.hh:29
virtual void run() override
Definition command.cc:52
Definition position.hh:20
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:739
Definition command.hh:298
void completeInstallable(AddCompletions &completions, std::string_view prefix)
Definition installables.cc:246
CompleterClosure getCompleteInstallable()
Definition installables.cc:239
virtual void run(ref< Store >)=0
void run(ref< StoreConfig >) override
Definition command.cc:102
ref< Store > getStore()
Definition command.cc:88
ref< StoreConfig > getStoreConfig()
Definition command.cc:69
void run() override
Definition command.cc:81
virtual void run(ref< StoreConfig >)=0
Definition command.hh:288
Definition flake.hh:142
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
std::set< std::string, std::less<> > StringSet
Definition types.hh:45