Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
command.hh
Go to the documentation of this file.
1#pragma once
3
6#include "lix/libutil/args.hh"
10#include "lix/libutil/async.hh"
11
12#include <optional>
13
14namespace nix {
15
16extern std::string programPath;
17
18extern char * * savedArgv;
19
20class EvalState;
21struct Pos;
22class Store;
23
24static constexpr Command::Category catHelp = -1;
25static constexpr Command::Category catSecondary = 100;
26static constexpr Command::Category catUtility = 101;
27static constexpr Command::Category catNixInstallation = 102;
28
29static constexpr auto installablesCategory = "Options that change the interpretation of [installables](@docroot@/command-ref/new-cli/nix.md#installables)";
30
31// For the overloaded run methods
32#pragma GCC diagnostic ignored "-Woverloaded-virtual"
33
37struct StoreCommand : virtual Command
38{
39 StoreCommand();
40 void run() override;
41 ref<Store> getStore();
42 virtual ref<Store> createStore();
46 virtual void run(ref<Store>) = 0;
47
48private:
49 std::optional<ref<Store>> _store;
50};
51
56struct CopyCommand : virtual StoreCommand
57{
58 std::string srcUri, dstUri;
59
60 CopyCommand();
61
62 ref<Store> createStore() override;
63
64 ref<Store> getDstStore();
65};
66
70struct EvalCommand : virtual StoreCommand, MixEvalArgs
71{
72 bool startReplOnEvalErrors = false;
73 bool ignoreExceptionsDuringTry = false;
74
75 EvalCommand();
76
77 ~EvalCommand();
78
79 ref<Store> getEvalStore();
80
81 virtual ref<eval_cache::CachingEvaluator> getEvaluator();
82
83private:
84 std::optional<ref<Store>> evalStore;
85
86 std::shared_ptr<eval_cache::CachingEvaluator> evalState;
87};
88
93struct MixFlakeOptions : virtual Args, EvalCommand
94{
95 flake::LockFlags lockFlags;
96
97 MixFlakeOptions();
98
107 virtual std::vector<FlakeRef> getFlakeRefsForCompletion()
108 { return {}; }
109};
110
111struct SourceExprCommand : virtual Args, MixFlakeOptions
112{
113 std::optional<Path> file;
114 std::optional<std::string> expr;
115
116 SourceExprCommand();
117
118 ref<eval_cache::CachingEvaluator> getEvaluator() override;
119
120 Installables parseInstallables(
121 EvalState & state, ref<Store> store, std::vector<std::string> ss);
122
123 ref<Installable> parseInstallable(
124 EvalState & state, ref<Store> store, const std::string & installable);
125
126 virtual Strings getDefaultFlakeAttrPaths();
127
128 virtual Strings getDefaultFlakeAttrPathPrefixes();
129
133 void completeInstallable(EvalState & state, AddCompletions & completions, std::string_view prefix);
134
140};
141
148struct MixReadOnlyOption : virtual Args
149{
150 MixReadOnlyOption();
151};
152
159struct RawInstallablesCommand : virtual Args, SourceExprCommand
160{
161 RawInstallablesCommand();
162
163 virtual void run(ref<Store> store, std::vector<std::string> && rawInstallables) = 0;
164
165 void run(ref<Store> store) override;
166
167 // FIXME make const after `CmdRepl`'s override is fixed up
168 virtual void applyDefaultInstallables(std::vector<std::string> & rawInstallables);
169
170 bool readFromStdIn = false;
171
172 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
173
174private:
175
176 std::vector<std::string> rawInstallables;
177};
178
183struct InstallablesCommand : RawInstallablesCommand
184{
185 virtual void run(ref<Store> store, Installables && installables) = 0;
186
187 void run(ref<Store> store, std::vector<std::string> && rawInstallables) override;
188};
189
193struct InstallableCommand : virtual Args, SourceExprCommand
194{
195 InstallableCommand();
196
197 virtual void run(ref<Store> store, ref<Installable> installable) = 0;
198
199 void run(ref<Store> store) override;
200
201 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
202
203private:
204
205 std::string _installable{"."};
206};
207
208struct MixOperateOnOptions : virtual Args
209{
210 OperateOn operateOn = OperateOn::Output;
211
212 MixOperateOnOptions();
213};
214
221struct BuiltPathsCommand : InstallablesCommand, virtual MixOperateOnOptions
222{
223private:
224
225 bool recursive = false;
226 bool all = false;
227
228protected:
229
230 Realise realiseMode = Realise::Derivation;
231
232public:
233
234 BuiltPathsCommand(bool recursive);
235
236 virtual void run(ref<Store> store, BuiltPaths && paths) = 0;
237
238 void run(ref<Store> store, Installables && installables) override;
239
240 void applyDefaultInstallables(std::vector<std::string> & rawInstallables) override;
241};
242
243struct StorePathsCommand : public BuiltPathsCommand
244{
245 StorePathsCommand(bool recursive = false);
246
247 virtual void run(ref<Store> store, StorePaths && storePaths) = 0;
248
249 void run(ref<Store> store, BuiltPaths && paths) override;
250};
251
255struct StorePathCommand : public StorePathsCommand
256{
257 virtual void run(ref<Store> store, const StorePath & storePath) = 0;
258
259 void run(ref<Store> store, StorePaths && storePaths) override;
260};
261
266{
267 using CommandMap = std::map<
268 std::vector<std::string>,
269 std::function<ref<Command>(AsyncIoRoot & aio)>
270 >;
271 static CommandMap * commands;
272
273 static void add(std::vector<std::string> && name,
274 std::function<ref<Command>(AsyncIoRoot & aio)> command)
275 {
276 if (!commands) {
277 commands = new CommandMap;
278 }
279 commands->emplace(name, command);
280 }
281
282 static nix::CommandMap getCommandsFor(const std::vector<std::string> & prefix);
283};
284
285template<typename Base>
286class MixAio : public Base
287{
288private:
289 AsyncIoRoot & aio_;
290
291public:
292 template<typename... Args>
293 MixAio(AsyncIoRoot & aio, Args &&... args)
294 : Base(std::forward<Args>(args)...)
295 , aio_(aio)
296 {
297 }
298
299 AsyncIoRoot & aio() override
300 {
301 return aio_;
302 }
303};
304
305template<class T>
306static void registerCommand(const std::string & name)
307{
308 CommandRegistry::add({name}, [](AsyncIoRoot & aio) {
309 return make_ref<MixAio<T>>(aio);
310 });
311}
312
313template<class T>
314static void registerCommand2(std::vector<std::string> && name)
315{
316 CommandRegistry::add(std::move(name), [](AsyncIoRoot & aio) {
317 return make_ref<MixAio<T>>(aio);
318 });
319}
320
321struct MixProfile : virtual StoreCommand
322{
323 std::optional<Path> profile;
324
325 MixProfile();
326
327 /* If 'profile' is set, make it point at 'storePath'. */
328 void updateProfile(const StorePath & storePath);
329
330 /* If 'profile' is set, make it point at the store path produced
331 by 'buildables'. */
332 void updateProfile(const BuiltPaths & buildables);
333};
334
335struct MixDefaultProfile : MixProfile
336{
337 MixDefaultProfile();
338};
339
340struct MixEnvironment : virtual Args {
341
342 StringSet keep, unset;
343 Strings stringsEnv;
344 std::vector<char*> vectorEnv;
345 bool ignoreEnvironment;
346
347 MixEnvironment();
348
349 /***
350 * Modify global environ based on `ignoreEnvironment`, `keep`, and
351 * `unset`. It's expected that exec will be called before this class
352 * goes out of scope, otherwise `environ` will become invalid.
353 */
354 void setEnviron();
355};
356
357void completeFlakeInputPath(
358 AddCompletions & completions,
359 EvalState & evalState,
360 const std::vector<FlakeRef> & flakeRefs,
361 std::string_view prefix);
362
363void completeFlakeRef(
364 AsyncIoRoot & aio, AddCompletions & completions, ref<Store> store, std::string_view prefix
365);
366
367void completeFlakeRefWithFragment(
368 AddCompletions & completions,
369 EvalState & evalState,
371 flake::LockFlags lockFlags,
372 Strings attrPathPrefixes,
373 const Strings & defaultFlakeAttrPaths,
374 std::string_view prefix);
375
376kj::Promise<Result<void>> printClosureDiff(
377 ref<Store> store,
378 const StorePath & beforePath,
379 const StorePath & afterPath,
380 bool json,
381 std::string_view indent);
382
383}
Definition args.hh:439
Definition args.hh:31
std::function< CompleterFun > CompleterClosure
Definition args.hh:151
Definition eval.hh:685
Definition path.hh:21
Definition store-api.hh:195
Definition ref.hh:19
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:101
Definition async.hh:39
Definition command.hh:266
Definition args.hh:333
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:847
Definition command.hh:184
virtual std::vector< FlakeRef > getFlakeRefsForCompletion()
Definition command.hh:107
Definition position.hh:19
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:823
CompleterClosure getCompleteInstallable()
Definition installables.cc:203
void completeInstallable(EvalState &state, AddCompletions &completions, std::string_view prefix)
Definition installables.cc:210
virtual void run(ref< Store >)=0
void run() override
Definition command.cc:52
Definition command.hh:256
Definition flake.hh:113