Make "nix-build -A <derivation>.<output>" do the right thing

For example, given a derivation with outputs "out", "man" and "bin":

  $ nix-build -A pkg

produces ./result pointing to the "out" output;

  $ nix-build -A pkg.man

produces ./result-man pointing to the "man" output;

  $ nix-build -A pkg.all

produces ./result, ./result-man and ./result-bin;

  $ nix-build -A pkg.all -A pkg2

produces ./result, ./result-man, ./result-bin and ./result-2.
This commit is contained in:
Eelco Dolstra 2012-11-26 15:39:10 +01:00
parent a3d6585c5a
commit 46a369ad95
10 changed files with 106 additions and 20 deletions

View file

@ -19,7 +19,7 @@ my $envCommand = "p=\$PATH; source \$stdenv/setup; PATH=\$PATH:\$p; exec $shell"
my @envExclude = ();
my $tmpDir = tempdir("nix-build.XXXXXX", CLEANUP => 1, TMPDIR => 1)
my $tmpDir = tempdir("nix-build.XXXXXX", CLEANUP => 0, TMPDIR => 1)
or die "cannot create a temporary directory";
my $outLink = "./result";
@ -181,15 +181,33 @@ foreach my $expr (@exprs) {
die;
}
# Ugly hackery to make "nix-build -A foo.all" produce symlinks
# ./result, ./result-dev, and so on, rather than ./result,
# ./result-2-dev, and so on. This combines multiple derivation
# paths into one "/nix/store/drv-path!out1,out2,..." argument.
my $prevDrvPath = "";
my @drvPaths2;
foreach my $drvPath (@drvPaths) {
my $target = readlink $drvPath or die "cannot read symlink `$drvPath'";
my $p = $drvPath; my $output = "out";
if ($drvPath =~ /(.*)!(.*)/) {
$p = $1; $output = $2;
} else {
$p = $drvPath;
}
my $target = readlink $p or die "cannot read symlink `$p'";
print STDERR "derivation is $target\n" if $verbose;
if ($target eq $prevDrvPath) {
push @drvPaths2, (pop @drvPaths2) . "," . $output;
} else {
push @drvPaths2, $target . "!" . $output;
$prevDrvPath = $target;
}
}
# Build.
my @outPaths;
$pid = open(OUTPATHS, "-|") || exec "$Nix::Config::binDir/nix-store", "--add-root", $outLink, "--indirect", "-r",
@buildArgs, @drvPaths;
@buildArgs, @drvPaths2;
while (<OUTPATHS>) {chomp; push @outPaths, $_;}
if (!close OUTPATHS) {
die "nix-store killed by signal " . ($? & 127) . "\n" if ($? & 127);

View file

@ -3197,11 +3197,13 @@ void LocalStore::buildPaths(const PathSet & drvPaths, bool repair)
Worker worker(*this);
Goals goals;
foreach (PathSet::const_iterator, i, drvPaths)
if (isDerivation(*i))
goals.insert(worker.makeDerivationGoal(*i, repair));
foreach (PathSet::const_iterator, i, drvPaths) {
DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i);
if (isDerivation(i2.first))
goals.insert(worker.makeDerivationGoal(i2.first, repair));
else
goals.insert(worker.makeSubstitutionGoal(*i, repair));
}
worker.run(goals);

View file

@ -252,4 +252,21 @@ Hash hashDerivationModulo(StoreAPI & store, Derivation drv)
}
DrvPathWithOutputs parseDrvPathWithOutputs(const string & s)
{
size_t n = s.find("!");
return n == s.npos
? DrvPathWithOutputs(s, std::set<string>())
: DrvPathWithOutputs(string(s, 0, n), tokenizeString<std::set<string> >(string(s, n + 1), ","));
}
Path makeDrvPathWithOutputs(const Path & drvPath, std::set<string> outputs)
{
return outputs.empty()
? drvPath
: drvPath + "!" + concatStringsSep(",", outputs);
}
}

View file

@ -79,4 +79,13 @@ typedef std::map<Path, Hash> DrvHashes;
extern DrvHashes drvHashes;
/* Split a string specifying a derivation and a set of outputs
(/nix/store/hash-foo!out1,out2,...) into the derivation path and
the outputs. */
typedef std::pair<string, std::set<string> > DrvPathWithOutputs;
DrvPathWithOutputs parseDrvPathWithOutputs(const string & s);
Path makeDrvPathWithOutputs(const Path & drvPath, std::set<string> outputs);
}

View file

