1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 21:41:48 +02:00

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.
This commit is contained in:
Eelco Dolstra 2019-11-27 00:05:30 +01:00
parent 2c6dbcd5e7
commit ca8caaec5e
9 changed files with 86 additions and 76 deletions

View file

@ -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. */

View file

@ -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:",

View file

@ -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(".");