Skip to content

Commit

Permalink
nix develop: Add convenience flags for running specific phases
Browse files Browse the repository at this point in the history
For example, for building the Nix flake, you would do:

  $ nix develop --configure
  $ nix develop --install
  $ nix develop --installcheck
  • Loading branch information
edolstra committed Aug 28, 2020
1 parent 50a8710 commit f156513
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/nix/develop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ struct Common : InstallableCommand, MixProfile
struct CmdDevelop : Common, MixEnvironment
{
std::vector<std::string> command;
std::optional<std::string> phase;

CmdDevelop()
{
Expand All @@ -277,6 +278,43 @@ struct CmdDevelop : Common, MixEnvironment
command = ss;
}}
});

addFlag({
.longName = "phase",
.description = "phase to run (e.g. `build` or `configure`)",
.labels = {"phase-name"},
.handler = {&phase},
});

addFlag({
.longName = "configure",
.description = "run the configure phase",
.handler = {&phase, {"configure"}},
});

addFlag({
.longName = "build",
.description = "run the build phase",
.handler = {&phase, {"build"}},
});

addFlag({
.longName = "check",
.description = "run the check phase",
.handler = {&phase, {"check"}},
});

addFlag({
.longName = "install",
.description = "run the install phase",
.handler = {&phase, {"install"}},
});

addFlag({
.longName = "installcheck",
.description = "run the installcheck phase",
.handler = {&phase, {"installCheck"}},
});
}

std::string description() override
Expand Down Expand Up @@ -314,9 +352,22 @@ struct CmdDevelop : Common, MixEnvironment

auto script = makeRcScript(buildEnvironment);

script += fmt("rm -f '%s'\n", rcFilePath);
if (verbosity >= lvlDebug)
script += "set -x\n";

script += fmt("rm -f '%s'\n", rcFilePath);

if (phase) {
if (!command.empty())
throw UsageError("you cannot use both '--command' and '--phase'");
// FIXME: foundMakefile is set by buildPhase, need to get
// rid of that.
script += fmt("foundMakefile=1\n");
script += fmt("runHook %1%Phase\n", *phase);
script += fmt("exit 0\n", *phase);
}

if (!command.empty()) {
else if (!command.empty()) {
std::vector<std::string> args;
for (auto s : command)
args.push_back(shellEscape(s));
Expand Down

6 comments on commit f156513

@Ericson2314
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want to add nixpkgs-specific functionality like this?

@edolstra
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix-shell has always been nixpkgs-specific (e.g. it assumes the existence of $stdenv/setup).

@matthewbauer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This should error when multiple phases are set since nix develop --configure --build is just nix develop --build

  • unpack might also be useful here when you're not in the correct directory (for instance, nix develop nixpkgs#git --unpack)

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented on f156513 Aug 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix-shell has always been nixpkgs-specific (e.g. it assumes the existence of $stdenv/setup).

Indeed it has, but that also caused me trouble in the past! (such as when i wanted to switch the propagated deps files to use newline or \0 as a separator.)

This change, in particular, would make something like NixOS/rfcs#32 harder to do, right?

@matthewbauer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should avoid the need for NixOS/rfcs#32, which is nice since it's a breaking change.

@Ericson2314
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewbauer But see the secondary goal, about custom phases still having pre and post hooks be run. It's still useful for that.

Please sign in to comment.