diff options
Diffstat (limited to 'src/nix/command.hh')
-rw-r--r-- | src/nix/command.hh | 151 |
1 files changed, 70 insertions, 81 deletions
diff --git a/src/nix/command.hh b/src/nix/command.hh index 2db22dba2..4f3f6eb9e 100644 --- a/src/nix/command.hh +++ b/src/nix/command.hh @@ -1,41 +1,23 @@ #pragma once +#include "installables.hh" #include "args.hh" #include "common-eval-args.hh" +#include <optional> + namespace nix { extern std::string programPath; -struct Value; -class Bindings; class EvalState; struct Pos; - -/* A command is an argument parser that can be executed by calling its - run() method. */ -struct Command : virtual Args -{ - virtual ~Command() { } - virtual std::string name() = 0; - virtual void prepare() { }; - virtual void run() = 0; - - struct Example - { - std::string description; - std::string command; - }; - - typedef std::list<Example> Examples; - - virtual Examples examples() { return Examples(); } - - void printHelp(const string & programName, std::ostream & out) override; -}; - class Store; +namespace flake { +enum HandleLockFile : unsigned int; +} + /* A command that requires a Nix store. */ struct StoreCommand : virtual Command { @@ -49,52 +31,44 @@ private: std::shared_ptr<Store> _store; }; -struct Buildable +struct EvalCommand : virtual StoreCommand, MixEvalArgs { - Path drvPath; // may be empty - std::map<std::string, Path> outputs; -}; + ref<EvalState> getEvalState(); -typedef std::vector<Buildable> Buildables; +private: -struct Installable + std::shared_ptr<EvalState> evalState; +}; + +struct MixFlakeOptions : virtual Args { - virtual ~Installable() { } + bool recreateLockFile = false; - virtual std::string what() = 0; + bool saveLockFile = true; - virtual Buildables toBuildables() - { - throw Error("argument '%s' cannot be built", what()); - } + bool useRegistries = true; - Buildable toBuildable(); + MixFlakeOptions(); - virtual Value * toValue(EvalState & state) - { - throw Error("argument '%s' cannot be evaluated", what()); - } + flake::HandleLockFile getLockFileMode(); }; -struct SourceExprCommand : virtual Args, StoreCommand, MixEvalArgs +struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions { - Path file; + std::optional<Path> file; + std::optional<std::string> expr; SourceExprCommand(); - /* Return a value representing the Nix expression from which we - are installing. This is either the file specified by ‘--file’, - or an attribute set constructed from $NIX_PATH, e.g. ‘{ nixpkgs - = import ...; bla = import ...; }’. */ - Value * getSourceExpr(EvalState & state); - - ref<EvalState> getEvalState(); + std::vector<std::shared_ptr<Installable>> parseInstallables( + ref<Store> store, std::vector<std::string> ss); -private: + std::shared_ptr<Installable> parseInstallable( + ref<Store> store, const std::string & installable); - std::shared_ptr<EvalState> evalState; + virtual Strings getDefaultFlakeAttrPaths(); - Value * vSourceExpr = 0; + virtual Strings getDefaultFlakeAttrPathPrefixes(); }; enum RealiseMode { Build, NoBuild, DryRun }; @@ -126,14 +100,14 @@ struct InstallableCommand : virtual Args, SourceExprCommand InstallableCommand() { - expectArg("installable", &_installable); + expectArg("installable", &_installable, true); } void prepare() override; private: - std::string _installable; + std::string _installable{""}; }; /* A command that operates on zero or more store paths. */ @@ -171,41 +145,24 @@ struct StorePathCommand : public InstallablesCommand void run(ref<Store> store) override; }; -typedef std::map<std::string, ref<Command>> Commands; - -/* An argument parser that supports multiple subcommands, - i.e. ‘<command> <subcommand>’. */ -class MultiCommand : virtual Args -{ -public: - Commands commands; - - std::shared_ptr<Command> command; - - MultiCommand(const Commands & commands); - - void printHelp(const string & programName, std::ostream & out) override; - - bool processFlag(Strings::iterator & pos, Strings::iterator end) override; - - bool processArgs(const Strings & args, bool finish) override; -}; - /* A helper class for registering commands globally. */ struct RegisterCommand { static Commands * commands; - RegisterCommand(ref<Command> command) + RegisterCommand(const std::string & name, + std::function<ref<Command>()> command) { if (!commands) commands = new Commands; - commands->emplace(command->name(), command); + commands->emplace(name, command); } }; -std::shared_ptr<Installable> parseInstallable( - SourceExprCommand & cmd, ref<Store> store, const std::string & installable, - bool useDefaultInstallables); +template<class T> +static RegisterCommand registerCommand(const std::string & name) +{ + return RegisterCommand(name, [](){ return make_ref<T>(); }); +} Buildables build(ref<Store> store, RealiseMode mode, std::vector<std::shared_ptr<Installable>> installables); @@ -224,4 +181,36 @@ PathSet toDerivations(ref<Store> store, filename:lineno. */ Strings editorFor(const Pos & pos); +struct MixProfile : virtual Args, virtual StoreCommand +{ + std::optional<Path> profile; + + MixProfile(); + + /* If 'profile' is set, make it point at 'storePath'. */ + void updateProfile(const Path & storePath); + + /* If 'profile' is set, make it point at the store path produced + by 'buildables'. */ + void updateProfile(const Buildables & buildables); +}; + +struct MixDefaultProfile : MixProfile +{ + MixDefaultProfile(); +}; + +struct MixEnvironment : virtual Args { + + StringSet keep, unset; + Strings stringsEnv; + std::vector<char*> vectorEnv; + bool ignoreEnvironment; + + MixEnvironment(); + + /* Modify global environ based on ignoreEnvironment, keep, and unset. It's expected that exec will be called before this class goes out of scope, otherwise environ will become invalid. */ + void setEnviron(); +}; + } |