Skip to content

Commit

Permalink
TEMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Nov 13, 2023
1 parent 51877bf commit 2b22320
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace nix {

HasEvalState::HasEvalState(AbstractArgs & args)
: MixEvalArgs(args)
: MixRepair(args)
, MixEvalArgs(args)
{
args.addFlag({
.longName = "debugger",
Expand Down
6 changes: 4 additions & 2 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ private:
struct EvalCommand : virtual DrvCommand, virtual HasEvalState
{
EvalCommand()
: HasEvalState(static_cast<DrvCommand &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<DrvCommand &>(*this))
{ }
};

Expand Down Expand Up @@ -95,7 +96,8 @@ struct ParseInstallableValueArgs : virtual ParseInstallableArgs, virtual MixFlak
struct SourceExprCommand : virtual EvalCommand, ParseInstallableValueArgs, virtual GetRawInstallables
{
SourceExprCommand()
: HasEvalState(static_cast<DrvCommand &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<DrvCommand &>(*this))
, MixFlakeOptions(static_cast<EvalCommand &>(*this))
, ParseInstallableValueArgs(static_cast<GetRawInstallables &>(*this))
{ }
Expand Down
1 change: 1 addition & 0 deletions src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace nix {

MixEvalArgs::MixEvalArgs(AbstractArgs & args)
: MixRepair(args)
{
args.addFlag({
.longName = "arg",
Expand Down
18 changes: 10 additions & 8 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void completeFlakeInputPath(
}

MixFlakeOptions::MixFlakeOptions(AbstractArgs & args)
: HasEvalState(args)
: MixRepair(args)
, HasEvalState(args)
{
auto category = "Common flake-related options";

Expand Down Expand Up @@ -90,7 +91,7 @@ MixFlakeOptions::MixFlakeOptions(AbstractArgs & args)
lockFlags.writeLockFile = false;
lockFlags.inputOverrides.insert_or_assign(
flake::parseInputPath(inputPath),
parseFlakeRef(flakeRef, absPath(getCommandBaseDir()), true));
parseFlakeRef(flakeRef, absPath(args.getCommandBaseDir()), true));
}},
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
if (n == 0) {
Expand Down Expand Up @@ -132,7 +133,7 @@ MixFlakeOptions::MixFlakeOptions(AbstractArgs & args)
auto evalState = getEvalState();
auto flake = flake::lockFlake(
*evalState,
parseFlakeRef(flakeRef, absPath(getCommandBaseDir())),
parseFlakeRef(flakeRef, absPath(args.getCommandBaseDir())),
{ .writeLockFile = false });
for (auto & [inputName, input] : flake.lockFile.root->inputs) {
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
Expand All @@ -151,7 +152,8 @@ MixFlakeOptions::MixFlakeOptions(AbstractArgs & args)
}

ParseInstallableValueArgs::ParseInstallableValueArgs(GetRawInstallables & args)
: HasEvalState(args)
: MixRepair(args)
, HasEvalState(args)
, MixFlakeOptions(args)
, args(args)
{
Expand Down Expand Up @@ -424,10 +426,10 @@ Installables ParseInstallableValueArgs::parseInstallables(
state->eval(e, *vFile);
}
else if (file) {
state->evalFile(lookupFileArg(*state, *file, CanonPath::fromCwd(getCommandBaseDir())), *vFile);
state->evalFile(lookupFileArg(*state, *file, CanonPath::fromCwd(args.getCommandBaseDir())), *vFile);
}
else {
CanonPath dir(CanonPath::fromCwd(getCommandBaseDir()));
CanonPath dir(CanonPath::fromCwd(args.getCommandBaseDir()));
auto e = state->parseExprFromString(*expr, state->rootPath(dir));
state->eval(e, *vFile);
}
Expand Down Expand Up @@ -463,7 +465,7 @@ Installables ParseInstallableValueArgs::parseInstallables(
}

try {
auto [flakeRef, fragment] = parseFlakeRefWithFragment(std::string { prefix }, absPath(getCommandBaseDir()));
auto [flakeRef, fragment] = parseFlakeRefWithFragment(std::string { prefix }, absPath(args.getCommandBaseDir()));
result.push_back(make_ref<InstallableFlake>(
this,
getEvalState(),
Expand Down Expand Up @@ -508,7 +510,7 @@ std::vector<FlakeRef> ParseInstallableValueArgs::getFlakeRefsForCompletion()
for (auto i : args.getRawInstallables())
res.push_back(parseFlakeRefWithFragment(
expandTilde(i),
absPath(getCommandBaseDir())).first);
absPath(args.getCommandBaseDir())).first);
return res;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libmain/common-args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ struct MixJSON : virtual Args
}
};

struct MixRepair : virtual Args
struct MixRepair
{
RepairFlag repair = NoRepair;

MixRepair()
MixRepair(AbstractArgs & args)
{
addFlag({
args.addFlag({
.longName = "repair",
.description =
"During evaluation, rewrite missing or corrupted files in the Nix store. "
Expand Down
22 changes: 12 additions & 10 deletions src/libutil/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ struct AbstractArgs

virtual void expectArgs(ExpectedArg && arg) = 0;

/**
* @brief Get the base directory for the command.
*
* @return Generally the working directory, but in case of a shebang
* interpreter, returns the directory of the script.
*
* This only returns the correct value after parseCmdline() has run.
*/
virtual Path getCommandBaseDir() const = 0;

static CompleterFun completePath;

static CompleterFun completeDir;
Expand All @@ -192,16 +202,6 @@ public:
*/
virtual std::string doc() { return ""; }

/**
* @brief Get the base directory for the command.
*
* @return Generally the working directory, but in case of a shebang
* interpreter, returns the directory of the script.
*
* This only returns the correct value after parseCmdline() has run.
*/
virtual Path getCommandBaseDir() const;

protected:

/**
Expand Down Expand Up @@ -276,6 +276,8 @@ public:
expectedArgs.emplace_back(std::move(arg));
}

virtual Path getCommandBaseDir() const override;

/**
* Expect a string argument.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ static void main_nix_build(int argc, char * * argv)
{
MyArgs(const std::string & programName,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
: LegacyArgs(programName, parseArg)
, MixEvalArgs((LegacyArgs &) *this)
: MixRepair(static_cast<LegacyArgs &>(*this))
, LegacyArgs(programName, parseArg)
, MixEvalArgs(static_cast<LegacyArgs &>(*this))
{ }
};

Expand Down
5 changes: 3 additions & 2 deletions src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,9 @@ static int main_nix_env(int argc, char * * argv)
{
MyArgs(const std::string & programName,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
: LegacyArgs(programName, parseArg)
, MixEvalArgs((LegacyArgs &) *this)
: MixRepair(static_cast<LegacyArgs &>(*this))
, LegacyArgs(programName, parseArg)
, MixEvalArgs(static_cast<LegacyArgs &>(*this))
{ }
};

Expand Down
5 changes: 3 additions & 2 deletions src/nix-instantiate/nix-instantiate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ static int main_nix_instantiate(int argc, char * * argv)
{
MyArgs(const std::string & programName,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg)
: LegacyArgs(programName, parseArg)
, MixEvalArgs((LegacyArgs &) *this)
: MixRepair(static_cast<LegacyArgs &>(*this))
, LegacyArgs(programName, parseArg)
, MixEvalArgs(static_cast<LegacyArgs &>(*this))
{ }
};

Expand Down
1 change: 1 addition & 0 deletions src/nix/common/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile, MixRepair
BuildMode buildMode = bmNormal;

CmdBuild()
: MixRepair(static_cast<Command &>(*this))
{
addFlag({
.longName = "out-link",
Expand Down
3 changes: 2 additions & 1 deletion src/nix/full/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct CmdBundle : InstallableValueCommand
std::optional<Path> outLink;

CmdBundle()
: HasEvalState(static_cast<Command &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<Command &>(*this))
, MixFlakeOptions(static_cast<Command &>(*this))
{
addFlag({
Expand Down
3 changes: 2 additions & 1 deletion src/nix/full/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace nix {
struct ParseInstallableValueAdapter : ParseInstallableValueArgs
{
ParseInstallableValueAdapter(GetRawInstallables & args)
: HasEvalState(args)
: MixRepair(args)
, HasEvalState(args)
, MixFlakeOptions(args)
, ParseInstallableValueArgs(args)
{ }
Expand Down
6 changes: 4 additions & 2 deletions src/nix/full/develop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ struct CmdDevelop : Common, MixEnvironment
std::optional<std::string> phase;

CmdDevelop()
: HasEvalState(static_cast<Command &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<Command &>(*this))
, MixFlakeOptions(static_cast<Command &>(*this))
{
addFlag({
Expand Down Expand Up @@ -671,7 +672,8 @@ struct CmdDevelop : Common, MixEnvironment
struct CmdPrintDevEnv : Common, MixJSON
{
CmdPrintDevEnv()
: HasEvalState(static_cast<Command &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<Command &>(*this))
, MixFlakeOptions(static_cast<Command &>(*this))
{ }

Expand Down
3 changes: 2 additions & 1 deletion src/nix/full/edit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ using namespace nix;
struct CmdEdit : InstallableValueCommand
{
CmdEdit()
: HasEvalState(static_cast<Command &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<Command &>(*this))
, MixFlakeOptions(static_cast<Command &>(*this))
{ }

Expand Down
3 changes: 2 additions & 1 deletion src/nix/full/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
std::optional<Path> writeTo;

CmdEval()
: HasEvalState(static_cast<Command &>(*this))
: MixRepair(static_cast<Command &>(*this))
, HasEvalState(static_cast<Command &>(*this))
, MixFlakeOptions(static_cast<Command &>(*this))
, InstallableValueCommand()
, MixReadOnlyOption(static_cast<Command &>(*this))
Expand Down
Loading

0 comments on commit 2b22320

Please sign in to comment.