2012-07-18 20:59:03 +02:00
|
|
|
|
#pragma once
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
2010-10-04 19:55:38 +02:00
|
|
|
|
#include "hash.hh"
|
|
|
|
|
#include "serialise.hh"
|
2016-03-24 11:41:00 +01:00
|
|
|
|
#include "crypto.hh"
|
2016-04-19 18:50:15 +02:00
|
|
|
|
#include "lru-cache.hh"
|
|
|
|
|
#include "sync.hh"
|
2010-10-04 19:55:38 +02:00
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
#include <atomic>
|
2015-08-21 13:57:53 +02:00
|
|
|
|
#include <limits>
|
2006-12-05 02:31:45 +01:00
|
|
|
|
#include <map>
|
2014-03-30 00:49:23 +01:00
|
|
|
|
#include <memory>
|
2016-04-19 18:50:15 +02:00
|
|
|
|
#include <string>
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
2016-02-15 14:48:38 +01:00
|
|
|
|
/* Size of the hash part of store paths, in base-32 characters. */
|
|
|
|
|
const size_t storePathHashLen = 32; // i.e. 160 bits
|
|
|
|
|
|
2016-05-04 13:36:54 +02:00
|
|
|
|
/* Magic header of exportPath() output (obsolete). */
|
2016-02-15 14:48:38 +01:00
|
|
|
|
const uint32_t exportMagic = 0x4558494e;
|
|
|
|
|
|
|
|
|
|
|
2006-12-05 02:31:45 +01:00
|
|
|
|
typedef std::map<Path, Path> Roots;
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
|
struct GCOptions
|
|
|
|
|
{
|
|
|
|
|
/* Garbage collector operation:
|
|
|
|
|
|
|
|
|
|
- `gcReturnLive': return the set of paths reachable from
|
|
|
|
|
(i.e. in the closure of) the roots.
|
|
|
|
|
|
|
|
|
|
- `gcReturnDead': return the set of paths not reachable from
|
|
|
|
|
the roots.
|
|
|
|
|
|
|
|
|
|
- `gcDeleteDead': actually delete the latter set.
|
|
|
|
|
|
|
|
|
|
- `gcDeleteSpecific': delete the paths listed in
|
|
|
|
|
`pathsToDelete', insofar as they are not reachable.
|
|
|
|
|
*/
|
|
|
|
|
typedef enum {
|
|
|
|
|
gcReturnLive,
|
|
|
|
|
gcReturnDead,
|
|
|
|
|
gcDeleteDead,
|
|
|
|
|
gcDeleteSpecific,
|
|
|
|
|
} GCAction;
|
|
|
|
|
|
2015-08-21 13:57:53 +02:00
|
|
|
|
GCAction action{gcDeleteDead};
|
2008-06-18 11:34:17 +02:00
|
|
|
|
|
|
|
|
|
/* If `ignoreLiveness' is set, then reachability from the roots is
|
|
|
|
|
ignored (dangerous!). However, the paths must still be
|
|
|
|
|
unreferenced *within* the store (i.e., there can be no other
|
|
|
|
|
store paths that depend on them). */
|
2015-08-21 13:57:53 +02:00
|
|
|
|
bool ignoreLiveness{false};
|
2008-06-18 11:34:17 +02:00
|
|
|
|
|
|
|
|
|
/* For `gcDeleteSpecific', the paths to delete. */
|
|
|
|
|
PathSet pathsToDelete;
|
|
|
|
|
|
2012-08-02 01:14:30 +02:00
|
|
|
|
/* Stop after at least `maxFreed' bytes have been freed. */
|
2015-08-21 13:57:53 +02:00
|
|
|
|
unsigned long long maxFreed{std::numeric_limits<unsigned long long>::max()};
|
2008-06-18 11:34:17 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-03-25 17:06:12 +01:00
|
|
|
|
struct GCResults
|
2008-06-18 11:34:17 +02:00
|
|
|
|
{
|
|
|
|
|
/* Depending on the action, the GC roots, or the paths that would
|
|
|
|
|
be or have been deleted. */
|
|
|
|
|
PathSet paths;
|
|
|
|
|
|
|
|
|
|
/* For `gcReturnDead', `gcDeleteDead' and `gcDeleteSpecific', the
|
|
|
|
|
number of bytes that would be or was freed. */
|
|
|
|
|
unsigned long long bytesFreed;
|
|
|
|
|
|
|
|
|
|
GCResults()
|
|
|
|
|
{
|
|
|
|
|
bytesFreed = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
2006-12-05 03:18:46 +01:00
|
|
|
|
|
|
|
|
|
|
2008-08-02 14:54:35 +02:00
|
|
|
|
struct SubstitutablePathInfo
|
|
|
|
|
{
|
|
|
|
|
Path deriver;
|
|
|
|
|
PathSet references;
|
|
|
|
|
unsigned long long downloadSize; /* 0 = unknown or inapplicable */
|
2010-11-17 15:31:42 +01:00
|
|
|
|
unsigned long long narSize; /* 0 = unknown */
|
2008-08-02 14:54:35 +02:00
|
|
|
|
};
|
|
|
|
|
|
download-from-binary-cache: parallelise fetching of NAR info files
Getting substitute information using the binary cache substituter has
non-trivial latency overhead. A package or NixOS system configuration
can have hundreds of dependencies, and in the worst case (when the
local info cache is empty) we have to do a separate HTTP request for
each of these. If the ping time to the server is t, getting N info
files will take tN seconds; e.g., with a ping time of 0.1s to
nixos.org, sequentially downloading 1000 info files (a typical NixOS
config) will take at least 100 seconds.
To fix this problem, the binary cache substituter can now perform
requests in parallel. This required changing the substituter
interface to support a function querySubstitutablePathInfos() that
queries multiple paths at the same time, and rewriting queryMissing()
to take advantage of parallelism. (Due to local caching,
parallelising queryMissing() is sufficient for most use cases, since
it's almost always called before building a derivation and thus fills
the local info cache.)
For example, parallelism speeds up querying all 1056 paths in a
particular NixOS system configuration from 116s to 2.6s. It works so
well because the eccentricity of the top-level derivation in the
dependency graph is only 9. So we only need 10 round-trips (when
using an unlimited number of parallel connections) to get everything.
Currently we do a maximum of 150 parallel connections to the server.
Thus it's important that the binary cache server (e.g. nixos.org) has
a high connection limit. Alternatively we could use HTTP pipelining,
but WWW::Curl doesn't support it and libcurl has a hard-coded limit of
5 requests per pipeline.
2012-07-07 01:08:20 +02:00
|
|
|
|
typedef std::map<Path, SubstitutablePathInfo> SubstitutablePathInfos;
|
|
|
|
|
|
2008-08-02 14:54:35 +02:00
|
|
|
|
|
2015-03-25 17:06:12 +01:00
|
|
|
|
struct ValidPathInfo
|
2010-11-16 18:11:46 +01:00
|
|
|
|
{
|
|
|
|
|
Path path;
|
|
|
|
|
Path deriver;
|
2016-02-16 11:49:12 +01:00
|
|
|
|
Hash narHash;
|
2010-11-16 18:11:46 +01:00
|
|
|
|
PathSet references;
|
2015-11-09 23:16:24 +01:00
|
|
|
|
time_t registrationTime = 0;
|
2016-03-30 15:50:45 +02:00
|
|
|
|
uint64_t narSize = 0; // 0 = unknown
|
|
|
|
|
uint64_t id; // internal use only
|
2015-11-09 23:16:24 +01:00
|
|
|
|
|
2016-03-21 18:05:47 +01:00
|
|
|
|
/* Whether the path is ultimately trusted, that is, it was built
|
|
|
|
|
locally or is content-addressable (e.g. added via addToStore()
|
|
|
|
|
or the result of a fixed-output derivation). */
|
|
|
|
|
bool ultimate = false;
|
|
|
|
|
|
|
|
|
|
StringSet sigs; // note: not necessarily verified
|
|
|
|
|
|
2015-11-09 23:16:24 +01:00
|
|
|
|
bool operator == (const ValidPathInfo & i) const
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
path == i.path
|
2016-02-16 11:49:12 +01:00
|
|
|
|
&& narHash == i.narHash
|
2015-11-09 23:16:24 +01:00
|
|
|
|
&& references == i.references;
|
|
|
|
|
}
|
2016-03-24 11:41:00 +01:00
|
|
|
|
|
|
|
|
|
/* Return a fingerprint of the store path to be used in binary
|
|
|
|
|
cache signatures. It contains the store path, the base-32
|
|
|
|
|
SHA-256 hash of the NAR serialisation of the path, the size of
|
|
|
|
|
the NAR, and the sorted references. The size field is strictly
|
|
|
|
|
speaking superfluous, but might prevent endless/excessive data
|
|
|
|
|
attacks. */
|
|
|
|
|
std::string fingerprint() const;
|
|
|
|
|
|
|
|
|
|
void sign(const SecretKey & secretKey);
|
|
|
|
|
|
|
|
|
|
/* Return the number of signatures on this .narinfo that were
|
|
|
|
|
produced by one of the specified keys. */
|
|
|
|
|
unsigned int checkSignatures(const PublicKeys & publicKeys) const;
|
2016-04-07 15:14:12 +02:00
|
|
|
|
|
|
|
|
|
/* Verify a single signature. */
|
|
|
|
|
bool checkSignature(const PublicKeys & publicKeys, const std::string & sig) const;
|
2016-04-19 18:50:15 +02:00
|
|
|
|
|
2016-04-20 14:12:38 +02:00
|
|
|
|
Strings shortRefs() const;
|
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
virtual ~ValidPathInfo() { }
|
2010-11-16 18:11:46 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef list<ValidPathInfo> ValidPathInfos;
|
|
|
|
|
|
|
|
|
|
|
2016-01-31 12:06:45 +01:00
|
|
|
|
enum BuildMode { bmNormal, bmRepair, bmCheck, bmHash };
|
2014-02-18 01:01:14 +01:00
|
|
|
|
|
|
|
|
|
|
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 17:57:40 +02:00
|
|
|
|
struct BuildResult
|
|
|
|
|
{
|
|
|
|
|
enum Status {
|
2015-07-20 03:15:45 +02:00
|
|
|
|
Built = 0,
|
|
|
|
|
Substituted,
|
|
|
|
|
AlreadyValid,
|
|
|
|
|
PermanentFailure,
|
|
|
|
|
InputRejected,
|
|
|
|
|
OutputRejected,
|
|
|
|
|
TransientFailure, // possibly transient
|
|
|
|
|
TimedOut,
|
|
|
|
|
MiscFailure,
|
2015-10-06 17:33:30 +02:00
|
|
|
|
DependencyFailed,
|
|
|
|
|
LogLimitExceeded,
|
2015-11-09 23:16:24 +01:00
|
|
|
|
NotDeterministic,
|
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 17:57:40 +02:00
|
|
|
|
} status = MiscFailure;
|
|
|
|
|
std::string errorMsg;
|
|
|
|
|
//time_t startTime = 0, stopTime = 0;
|
2015-07-20 03:15:45 +02:00
|
|
|
|
bool success() {
|
|
|
|
|
return status == Built || status == Substituted || status == AlreadyValid;
|
|
|
|
|
}
|
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 17:57:40 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct BasicDerivation;
|
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 14:28:26 +01:00
|
|
|
|
struct Derivation;
|
2016-03-14 15:32:34 +01:00
|
|
|
|
class FSAccessor;
|
2016-04-20 14:12:38 +02:00
|
|
|
|
class NarInfoDiskCache;
|
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 17:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
2016-02-25 17:43:19 +01:00
|
|
|
|
class Store : public std::enable_shared_from_this<Store>
|
2006-11-30 18:43:04 +01:00
|
|
|
|
{
|
2016-06-01 14:49:12 +02:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
typedef std::map<std::string, std::string> Params;
|
|
|
|
|
|
|
|
|
|
const Path storeDir;
|
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
struct State
|
|
|
|
|
{
|
2016-04-21 17:53:47 +02:00
|
|
|
|
LRUCache<std::string, std::shared_ptr<ValidPathInfo>> pathInfoCache{64 * 1024};
|
2016-04-19 18:50:15 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Sync<State> state;
|
|
|
|
|
|
2016-04-20 14:12:38 +02:00
|
|
|
|
std::shared_ptr<NarInfoDiskCache> diskCache;
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
Store(const Params & params);
|
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
|
public:
|
|
|
|
|
|
2016-02-04 14:48:42 +01:00
|
|
|
|
virtual ~Store() { }
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
2016-04-29 16:26:16 +02:00
|
|
|
|
virtual std::string getUri() = 0;
|
2016-04-20 14:12:38 +02:00
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
/* Return true if ‘path’ is in the Nix store (but not the Nix
|
|
|
|
|
store itself). */
|
|
|
|
|
bool isInStore(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Return true if ‘path’ is a store path, i.e. a direct child of
|
|
|
|
|
the Nix store. */
|
|
|
|
|
bool isStorePath(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Throw an exception if ‘path’ is not a store path. */
|
|
|
|
|
void assertStorePath(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Chop off the parts after the top-level store name, e.g.,
|
|
|
|
|
/nix/store/abcd-foo/bar => /nix/store/abcd-foo. */
|
|
|
|
|
Path toStorePath(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Follow symlinks until we end up with a path in the Nix store. */
|
|
|
|
|
Path followLinksToStore(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Same as followLinksToStore(), but apply toStorePath() to the
|
|
|
|
|
result. */
|
|
|
|
|
Path followLinksToStorePath(const Path & path) const;
|
|
|
|
|
|
|
|
|
|
/* Constructs a unique store path name. */
|
|
|
|
|
Path makeStorePath(const string & type,
|
|
|
|
|
const Hash & hash, const string & name) const;
|
|
|
|
|
|
|
|
|
|
Path makeOutputPath(const string & id,
|
|
|
|
|
const Hash & hash, const string & name) const;
|
|
|
|
|
|
|
|
|
|
Path makeFixedOutputPath(bool recursive,
|
|
|
|
|
HashType hashAlgo, Hash hash, string name) const;
|
|
|
|
|
|
|
|
|
|
/* This is the preparatory part of addToStore() and
|
|
|
|
|
addToStoreFixed(); it computes the store path to which srcPath
|
|
|
|
|
is to be copied. Returns the store path and the cryptographic
|
|
|
|
|
hash of the contents of srcPath. */
|
|
|
|
|
std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath,
|
|
|
|
|
bool recursive = true, HashType hashAlgo = htSHA256,
|
|
|
|
|
PathFilter & filter = defaultPathFilter) const;
|
|
|
|
|
|
|
|
|
|
/* Preparatory part of addTextToStore().
|
|
|
|
|
|
|
|
|
|
!!! Computation of the path should take the references given to
|
|
|
|
|
addTextToStore() into account, otherwise we have a (relatively
|
|
|
|
|
minor) security hole: a caller can register a source file with
|
|
|
|
|
bogus references. If there are too many references, the path may
|
|
|
|
|
not be garbage collected when it has to be (not really a problem,
|
|
|
|
|
the caller could create a root anyway), or it may be garbage
|
|
|
|
|
collected when it shouldn't be (more serious).
|
|
|
|
|
|
|
|
|
|
Hashing the references would solve this (bogus references would
|
|
|
|
|
simply yield a different store path, so other users wouldn't be
|
|
|
|
|
affected), but it has some backwards compatibility issues (the
|
|
|
|
|
hashing scheme changes), so I'm not doing that for now. */
|
|
|
|
|
Path computeStorePathForText(const string & name, const string & s,
|
|
|
|
|
const PathSet & references) const;
|
|
|
|
|
|
2015-03-25 17:06:12 +01:00
|
|
|
|
/* Check whether a path is valid. */
|
2016-04-19 18:50:15 +02:00
|
|
|
|
bool isValidPath(const Path & path);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
virtual bool isValidPathUncached(const Path & path) = 0;
|
|
|
|
|
|
|
|
|
|
public:
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
2012-07-11 17:08:47 +02:00
|
|
|
|
/* Query which of the given paths is valid. */
|
|
|
|
|
virtual PathSet queryValidPaths(const PathSet & paths) = 0;
|
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
|
/* Query the set of all valid paths. Note that for some store
|
|
|
|
|
backends, the name part of store paths may be omitted
|
|
|
|
|
(i.e. you'll get /nix/store/<hash> rather than
|
|
|
|
|
/nix/store/<hash>-<name>). Use queryPathInfo() to obtain the
|
|
|
|
|
full store path. */
|
2012-07-11 16:49:04 +02:00
|
|
|
|
virtual PathSet queryAllValidPaths() = 0;
|
2008-01-29 19:17:36 +01:00
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
|
/* Query information about a valid path. It is permitted to omit
|
|
|
|
|
the name part of the store path. */
|
2016-04-19 18:50:15 +02:00
|
|
|
|
ref<const ValidPathInfo> queryPathInfo(const Path & path);
|
|
|
|
|
|
|
|
|
|
protected:
|
2010-11-16 18:11:46 +01:00
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
virtual std::shared_ptr<ValidPathInfo> queryPathInfoUncached(const Path & path) = 0;
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
public:
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
|
|
|
|
/* Queries the set of incoming FS references for a store path.
|
|
|
|
|
The result is not cleared. */
|
2006-12-01 19:00:01 +01:00
|
|
|
|
virtual void queryReferrers(const Path & path,
|
2006-11-30 18:43:04 +01:00
|
|
|
|
PathSet & referrers) = 0;
|
|
|
|
|
|
2012-12-20 18:41:44 +01:00
|
|
|
|
/* Return all currently valid derivations that have `path' as an
|
|
|
|
|
output. (Note that the result of `queryDeriver()' is the
|
|
|
|
|
derivation that was actually used to produce `path', which may
|
|
|
|
|
not exist anymore.) */
|
|
|
|
|
virtual PathSet queryValidDerivers(const Path & path) = 0;
|
|
|
|
|
|
2010-02-22 13:44:36 +01:00
|
|
|
|
/* Query the outputs of the derivation denoted by `path'. */
|
|
|
|
|
virtual PathSet queryDerivationOutputs(const Path & path) = 0;
|
2011-11-06 07:28:20 +01:00
|
|
|
|
|
|
|
|
|
/* Query the output names of the derivation denoted by `path'. */
|
|
|
|
|
virtual StringSet queryDerivationOutputNames(const Path & path) = 0;
|
2012-07-18 00:55:39 +02:00
|
|
|
|
|
|
|
|
|
/* Query the full store path given the hash part of a valid store
|
|
|
|
|
path, or "" if the path doesn't exist. */
|
|
|
|
|
virtual Path queryPathFromHashPart(const string & hashPart) = 0;
|
2015-03-25 17:06:12 +01:00
|
|
|
|
|
2012-07-11 23:52:18 +02:00
|
|
|
|
/* Query which of the given paths have substitutes. */
|
|
|
|
|
virtual PathSet querySubstitutablePaths(const PathSet & paths) = 0;
|
2007-08-12 02:29:28 +02:00
|
|
|
|
|
2012-07-11 16:43:24 +02:00
|
|
|
|
/* Query substitute info (i.e. references, derivers and download
|
|
|
|
|
sizes) of a set of paths. If a path does not have substitute
|
|
|
|
|
info, it's omitted from the resulting ‘infos’ map. */
|
download-from-binary-cache: parallelise fetching of NAR info files
Getting substitute information using the binary cache substituter has
non-trivial latency overhead. A package or NixOS system configuration
can have hundreds of dependencies, and in the worst case (when the
local info cache is empty) we have to do a separate HTTP request for
each of these. If the ping time to the server is t, getting N info
files will take tN seconds; e.g., with a ping time of 0.1s to
nixos.org, sequentially downloading 1000 info files (a typical NixOS
config) will take at least 100 seconds.
To fix this problem, the binary cache substituter can now perform
requests in parallel. This required changing the substituter
interface to support a function querySubstitutablePathInfos() that
queries multiple paths at the same time, and rewriting queryMissing()
to take advantage of parallelism. (Due to local caching,
parallelising queryMissing() is sufficient for most use cases, since
it's almost always called before building a derivation and thus fills
the local info cache.)
For example, parallelism speeds up querying all 1056 paths in a
particular NixOS system configuration from 116s to 2.6s. It works so
well because the eccentricity of the top-level derivation in the
dependency graph is only 9. So we only need 10 round-trips (when
using an unlimited number of parallel connections) to get everything.
Currently we do a maximum of 150 parallel connections to the server.
Thus it's important that the binary cache server (e.g. nixos.org) has
a high connection limit. Alternatively we could use HTTP pipelining,
but WWW::Curl doesn't support it and libcurl has a hard-coded limit of
5 requests per pipeline.
2012-07-07 01:08:20 +02:00
|
|
|
|
virtual void querySubstitutablePathInfos(const PathSet & paths,
|
|
|
|
|
SubstitutablePathInfos & infos) = 0;
|
2015-03-25 17:06:12 +01:00
|
|
|
|
|
2016-05-30 13:33:05 +02:00
|
|
|
|
virtual bool wantMassQuery() { return false; }
|
|
|
|
|
|
2016-05-04 13:36:54 +02:00
|
|
|
|
/* Import a path into the store. */
|
|
|
|
|
virtual void addToStore(const ValidPathInfo & info, const std::string & nar,
|
2016-05-31 11:18:45 +02:00
|
|
|
|
bool repair = false, bool dontCheckSigs = false) = 0;
|
2016-05-04 13:36:54 +02:00
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
|
/* Copy the contents of a path to the store and register the
|
2006-12-01 21:51:18 +01:00
|
|
|
|
validity the resulting path. The resulting path is returned.
|
2011-12-01 14:48:48 +01:00
|
|
|
|
The function object `filter' can be used to exclude files (see
|
2006-12-13 00:05:01 +01:00
|
|
|
|
libutil/archive.hh). */
|
2015-03-25 17:06:12 +01:00
|
|
|
|
virtual Path addToStore(const string & name, const Path & srcPath,
|
2008-12-03 17:10:17 +01:00
|
|
|
|
bool recursive = true, HashType hashAlgo = htSHA256,
|
2012-10-03 21:09:18 +02:00
|
|
|
|
PathFilter & filter = defaultPathFilter, bool repair = false) = 0;
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
|
|
|
|
/* Like addToStore, but the contents written to the output path is
|
|
|
|
|
a regular file containing the given string. */
|
2008-12-03 16:06:30 +01:00
|
|
|
|
virtual Path addTextToStore(const string & name, const string & s,
|
2012-10-03 21:09:18 +02:00
|
|
|
|
const PathSet & references, bool repair = false) = 0;
|
2006-11-30 19:02:04 +01:00
|
|
|
|
|
2016-03-21 17:55:57 +01:00
|
|
|
|
/* Write a NAR dump of a store path. */
|
2016-03-22 14:21:45 +01:00
|
|
|
|
virtual void narFromPath(const Path & path, Sink & sink) = 0;
|
2016-03-21 17:55:57 +01:00
|
|
|
|
|
2012-06-27 22:58:15 +02:00
|
|
|
|
/* For each path, if it's a derivation, build it. Building a
|
|
|
|
|
derivation means ensuring that the output paths are valid. If
|
2006-11-30 19:02:04 +01:00
|
|
|
|
they are already valid, this is a no-op. Otherwise, validity
|
2007-08-12 02:29:28 +02:00
|
|
|
|
can be reached in two ways. First, if the output paths is
|
|
|
|
|
substitutable, then build the path that way. Second, the
|
|
|
|
|
output paths can be created by running the builder, after
|
2012-06-27 22:58:15 +02:00
|
|
|
|
recursively building any sub-derivations. For inputs that are
|
|
|
|
|
not derivations, substitute them. */
|
2014-02-18 01:01:14 +01:00
|
|
|
|
virtual void buildPaths(const PathSet & paths, BuildMode buildMode = bmNormal) = 0;
|
2006-11-30 19:02:04 +01:00
|
|
|
|
|
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 17:57:40 +02:00
|
|
|
|
/* Build a single non-materialized derivation (i.e. not from an
|
|
|
|
|
on-disk .drv file). Note that ‘drvPath’ is only used for
|
|
|
|
|
informational purposes. */
|
|
|
|
|
virtual BuildResult buildDerivation(const Path & drvPath, const BasicDerivation & drv,
|
|
|
|
|
BuildMode buildMode = bmNormal) = 0;
|
|
|
|
|
|
2006-11-30 19:02:04 +01:00
|
|
|
|
/* Ensure that a path is valid. If it is not currently valid, it
|
|
|
|
|
may be made valid by running a substitute (if defined for the
|
|
|
|
|
path). */
|
2006-12-01 19:00:01 +01:00
|
|
|
|
virtual void ensurePath(const Path & path) = 0;
|
2006-12-02 17:41:36 +01:00
|
|
|
|
|
|
|
|
|
/* Add a store path as a temporary root of the garbage collector.
|
|
|
|
|
The root disappears as soon as we exit. */
|
|
|
|
|
virtual void addTempRoot(const Path & path) = 0;
|
|
|
|
|
|
2006-12-05 00:29:16 +01:00
|
|
|
|
/* Add an indirect root, which is merely a symlink to `path' from
|
|
|
|
|
/nix/var/nix/gcroots/auto/<hash of `path'>. `path' is supposed
|
|
|
|
|
to be a symlink to a store path. The garbage collector will
|
|
|
|
|
automatically remove the indirect root when it finds that
|
|
|
|
|
`path' has disappeared. */
|
|
|
|
|
virtual void addIndirectRoot(const Path & path) = 0;
|
|
|
|
|
|
2006-12-02 17:41:36 +01:00
|
|
|
|
/* Acquire the global GC lock, then immediately release it. This
|
|
|
|
|
function must be called after registering a new permanent root,
|
|
|
|
|
but before exiting. Otherwise, it is possible that a running
|
|
|
|
|
garbage collector doesn't see the new root and deletes the
|
|
|
|
|
stuff we've just built. By acquiring the lock briefly, we
|
|
|
|
|
ensure that either:
|
|
|
|
|
|
|
|
|
|
- The collector is already running, and so we block until the
|
|
|
|
|
collector is finished. The collector will know about our
|
|
|
|
|
*temporary* locks, which should include whatever it is we
|
|
|
|
|
want to register as a permanent lock.
|
|
|
|
|
|
|
|
|
|
- The collector isn't running, or it's just started but hasn't
|
|
|
|
|
acquired the GC lock yet. In that case we get and release
|
|
|
|
|
the lock right away, then exit. The collector scans the
|
|
|
|
|
permanent root and sees our's.
|
|
|
|
|
|
|
|
|
|
In either case the permanent root is seen by the collector. */
|
|
|
|
|
virtual void syncWithGC() = 0;
|
|
|
|
|
|
2006-12-05 02:31:45 +01:00
|
|
|
|
/* Find the roots of the garbage collector. Each root is a pair
|
|
|
|
|
(link, storepath) where `link' is the path of the symlink
|
|
|
|
|
outside of the Nix store that point to `storePath'. */
|
|
|
|
|
virtual Roots findRoots() = 0;
|
|
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
|
/* Perform a garbage collection. */
|
|
|
|
|
virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
|
2010-05-04 12:45:10 +02:00
|
|
|
|
|
2010-11-16 18:11:46 +01:00
|
|
|
|
/* Return a string representing information about the path that
|
|
|
|
|
can be loaded into the database using `nix-store --load-db' or
|
|
|
|
|
`nix-store --register-validity'. */
|
|
|
|
|
string makeValidityRegistration(const PathSet & paths,
|
|
|
|
|
bool showDerivers, bool showHash);
|
2014-09-01 22:21:42 +02:00
|
|
|
|
|
|
|
|
|
/* Optimise the disk space usage of the Nix store by hard-linking files
|
|
|
|
|
with the same contents. */
|
|
|
|
|
virtual void optimiseStore() = 0;
|
2015-06-01 23:20:11 +02:00
|
|
|
|
|
|
|
|
|
/* Check the integrity of the Nix store. Returns true if errors
|
|
|
|
|
remain. */
|
|
|
|
|
virtual bool verifyStore(bool checkContents, bool repair) = 0;
|
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 14:28:26 +01:00
|
|
|
|
|
2016-02-25 17:43:19 +01:00
|
|
|
|
/* Return an object to access files in the Nix store. */
|
|
|
|
|
virtual ref<FSAccessor> getFSAccessor() = 0;
|
|
|
|
|
|
2016-04-05 15:30:22 +02:00
|
|
|
|
/* Add signatures to the specified store path. The signatures are
|
|
|
|
|
not verified. */
|
|
|
|
|
virtual void addSignatures(const Path & storePath, const StringSet & sigs) = 0;
|
|
|
|
|
|
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 14:28:26 +01:00
|
|
|
|
/* Utility functions. */
|
|
|
|
|
|
|
|
|
|
/* Read a derivation, after ensuring its existence through
|
|
|
|
|
ensurePath(). */
|
|
|
|
|
Derivation derivationFromPath(const Path & drvPath);
|
|
|
|
|
|
|
|
|
|
/* Place in `paths' the set of all store paths in the file system
|
|
|
|
|
closure of `storePath'; that is, all paths than can be directly
|
|
|
|
|
or indirectly reached from it. `paths' is not cleared. If
|
|
|
|
|
`flipDirection' is true, the set of paths that can reach
|
|
|
|
|
`storePath' is returned; that is, the closures under the
|
|
|
|
|
`referrers' relation instead of the `references' relation is
|
|
|
|
|
returned. */
|
|
|
|
|
void computeFSClosure(const Path & path,
|
|
|
|
|
PathSet & paths, bool flipDirection = false,
|
|
|
|
|
bool includeOutputs = false, bool includeDerivers = false);
|
|
|
|
|
|
|
|
|
|
/* Given a set of paths that are to be built, return the set of
|
|
|
|
|
derivations that will be built, and the set of output paths
|
|
|
|
|
that will be substituted. */
|
|
|
|
|
void queryMissing(const PathSet & targets,
|
|
|
|
|
PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown,
|
|
|
|
|
unsigned long long & downloadSize, unsigned long long & narSize);
|
|
|
|
|
|
|
|
|
|
/* Sort a set of paths topologically under the references
|
|
|
|
|
relation. If p refers to q, then p preceeds q in this list. */
|
|
|
|
|
Paths topoSortPaths(const PathSet & paths);
|
|
|
|
|
|
2016-05-04 13:36:54 +02:00
|
|
|
|
/* Export multiple paths in the format expected by ‘nix-store
|
|
|
|
|
--import’. */
|
|
|
|
|
void exportPaths(const Paths & paths, Sink & sink);
|
|
|
|
|
|
|
|
|
|
void exportPath(const Path & path, Sink & sink);
|
|
|
|
|
|
|
|
|
|
/* Import a sequence of NAR dumps created by exportPaths() into
|
|
|
|
|
the Nix store. Optionally, the contents of the NARs are
|
|
|
|
|
preloaded into the specified FS accessor to speed up subsequent
|
|
|
|
|
access. */
|
2016-05-31 11:18:45 +02:00
|
|
|
|
Paths importPaths(Source & source, std::shared_ptr<FSAccessor> accessor,
|
|
|
|
|
bool dontCheckSigs = false);
|
2016-05-04 13:36:54 +02:00
|
|
|
|
|
2016-04-19 18:50:15 +02:00
|
|
|
|
struct Stats
|
|
|
|
|
{
|
|
|
|
|
std::atomic<uint64_t> narInfoRead{0};
|
|
|
|
|
std::atomic<uint64_t> narInfoReadAverted{0};
|
|
|
|
|
std::atomic<uint64_t> narInfoMissing{0};
|
|
|
|
|
std::atomic<uint64_t> narInfoWrite{0};
|
|
|
|
|
std::atomic<uint64_t> pathInfoCacheSize{0};
|
|
|
|
|
std::atomic<uint64_t> narRead{0};
|
|
|
|
|
std::atomic<uint64_t> narReadBytes{0};
|
|
|
|
|
std::atomic<uint64_t> narReadCompressedBytes{0};
|
|
|
|
|
std::atomic<uint64_t> narWrite{0};
|
|
|
|
|
std::atomic<uint64_t> narWriteAverted{0};
|
|
|
|
|
std::atomic<uint64_t> narWriteBytes{0};
|
|
|
|
|
std::atomic<uint64_t> narWriteCompressedBytes{0};
|
|
|
|
|
std::atomic<uint64_t> narWriteCompressionTimeMs{0};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Stats & getStats();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
Stats stats;
|
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 17:43:19 +01:00
|
|
|
|
class LocalFSStore : public Store
|
|
|
|
|
{
|
2016-03-21 17:55:57 +01:00
|
|
|
|
public:
|
2016-06-02 13:33:49 +02:00
|
|
|
|
const Path stateDir;
|
|
|
|
|
|
|
|
|
|
LocalFSStore(const Params & params);
|
|
|
|
|
|
2016-03-22 14:21:45 +01:00
|
|
|
|
void narFromPath(const Path & path, Sink & sink) override;
|
2016-02-25 17:43:19 +01:00
|
|
|
|
ref<FSAccessor> getFSAccessor() override;
|
2016-06-02 13:33:49 +02:00
|
|
|
|
|
|
|
|
|
/* Register a permanent GC root. */
|
|
|
|
|
Path addPermRoot(const Path & storePath,
|
|
|
|
|
const Path & gcRoot, bool indirect, bool allowOutsideRootsDir = false);
|
2016-06-02 18:43:36 +02:00
|
|
|
|
|
|
|
|
|
virtual Path getRealStoreDir() { return storeDir; }
|
2016-06-03 15:45:11 +02:00
|
|
|
|
|
|
|
|
|
Path toRealPath(const Path & storePath)
|
|
|
|
|
{
|
|
|
|
|
return getRealStoreDir() + "/" + baseNameOf(storePath);
|
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-07-20 20:10:47 +02:00
|
|
|
|
/* Extract the name part of the given store path. */
|
|
|
|
|
string storePathToName(const Path & path);
|
2015-03-25 17:06:12 +01:00
|
|
|
|
|
2016-02-15 12:49:01 +01:00
|
|
|
|
/* Extract the hash part of the given store path. */
|
|
|
|
|
string storePathToHash(const Path & path);
|
|
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
|
/* Check whether ‘name’ is a valid store path name part, i.e. contains
|
|
|
|
|
only the characters [a-zA-Z0-9\+\-\.\_\?\=] and doesn't start with
|
|
|
|
|
a dot. */
|
2006-11-30 18:43:04 +01:00
|
|
|
|
void checkStoreName(const string & name);
|
|
|
|
|
|
2007-11-29 17:18:24 +01:00
|
|
|
|
|
2016-05-03 14:45:50 +02:00
|
|
|
|
/* Copy a path from one store to another. */
|
|
|
|
|
void copyStorePath(ref<Store> srcStore, ref<Store> dstStore,
|
2016-05-12 15:42:19 +02:00
|
|
|
|
const Path & storePath, bool repair = false);
|
2016-05-03 14:45:50 +02:00
|
|
|
|
|
|
|
|
|
|
2006-12-05 03:18:46 +01:00
|
|
|
|
/* Remove the temporary roots file for this process. Any temporary
|
|
|
|
|
root becomes garbage after this point unless it has been registered
|
|
|
|
|
as a (permanent) root. */
|
|
|
|
|
void removeTempRoots();
|
|
|
|
|
|
|
|
|
|
|
2016-02-24 14:48:16 +01:00
|
|
|
|
/* Return a Store object to access the Nix store denoted by
|
|
|
|
|
‘uri’ (slight misnomer...). Supported values are:
|
|
|
|
|
|
|
|
|
|
* ‘direct’: The Nix store in /nix/store and database in
|
|
|
|
|
/nix/var/nix/db, accessed directly.
|
|
|
|
|
|
|
|
|
|
* ‘daemon’: The Nix store accessed via a Unix domain socket
|
|
|
|
|
connection to nix-daemon.
|
|
|
|
|
|
|
|
|
|
* ‘file://<path>’: A binary cache stored in <path>.
|
|
|
|
|
|
|
|
|
|
If ‘uri’ is empty, it defaults to ‘direct’ or ‘daemon’ depending on
|
|
|
|
|
whether the user has write access to the local Nix store/database.
|
2016-02-24 17:33:53 +01:00
|
|
|
|
set to true *unless* you're going to collect garbage. */
|
|
|
|
|
ref<Store> openStoreAt(const std::string & uri);
|
2016-02-24 14:48:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Open the store indicated by the ‘NIX_REMOTE’ environment variable. */
|
2016-02-24 17:33:53 +01:00
|
|
|
|
ref<Store> openStore();
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
|
|
|
|
|
2016-04-29 13:57:08 +02:00
|
|
|
|
/* Return the default substituter stores, defined by the
|
|
|
|
|
‘substituters’ option and various legacy options like
|
|
|
|
|
‘binary-caches’. */
|
|
|
|
|
std::list<ref<Store>> getDefaultSubstituters();
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 16:11:11 +01:00
|
|
|
|
/* Store implementation registration. */
|
2016-04-29 16:26:16 +02:00
|
|
|
|
typedef std::function<std::shared_ptr<Store>(
|
2016-06-01 14:49:12 +02:00
|
|
|
|
const std::string & uri, const Store::Params & params)> OpenStore;
|
2016-02-29 16:11:11 +01:00
|
|
|
|
|
|
|
|
|
struct RegisterStoreImplementation
|
|
|
|
|
{
|
|
|
|
|
typedef std::vector<OpenStore> Implementations;
|
|
|
|
|
static Implementations * implementations;
|
|
|
|
|
|
|
|
|
|
RegisterStoreImplementation(OpenStore fun)
|
|
|
|
|
{
|
|
|
|
|
if (!implementations) implementations = new Implementations;
|
|
|
|
|
implementations->push_back(fun);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
|
/* Display a set of paths in human-readable form (i.e., between quotes
|
|
|
|
|
and separated by commas). */
|
|
|
|
|
string showPaths(const PathSet & paths);
|
2006-11-30 18:43:04 +01:00
|
|
|
|
|
|
|
|
|
|
2008-01-29 19:17:36 +01:00
|
|
|
|
ValidPathInfo decodeValidPathInfo(std::istream & str,
|
|
|
|
|
bool hashGiven = false);
|
2007-08-12 02:29:28 +02:00
|
|
|
|
|
|
|
|
|
|
2011-12-30 15:47:14 +01:00
|
|
|
|
MakeError(SubstError, Error)
|
|
|
|
|
MakeError(BuildError, Error) /* denotes a permanent build failure */
|
2016-04-15 15:11:34 +02:00
|
|
|
|
MakeError(InvalidPath, Error)
|
2011-12-30 15:47:14 +01:00
|
|
|
|
|
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
|
}
|