@ -82,20 +82,23 @@ void queryMissing(StoreAPI & store, const PathSet & targets,
if (done.find(*i) != done.end()) continue;
done.insert(*i);
if (isDerivation(*i)) {
if (!store.isValidPath(*i)) {
DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i);
if (isDerivation(i2.first)) {
if (!store.isValidPath(i2.first)) {
// FIXME: we could try to substitute p.
unknown.insert(*i);
continue;
}
Derivation drv = derivationFromPath(store, *i);
Derivation drv = derivationFromPath(store, i2.first);
PathSet invalid;
// FIXME: only fetch the desired outputs
foreach (DerivationOutputs::iterator, j, drv.outputs)
if (!store.isValidPath(j->second.path)) invalid.insert(j->second.path);
if (invalid.empty()) continue;
todoDrv.insert(*i);
todoDrv.insert(i2.first);
if (settings.useSubstitutes) query.insert(invalid.begin(), invalid.end());
}

View file

@ -997,13 +997,14 @@ template<class C> C tokenizeString(const string & s, const string & separators)
string::size_type end = s.find_first_of(separators, pos + 1);
if (end == string::npos) end = s.size();
string token(s, pos, end - pos);
result.push_back(token);
result.insert(result.end(), token);
pos = s.find_first_not_of(separators, end);
}
return result;
}
template Strings tokenizeString(const string & s, const string & separators);
template StringSet tokenizeString(const string & s, const string & separators);
template vector<string> tokenizeString(const string & s, const string & separators);
@ -1018,6 +1019,17 @@ string concatStringsSep(const string & sep, const Strings & ss)
}
string concatStringsSep(const string & sep, const StringSet & ss)
{
string s;
foreach (StringSet::const_iterator, i, ss) {
if (s.size() != 0) s += sep;
s += *i;
}
return s;
}
string chomp(const string & s)
{
size_t i = s.find_last_not_of(" \n\r\t");

View file

@ -293,6 +293,7 @@ template<class C> C tokenizeString(const string & s, const string & separators =
/* Concatenate the given strings with a separator between the
elements. */
string concatStringsSep(const string & sep, const Strings & ss);
string concatStringsSep(const string & sep, const StringSet & ss);
/* Remove trailing whitespace from a string. */

View file

@ -8,6 +8,7 @@
#include "util.hh"
#include "store-api.hh"
#include "common-opts.hh"
#include "misc.hh"
#include <map>
#include <iostream>
@ -59,6 +60,19 @@ void processExpr(EvalState & state, const Strings & attrPaths,
getDerivations(state, v, "", autoArgs, drvs, false);
foreach (DrvInfos::iterator, i, drvs) {
Path drvPath = i->queryDrvPath(state);
/* What output do we want? */
Path outPath = i->queryOutPath(state);
Derivation drv = derivationFromPath(*store, drvPath);
string outputName;
foreach (DerivationOutputs::iterator, i, drv.outputs)
if (i->second.path == outPath) {
outputName = i->first;
break;
}
if (outputName == "")
throw Error(format("derivation `%1%' does not have an output `%2%'") % drvPath % outPath);
if (gcRoot == "")
printGCWarning();
else {
@ -66,7 +80,7 @@ void processExpr(EvalState & state, const Strings & attrPaths,
if (++rootNr > 1) rootName += "-" + int2String(rootNr);
drvPath = addPermRoot(*store, drvPath, rootName, indirectRoot);
}
std::cout << format("%1%\n") % drvPath;
std::cout << format("%1%%2%\n") % drvPath % (outputName != "out" ? "!" + outputName : "");
}
}
}

View file

@ -60,13 +60,21 @@ static Path useDeriver(Path path)
other paths it means ensure their validity. */
static PathSet realisePath(const Path & path, bool build = true)
{
if (isDerivation(path)) {
DrvPathWithOutputs p = parseDrvPathWithOutputs(path);
if (isDerivation(p.first)) {
if (build) store->buildPaths(singleton<PathSet>(path));
Derivation drv = derivationFromPath(*store, path);
Derivation drv = derivationFromPath(*store, p.first);
rootNr++;
if (p.second.empty())
foreach (DerivationOutputs::iterator, i, drv.outputs) p.second.insert(i->first);
PathSet outputs;
foreach (DerivationOutputs::iterator, i, drv.outputs) {
foreach (StringSet::iterator, j, p.second) {
DerivationOutputs::iterator i = drv.outputs.find(*j);
if (i == drv.outputs.end())
throw Error(format("derivation `%1%' does not have an output named `%2%'") % p.first % *j);
Path outPath = i->second.path;
if (gcRoot == "")
printGCWarning();
@ -103,8 +111,10 @@ static void opRealise(Strings opFlags, Strings opArgs)
else throw UsageError(format("unknown flag `%1%'") % *i);
Paths paths;
foreach (Strings::iterator, i, opArgs)
paths.push_back(followLinksToStorePath(*i));
foreach (Strings::iterator, i, opArgs) {
DrvPathWithOutputs p = parseDrvPathWithOutputs(*i);
paths.push_back(makeDrvPathWithOutputs(followLinksToStorePath(p.first), p.second));
}
unsigned long long downloadSize, narSize;
PathSet willBuild, willSubstitute, unknown;