diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index 357785a93d0..9052f44fd10 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(); Installables 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(); +}; + /* Like InstallablesCommand but the installables are not loaded */ struct RawInstallablesCommand : virtual Args, SourceExprCommand { @@ -142,7 +146,7 @@ struct InstallablesCommand : RawInstallablesCommand /* A command that operates on exactly one "installable" */ struct InstallableCommand : virtual Args, SourceExprCommand { - InstallableCommand(bool supportReadOnlyMode = false); + InstallableCommand(); virtual void runI(ref store, ref installable) = 0; diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index a4555076943..f1ba7ed3c7f 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 @@ Installables SourceExprCommand::parseInstallables( { Installables result; - if (readOnlyMode) { - settings.readOnlyMode = true; - } - if (file || expr) { if (file && expr) throw UsageError("'--file' and '--expr' are exclusive"); @@ -733,8 +730,8 @@ std::vector InstallablesCommand::getFlakesForCompletion() return res; } -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 c0d9ea456c5..421a1c246b2 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",