Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make --read-only a separate mixin #7755

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ struct SourceExprCommand : virtual Args, MixFlakeOptions
{
std::optional<Path> file;
std::optional<std::string> expr;
bool readOnlyMode = false;

SourceExprCommand(bool supportReadOnlyMode = false);
SourceExprCommand();

std::vector<std::shared_ptr<Installable>> parseInstallables(
ref<Store> store, std::vector<std::string> ss);
Expand All @@ -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
Expand All @@ -136,7 +140,7 @@ struct InstallableCommand : virtual Args, SourceExprCommand
{
std::shared_ptr<Installable> installable;

InstallableCommand(bool supportReadOnlyMode = false);
InstallableCommand();

void prepare() override;

Expand Down
31 changes: 14 additions & 17 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void MixFlakeOptions::completionHook()
completeFlakeInput(*prefix);
}

SourceExprCommand::SourceExprCommand(bool supportReadOnlyMode)
SourceExprCommand::SourceExprCommand()
{
addFlag({
.longName = "file",
Expand All @@ -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()
Expand Down Expand Up @@ -426,10 +427,6 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
{
std::vector<std::shared_ptr<Installable>> result;

if (readOnlyMode) {
settings.readOnlyMode = true;
}

if (file || expr) {
if (file && expr)
throw UsageError("'--file' and '--expr' are exclusive");
Expand Down Expand Up @@ -724,8 +721,8 @@ std::vector<std::string> InstallablesCommand::getFlakesForCompletion()
return _installables;
}

InstallableCommand::InstallableCommand(bool supportReadOnlyMode)
: SourceExprCommand(supportReadOnlyMode)
InstallableCommand::InstallableCommand()
: SourceExprCommand()
{
expectArgs({
.label = "installable",
Expand Down
4 changes: 2 additions & 2 deletions src/nix/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

using namespace nix;

struct CmdEval : MixJSON, InstallableCommand
struct CmdEval : MixJSON, InstallableCommand, MixReadOnlyOption
{
bool raw = false;
std::optional<std::string> apply;
std::optional<Path> writeTo;

CmdEval() : InstallableCommand(true /* supportReadOnlyMode */)
CmdEval() : InstallableCommand()
{
addFlag({
.longName = "raw",
Expand Down