2020-10-11 16:17:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "parsed-derivations.hh"
|
|
|
|
#include "lock.hh"
|
2021-02-26 15:20:33 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "pathlocks.hh"
|
2020-10-12 17:15:32 +00:00
|
|
|
#include "goal.hh"
|
2009-01-12 16:30:32 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
using std::map;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2014-01-21 17:29:55 +00:00
|
|
|
struct HookInstance;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
typedef enum {rpAccept, rpDecline, rpPostpone} HookReply;
|
|
|
|
|
2020-09-15 15:19:45 +00:00
|
|
|
/* Unless we are repairing, we don't both to test validity and just assume it,
|
|
|
|
so the choices are `Absent` or `Valid`. */
|
|
|
|
enum struct PathStatus {
|
|
|
|
Corrupt,
|
|
|
|
Absent,
|
|
|
|
Valid,
|
|
|
|
};
|
|
|
|
|
2020-09-04 15:15:51 +00:00
|
|
|
struct InitialOutputStatus {
|
2020-08-07 19:09:26 +00:00
|
|
|
StorePath path;
|
2020-09-15 15:19:45 +00:00
|
|
|
PathStatus status;
|
2020-08-07 19:09:26 +00:00
|
|
|
/* Valid in the store, and additionally non-corrupt if we are repairing */
|
|
|
|
bool isValid() const {
|
2020-09-15 15:19:45 +00:00
|
|
|
return status == PathStatus::Valid;
|
|
|
|
}
|
|
|
|
/* Merely present, allowed to be corrupt */
|
|
|
|
bool isPresent() const {
|
|
|
|
return status == PathStatus::Corrupt
|
|
|
|
|| status == PathStatus::Valid;
|
2020-08-07 19:09:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-04 15:15:51 +00:00
|
|
|
struct InitialOutput {
|
2020-08-07 19:09:26 +00:00
|
|
|
bool wanted;
|
2021-02-04 13:41:49 +00:00
|
|
|
Hash outputHash;
|
2020-09-04 15:15:51 +00:00
|
|
|
std::optional<InitialOutputStatus> known;
|
2020-08-07 19:09:26 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 13:36:15 +00:00
|
|
|
struct DerivationGoal : public Goal
|
2004-05-11 18:05:44 +00: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 15:57:40 +00:00
|
|
|
/* Whether to use an on-disk .drv file. */
|
|
|
|
bool useDerivation;
|
|
|
|
|
2005-01-20 16:01:07 +00:00
|
|
|
/* The path of the derivation. */
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath drvPath;
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2021-12-13 15:56:44 +00:00
|
|
|
/* The goal for the corresponding resolved derivation */
|
|
|
|
std::shared_ptr<DerivationGoal> resolvedDrvGoal;
|
2021-01-27 09:03:05 +00:00
|
|
|
|
2012-11-26 16:15:09 +00:00
|
|
|
/* The specific outputs that we need to build. Empty means all of
|
|
|
|
them. */
|
|
|
|
StringSet wantedOutputs;
|
|
|
|
|
|
|
|
/* Whether additional wanted outputs have been added. */
|
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
|
|
|
bool needRestart = false;
|
2012-11-26 16:15:09 +00:00
|
|
|
|
2013-01-02 11:38:28 +00:00
|
|
|
/* Whether to retry substituting the outputs after building the
|
2022-03-24 22:25:12 +00:00
|
|
|
inputs. This is done in case of an incomplete closure. */
|
|
|
|
bool retrySubstitution = false;
|
|
|
|
|
|
|
|
/* Whether we've retried substitution, in which case we won't try
|
|
|
|
again. */
|
|
|
|
bool retriedSubstitution = false;
|
2013-01-02 11:38:28 +00:00
|
|
|
|
2005-01-20 16:01:07 +00:00
|
|
|
/* The derivation stored at drvPath. */
|
2021-02-23 13:12:11 +00:00
|
|
|
std::unique_ptr<Derivation> drv;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2018-09-28 12:31:16 +00:00
|
|
|
std::unique_ptr<ParsedDerivation> parsedDrv;
|
2018-09-28 10:43:01 +00:00
|
|
|
|
2004-05-11 18:05:44 +00:00
|
|
|
/* The remainder is state held during the build. */
|
|
|
|
|
2020-08-07 19:09:26 +00:00
|
|
|
/* Locks on (fixed) output paths. */
|
2004-05-11 18:05:44 +00:00
|
|
|
PathLocks outputLocks;
|
|
|
|
|
2005-01-19 11:16:11 +00:00
|
|
|
/* All input paths (that is, the union of FS closures of the
|
|
|
|
immediate input paths). */
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet inputPaths;
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2020-09-04 15:15:51 +00:00
|
|
|
std::map<std::string, InitialOutput> initialOutputs;
|
2013-06-13 14:43:20 +00:00
|
|
|
|
2004-05-11 18:05:44 +00:00
|
|
|
/* File descriptor for the log file. */
|
2012-07-17 13:40:12 +00:00
|
|
|
AutoCloseFD fdLogFile;
|
2016-05-04 13:46:25 +00:00
|
|
|
std::shared_ptr<BufferedSink> logFileSink, logSink;
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2013-09-02 09:58:18 +00:00
|
|
|
/* Number of bytes received from the builder's stdout/stderr. */
|
|
|
|
unsigned long logSize;
|
|
|
|
|
2016-04-25 14:47:46 +00:00
|
|
|
/* The most recent log lines. */
|
|
|
|
std::list<std::string> logTail;
|
|
|
|
|
|
|
|
std::string currentLogLine;
|
2016-04-28 12:27:00 +00:00
|
|
|
size_t currentLogLinePos = 0; // to handle carriage return
|
2016-04-25 14:47:46 +00:00
|
|
|
|
2017-10-24 11:41:52 +00:00
|
|
|
std::string currentHookLine;
|
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
/* The build hook. */
|
2017-01-19 14:15:09 +00:00
|
|
|
std::unique_ptr<HookInstance> hook;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2020-03-15 06:23:17 +00:00
|
|
|
/* The sort of derivation we are building. */
|
|
|
|
DerivationType derivationType;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2005-01-19 11:16:11 +00:00
|
|
|
typedef void (DerivationGoal::*GoalState)();
|
2004-06-18 18:09:32 +00:00
|
|
|
GoalState state;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2014-02-17 22:04:52 +00:00
|
|
|
BuildMode buildMode;
|
|
|
|
|
2015-11-09 22:16:24 +00:00
|
|
|
/* The current round, if we're building multiple times. */
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t curRound = 1;
|
2015-11-09 22:16:24 +00:00
|
|
|
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t nrRounds;
|
2015-11-09 22:16:24 +00:00
|
|
|
|
2017-08-15 13:31:59 +00:00
|
|
|
std::unique_ptr<MaintainCount<uint64_t>> mcExpectedBuilds, mcRunningBuilds;
|
|
|
|
|
|
|
|
std::unique_ptr<Activity> act;
|
|
|
|
|
2020-06-15 14:03:29 +00:00
|
|
|
/* Activity that denotes waiting for a lock. */
|
|
|
|
std::unique_ptr<Activity> actLock;
|
|
|
|
|
2017-08-21 10:01:21 +00:00
|
|
|
std::map<ActivityId, Activity> builderActivities;
|
|
|
|
|
2017-10-24 12:24:57 +00:00
|
|
|
/* The remote machine on which we're building. */
|
|
|
|
std::string machineName;
|
|
|
|
|
2020-08-22 20:44:47 +00:00
|
|
|
DerivationGoal(const StorePath & drvPath,
|
|
|
|
const StringSet & wantedOutputs, Worker & worker,
|
|
|
|
BuildMode buildMode = bmNormal);
|
2020-06-16 20:20:18 +00:00
|
|
|
DerivationGoal(const StorePath & drvPath, const BasicDerivation & drv,
|
2020-08-22 20:44:47 +00:00
|
|
|
const StringSet & wantedOutputs, Worker & worker,
|
|
|
|
BuildMode buildMode = bmNormal);
|
2021-02-26 15:20:33 +00:00
|
|
|
virtual ~DerivationGoal();
|
2019-05-12 20:47:41 +00:00
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
void timedOut(Error && ex) override;
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string key() override;
|
2014-11-24 15:48:04 +00:00
|
|
|
|
2015-09-17 23:22:06 +00:00
|
|
|
void work() override;
|
2004-06-19 21:45:04 +00:00
|
|
|
|
2012-11-26 16:15:09 +00:00
|
|
|
/* Add wanted outputs to an already existing derivation goal. */
|
|
|
|
void addWantedOutputs(const StringSet & outputs);
|
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
/* The states. */
|
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
|
|
|
void getDerivation();
|
|
|
|
void loadDerivation();
|
2006-12-07 23:58:36 +00:00
|
|
|
void haveDerivation();
|
2020-08-07 19:09:26 +00:00
|
|
|
void outputsSubstitutionTried();
|
|
|
|
void gaveUpOnSubstitution();
|
2012-10-03 14:38:09 +00:00
|
|
|
void closureRepaired();
|
2005-01-19 11:16:11 +00:00
|
|
|
void inputsRealised();
|
2004-06-18 18:09:32 +00:00
|
|
|
void tryToBuild();
|
2021-02-26 15:20:33 +00:00
|
|
|
virtual void tryLocalBuild();
|
2004-06-18 18:09:32 +00:00
|
|
|
void buildDone();
|
|
|
|
|
2020-08-22 20:44:47 +00:00
|
|
|
void resolvedFinished();
|
|
|
|
|
2004-06-19 21:45:04 +00:00
|
|
|
/* Is the build hook willing to perform the build? */
|
|
|
|
HookReply tryBuildHook();
|
|
|
|
|
2021-02-26 15:20:33 +00:00
|
|
|
virtual int getChildStatus();
|
2012-06-25 19:45:16 +00:00
|
|
|
|
2014-02-17 21:25:15 +00:00
|
|
|
/* Check that the derivation outputs all exist and register them
|
|
|
|
as valid. */
|
2022-03-08 18:50:46 +00:00
|
|
|
virtual DrvOutputs registerOutputs();
|
2018-10-22 19:49:56 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
/* Open a log file and a pipe to it. */
|
2008-11-12 11:08:27 +00:00
|
|
|
Path openLogFile();
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2021-03-08 16:32:20 +00:00
|
|
|
/* Sign the newly built realisation if the store allows it */
|
|
|
|
virtual void signRealisation(Realisation&) {}
|
|
|
|
|
2012-05-30 14:12:29 +00:00
|
|
|
/* Close the log file. */
|
|
|
|
void closeLogFile();
|
|
|
|
|
2021-02-26 15:20:33 +00:00
|
|
|
/* Close the read side of the logger pipe. */
|
|
|
|
virtual void closeReadPipes();
|
|
|
|
|
|
|
|
/* Cleanup hooks for buildDone() */
|
|
|
|
virtual void cleanupHookFinally();
|
|
|
|
virtual void cleanupPreChildKill();
|
|
|
|
virtual void cleanupPostChildKill();
|
|
|
|
virtual bool cleanupDecideWhetherDiskFull();
|
|
|
|
virtual void cleanupPostOutputsRegisteredModeCheck();
|
|
|
|
virtual void cleanupPostOutputsRegisteredModeNonCheck();
|
|
|
|
|
|
|
|
virtual bool isReadDesc(int fd);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2004-06-29 09:41:50 +00:00
|
|
|
/* Callback used by the worker to write to the log. */
|
2022-02-25 15:00:00 +00:00
|
|
|
void handleChildOutput(int fd, std::string_view data) override;
|
2015-09-17 23:22:06 +00:00
|
|
|
void handleEOF(int fd) override;
|
2016-04-25 14:47:46 +00:00
|
|
|
void flushLine();
|
2004-06-29 09:41:50 +00:00
|
|
|
|
2020-08-07 19:09:26 +00:00
|
|
|
/* Wrappers around the corresponding Store methods that first consult the
|
|
|
|
derivation. This is currently needed because when there is no drv file
|
|
|
|
there also is no DB entry. */
|
2020-08-20 18:14:12 +00:00
|
|
|
std::map<std::string, std::optional<StorePath>> queryPartialDerivationOutputMap();
|
|
|
|
OutputPathMap queryDerivationOutputMap();
|
2020-08-07 19:09:26 +00:00
|
|
|
|
2022-03-08 18:50:46 +00:00
|
|
|
/* Update 'initialOutputs' to determine the current status of the
|
|
|
|
outputs of the derivation. Also returns a Boolean denoting
|
|
|
|
whether all outputs are valid and non-corrupt, and a
|
|
|
|
'DrvOutputs' structure containing the valid and wanted
|
|
|
|
outputs. */
|
|
|
|
std::pair<bool, DrvOutputs> checkPathValidity();
|
|
|
|
|
|
|
|
/* Aborts if any output is not valid or corrupt, and otherwise
|
|
|
|
returns a 'DrvOutputs' structure containing the wanted
|
|
|
|
outputs. */
|
|
|
|
DrvOutputs assertPathValidity();
|
2009-03-25 21:05:42 +00:00
|
|
|
|
2006-12-08 18:41:48 +00:00
|
|
|
/* Forcibly kill the child process, if any. */
|
2021-02-26 15:20:33 +00:00
|
|
|
virtual void killChild();
|
2012-10-03 14:38:09 +00:00
|
|
|
|
|
|
|
void repairClosure();
|
2015-07-20 01:15:45 +00:00
|
|
|
|
2020-05-14 14:00:54 +00:00
|
|
|
void started();
|
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
void done(
|
|
|
|
BuildResult::Status status,
|
2022-03-08 18:50:46 +00:00
|
|
|
DrvOutputs builtOutputs = {},
|
2020-06-15 17:25:35 +00:00
|
|
|
std::optional<Error> ex = {});
|
2018-04-17 10:03:27 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet exportReferences(const StorePathSet & storePaths);
|
2004-05-11 18:05:44 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 15:20:33 +00:00
|
|
|
MakeError(NotDeterministic, BuildError);
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|