2005-01-19 16:39:47 +00:00
|
|
|
#include "derivations.hh"
|
2006-11-30 17:43:04 +00:00
|
|
|
#include "store-api.hh"
|
2006-12-01 21:00:39 +00:00
|
|
|
#include "globals.hh"
|
2008-08-25 13:31:57 +00:00
|
|
|
#include "util.hh"
|
2011-07-20 18:10:47 +00:00
|
|
|
#include "misc.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2011-07-20 18:10:47 +00:00
|
|
|
void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash & hash) const
|
|
|
|
{
|
|
|
|
recursive = false;
|
|
|
|
string algo = hashAlgo;
|
|
|
|
|
|
|
|
if (string(algo, 0, 2) == "r:") {
|
|
|
|
recursive = true;
|
|
|
|
algo = string(algo, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashType = parseHashType(algo);
|
|
|
|
if (hashType == htUnknown)
|
|
|
|
throw Error(format("unknown hash algorithm `%1%'") % algo);
|
|
|
|
|
|
|
|
hash = parseHash(hashType, this->hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-19 14:36:00 +00:00
|
|
|
Path writeDerivation(const Derivation & drv, const string & name)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
2005-01-25 21:28:25 +00:00
|
|
|
PathSet references;
|
|
|
|
references.insert(drv.inputSrcs.begin(), drv.inputSrcs.end());
|
2009-04-21 11:52:16 +00:00
|
|
|
foreach (DerivationInputs::const_iterator, i, drv.inputDrvs)
|
2005-01-25 21:28:25 +00:00
|
|
|
references.insert(i->first);
|
|
|
|
/* Note that the outputs of a derivation are *not* references
|
|
|
|
(that can be missing (of course) and should not necessarily be
|
|
|
|
held during a garbage collection). */
|
2006-12-01 21:00:39 +00:00
|
|
|
string suffix = name + drvExtension;
|
2010-04-19 13:46:58 +00:00
|
|
|
string contents = unparseDerivation(drv);
|
2006-12-01 21:00:39 +00:00
|
|
|
return readOnlyMode
|
2007-01-29 15:51:37 +00:00
|
|
|
? computeStorePathForText(suffix, contents, references)
|
2006-12-01 21:00:39 +00:00
|
|
|
: store->addTextToStore(suffix, contents, references);
|
2003-07-04 12:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
static Path parsePath(std::istream & str)
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
{
|
2010-04-19 13:46:58 +00:00
|
|
|
string s = parseString(str);
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
if (s.size() == 0 || s[0] != '/')
|
2005-01-20 16:01:07 +00:00
|
|
|
throw Error(format("bad path `%1%' in derivation") % s);
|
2010-04-19 13:46:58 +00:00
|
|
|
return s;
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
static StringSet parseStrings(std::istream & str, bool arePaths)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
2010-04-19 13:46:58 +00:00
|
|
|
StringSet res;
|
|
|
|
while (!endOfList(str))
|
|
|
|
res.insert(arePaths ? parsePath(str) : parseString(str));
|
|
|
|
return res;
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
2010-04-19 13:46:58 +00:00
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
Derivation parseDerivation(const string & s)
|
2003-07-15 21:24:05 +00:00
|
|
|
{
|
2005-01-19 11:16:11 +00:00
|
|
|
Derivation drv;
|
2010-04-19 13:46:58 +00:00
|
|
|
std::istringstream str(s);
|
|
|
|
expect(str, "Derive([");
|
2003-11-16 17:46:31 +00:00
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
/* Parse the list of outputs. */
|
|
|
|
while (!endOfList(str)) {
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
DerivationOutput out;
|
2010-04-19 13:46:58 +00:00
|
|
|
expect(str, "("); string id = parseString(str);
|
|
|
|
expect(str, ","); out.path = parsePath(str);
|
|
|
|
expect(str, ","); out.hashAlgo = parseString(str);
|
|
|
|
expect(str, ","); out.hash = parseString(str);
|
|
|
|
expect(str, ")");
|
|
|
|
drv.outputs[id] = out;
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
}
|
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
/* Parse the list of input derivations. */
|
|
|
|
expect(str, ",[");
|
|
|
|
while (!endOfList(str)) {
|
|
|
|
expect(str, "(");
|
|
|
|
Path drvPath = parsePath(str);
|
|
|
|
expect(str, ",[");
|
|
|
|
drv.inputDrvs[drvPath] = parseStrings(str, false);
|
|
|
|
expect(str, ")");
|
2005-01-20 14:10:19 +00:00
|
|
|
}
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
expect(str, ",["); drv.inputSrcs = parseStrings(str, true);
|
|
|
|
expect(str, ","); drv.platform = parseString(str);
|
|
|
|
expect(str, ","); drv.builder = parseString(str);
|
|
|
|
|
|
|
|
/* Parse the builder arguments. */
|
|
|
|
expect(str, ",[");
|
|
|
|
while (!endOfList(str))
|
|
|
|
drv.args.push_back(parseString(str));
|
|
|
|
|
|
|
|
/* Parse the environment variables. */
|
|
|
|
expect(str, ",[");
|
|
|
|
while (!endOfList(str)) {
|
|
|
|
expect(str, "("); string name = parseString(str);
|
|
|
|
expect(str, ","); string value = parseString(str);
|
|
|
|
expect(str, ")");
|
|
|
|
drv.env[name] = value;
|
2003-08-15 12:32:37 +00:00
|
|
|
}
|
2010-04-19 13:46:58 +00:00
|
|
|
|
|
|
|
expect(str, ")");
|
|
|
|
return drv;
|
|
|
|
}
|
2003-08-15 12:32:37 +00:00
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
static void printString(string & res, const string & s)
|
2010-04-19 13:46:58 +00:00
|
|
|
{
|
2010-04-21 19:25:50 +00:00
|
|
|
res += '"';
|
2010-04-19 13:46:58 +00:00
|
|
|
for (const char * i = s.c_str(); *i; i++)
|
2010-04-21 19:25:50 +00:00
|
|
|
if (*i == '\"' || *i == '\\') { res += "\\"; res += *i; }
|
|
|
|
else if (*i == '\n') res += "\\n";
|
|
|
|
else if (*i == '\r') res += "\\r";
|
|
|
|
else if (*i == '\t') res += "\\t";
|
|
|
|
else res += *i;
|
|
|
|
res += '"';
|
2003-07-16 11:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 13:46:58 +00:00
|
|
|
template<class ForwardIterator>
|
2010-04-21 19:25:50 +00:00
|
|
|
static void printStrings(string & res, ForwardIterator i, ForwardIterator j)
|
2003-07-15 22:28:27 +00:00
|
|
|
{
|
2010-04-21 19:25:50 +00:00
|
|
|
res += '[';
|
2010-04-19 13:46:58 +00:00
|
|
|
bool first = true;
|
|
|
|
for ( ; i != j; ++i) {
|
2010-04-21 19:25:50 +00:00
|
|
|
if (first) first = false; else res += ',';
|
|
|
|
printString(res, *i);
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
2010-04-21 19:25:50 +00:00
|
|
|
res += ']';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string unparseDerivation(const Derivation & drv)
|
|
|
|
{
|
2010-04-21 19:25:50 +00:00
|
|
|
string s;
|
|
|
|
s.reserve(65536);
|
|
|
|
s += "Derive([";
|
2010-04-19 13:46:58 +00:00
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
foreach (DerivationOutputs::const_iterator, i, drv.outputs) {
|
2010-04-21 19:25:50 +00:00
|
|
|
if (first) first = false; else s += ',';
|
|
|
|
s += '('; printString(s, i->first);
|
|
|
|
s += ','; printString(s, i->second.path);
|
|
|
|
s += ','; printString(s, i->second.hashAlgo);
|
|
|
|
s += ','; printString(s, i->second.hash);
|
|
|
|
s += ')';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "],[";
|
2010-04-19 13:46:58 +00:00
|
|
|
first = true;
|
|
|
|
foreach (DerivationInputs::const_iterator, i, drv.inputDrvs) {
|
2010-04-21 19:25:50 +00:00
|
|
|
if (first) first = false; else s += ',';
|
|
|
|
s += '('; printString(s, i->first);
|
|
|
|
s += ','; printStrings(s, i->second.begin(), i->second.end());
|
|
|
|
s += ')';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "],";
|
|
|
|
printStrings(s, drv.inputSrcs.begin(), drv.inputSrcs.end());
|
2010-04-19 13:46:58 +00:00
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += ','; printString(s, drv.platform);
|
|
|
|
s += ','; printString(s, drv.builder);
|
|
|
|
s += ','; printStrings(s, drv.args.begin(), drv.args.end());
|
2010-04-19 13:46:58 +00:00
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += ",[";
|
2010-04-19 13:46:58 +00:00
|
|
|
first = true;
|
|
|
|
foreach (StringPairs::const_iterator, i, drv.env) {
|
2010-04-21 19:25:50 +00:00
|
|
|
if (first) first = false; else s += ',';
|
|
|
|
s += '('; printString(s, i->first);
|
|
|
|
s += ','; printString(s, i->second);
|
|
|
|
s += ')';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "])";
|
2005-01-20 14:10:19 +00:00
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
return s;
|
2003-07-15 22:28:27 +00:00
|
|
|
}
|
2005-01-19 14:36:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool isDerivation(const string & fileName)
|
|
|
|
{
|
2008-08-25 13:31:57 +00:00
|
|
|
return hasSuffix(fileName, drvExtension);
|
2005-01-19 14:36:00 +00:00
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
|
2011-07-20 18:10:47 +00:00
|
|
|
bool isFixedOutputDrv(const Derivation & drv)
|
|
|
|
{
|
|
|
|
return drv.outputs.size() == 1 &&
|
|
|
|
drv.outputs.begin()->first == "out" &&
|
|
|
|
drv.outputs.begin()->second.hash != "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DrvHashes drvHashes;
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns the hash of a derivation modulo fixed-output
|
|
|
|
subderivations. A fixed-output derivation is a derivation with one
|
|
|
|
output (`out') for which an expected hash and hash algorithm are
|
|
|
|
specified (using the `outputHash' and `outputHashAlgo'
|
|
|
|
attributes). We don't want changes to such derivations to
|
|
|
|
propagate upwards through the dependency graph, changing output
|
|
|
|
paths everywhere.
|
|
|
|
|
|
|
|
For instance, if we change the url in a call to the `fetchurl'
|
|
|
|
function, we do not want to rebuild everything depending on it
|
|
|
|
(after all, (the hash of) the file being downloaded is unchanged).
|
|
|
|
So the *output paths* should not change. On the other hand, the
|
|
|
|
*derivation paths* should change to reflect the new dependency
|
|
|
|
graph.
|
|
|
|
|
|
|
|
That's what this function does: it returns a hash which is just the
|
|
|
|
hash of the derivation ATerm, except that any input derivation
|
|
|
|
paths have been replaced by the result of a recursive call to this
|
|
|
|
function, and that for fixed-output derivations we return a hash of
|
|
|
|
its output path. */
|
|
|
|
Hash hashDerivationModulo(Derivation drv)
|
|
|
|
{
|
|
|
|
/* Return a fixed hash for fixed-output derivations. */
|
|
|
|
if (isFixedOutputDrv(drv)) {
|
|
|
|
DerivationOutputs::const_iterator i = drv.outputs.begin();
|
|
|
|
return hashString(htSHA256, "fixed:out:"
|
|
|
|
+ i->second.hashAlgo + ":"
|
|
|
|
+ i->second.hash + ":"
|
|
|
|
+ i->second.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For other derivations, replace the inputs paths with recursive
|
|
|
|
calls to this function.*/
|
|
|
|
DerivationInputs inputs2;
|
|
|
|
foreach (DerivationInputs::const_iterator, i, drv.inputDrvs) {
|
|
|
|
Hash h = drvHashes[i->first];
|
|
|
|
if (h.type == htUnknown) {
|
|
|
|
Derivation drv2 = derivationFromPath(i->first);
|
|
|
|
h = hashDerivationModulo(drv2);
|
|
|
|
drvHashes[i->first] = h;
|
|
|
|
}
|
|
|
|
inputs2[printHash(h)] = i->second;
|
|
|
|
}
|
|
|
|
drv.inputDrvs = inputs2;
|
|
|
|
|
|
|
|
return hashString(htSHA256, unparseDerivation(drv));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|