diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2019-11-27 00:05:30 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2019-11-27 00:05:30 +0100 |
commit | ca8caaec5e7581c37b46f79622c81adf52f06314 (patch) | |
tree | 0e0693236f221f1d9ac0fba265a01c196396cb6a /src/nix | |
parent | 2c6dbcd5e7a16227e0aef008c62c81220f4b3ffc (diff) |
nix: Add --expr flag
This replaces the '(...)' installable syntax, which is not very
discoverable. The downside is that you can't have multiple expressions
or mix expressions and other installables.
Diffstat (limited to 'src/nix')
-rw-r--r-- | src/nix/command.hh | 3 | ||||
-rw-r--r-- | src/nix/eval.cc | 2 | ||||
-rw-r--r-- | src/nix/installables.cc | 33 |
3 files changed, 24 insertions, 14 deletions
diff --git a/src/nix/command.hh b/src/nix/command.hh index 82dbb55d0..42c14927f 100644 --- a/src/nix/command.hh +++ b/src/nix/command.hh @@ -56,6 +56,7 @@ struct MixFlakeOptions : virtual Args struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions { std::optional<Path> file; + std::optional<std::string> expr; SourceExprCommand(); @@ -106,7 +107,7 @@ struct InstallableCommand : virtual Args, SourceExprCommand private: - std::string _installable{"."}; + std::string _installable{""}; }; /* A command that operates on zero or more store paths. */ diff --git a/src/nix/eval.cc b/src/nix/eval.cc index 276fdf003..a991ee608 100644 --- a/src/nix/eval.cc +++ b/src/nix/eval.cc @@ -28,7 +28,7 @@ struct CmdEval : MixJSON, InstallableCommand return { Example{ "To evaluate a Nix expression given on the command line:", - "nix eval '(1 + 2)'" + "nix eval --expr '1 + 2'" }, Example{ "To evaluate a Nix expression from a file or URI:", diff --git a/src/nix/installables.cc b/src/nix/installables.cc index 671cf513a..3e0fe2606 100644 --- a/src/nix/installables.cc +++ b/src/nix/installables.cc @@ -51,8 +51,14 @@ SourceExprCommand::SourceExprCommand() .shortName('f') .longName("file") .label("file") - .description("evaluate a set of attributes from FILE (deprecated)") + .description("evaluate attributes from FILE") .dest(&file); + + mkFlag() + .longName("expr") + .label("expr") + .description("evaluate attributes from EXPR") + .dest(&expr); } Strings SourceExprCommand::getDefaultFlakeAttrPaths() @@ -378,19 +384,25 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables( { std::vector<std::shared_ptr<Installable>> result; - if (file) { + if (file || expr) { + if (file && expr) + throw UsageError("'--file' and '--expr' are exclusive"); + // FIXME: backward compatibility hack - evalSettings.pureEval = false; + if (file) evalSettings.pureEval = false; auto state = getEvalState(); auto vFile = state->allocValue(); - state->evalFile(lookupFileArg(*state, *file), *vFile); - if (ss.empty()) - ss = {""}; + if (file) + state->evalFile(lookupFileArg(*state, *file), *vFile); + else { + auto e = state->parseExprFromString(*expr, absPath(".")); + state->eval(e, *vFile); + } for (auto & s : ss) - result.push_back(std::make_shared<InstallableAttrPath>(*this, vFile, s)); + result.push_back(std::make_shared<InstallableAttrPath>(*this, vFile, s == "." ? "" : s)); } else { @@ -407,10 +419,7 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables( size_t hash; std::optional<Path> storePath; - if (s.compare(0, 1, "(") == 0) - result.push_back(std::make_shared<InstallableExpr>(*this, s)); - - else if (hasPrefix(s, "nixpkgs.")) { + if (hasPrefix(s, "nixpkgs.")) { bool static warned; warnOnce(warned, "the syntax 'nixpkgs.<attr>' is deprecated; use 'nixpkgs:<attr>' instead"); result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef("nixpkgs"), @@ -532,7 +541,7 @@ PathSet toDerivations(ref<Store> store, void InstallablesCommand::prepare() { - if (_installables.empty() && !file && useDefaultInstallables()) + if (_installables.empty() && useDefaultInstallables()) // FIXME: commands like "nix install" should not have a // default, probably. _installables.push_back("."); |