From 46a369ad9558939bc2c6ee588df483ca503bbb5a Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 26 Nov 2012 15:39:10 +0100 Subject: [PATCH] Make "nix-build -A ." 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. --- corepkgs/derivation.nix | 6 +++--- scripts/nix-build.in | 24 +++++++++++++++++++++--- src/libstore/build.cc | 8 +++++--- src/libstore/derivations.cc | 17 +++++++++++++++++ src/libstore/derivations.hh | 9 +++++++++ src/libstore/misc.cc | 11 +++++++---- src/libutil/util.cc | 14 +++++++++++++- src/libutil/util.hh | 1 + src/nix-instantiate/nix-instantiate.cc | 16 +++++++++++++++- src/nix-store/nix-store.cc | 20 +++++++++++++++----- 10 files changed, 106 insertions(+), 20 deletions(-) diff --git a/corepkgs/derivation.nix b/corepkgs/derivation.nix index 757108be3..c0fbe8082 100644 --- a/corepkgs/derivation.nix +++ b/corepkgs/derivation.nix @@ -6,7 +6,7 @@ drvAttrs @ { outputs ? [ "out" ], ... }: let strict = derivationStrict drvAttrs; - + commonAttrs = drvAttrs // (builtins.listToAttrs outputsList) // { all = map (x: x.value) outputsList; inherit drvAttrs; @@ -21,7 +21,7 @@ let inherit outputName; }; }; - + outputsList = map outputToAttrListElement outputs; - + in (builtins.head outputsList).value diff --git a/scripts/nix-build.in b/scripts/nix-build.in index 427bc605b..b82cb2693 100755 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -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 () {chomp; push @outPaths, $_;} if (!close OUTPATHS) { die "nix-store killed by signal " . ($? & 127) . "\n" if ($? & 127); diff --git a/src/libstore/build.cc b/src/libstore/build.cc index 9e0db9ee7..5e5cd6b23 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -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); diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index e0a4f43c0..1551ae28a 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -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()) + : DrvPathWithOutputs(string(s, 0, n), tokenizeString >(string(s, n + 1), ",")); +} + + +Path makeDrvPathWithOutputs(const Path & drvPath, std::set outputs) +{ + return outputs.empty() + ? drvPath + : drvPath + "!" + concatStringsSep(",", outputs); +} + + } diff --git a/src/libstore/derivations.hh b/src/libstore/derivations.hh index 7d38e60c0..6b7c3e6ab 100644 --- a/src/libstore/derivations.hh +++ b/src/libstore/derivations.hh @@ -79,4 +79,13 @@ typedef std::map 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 > DrvPathWithOutputs; +DrvPathWithOutputs parseDrvPathWithOutputs(const string & s); + +Path makeDrvPathWithOutputs(const Path & drvPath, std::set outputs); + + } diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc index 3ce300e30..dacd1d3d7 100644 --- a/src/libstore/misc.cc +++ b/src/libstore/misc.cc @@ -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()); } diff --git a/src/libutil/util.cc b/src/libutil/util.cc index e208701ce..1308eac31 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -997,13 +997,14 @@ template 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 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"); diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 87b63f6e9..746b2dd58 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -293,6 +293,7 @@ template 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. */ diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc index ab0c8cf28..a5053c323 100644 --- a/src/nix-instantiate/nix-instantiate.cc +++ b/src/nix-instantiate/nix-instantiate.cc @@ -8,6 +8,7 @@ #include "util.hh" #include "store-api.hh" #include "common-opts.hh" +#include "misc.hh" #include #include @@ -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 : ""); } } } diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index e973beda9..c0da37d25 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -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(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;