about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2012-11-26T14·39+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2012-11-26T14·39+0100
commit46a369ad9558939bc2c6ee588df483ca503bbb5a (patch)
tree7a3fc4d49d0a5fb29d1c6e139672d91f86e71f47
parenta3d6585c5a1006d4f9ebd2163d06f86ab71a4a3e (diff)
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.
-rw-r--r--corepkgs/derivation.nix6
-rwxr-xr-xscripts/nix-build.in24
-rw-r--r--src/libstore/build.cc8
-rw-r--r--src/libstore/derivations.cc17
-rw-r--r--src/libstore/derivations.hh9
-rw-r--r--src/libstore/misc.cc11
-rw-r--r--src/libutil/util.cc14
-rw-r--r--src/libutil/util.hh1
-rw-r--r--src/nix-instantiate/nix-instantiate.cc16
-rw-r--r--src/nix-store/nix-store.cc20
10 files changed, 106 insertions, 20 deletions
diff --git a/corepkgs/derivation.nix b/corepkgs/derivation.nix
index 757108be3614..c0fbe8082cd3 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 427bc605b562..b82cb2693d6e 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 (<OUTPATHS>) {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 9e0db9ee7f0a..5e5cd6b23bb9 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 e0a4f43c0bb8..1551ae28a81a 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<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);
+}
+
+
 }
diff --git a/src/libstore/derivations.hh b/src/libstore/derivations.hh
index 7d38e60c0fe6..6b7c3e6ab189 100644
--- a/src/libstore/derivations.hh
+++ b/src/libstore/derivations.hh
@@ -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);
+
+
 }
diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc
index 3ce300e30678..dacd1d3d7474 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 e208701cea8a..1308eac31293 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -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");
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 87b63f6e998a..746b2dd5859f 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -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. */
diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc
index ab0c8cf28bdf..a5053c32376d 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 <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 : "");
                 }
             }
         }
diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc
index e973beda9f0f..c0da37d251da 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<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;