diff options
Diffstat (limited to 'src/nix/command.hh')
-rw-r--r-- | src/nix/command.hh | 136 |
1 files changed, 57 insertions, 79 deletions
diff --git a/src/nix/command.hh b/src/nix/command.hh index fad404c73..13f3a0dc9 100644 --- a/src/nix/command.hh +++ b/src/nix/command.hh @@ -1,39 +1,22 @@ #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; - -/* A command is an argument parser that can be executed by calling its - run() method. */ -struct Command : virtual Args -{ - 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 { @@ -47,50 +30,43 @@ 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(); + +private: -typedef std::vector<Buildable> Buildables; + std::shared_ptr<EvalState> evalState; +}; -struct Installable +struct MixFlakeOptions : virtual Args { - virtual std::string what() = 0; + bool recreateLockFile = false; - virtual Buildables toBuildables() - { - throw Error("argument '%s' cannot be built", what()); - } + bool saveLockFile = true; - Buildable toBuildable(); + bool useRegistries = true; - virtual Value * toValue(EvalState & state) - { - throw Error("argument '%s' cannot be evaluated", what()); - } + MixFlakeOptions(); + + flake::HandleLockFile getLockFileMode(); }; -struct SourceExprCommand : virtual Args, StoreCommand, MixEvalArgs +struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions { - Path file; + std::optional<Path> file; 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 }; @@ -122,14 +98,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. */ @@ -167,41 +143,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); @@ -216,4 +175,23 @@ PathSet toDerivations(ref<Store> store, std::vector<std::shared_ptr<Installable>> installables, bool useDeriver = false); +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(); +}; + } |