From 0258ac9c2a49eb0cc632bc02d72093392bd37bfc Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 4 Feb 2023 20:45:40 -0500 Subject: [PATCH] Make `--read-only` a separate mixin It is independent of SourceExprCommand, which is about parsing installables, except for the fact that parsing installables is one of the many things influenced by read-only mode. --- src/libcmd/command.hh | 10 +++++++--- src/libcmd/installables.cc | 31 ++++++++++++++----------------- src/nix/eval.cc | 4 ++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index 1516daa00..b6d554aab 100644 --- a/src/libcmd/command.hh +++ b/src/libcmd/command.hh @@ -94,9 +94,8 @@ struct SourceExprCommand : virtual Args, MixFlakeOptions { std::optional file; std::optional expr; - bool readOnlyMode = false; - SourceExprCommand(bool supportReadOnlyMode = false); + SourceExprCommand(); std::vector> parseInstallables( ref store, std::vector ss); @@ -111,6 +110,11 @@ struct SourceExprCommand : virtual Args, MixFlakeOptions void completeInstallable(std::string_view prefix); }; +struct MixReadOnlyOption : virtual Args +{ + MixReadOnlyOption(); +}; + /* A command that operates on a list of "installables", which can be store paths, attribute paths, Nix expressions, etc. */ struct InstallablesCommand : virtual Args, SourceExprCommand @@ -136,7 +140,7 @@ struct InstallableCommand : virtual Args, SourceExprCommand { std::shared_ptr installable; - InstallableCommand(bool supportReadOnlyMode = false); + InstallableCommand(); void prepare() override; diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index cfc13a60f..00c6f9516 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -147,7 +147,7 @@ void MixFlakeOptions::completionHook() completeFlakeInput(*prefix); } -SourceExprCommand::SourceExprCommand(bool supportReadOnlyMode) +SourceExprCommand::SourceExprCommand() { addFlag({ .longName = "file", @@ -169,17 +169,18 @@ SourceExprCommand::SourceExprCommand(bool supportReadOnlyMode) .labels = {"expr"}, .handler = {&expr} }); +} - if (supportReadOnlyMode) { - addFlag({ - .longName = "read-only", - .description = - "Do not instantiate each evaluated derivation. " - "This improves performance, but can cause errors when accessing " - "store paths of derivations during evaluation.", - .handler = {&readOnlyMode, true}, - }); - } +MixReadOnlyOption::MixReadOnlyOption() +{ + addFlag({ + .longName = "read-only", + .description = + "Do not instantiate each evaluated derivation. " + "This improves performance, but can cause errors when accessing " + "store paths of derivations during evaluation.", + .handler = {&settings.readOnlyMode, true}, + }); } Strings SourceExprCommand::getDefaultFlakeAttrPaths() @@ -426,10 +427,6 @@ std::vector> SourceExprCommand::parseInstallables( { std::vector> result; - if (readOnlyMode) { - settings.readOnlyMode = true; - } - if (file || expr) { if (file && expr) throw UsageError("'--file' and '--expr' are exclusive"); @@ -724,8 +721,8 @@ std::vector InstallablesCommand::getFlakesForCompletion() return _installables; } -InstallableCommand::InstallableCommand(bool supportReadOnlyMode) - : SourceExprCommand(supportReadOnlyMode) +InstallableCommand::InstallableCommand() + : SourceExprCommand() { expectArgs({ .label = "installable", diff --git a/src/nix/eval.cc b/src/nix/eval.cc index ccee074e9..a579213fd 100644 --- a/src/nix/eval.cc +++ b/src/nix/eval.cc @@ -11,13 +11,13 @@ using namespace nix; -struct CmdEval : MixJSON, InstallableCommand +struct CmdEval : MixJSON, InstallableCommand, MixReadOnlyOption { bool raw = false; std::optional apply; std::optional writeTo; - CmdEval() : InstallableCommand(true /* supportReadOnlyMode */) + CmdEval() : InstallableCommand() { addFlag({ .longName = "raw",