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"
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
#include "worker-protocol.hh"
|
2016-06-02 16:43:36 +00:00
|
|
|
#include "fs-accessor.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2020-07-12 16:12:21 +00:00
|
|
|
std::optional<StorePath> DerivationOutput::pathOpt(const Store & store, std::string_view drvName) const
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
{
|
2020-07-08 23:11:39 +00:00
|
|
|
return std::visit(overloaded {
|
2020-07-12 16:12:21 +00:00
|
|
|
[](DerivationOutputInputAddressed doi) -> std::optional<StorePath> {
|
|
|
|
return { doi.path };
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFixed dof) -> std::optional<StorePath> {
|
2020-07-12 16:12:21 +00:00
|
|
|
return {
|
|
|
|
store.makeFixedOutputPath(dof.hash.method, dof.hash.hash, drvName)
|
|
|
|
};
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[](DerivationOutputCAFloating dof) -> std::optional<StorePath> {
|
2020-07-12 16:12:21 +00:00
|
|
|
return std::nullopt;
|
2020-07-08 23:11:39 +00:00
|
|
|
},
|
|
|
|
}, output);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-03 17:38:54 +00:00
|
|
|
bool derivationIsCA(DerivationType dt) {
|
|
|
|
switch (dt) {
|
2020-08-05 14:49:25 +00:00
|
|
|
case DerivationType::InputAddressed: return false;
|
2020-06-03 17:38:54 +00:00
|
|
|
case DerivationType::CAFixed: return true;
|
2020-07-17 19:55:41 +00:00
|
|
|
case DerivationType::CAFloating: return true;
|
2020-06-03 17:38:54 +00:00
|
|
|
};
|
|
|
|
// Since enums can have non-variant values, but making a `default:` would
|
|
|
|
// disable exhaustiveness warnings.
|
2020-07-28 19:39:45 +00:00
|
|
|
assert(false);
|
2020-06-03 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool derivationIsFixed(DerivationType dt) {
|
|
|
|
switch (dt) {
|
2020-08-05 14:49:25 +00:00
|
|
|
case DerivationType::InputAddressed: return false;
|
2020-06-03 17:38:54 +00:00
|
|
|
case DerivationType::CAFixed: return true;
|
2020-07-17 19:55:41 +00:00
|
|
|
case DerivationType::CAFloating: return false;
|
2020-06-03 17:38:54 +00:00
|
|
|
};
|
2020-07-28 19:39:45 +00:00
|
|
|
assert(false);
|
2020-06-03 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool derivationIsImpure(DerivationType dt) {
|
|
|
|
switch (dt) {
|
2020-08-05 14:49:25 +00:00
|
|
|
case DerivationType::InputAddressed: return false;
|
2020-06-03 17:38:54 +00:00
|
|
|
case DerivationType::CAFixed: return true;
|
2020-07-17 19:55:41 +00:00
|
|
|
case DerivationType::CAFloating: return false;
|
2020-06-03 17:38:54 +00:00
|
|
|
};
|
2020-07-28 19:39:45 +00:00
|
|
|
assert(false);
|
2020-06-03 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
|
|
|
bool BasicDerivation::isBuiltin() const
|
|
|
|
{
|
|
|
|
return string(builder, 0, 8) == "builtin:";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath writeDerivation(ref<Store> store,
|
2020-08-09 20:32:35 +00:00
|
|
|
const Derivation & drv, RepairFlag repair)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
2020-06-16 20:20:18 +00:00
|
|
|
auto references = drv.inputSrcs;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : drv.inputDrvs)
|
2020-06-16 20:20:18 +00:00
|
|
|
references.insert(i.first);
|
2005-01-25 21:28:25 +00:00
|
|
|
/* 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). */
|
2020-08-09 20:32:35 +00:00
|
|
|
auto suffix = std::string(drv.name) + drvExtension;
|
2020-02-17 14:53:59 +00:00
|
|
|
auto contents = drv.unparse(*store, false);
|
2012-07-30 23:55:41 +00:00
|
|
|
return settings.readOnlyMode
|
2016-06-01 12:49:12 +00:00
|
|
|
? store->computeStorePathForText(suffix, contents, references)
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
: store->addTextToStore(suffix, contents, references, repair);
|
2003-07-04 12:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-14 08:54:57 +00:00
|
|
|
/* Read string `s' from stream `str'. */
|
|
|
|
static void expect(std::istream & str, const string & s)
|
|
|
|
{
|
|
|
|
char s2[s.size()];
|
|
|
|
str.read(s2, s.size());
|
|
|
|
if (string(s2, s.size()) != s)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw FormatError("expected string '%1%'", s);
|
2016-09-14 08:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read a C-style string from stream `str'. */
|
|
|
|
static string parseString(std::istream & str)
|
|
|
|
{
|
|
|
|
string res;
|
|
|
|
expect(str, "\"");
|
|
|
|
int c;
|
|
|
|
while ((c = str.get()) != '"')
|
|
|
|
if (c == '\\') {
|
|
|
|
c = str.get();
|
|
|
|
if (c == 'n') res += '\n';
|
|
|
|
else if (c == 'r') res += '\r';
|
|
|
|
else if (c == 't') res += '\t';
|
|
|
|
else res += c;
|
|
|
|
}
|
|
|
|
else res += c;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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] != '/')
|
2020-04-21 23:07:07 +00:00
|
|
|
throw FormatError("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
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
* 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
|
|
|
|
2016-09-14 08:54:57 +00:00
|
|
|
static bool endOfList(std::istream & str)
|
|
|
|
{
|
|
|
|
if (str.peek() == ',') {
|
|
|
|
str.get();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (str.peek() == ']') {
|
|
|
|
str.get();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2020-08-09 20:51:34 +00:00
|
|
|
static DerivationOutput parseDerivationOutput(const Store & store,
|
|
|
|
StorePath path, std::string_view hashAlgo, std::string_view hash)
|
2020-03-23 03:43:07 +00:00
|
|
|
{
|
|
|
|
if (hashAlgo != "") {
|
2020-06-17 04:55:47 +00:00
|
|
|
auto method = FileIngestionMethod::Flat;
|
2020-03-23 03:43:07 +00:00
|
|
|
if (string(hashAlgo, 0, 2) == "r:") {
|
|
|
|
method = FileIngestionMethod::Recursive;
|
2020-08-09 20:51:34 +00:00
|
|
|
hashAlgo = hashAlgo.substr(2);
|
2020-03-23 03:43:07 +00:00
|
|
|
}
|
|
|
|
const HashType hashType = parseHashType(hashAlgo);
|
|
|
|
|
2020-07-12 16:12:21 +00:00
|
|
|
return hash != ""
|
|
|
|
? DerivationOutput {
|
2020-08-05 14:49:25 +00:00
|
|
|
.output = DerivationOutputCAFixed {
|
2020-07-12 16:12:21 +00:00
|
|
|
.hash = FixedOutputHash {
|
|
|
|
.method = std::move(method),
|
2020-08-05 14:37:42 +00:00
|
|
|
.hash = Hash::parseNonSRIUnprefixed(hash, hashType),
|
2020-07-12 16:12:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 17:56:36 +00:00
|
|
|
: (settings.requireExperimentalFeature("ca-derivations"),
|
|
|
|
DerivationOutput {
|
2020-08-05 14:49:25 +00:00
|
|
|
.output = DerivationOutputCAFloating {
|
2020-07-12 16:12:21 +00:00
|
|
|
.method = std::move(method),
|
|
|
|
.hashType = std::move(hashType),
|
|
|
|
},
|
2020-07-27 17:56:36 +00:00
|
|
|
});
|
2020-07-08 23:11:39 +00:00
|
|
|
} else
|
|
|
|
return DerivationOutput {
|
2020-07-12 15:56:20 +00:00
|
|
|
.output = DerivationOutputInputAddressed {
|
2020-07-08 23:11:39 +00:00
|
|
|
.path = std::move(path),
|
|
|
|
}
|
|
|
|
};
|
2020-03-23 03:43:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:51:34 +00:00
|
|
|
static DerivationOutput parseDerivationOutput(const Store & store, std::istringstream & str)
|
|
|
|
{
|
|
|
|
expect(str, ","); auto path = store.parseStorePath(parsePath(str));
|
|
|
|
expect(str, ","); const auto hashAlgo = parseString(str);
|
|
|
|
expect(str, ","); const auto hash = parseString(str);
|
|
|
|
expect(str, ")");
|
|
|
|
|
|
|
|
return parseDerivationOutput(store, std::move(path), hashAlgo, hash);
|
|
|
|
}
|
|
|
|
|
2020-03-23 03:43:07 +00:00
|
|
|
|
2020-08-01 19:38:35 +00:00
|
|
|
Derivation parseDerivation(const Store & store, std::string && s, std::string_view name)
|
2003-07-15 21:24:05 +00:00
|
|
|
{
|
2005-01-19 11:16:11 +00:00
|
|
|
Derivation drv;
|
2020-07-08 19:38:01 +00:00
|
|
|
drv.name = name;
|
|
|
|
|
2020-07-13 16:22:56 +00:00
|
|
|
std::istringstream str(std::move(s));
|
2010-04-19 13:46:58 +00:00
|
|
|
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)) {
|
2019-12-05 18:11:09 +00:00
|
|
|
expect(str, "("); std::string id = parseString(str);
|
2020-06-17 04:55:47 +00:00
|
|
|
auto output = parseDerivationOutput(store, str);
|
|
|
|
drv.outputs.emplace(std::move(id), std::move(output));
|
* 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, ",[");
|
2019-12-05 18:11:09 +00:00
|
|
|
drv.inputDrvs.insert_or_assign(store.parseStorePath(drvPath), parseStrings(str, false));
|
2010-04-19 13:46:58 +00:00
|
|
|
expect(str, ")");
|
2005-01-20 14:10:19 +00:00
|
|
|
}
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
expect(str, ",["); drv.inputSrcs = store.parseStorePathSet(parseStrings(str, true));
|
2010-04-19 13:46:58 +00:00
|
|
|
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
|
|
|
}
|
2012-07-30 23:55:41 +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
|
|
|
|
2020-02-23 15:36:19 +00:00
|
|
|
static void printString(string & res, std::string_view s)
|
|
|
|
{
|
|
|
|
char buf[s.size() * 2 + 2];
|
|
|
|
char * p = buf;
|
|
|
|
*p++ = '"';
|
|
|
|
for (auto c : s)
|
|
|
|
if (c == '\"' || c == '\\') { *p++ = '\\'; *p++ = c; }
|
|
|
|
else if (c == '\n') { *p++ = '\\'; *p++ = 'n'; }
|
|
|
|
else if (c == '\r') { *p++ = '\\'; *p++ = 'r'; }
|
|
|
|
else if (c == '\t') { *p++ = '\\'; *p++ = 't'; }
|
|
|
|
else *p++ = c;
|
|
|
|
*p++ = '"';
|
|
|
|
res.append(buf, p - buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void printUnquotedString(string & res, std::string_view s)
|
2010-04-19 13:46:58 +00:00
|
|
|
{
|
2010-04-21 19:25:50 +00:00
|
|
|
res += '"';
|
2020-02-23 15:36:19 +00:00
|
|
|
res.append(s);
|
2010-04-21 19:25:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-23 15:36:19 +00:00
|
|
|
template<class ForwardIterator>
|
|
|
|
static void printUnquotedStrings(string & res, ForwardIterator i, ForwardIterator j)
|
|
|
|
{
|
|
|
|
res += '[';
|
|
|
|
bool first = true;
|
|
|
|
for ( ; i != j; ++i) {
|
|
|
|
if (first) first = false; else res += ',';
|
|
|
|
printUnquotedString(res, *i);
|
|
|
|
}
|
|
|
|
res += ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
string Derivation::unparse(const Store & store, bool maskOutputs,
|
|
|
|
std::map<std::string, StringSet> * actualInputs) const
|
2010-04-19 13:46:58 +00:00
|
|
|
{
|
2010-04-21 19:25:50 +00:00
|
|
|
string s;
|
|
|
|
s.reserve(65536);
|
|
|
|
s += "Derive([";
|
2010-04-19 13:46:58 +00:00
|
|
|
|
2020-01-21 20:14:13 +00:00
|
|
|
bool first = true;
|
|
|
|
for (auto & i : outputs) {
|
|
|
|
if (first) first = false; else s += ',';
|
2020-02-23 15:36:19 +00:00
|
|
|
s += '('; printUnquotedString(s, i.first);
|
2020-07-08 23:11:39 +00:00
|
|
|
s += ','; printUnquotedString(s, maskOutputs ? "" : store.printStorePath(i.second.path(store, name)));
|
2020-07-12 16:12:21 +00:00
|
|
|
std::visit(overloaded {
|
|
|
|
[&](DerivationOutputInputAddressed doi) {
|
|
|
|
s += ','; printUnquotedString(s, "");
|
|
|
|
s += ','; printUnquotedString(s, "");
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFixed dof) {
|
2020-07-12 16:12:21 +00:00
|
|
|
s += ','; printUnquotedString(s, dof.hash.printMethodAlgo());
|
|
|
|
s += ','; printUnquotedString(s, dof.hash.hash.to_string(Base16, false));
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFloating dof) {
|
2020-07-12 16:12:21 +00:00
|
|
|
s += ','; printUnquotedString(s, makeFileIngestionPrefix(dof.method) + printHashType(dof.hashType));
|
|
|
|
s += ','; printUnquotedString(s, "");
|
|
|
|
},
|
2020-07-12 16:12:21 +00:00
|
|
|
}, i.second.output);
|
2020-01-21 20:14:13 +00:00
|
|
|
s += ')';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "],[";
|
2020-01-21 20:14:13 +00:00
|
|
|
first = true;
|
2019-12-05 18:11:09 +00:00
|
|
|
if (actualInputs) {
|
|
|
|
for (auto & i : *actualInputs) {
|
|
|
|
if (first) first = false; else s += ',';
|
2020-02-23 15:36:19 +00:00
|
|
|
s += '('; printUnquotedString(s, i.first);
|
|
|
|
s += ','; printUnquotedStrings(s, i.second.begin(), i.second.end());
|
2019-12-05 18:11:09 +00:00
|
|
|
s += ')';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto & i : inputDrvs) {
|
|
|
|
if (first) first = false; else s += ',';
|
2020-02-23 15:36:19 +00:00
|
|
|
s += '('; printUnquotedString(s, store.printStorePath(i.first));
|
|
|
|
s += ','; printUnquotedStrings(s, i.second.begin(), i.second.end());
|
2019-12-05 18:11:09 +00:00
|
|
|
s += ')';
|
|
|
|
}
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "],";
|
2019-12-05 18:11:09 +00:00
|
|
|
auto paths = store.printStorePathSet(inputSrcs); // FIXME: slow
|
2020-02-23 15:36:19 +00:00
|
|
|
printUnquotedStrings(s, paths.begin(), paths.end());
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2020-02-23 15:36:19 +00:00
|
|
|
s += ','; printUnquotedString(s, platform);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
s += ','; printString(s, builder);
|
|
|
|
s += ','; printStrings(s, args.begin(), 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;
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
for (auto & i : env) {
|
2010-04-21 19:25:50 +00:00
|
|
|
if (first) first = false; else s += ',';
|
2015-07-17 17:24:28 +00:00
|
|
|
s += '('; printString(s, i.first);
|
2020-01-21 20:14:13 +00:00
|
|
|
s += ','; printString(s, maskOutputs && outputs.count(i.first) ? "" : i.second);
|
2010-04-21 19:25:50 +00:00
|
|
|
s += ')';
|
2010-04-19 13:46:58 +00:00
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2010-04-21 19:25:50 +00:00
|
|
|
s += "])";
|
2012-07-30 23:55:41 +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
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
// FIXME: remove
|
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
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2020-03-15 06:23:17 +00:00
|
|
|
DerivationType BasicDerivation::type() const
|
2011-07-20 18:10:47 +00:00
|
|
|
{
|
2020-07-17 19:55:41 +00:00
|
|
|
std::set<std::string_view> inputAddressedOutputs, fixedCAOutputs, floatingCAOutputs;
|
|
|
|
std::optional<HashType> floatingHashType;
|
2020-07-17 15:33:27 +00:00
|
|
|
for (auto & i : outputs) {
|
|
|
|
std::visit(overloaded {
|
|
|
|
[&](DerivationOutputInputAddressed _) {
|
|
|
|
inputAddressedOutputs.insert(i.first);
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFixed _) {
|
2020-07-17 15:33:27 +00:00
|
|
|
fixedCAOutputs.insert(i.first);
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFloating dof) {
|
2020-07-17 19:55:41 +00:00
|
|
|
floatingCAOutputs.insert(i.first);
|
|
|
|
if (!floatingHashType) {
|
|
|
|
floatingHashType = dof.hashType;
|
|
|
|
} else {
|
|
|
|
if (*floatingHashType != dof.hashType)
|
|
|
|
throw Error("All floating outputs must use the same hash type");
|
|
|
|
}
|
2020-07-17 15:33:27 +00:00
|
|
|
},
|
|
|
|
}, i.second.output);
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:55:41 +00:00
|
|
|
if (inputAddressedOutputs.empty() && fixedCAOutputs.empty() && floatingCAOutputs.empty()) {
|
2020-07-17 15:33:27 +00:00
|
|
|
throw Error("Must have at least one output");
|
2020-07-17 19:55:41 +00:00
|
|
|
} else if (! inputAddressedOutputs.empty() && fixedCAOutputs.empty() && floatingCAOutputs.empty()) {
|
2020-08-05 14:49:25 +00:00
|
|
|
return DerivationType::InputAddressed;
|
2020-07-17 19:55:41 +00:00
|
|
|
} else if (inputAddressedOutputs.empty() && ! fixedCAOutputs.empty() && floatingCAOutputs.empty()) {
|
2020-07-17 15:33:27 +00:00
|
|
|
if (fixedCAOutputs.size() > 1)
|
|
|
|
// FIXME: Experimental feature?
|
|
|
|
throw Error("Only one fixed output is allowed for now");
|
|
|
|
if (*fixedCAOutputs.begin() != "out")
|
|
|
|
throw Error("Single fixed output must be named \"out\"");
|
2020-06-03 17:38:54 +00:00
|
|
|
return DerivationType::CAFixed;
|
2020-07-17 19:55:41 +00:00
|
|
|
} else if (inputAddressedOutputs.empty() && fixedCAOutputs.empty() && ! floatingCAOutputs.empty()) {
|
|
|
|
return DerivationType::CAFloating;
|
2020-06-21 21:05:37 +00:00
|
|
|
} else {
|
2020-07-17 15:33:27 +00:00
|
|
|
throw Error("Can't mix derivation output types");
|
2020-03-15 06:23:17 +00:00
|
|
|
}
|
2011-07-20 18:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DrvHashes drvHashes;
|
|
|
|
|
2020-03-19 04:37:57 +00:00
|
|
|
/* pathDerivationModulo and hashDerivationModulo are mutually recursive
|
|
|
|
*/
|
2011-07-20 18:10:47 +00:00
|
|
|
|
2020-03-19 04:37:57 +00:00
|
|
|
/* Look up the derivation by value and memoize the
|
|
|
|
`hashDerivationModulo` call.
|
|
|
|
*/
|
2020-06-21 16:43:17 +00:00
|
|
|
static const DrvHashModulo & pathDerivationModulo(Store & store, const StorePath & drvPath)
|
2020-03-19 04:37:57 +00:00
|
|
|
{
|
|
|
|
auto h = drvHashes.find(drvPath);
|
|
|
|
if (h == drvHashes.end()) {
|
|
|
|
assert(store.isValidPath(drvPath));
|
|
|
|
// Cache it
|
|
|
|
h = drvHashes.insert_or_assign(
|
2020-06-17 03:51:02 +00:00
|
|
|
drvPath,
|
2020-03-19 04:37:57 +00:00
|
|
|
hashDerivationModulo(
|
|
|
|
store,
|
2020-06-17 03:51:02 +00:00
|
|
|
store.readDerivation(drvPath),
|
2020-03-19 04:37:57 +00:00
|
|
|
false)).first;
|
|
|
|
}
|
|
|
|
return h->second;
|
|
|
|
}
|
2011-07-20 18:10:47 +00:00
|
|
|
|
2020-03-19 04:37:57 +00:00
|
|
|
/* See the header for interface details. These are the implementation details.
|
|
|
|
|
2020-03-20 03:37:52 +00:00
|
|
|
For fixed-output derivations, each hash in the map is not the
|
2020-03-19 04:37:57 +00:00
|
|
|
corresponding output's content hash, but a hash of that hash along
|
|
|
|
with other constant data. The key point is that the value is a pure
|
|
|
|
function of the output's contents, and there are no preimage attacks
|
2020-03-20 03:37:52 +00:00
|
|
|
either spoofing an output's contents for a derivation, or
|
|
|
|
spoofing a derivation for an output's contents.
|
2020-03-19 04:37:57 +00:00
|
|
|
|
|
|
|
For regular derivations, it looks up each subderivation from its hash
|
|
|
|
and recurs. If the subderivation is also regular, it simply
|
|
|
|
substitutes the derivation path with its hash. If the subderivation
|
|
|
|
is fixed-output, however, it takes each output hash and pretends it
|
|
|
|
is a derivation hash producing a single "out" output. This is so we
|
|
|
|
don't leak the provenance of fixed outputs, reducing pointless cache
|
|
|
|
misses as the build itself won't know this.
|
|
|
|
*/
|
|
|
|
DrvHashModulo hashDerivationModulo(Store & store, const Derivation & drv, bool maskOutputs)
|
2011-07-20 18:10:47 +00:00
|
|
|
{
|
|
|
|
/* Return a fixed hash for fixed-output derivations. */
|
2020-03-15 06:23:17 +00:00
|
|
|
switch (drv.type()) {
|
2020-07-17 19:55:41 +00:00
|
|
|
case DerivationType::CAFloating:
|
|
|
|
throw Error("Regular input-addressed derivations are not yet allowed to depend on CA derivations");
|
2020-06-03 17:38:54 +00:00
|
|
|
case DerivationType::CAFixed: {
|
2020-03-19 04:37:57 +00:00
|
|
|
std::map<std::string, Hash> outputHashes;
|
2020-07-28 17:59:24 +00:00
|
|
|
for (const auto & i : drv.outputsAndPaths(store)) {
|
2020-08-05 19:45:33 +00:00
|
|
|
auto & dof = std::get<DerivationOutputCAFixed>(i.second.first.output);
|
2020-07-17 14:28:33 +00:00
|
|
|
auto hash = hashString(htSHA256, "fixed:out:"
|
|
|
|
+ dof.hash.printMethodAlgo() + ":"
|
|
|
|
+ dof.hash.hash.to_string(Base16, false) + ":"
|
2020-07-28 17:59:24 +00:00
|
|
|
+ store.printStorePath(i.second.second));
|
2020-07-17 14:28:33 +00:00
|
|
|
outputHashes.insert_or_assign(i.first, std::move(hash));
|
2020-03-19 04:37:57 +00:00
|
|
|
}
|
|
|
|
return outputHashes;
|
2011-07-20 18:10:47 +00:00
|
|
|
}
|
2020-08-05 14:49:25 +00:00
|
|
|
case DerivationType::InputAddressed:
|
2020-03-15 06:23:17 +00:00
|
|
|
break;
|
2011-07-20 18:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For other derivations, replace the inputs paths with recursive
|
2020-03-19 04:37:57 +00:00
|
|
|
calls to this function. */
|
2019-12-05 18:11:09 +00:00
|
|
|
std::map<std::string, StringSet> inputs2;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : drv.inputDrvs) {
|
2020-06-21 16:43:17 +00:00
|
|
|
const auto & res = pathDerivationModulo(store, i.first);
|
|
|
|
std::visit(overloaded {
|
|
|
|
// Regular non-CA derivation, replace derivation
|
|
|
|
[&](Hash drvHash) {
|
|
|
|
inputs2.insert_or_assign(drvHash.to_string(Base16, false), i.second);
|
|
|
|
},
|
2020-03-19 04:37:57 +00:00
|
|
|
// CA derivation's output hashes
|
2020-06-21 16:43:17 +00:00
|
|
|
[&](CaOutputHashes outputHashes) {
|
2020-06-21 16:51:39 +00:00
|
|
|
std::set<std::string> justOut = { "out" };
|
2020-06-21 16:43:17 +00:00
|
|
|
for (auto & output : i.second) {
|
|
|
|
/* Put each one in with a single "out" output.. */
|
|
|
|
const auto h = outputHashes.at(output);
|
|
|
|
inputs2.insert_or_assign(
|
|
|
|
h.to_string(Base16, false),
|
|
|
|
justOut);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}, res);
|
2011-07-20 18:10:47 +00:00
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
return hashString(htSHA256, drv.unparse(store, maskOutputs, &inputs2));
|
2011-07-20 18:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::string StorePathWithOutputs::to_string(const Store & store) const
|
2012-11-26 14:39:10 +00:00
|
|
|
{
|
|
|
|
return outputs.empty()
|
2019-12-05 18:11:09 +00:00
|
|
|
? store.printStorePath(path)
|
|
|
|
: store.printStorePath(path) + "!" + concatStringsSep(",", outputs);
|
2012-11-26 14:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-26 16:15:09 +00:00
|
|
|
bool wantOutput(const string & output, const std::set<string> & wanted)
|
|
|
|
{
|
|
|
|
return wanted.empty() || wanted.find(output) != wanted.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-08 23:11:39 +00:00
|
|
|
StorePathSet BasicDerivation::outputPaths(const Store & store) const
|
2015-06-10 14:17:06 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet paths;
|
2020-07-28 17:59:24 +00:00
|
|
|
for (auto & i : outputsAndPaths(store))
|
|
|
|
paths.insert(i.second.second);
|
2015-06-10 14:17:06 +00:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2020-03-23 03:43:07 +00:00
|
|
|
static DerivationOutput readDerivationOutput(Source & in, const Store & store)
|
|
|
|
{
|
|
|
|
auto path = store.parseStorePath(readString(in));
|
2020-08-09 20:51:34 +00:00
|
|
|
const auto hashAlgo = readString(in);
|
|
|
|
const auto hash = readString(in);
|
2020-03-23 03:43:07 +00:00
|
|
|
|
2020-08-09 20:51:34 +00:00
|
|
|
return parseDerivationOutput(store, std::move(path), hashAlgo, hash);
|
2020-03-23 03:43:07 +00:00
|
|
|
}
|
2015-06-10 14:17:06 +00:00
|
|
|
|
2020-06-12 10:46:33 +00:00
|
|
|
StringSet BasicDerivation::outputNames() const
|
|
|
|
{
|
|
|
|
StringSet names;
|
|
|
|
for (auto & i : outputs)
|
|
|
|
names.insert(i.first);
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:42:02 +00:00
|
|
|
DerivationOutputsAndPaths BasicDerivation::outputsAndPaths(const Store & store) const {
|
|
|
|
DerivationOutputsAndPaths outsAndPaths;
|
|
|
|
for (auto output : outputs)
|
|
|
|
outsAndPaths.insert(std::make_pair(
|
|
|
|
output.first,
|
|
|
|
std::make_pair(output.second, output.second.path(store, name))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return outsAndPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
DerivationOutputsAndOptPaths BasicDerivation::outputsAndOptPaths(const Store & store) const {
|
|
|
|
DerivationOutputsAndOptPaths outsAndOptPaths;
|
|
|
|
for (auto output : outputs)
|
|
|
|
outsAndOptPaths.insert(std::make_pair(
|
|
|
|
output.first,
|
|
|
|
std::make_pair(output.second, output.second.pathOpt(store, output.first))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return outsAndOptPaths;
|
|
|
|
}
|
2020-06-12 10:46:33 +00:00
|
|
|
|
2020-07-12 15:26:30 +00:00
|
|
|
std::string_view BasicDerivation::nameFromPath(const StorePath & drvPath) {
|
|
|
|
auto nameWithSuffix = drvPath.name();
|
|
|
|
constexpr std::string_view extension = ".drv";
|
|
|
|
assert(hasSuffix(nameWithSuffix, extension));
|
|
|
|
nameWithSuffix.remove_suffix(extension.size());
|
|
|
|
return nameWithSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-12 15:02:36 +00:00
|
|
|
Source & readDerivation(Source & in, const Store & store, BasicDerivation & drv, std::string_view name)
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
{
|
2020-07-08 19:38:01 +00:00
|
|
|
drv.name = name;
|
|
|
|
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
drv.outputs.clear();
|
2017-03-01 12:52:54 +00:00
|
|
|
auto nr = readNum<size_t>(in);
|
|
|
|
for (size_t n = 0; n < nr; n++) {
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
auto name = readString(in);
|
2020-03-23 03:43:07 +00:00
|
|
|
auto output = readDerivationOutput(in, store);
|
2020-06-17 04:55:47 +00:00
|
|
|
drv.outputs.emplace(std::move(name), std::move(output));
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
drv.inputSrcs = readStorePaths<StorePathSet>(store, in);
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
in >> drv.platform >> drv.builder;
|
|
|
|
drv.args = readStrings<Strings>(in);
|
|
|
|
|
2017-03-01 12:52:54 +00:00
|
|
|
nr = readNum<size_t>(in);
|
|
|
|
for (size_t n = 0; n < nr; n++) {
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
auto key = readString(in);
|
|
|
|
auto value = readString(in);
|
|
|
|
drv.env[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void writeDerivation(Sink & out, const Store & store, const BasicDerivation & drv)
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
{
|
|
|
|
out << drv.outputs.size();
|
2020-07-28 17:59:24 +00:00
|
|
|
for (auto & i : drv.outputsAndPaths(store)) {
|
2020-03-23 03:43:07 +00:00
|
|
|
out << i.first
|
2020-07-28 17:59:24 +00:00
|
|
|
<< store.printStorePath(i.second.second);
|
2020-07-12 16:12:21 +00:00
|
|
|
std::visit(overloaded {
|
|
|
|
[&](DerivationOutputInputAddressed doi) {
|
|
|
|
out << "" << "";
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFixed dof) {
|
2020-07-12 16:12:21 +00:00
|
|
|
out << dof.hash.printMethodAlgo()
|
|
|
|
<< dof.hash.hash.to_string(Base16, false);
|
|
|
|
},
|
2020-08-05 14:49:25 +00:00
|
|
|
[&](DerivationOutputCAFloating dof) {
|
2020-07-12 16:12:21 +00:00
|
|
|
out << (makeFileIngestionPrefix(dof.method) + printHashType(dof.hashType))
|
|
|
|
<< "";
|
|
|
|
},
|
2020-07-28 17:59:24 +00:00
|
|
|
}, i.second.first.output);
|
2020-06-25 13:50:30 +00:00
|
|
|
}
|
2019-12-05 18:11:09 +00:00
|
|
|
writeStorePaths(store, out, drv.inputSrcs);
|
|
|
|
out << drv.platform << drv.builder << drv.args;
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
out << drv.env.size();
|
|
|
|
for (auto & i : drv.env)
|
|
|
|
out << i.first << i.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-17 13:12:54 +00:00
|
|
|
std::string hashPlaceholder(const std::string & outputName)
|
|
|
|
{
|
|
|
|
// FIXME: memoize?
|
2017-07-04 12:47:59 +00:00
|
|
|
return "/" + hashString(htSHA256, "nix-output:" + outputName).to_string(Base32, false);
|
2016-08-17 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|