Nix 2.26.3
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 "args.hh"
6#include "common-eval-args.hh"
7#include "path.hh"
8#include "flake/lockfile.hh"
9
10#include <optional>
11
12namespace nix {
13
14extern std::string programPath;
15
16extern char ** savedArgv;
17
18class EvalState;
19struct Pos;
20class Store;
21class LocalFSStore;
22
23static constexpr Command::Category catHelp = -1;
24static constexpr Command::Category catSecondary = 100;
25static constexpr Command::Category catUtility = 101;
26static constexpr Command::Category catNixInstallation = 102;
27
28static constexpr auto installablesCategory =
29 "Options that change the interpretation of [installables](@docroot@/command-ref/new-cli/nix.md#installables)";
30
31struct NixMultiCommand : MultiCommand, virtual Command
32{
33 nlohmann::json toJSON() override;
34
35 using MultiCommand::MultiCommand;
36
37 virtual void run() override;
38};
39
40// For the overloaded run methods
41#pragma GCC diagnostic ignored "-Woverloaded-virtual"
42
46struct StoreCommand : virtual Command
47{
48 StoreCommand();
49 void run() override;
50
55
60 {
61 return getStore();
62 }
63
64 virtual ref<Store> createStore();
68 virtual void run(ref<Store>) = 0;
69
70private:
71 std::shared_ptr<Store> _store;
72};
73
78struct CopyCommand : virtual StoreCommand
79{
80 std::string srcUri, dstUri;
81
82 CopyCommand();
83
84 ref<Store> createStore() override;
85
86 ref<Store> getDstStore() override;
87};
88
92struct EvalCommand : virtual StoreCommand, MixEvalArgs
93{
94 bool startReplOnEvalErrors = false;
95 bool ignoreExceptionsDuringTry = false;
96
97 EvalCommand();
98
99 ~EvalCommand();
100
101 ref<Store> getEvalStore();
102
103 ref<EvalState> getEvalState();
104
105private:
106 std::shared_ptr<Store> evalStore;
107
108 std::shared_ptr<EvalState> evalState;
109};
110
115struct MixFlakeOptions : virtual Args, EvalCommand
116{
117 flake::LockFlags lockFlags;
118
119 MixFlakeOptions();
120
129 virtual std::vector<FlakeRef> getFlakeRefsForCompletion()
130 {
131 return {};
132 }
133};
134
135struct SourceExprCommand : virtual Args, MixFlakeOptions
136{
137 std::optional<Path> file;
138 std::optional<std::string> expr;
139
140 SourceExprCommand();
141
142 Installables parseInstallables(ref<Store> store, std::vector<std::string> ss);
143
144 ref<Installable> parseInstallable(ref<Store> store, const std::string & installable);
145
146 virtual Strings getDefaultFlakeAttrPaths();
147
148 virtual Strings getDefaultFlakeAttrPathPrefixes();
149
153 void completeInstallable(AddCompletions & completions, std::string_view prefix);
154
160};
161
168struct MixReadOnlyOption : virtual Args
169{
170 MixReadOnlyOption();
171};
172
179struct RawInstallablesCommand : virtual Args, SourceExprCommand
180{
181 RawInstallablesCommand();
182
183 virtual void run(ref<Store> store, std::vector<std::string> && rawInstallables) = 0;
184
185 void run(ref<Store> store) override;
186
187 // FIXME make const after `CmdRepl`'s override is fixed up
188 virtual void applyDefaultInstallables(std::vector<std::string> & rawInstallables);
189
190 bool readFromStdIn = false;
191
192 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
193
194private:
195
196 std::vector<std::string> rawInstallables;
197};
198
203struct InstallablesCommand : RawInstallablesCommand
204{
205 virtual void run(ref<Store> store, Installables && installables) = 0;
206
207 void run(ref<Store> store, std::vector<std::string> && rawInstallables) override;
208};
209
213struct InstallableCommand : virtual Args, SourceExprCommand
214{
215 InstallableCommand();
216
217 virtual void run(ref<Store> store, ref<Installable> installable) = 0;
218
219 void run(ref<Store> store) override;
220
221 std::vector<FlakeRef> getFlakeRefsForCompletion() override;
222
223private:
224
225 std::string _installable{"."};
226};
227
228struct MixOperateOnOptions : virtual Args
229{
230 OperateOn operateOn = OperateOn::Output;
231
232 MixOperateOnOptions();
233};
234
241struct BuiltPathsCommand : InstallablesCommand, virtual MixOperateOnOptions
242{
243private:
244
245 bool recursive = false;
246 bool all = false;
247
248protected:
249
250 Realise realiseMode = Realise::Derivation;
251
252public:
253
254 BuiltPathsCommand(bool recursive = false);
255
256 virtual void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) = 0;
257
258 void run(ref<Store> store, Installables && installables) override;
259
260 void applyDefaultInstallables(std::vector<std::string> & rawInstallables) override;
261};
262
263struct StorePathsCommand : public BuiltPathsCommand
264{
265 StorePathsCommand(bool recursive = false);
266
267 virtual void run(ref<Store> store, StorePaths && storePaths) = 0;
268
269 void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) override;
270};
271
275struct StorePathCommand : public StorePathsCommand
276{
277 virtual void run(ref<Store> store, const StorePath & storePath) = 0;
278
279 void run(ref<Store> store, StorePaths && storePaths) override;
280};
281
285struct RegisterCommand
286{
287 typedef std::map<std::vector<std::string>, std::function<ref<Command>()>> Commands;
288 static Commands * commands;
289
290 RegisterCommand(std::vector<std::string> && name, std::function<ref<Command>()> command)
291 {
292 if (!commands)
293 commands = new Commands;
294 commands->emplace(name, command);
295 }
296
297 static nix::Commands getCommandsFor(const std::vector<std::string> & prefix);
298};
299
300template<class T>
301static RegisterCommand registerCommand(const std::string & name)
302{
303 return RegisterCommand({name}, []() { return make_ref<T>(); });
304}
305
306template<class T>
307static RegisterCommand registerCommand2(std::vector<std::string> && name)
308{
309 return RegisterCommand(std::move(name), []() { return make_ref<T>(); });
310}
311
312struct MixProfile : virtual StoreCommand
313{
314 std::optional<Path> profile;
315
316 MixProfile();
317
318 /* If 'profile' is set, make it point at 'storePath'. */
319 void updateProfile(const StorePath & storePath);
320
321 /* If 'profile' is set, make it point at the store path produced
322 by 'buildables'. */
323 void updateProfile(const BuiltPaths & buildables);
324};
325
326struct MixDefaultProfile : MixProfile
327{
328 MixDefaultProfile();
329};
330
331struct MixEnvironment : virtual Args
332{
333
334 StringSet keepVars;
335 StringSet unsetVars;
336 std::map<std::string, std::string> setVars;
337 bool ignoreEnvironment;
338
339 MixEnvironment();
340
341 /***
342 * Modify global environ based on `ignoreEnvironment`, `keep`,
343 * `unset`, and `added`. It's expected that exec will be called
344 * before this class goes out of scope, otherwise `environ` will
345 * become invalid.
346 */
347 void setEnviron();
348};
349
350void completeFlakeInputPath(
351 AddCompletions & completions,
352 ref<EvalState> evalState,
353 const std::vector<FlakeRef> & flakeRefs,
354 std::string_view prefix);
355
356void completeFlakeRef(AddCompletions & completions, ref<Store> store, std::string_view prefix);
357
358void completeFlakeRefWithFragment(
359 AddCompletions & completions,
360 ref<EvalState> evalState,
361 flake::LockFlags lockFlags,
362 Strings attrPathPrefixes,
363 const Strings & defaultFlakeAttrPaths,
364 std::string_view prefix);
365
366std::string showVersions(const std::set<std::string> & versions);
367
368void printClosureDiff(
369 ref<Store> store, const StorePath & beforePath, const StorePath & afterPath, std::string_view indent);
370
375void createOutLinks(const std::filesystem::path & outLink, const BuiltPaths & buildables, LocalFSStore & store);
376
377}
Definition args.hh:419
Definition args.hh:28
std::function< CompleterFun > CompleterClosure
Definition args.hh:167
Definition eval.hh:182
Definition local-fs-store.hh:46
Definition path.hh:27
Definition store-api.hh:169
Definition ref.hh:15
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
ChunkedVector< std::string, 8192 > store
Definition lexer.l:1011
const std::string_view & name
Definition lexer.l:1709
Definition args.hh:351
ref< Store > getDstStore() override
Definition command.cc:96
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:882
Definition command.hh:204
virtual std::vector< FlakeRef > getFlakeRefsForCompletion()
Definition command.hh:129
Definition command.hh:32
virtual void run() override
Definition command.cc:40
Definition position.hh:20
std::vector< FlakeRef > getFlakeRefsForCompletion() override
Definition installables.cc:856
Definition command.hh:286
void completeInstallable(AddCompletions &completions, std::string_view prefix)
Definition installables.cc:260
CompleterClosure getCompleteInstallable()
Definition installables.cc:253
virtual void run(ref< Store >)=0
void run() override
Definition command.cc:69
virtual ref< Store > getDstStore()
Definition command.hh:59
ref< Store > getStore()
Definition command.cc:57
Definition command.hh:276