2006-11-30 19:54:43 +00:00
# include "serialise.hh"
# include "util.hh"
2021-03-02 03:50:41 +00:00
# include "path-with-outputs.hh"
2022-03-01 18:31:36 +00:00
# include "gc-store.hh"
2020-09-07 09:26:09 +00:00
# include "remote-fs-accessor.hh"
2022-03-01 19:43:07 +00:00
# include "build-result.hh"
2006-11-30 18:35:50 +00:00
# include "remote-store.hh"
2023-04-17 17:40:46 +00:00
# include "remote-store-connection.hh"
2006-11-30 20:13:59 +00:00
# include "worker-protocol.hh"
2022-03-08 21:53:26 +00:00
# include "worker-protocol-impl.hh"
2006-11-30 20:45:20 +00:00
# include "archive.hh"
2006-12-04 13:09:16 +00:00
# include "globals.hh"
2015-09-03 10:56:59 +00:00
# include "derivations.hh"
2016-02-23 14:00:59 +00:00
# include "pool.hh"
2018-04-16 09:14:39 +00:00
# include "finally.hh"
2020-07-02 15:04:31 +00:00
# include "logging.hh"
2020-09-21 16:40:11 +00:00
# include "callback.hh"
2020-11-02 12:57:58 +00:00
# include "filetransfer.hh"
2021-01-26 09:48:41 +00:00
# include <nlohmann/json.hpp>
2006-11-30 18:35:50 +00:00
namespace nix {
2016-09-02 18:15:04 +00:00
/* TODO: Separate these store impls into different files, give them better names */
2017-03-03 18:05:50 +00:00
RemoteStore : : RemoteStore ( const Params & params )
2020-12-20 15:33:12 +00:00
: RemoteStoreConfig ( params )
, Store ( params )
2016-06-01 12:49:12 +00:00
, connections ( make_ref < Pool < Connection > > (
2017-04-13 13:55:38 +00:00
std : : max ( 1 , ( int ) maxConnections ) ,
2020-08-19 19:34:47 +00:00
[ this ] ( ) {
auto conn = openConnectionWrapper ( ) ;
try {
initConnection ( * conn ) ;
} catch ( . . . ) {
failed = true ;
throw ;
}
return conn ;
} ,
2017-09-14 16:10:38 +00:00
[ this ] ( const ref < Connection > & r ) {
return
r - > to . good ( )
& & r - > from . good ( )
& & std : : chrono : : duration_cast < std : : chrono : : seconds > (
std : : chrono : : steady_clock : : now ( ) - r - > startTime ) . count ( ) < maxConnectionAge ;
}
2016-02-24 10:39:56 +00:00
) )
2006-12-04 13:28:14 +00:00
{
2008-12-11 14:30:25 +00:00
}
2017-03-03 18:35:34 +00:00
ref < RemoteStore : : Connection > RemoteStore : : openConnectionWrapper ( )
{
if ( failed )
2017-07-30 11:27:57 +00:00
throw Error ( " opening a connection to remote store '%s' previously failed " , getUri ( ) ) ;
2017-03-03 18:35:34 +00:00
try {
return openConnection ( ) ;
} catch ( . . . ) {
failed = true ;
throw ;
}
}
2016-09-02 18:15:04 +00:00
void RemoteStore : : initConnection ( Connection & conn )
{
2016-02-23 14:00:59 +00:00
/* Send the magic greeting, check for the reply. */
try {
2024-03-04 07:12:15 +00:00
conn . from . endOfFileError = " Nix daemon disconnected unexpectedly (maybe it crashed?) " ;
2016-09-02 18:15:04 +00:00
conn . to < < WORKER_MAGIC_1 ;
conn . to . flush ( ) ;
2021-09-23 15:48:52 +00:00
StringSink saved ;
try {
TeeSource tee ( conn . from , saved ) ;
unsigned int magic = readInt ( tee ) ;
if ( magic ! = WORKER_MAGIC_2 )
throw Error ( " protocol mismatch " ) ;
} catch ( SerialisationError & e ) {
2021-09-23 16:01:04 +00:00
/* In case the other side is waiting for our input, close
it . */
conn . closeWrite ( ) ;
2021-09-23 15:48:52 +00:00
auto msg = conn . from . drain ( ) ;
2022-01-17 21:20:05 +00:00
throw Error ( " protocol mismatch, got '%s' " , chomp ( saved . s + msg ) ) ;
2021-09-23 15:48:52 +00:00
}
2016-02-23 14:00:59 +00:00
2017-03-01 12:52:54 +00:00
conn . from > > conn . daemonVersion ;
2016-09-02 18:15:04 +00:00
if ( GET_PROTOCOL_MAJOR ( conn . daemonVersion ) ! = GET_PROTOCOL_MAJOR ( PROTOCOL_VERSION ) )
2016-02-23 14:00:59 +00:00
throw Error ( " Nix daemon protocol version not supported " ) ;
2016-09-02 18:15:04 +00:00
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) < 10 )
2016-07-27 13:03:20 +00:00
throw Error ( " the Nix daemon version is too old " ) ;
2016-09-02 18:15:04 +00:00
conn . to < < PROTOCOL_VERSION ;
2016-02-23 14:00:59 +00:00
2016-09-02 18:15:04 +00:00
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) > = 14 ) {
2021-12-22 14:49:51 +00:00
// Obsolete CPU affinity.
conn . to < < 0 ;
2016-02-23 14:00:59 +00:00
}
2016-09-02 18:15:04 +00:00
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) > = 11 )
2022-01-25 20:14:27 +00:00
conn . to < < false ; // obsolete reserveSpace
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) > = 33 ) {
conn . to . flush ( ) ;
conn . daemonNixVersion = readString ( conn . from ) ;
}
2016-02-23 14:00:59 +00:00
2022-12-26 20:21:08 +00:00
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) > = 35 ) {
2023-04-17 17:40:46 +00:00
conn . remoteTrustsUs = WorkerProto : : Serialise < std : : optional < TrustedFlag > > : : read ( * this , conn ) ;
2022-12-26 20:21:08 +00:00
} else {
// We don't know the answer; protocol to old.
conn . remoteTrustsUs = std : : nullopt ;
}
2018-10-16 21:36:15 +00:00
auto ex = conn . processStderr ( ) ;
if ( ex ) std : : rethrow_exception ( ex ) ;
2016-02-23 14:00:59 +00:00
}
catch ( Error & e ) {
2017-07-30 11:27:57 +00:00
throw Error ( " cannot open connection to remote store '%s': %s " , getUri ( ) , e . what ( ) ) ;
2016-02-23 14:00:59 +00:00
}
setOptions ( conn ) ;
2006-11-30 18:35:50 +00:00
}
2016-09-02 18:15:04 +00:00
void RemoteStore : : setOptions ( Connection & conn )
2007-09-18 09:11:20 +00:00
{
2023-05-26 15:07:25 +00:00
conn . to < < WorkerProto : : Op : : SetOptions
2015-07-19 23:16:16 +00:00
< < settings . keepFailed
< < settings . keepGoing
< < settings . tryFallback
2020-06-18 22:09:22 +00:00
< < verbosity
2015-07-19 23:16:16 +00:00
< < settings . maxBuildJobs
2016-07-27 13:03:20 +00:00
< < settings . maxSilentTime
2017-10-24 09:00:16 +00:00
< < true
2020-06-18 22:09:22 +00:00
< < ( settings . verboseBuild ? lvlError : lvlVomit )
2016-07-27 13:03:20 +00:00
< < 0 // obsolete log type
< < 0 /* obsolete print build trace */
< < settings . buildCores
< < settings . useSubstitutes ;
2012-07-30 21:13:25 +00:00
2016-09-02 18:15:04 +00:00
if ( GET_PROTOCOL_MINOR ( conn . daemonVersion ) > = 12 ) {
2018-03-27 16:41:31 +00:00
std : : map < std : : string , Config : : SettingInfo > overrides ;
2020-11-02 12:57:58 +00:00
settings . getSettings ( overrides , true ) ; // libstore settings
fileTransferSettings . getSettings ( overrides , true ) ;
2019-08-28 14:29:44 +00:00
overrides . erase ( settings . keepFailed . name ) ;
overrides . erase ( settings . keepGoing . name ) ;
overrides . erase ( settings . tryFallback . name ) ;
overrides . erase ( settings . maxBuildJobs . name ) ;
overrides . erase ( settings . maxSilentTime . name ) ;
overrides . erase ( settings . buildCores . name ) ;
overrides . erase ( settings . useSubstitutes . name ) ;
2020-07-02 15:04:31 +00:00
overrides . erase ( loggerSettings . showTrace . name ) ;
2023-03-17 14:33:48 +00:00
overrides . erase ( experimentalFeatureSettings . experimentalFeatures . name ) ;
2023-02-02 01:05:56 +00:00
overrides . erase ( settings . pluginFiles . name ) ;
2016-09-02 18:15:04 +00:00
conn . to < < overrides . size ( ) ;
2015-07-19 23:16:16 +00:00
for ( auto & i : overrides )
2018-03-27 16:41:31 +00:00
conn . to < < i . first < < i . second . value ;
2012-07-31 22:19:44 +00:00
}
2018-10-16 21:36:15 +00:00
auto ex = conn . processStderr ( ) ;
if ( ex ) std : : rethrow_exception ( ex ) ;
}
2023-04-17 16:14:15 +00:00
RemoteStore : : ConnectionHandle : : ~ ConnectionHandle ( )
2018-10-16 21:36:15 +00:00
{
2023-04-17 16:14:15 +00:00
if ( ! daemonException & & std : : uncaught_exceptions ( ) ) {
handle . markBad ( ) ;
debug ( " closing daemon connection because of an exception " ) ;
2018-10-16 21:36:15 +00:00
}
2023-04-17 16:14:15 +00:00
}
2018-10-16 21:36:15 +00:00
2023-04-17 16:14:15 +00:00
void RemoteStore : : ConnectionHandle : : processStderr ( Sink * sink , Source * source , bool flush )
{
auto ex = handle - > processStderr ( sink , source , flush ) ;
if ( ex ) {
daemonException = true ;
2023-09-07 02:57:37 +00:00
try {
std : : rethrow_exception ( ex ) ;
} catch ( const Error & e ) {
// Nix versions before #4628 did not have an adequate behavior for reporting that the derivation format was upgraded.
// To avoid having to add compatibility logic in many places, we expect to catch almost all occurrences of the
// old incomprehensible error here, so that we can explain to users what's going on when their daemon is
// older than #4628 (2023).
if ( experimentalFeatureSettings . isEnabled ( Xp : : DynamicDerivations ) & &
GET_PROTOCOL_MINOR ( handle - > daemonVersion ) < = 35 )
{
auto m = e . msg ( ) ;
if ( m . find ( " parsing derivation " ) ! = std : : string : : npos & &
m . find ( " expected string " ) ! = std : : string : : npos & &
m . find ( " Derive([ " ) ! = std : : string : : npos )
2024-03-04 06:33:32 +00:00
throw Error ( " %s, this might be because the daemon is too old to understand dependencies on dynamic derivations. Check to see if the raw derivation is in the form '%s' " , std : : move ( m ) , " DrvWithVersion(..) " ) ;
2023-09-07 02:57:37 +00:00
}
throw ;
}
2018-10-16 21:36:15 +00:00
}
2023-04-17 16:14:15 +00:00
}
2018-10-16 21:36:15 +00:00
2023-04-17 16:14:15 +00:00
RemoteStore : : ConnectionHandle RemoteStore : : getConnection ( )
2018-10-16 21:36:15 +00:00
{
return ConnectionHandle ( connections - > get ( ) ) ;
2007-09-18 09:11:20 +00:00
}
2021-11-05 15:17:49 +00:00
void RemoteStore : : setOptions ( )
{
setOptions ( * ( getConnection ( ) . handle ) ) ;
}
2007-09-18 09:11:20 +00:00
2019-12-05 18:11:09 +00:00
bool RemoteStore : : isValidPathUncached ( const StorePath & path )
2006-11-30 18:35:50 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : IsValidPath < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2017-03-01 12:52:54 +00:00
return readInt ( conn - > from ) ;
2006-11-30 18:35:50 +00:00
}
2019-12-05 18:11:09 +00:00
StorePathSet RemoteStore : : queryValidPaths ( const StorePathSet & paths , SubstituteFlag maybeSubstitute )
2012-07-11 15:08:47 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2016-02-23 14:00:59 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 12 ) {
2019-12-05 18:11:09 +00:00
StorePathSet res ;
2015-07-17 17:24:28 +00:00
for ( auto & i : paths )
2020-06-16 20:20:18 +00:00
if ( isValidPath ( i ) ) res . insert ( i ) ;
2012-07-11 15:08:47 +00:00
return res ;
} else {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryValidPaths ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , paths ) ;
2020-10-21 19:31:19 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 27 ) {
2024-03-04 06:16:31 +00:00
conn - > to < < maybeSubstitute ;
2020-10-21 19:31:19 +00:00
}
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2012-07-11 15:08:47 +00:00
}
}
2019-12-05 18:11:09 +00:00
StorePathSet RemoteStore : : queryAllValidPaths ( )
2008-01-29 18:17:36 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryAllValidPaths ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2008-01-29 18:17:36 +00:00
}
2019-12-05 18:11:09 +00:00
StorePathSet RemoteStore : : querySubstitutablePaths ( const StorePathSet & paths )
2006-11-30 22:43:55 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2016-02-23 14:00:59 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 12 ) {
2019-12-05 18:11:09 +00:00
StorePathSet res ;
2015-07-17 17:24:28 +00:00
for ( auto & i : paths ) {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : HasSubstitutes < < printStorePath ( i ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2020-06-16 20:20:18 +00:00
if ( readInt ( conn - > from ) ) res . insert ( i ) ;
2012-07-11 21:52:18 +00:00
}
return res ;
} else {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QuerySubstitutablePaths ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , paths ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2012-07-11 21:52:18 +00:00
}
2006-11-30 18:35:50 +00:00
}
2020-06-17 19:03:05 +00:00
void RemoteStore : : querySubstitutablePathInfos ( const StorePathCAMap & pathsMap , SubstitutablePathInfos & infos )
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-06 23:08:20 +00:00
{
2020-06-17 19:03:05 +00:00
if ( pathsMap . empty ( ) ) return ;
2012-07-11 14:43:24 +00:00
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2012-07-30 21:13:25 +00:00
2016-02-23 14:00:59 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 12 ) {
2012-07-30 21:13:25 +00:00
2020-06-17 19:03:05 +00:00
for ( auto & i : pathsMap ) {
2012-07-11 14:43:24 +00:00
SubstitutablePathInfo info ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QuerySubstitutablePathInfo < < printStorePath ( i . first ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
unsigned int reply = readInt ( conn - > from ) ;
2012-07-11 14:43:24 +00:00
if ( reply = = 0 ) continue ;
2019-12-05 18:11:09 +00:00
auto deriver = readString ( conn - > from ) ;
if ( deriver ! = " " )
info . deriver = parseStorePath ( deriver ) ;
2023-04-17 17:40:46 +00:00
info . references = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2016-02-23 14:00:59 +00:00
info . downloadSize = readLongLong ( conn - > from ) ;
2016-07-27 13:03:20 +00:00
info . narSize = readLongLong ( conn - > from ) ;
2020-06-17 19:03:05 +00:00
infos . insert_or_assign ( i . first , std : : move ( info ) ) ;
2012-07-11 14:43:24 +00:00
}
2012-07-30 21:13:25 +00:00
2012-07-11 14:43:24 +00:00
} else {
2012-07-30 21:13:25 +00:00
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QuerySubstitutablePathInfos ;
2020-06-19 22:06:19 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 22 ) {
StorePathSet paths ;
for ( auto & path : pathsMap )
paths . insert ( path . first ) ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , paths ) ;
2020-06-19 22:06:19 +00:00
} else
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , pathsMap ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2017-03-01 12:52:54 +00:00
size_t count = readNum < size_t > ( conn - > from ) ;
for ( size_t n = 0 ; n < count ; n + + ) {
2023-01-14 21:38:43 +00:00
SubstitutablePathInfo & info ( infos [ parseStorePath ( readString ( conn - > from ) ) ] ) ;
2019-12-05 18:11:09 +00:00
auto deriver = readString ( conn - > from ) ;
if ( deriver ! = " " )
info . deriver = parseStorePath ( deriver ) ;
2023-04-17 17:40:46 +00:00
info . references = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2016-02-23 14:00:59 +00:00
info . downloadSize = readLongLong ( conn - > from ) ;
info . narSize = readLongLong ( conn - > from ) ;
2012-07-11 14:43:24 +00:00
}
2012-07-30 21:13:25 +00: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-06 23:08:20 +00:00
}
}
2019-12-05 18:11:09 +00:00
void RemoteStore : : queryPathInfoUncached ( const StorePath & path ,
2018-09-25 16:54:16 +00:00
Callback < std : : shared_ptr < const ValidPathInfo > > callback ) noexcept
2010-11-16 17:11:46 +00:00
{
2018-03-27 20:16:01 +00:00
try {
2020-09-18 09:22:13 +00:00
std : : shared_ptr < const ValidPathInfo > info ;
2018-04-09 19:26:16 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryPathInfo < < printStorePath ( path ) ;
2018-04-09 19:26:16 +00:00
try {
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2018-04-09 19:26:16 +00:00
} catch ( Error & e ) {
// Ugly backwards compatibility hack.
if ( e . msg ( ) . find ( " is not valid " ) ! = std : : string : : npos )
2022-12-07 11:58:58 +00:00
throw InvalidPath ( std : : move ( e . info ( ) ) ) ;
2018-04-09 19:26:16 +00:00
throw ;
}
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 17 ) {
bool valid ; conn - > from > > valid ;
2019-12-05 18:11:09 +00:00
if ( ! valid ) throw InvalidPath ( " path '%s' is not valid " , printStorePath ( path ) ) ;
2018-04-09 19:26:16 +00:00
}
2021-07-26 11:31:09 +00:00
info = std : : make_shared < ValidPathInfo > (
2024-03-04 03:59:31 +00:00
StorePath { path } ,
WorkerProto : : Serialise < UnkeyedValidPathInfo > : : read ( * this , * conn ) ) ;
2016-09-16 16:54:14 +00:00
}
2018-03-27 20:16:01 +00:00
callback ( std : : move ( info ) ) ;
} catch ( . . . ) { callback . rethrow ( ) ; }
2010-11-16 17:11:46 +00:00
}
2019-12-05 18:11:09 +00:00
void RemoteStore : : queryReferrers ( const StorePath & path ,
StorePathSet & referrers )
2006-11-30 18:35:50 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryReferrers < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
for ( auto & i : WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) )
2020-06-16 20:20:18 +00:00
referrers . insert ( i ) ;
2006-11-30 18:35:50 +00:00
}
2019-12-05 18:11:09 +00:00
StorePathSet RemoteStore : : queryValidDerivers ( const StorePath & path )
2012-12-20 17:41:44 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryValidDerivers < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2012-12-20 17:41:44 +00:00
}
2019-12-05 18:11:09 +00:00
StorePathSet RemoteStore : : queryDerivationOutputs ( const StorePath & path )
2010-02-22 12:44:36 +00:00
{
2020-11-16 07:35:50 +00:00
if ( GET_PROTOCOL_MINOR ( getProtocol ( ) ) > = 0x16 ) {
2020-06-10 09:20:52 +00:00
return Store : : queryDerivationOutputs ( path ) ;
}
2020-11-16 07:35:50 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryDerivationOutputs < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2010-02-22 12:44:36 +00:00
}
2023-07-19 18:52:35 +00:00
std : : map < std : : string , std : : optional < StorePath > > RemoteStore : : queryPartialDerivationOutputMap ( const StorePath & path , Store * evalStore_ )
2020-06-10 09:20:52 +00:00
{
2020-09-07 09:26:09 +00:00
if ( GET_PROTOCOL_MINOR ( getProtocol ( ) ) > = 0x16 ) {
2023-07-19 18:52:35 +00:00
if ( ! evalStore_ ) {
auto conn ( getConnection ( ) ) ;
conn - > to < < WorkerProto : : Op : : QueryDerivationOutputMap < < printStorePath ( path ) ;
conn . processStderr ( ) ;
return WorkerProto : : Serialise < std : : map < std : : string , std : : optional < StorePath > > > : : read ( * this , * conn ) ;
} else {
auto & evalStore = * evalStore_ ;
auto outputs = evalStore . queryStaticPartialDerivationOutputMap ( path ) ;
// union with the first branch overriding the statically-known ones
// when non-`std::nullopt`.
for ( auto & & [ outputName , optPath ] : queryPartialDerivationOutputMap ( path , nullptr ) ) {
if ( optPath )
outputs . insert_or_assign ( std : : move ( outputName ) , std : : move ( optPath ) ) ;
else
outputs . insert ( { std : : move ( outputName ) , std : : nullopt } ) ;
}
return outputs ;
}
2020-09-07 09:26:09 +00:00
} else {
2023-07-19 18:52:35 +00:00
auto & evalStore = evalStore_ ? * evalStore_ : * this ;
2020-09-07 09:26:09 +00:00
// Fallback for old daemon versions.
// For floating-CA derivations (and their co-dependencies) this is an
// under-approximation as it only returns the paths that can be inferred
// from the derivation itself (and not the ones that are known because
// the have been built), but as old stores don't handle floating-CA
// derivations this shouldn't matter
2023-07-19 18:52:35 +00:00
return evalStore . queryStaticPartialDerivationOutputMap ( path ) ;
2020-09-07 09:26:09 +00:00
}
2020-06-10 09:20:52 +00:00
}
2019-12-05 18:11:09 +00:00
std : : optional < StorePath > RemoteStore : : queryPathFromHashPart ( const std : : string & hashPart )
2012-07-17 22:55:39 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryPathFromHashPart < < hashPart ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
Path path = readString ( conn - > from ) ;
2019-12-05 18:11:09 +00:00
if ( path . empty ( ) ) return { } ;
return parseStorePath ( path ) ;
2012-07-17 22:55:39 +00:00
}
2020-09-22 09:40:19 +00:00
ref < const ValidPathInfo > RemoteStore : : addCAToStore (
Source & dump ,
2022-02-15 14:08:06 +00:00
std : : string_view name ,
2020-09-22 09:40:19 +00:00
ContentAddressMethod caMethod ,
2020-10-12 23:51:23 +00:00
HashType hashType ,
2020-09-22 09:40:19 +00:00
const StorePathSet & references ,
RepairFlag repair )
2020-09-17 17:27:11 +00:00
{
2020-09-22 13:28:20 +00:00
std : : optional < ConnectionHandle > conn_ ( getConnection ( ) ) ;
auto & conn = * conn_ ;
2020-09-17 17:27:11 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 25 ) {
conn - > to
2023-05-26 15:07:25 +00:00
< < WorkerProto : : Op : : AddToStore
2020-09-17 17:27:11 +00:00
< < name
2023-04-01 20:40:32 +00:00
< < caMethod . render ( hashType ) ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , references ) ;
2020-09-18 08:06:34 +00:00
conn - > to < < repair ;
2020-09-17 17:27:11 +00:00
2020-10-30 20:47:34 +00:00
// The dump source may invoke the store, so we need to make some room.
connections - > incCapacity ( ) ;
{
Finally cleanup ( [ & ] ( ) { connections - > decCapacity ( ) ; } ) ;
conn . withFramedSink ( [ & ] ( Sink & sink ) {
dump . drainInto ( sink ) ;
} ) ;
}
2020-09-17 17:27:11 +00:00
2021-07-26 11:31:09 +00:00
return make_ref < ValidPathInfo > (
2024-03-04 03:59:31 +00:00
WorkerProto : : Serialise < ValidPathInfo > : : read ( * this , * conn ) ) ;
2020-09-17 17:27:11 +00:00
}
else {
if ( repair ) throw Error ( " repairing is not supported when building through the Nix daemon protocol < 1.25 " ) ;
2020-09-17 18:19:15 +00:00
std : : visit ( overloaded {
2023-04-17 23:02:45 +00:00
[ & ] ( const TextIngestionMethod & thm ) - > void {
2020-10-12 23:51:23 +00:00
if ( hashType ! = htSHA256 )
2023-05-09 17:24:53 +00:00
throw UnimplementedError ( " When adding text-hashed data called '%s', only SHA-256 is supported but '%s' was given " ,
name , printHashType ( hashType ) ) ;
2020-09-17 18:19:15 +00:00
std : : string s = dump . drain ( ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : AddTextToStore < < name < < s ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , references ) ;
2020-09-17 18:19:15 +00:00
conn . processStderr ( ) ;
} ,
2021-10-01 17:25:22 +00:00
[ & ] ( const FileIngestionMethod & fim ) - > void {
2020-09-17 18:19:15 +00:00
conn - > to
2023-05-26 15:07:25 +00:00
< < WorkerProto : : Op : : AddToStore
2020-09-17 18:19:15 +00:00
< < name
2020-10-12 23:51:23 +00:00
< < ( ( hashType = = htSHA256 & & fim = = FileIngestionMethod : : Recursive ) ? 0 : 1 ) /* backwards compatibility hack */
< < ( fim = = FileIngestionMethod : : Recursive ? 1 : 0 )
< < printHashType ( hashType ) ;
2020-09-17 18:19:15 +00:00
2020-09-17 17:27:11 +00:00
try {
2020-09-17 18:19:15 +00:00
conn - > to . written = 0 ;
connections - > incCapacity ( ) ;
{
Finally cleanup ( [ & ] ( ) { connections - > decCapacity ( ) ; } ) ;
2020-10-12 23:51:23 +00:00
if ( fim = = FileIngestionMethod : : Recursive ) {
2020-09-17 18:19:15 +00:00
dump . drainInto ( conn - > to ) ;
} else {
std : : string contents = dump . drain ( ) ;
dumpString ( contents , conn - > to ) ;
}
}
2020-09-17 17:27:11 +00:00
conn . processStderr ( ) ;
2020-09-17 18:19:15 +00:00
} catch ( SysError & e ) {
/* Daemon closed while we were sending the path. Probably OOM
or I / O error . */
if ( e . errNo = = EPIPE )
try {
conn . processStderr ( ) ;
} catch ( EndOfFile & e ) { }
throw ;
}
2020-09-17 17:27:11 +00:00
2020-09-17 18:19:15 +00:00
}
2023-03-30 21:12:49 +00:00
} , caMethod . raw ) ;
2020-09-18 09:22:13 +00:00
auto path = parseStorePath ( readString ( conn - > from ) ) ;
2020-09-22 13:28:20 +00:00
// Release our connection to prevent a deadlock in queryPathInfo().
conn_ . reset ( ) ;
2020-09-18 09:22:13 +00:00
return queryPathInfo ( path ) ;
2020-09-17 17:27:11 +00:00
}
}
2020-09-22 09:40:19 +00:00
2022-02-15 14:08:06 +00:00
StorePath RemoteStore : : addToStoreFromDump ( Source & dump , std : : string_view name ,
2021-11-09 09:24:49 +00:00
FileIngestionMethod method , HashType hashType , RepairFlag repair , const StorePathSet & references )
2020-09-17 18:19:15 +00:00
{
2020-10-12 23:51:23 +00:00
return addCAToStore ( dump , name , method , hashType , references , repair ) - > path ;
2020-09-17 18:19:15 +00:00
}
2020-09-17 17:27:11 +00:00
2018-03-21 22:42:21 +00:00
void RemoteStore : : addToStore ( const ValidPathInfo & info , Source & source ,
2020-07-13 15:37:44 +00:00
RepairFlag repair , CheckSigsFlag checkSigs )
2016-05-04 11:36:54 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2016-11-09 17:45:06 +00:00
2016-11-09 17:57:22 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 18 ) {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : ImportPaths ;
2016-11-09 17:57:22 +00:00
2018-03-21 22:46:03 +00:00
auto source2 = sinkToSource ( [ & ] ( Sink & sink ) {
sink < < 1 // == path follows
;
copyNAR ( source , sink ) ;
sink
< < exportMagic
2019-12-05 18:11:09 +00:00
< < printStorePath ( info . path ) ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , info . references ) ;
2019-12-05 18:11:09 +00:00
sink
< < ( info . deriver ? printStorePath ( * info . deriver ) : " " )
2018-03-21 22:46:03 +00:00
< < 0 // == no legacy signature
< < 0 // == no path follows
;
} ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( 0 , source2 . get ( ) ) ;
2016-11-09 17:57:22 +00:00
2023-04-17 17:40:46 +00:00
auto importedPaths = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2023-01-14 21:38:43 +00:00
assert ( importedPaths . size ( ) < = 1 ) ;
2016-11-09 17:57:22 +00:00
}
2016-11-09 17:45:06 +00:00
2016-11-09 17:57:22 +00:00
else {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : AddToStoreNar
2019-12-05 18:11:09 +00:00
< < printStorePath ( info . path )
< < ( info . deriver ? printStorePath ( * info . deriver ) : " " )
2020-06-18 22:09:22 +00:00
< < info . narHash . to_string ( Base16 , false ) ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , info . references ) ;
2019-12-05 18:11:09 +00:00
conn - > to < < info . registrationTime < < info . narSize
2020-06-02 00:37:43 +00:00
< < info . ultimate < < info . sigs < < renderContentAddress ( info . ca )
2017-06-28 16:11:01 +00:00
< < repair < < ! checkSigs ;
2020-07-28 22:48:39 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 23 ) {
2020-09-17 15:36:16 +00:00
conn . withFramedSink ( [ & ] ( Sink & sink ) {
2020-07-28 22:48:39 +00:00
copyNAR ( source , sink ) ;
2020-09-17 15:36:16 +00:00
} ) ;
2020-07-28 22:48:39 +00:00
} else if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 21 ) {
conn . processStderr ( 0 , & source ) ;
} else {
copyNAR ( source , conn - > to ) ;
conn . processStderr ( 0 , nullptr ) ;
}
2016-11-09 17:57:22 +00:00
}
2016-05-04 11:36:54 +00:00
}
2022-06-08 12:03:46 +00:00
void RemoteStore : : addMultipleToStore (
PathsSource & pathsToCopy ,
Activity & act ,
RepairFlag repair ,
CheckSigsFlag checkSigs )
{
auto source = sinkToSource ( [ & ] ( Sink & sink ) {
sink < < pathsToCopy . size ( ) ;
for ( auto & [ pathInfo , pathSource ] : pathsToCopy ) {
2024-03-04 03:59:31 +00:00
WorkerProto : : Serialise < ValidPathInfo > : : write ( * this ,
WorkerProto : : WriteConn {
. to = sink ,
. version = 16 ,
} ,
pathInfo ) ;
2022-06-08 12:03:46 +00:00
pathSource - > drainInto ( sink ) ;
}
} ) ;
addMultipleToStore ( * source , repair , checkSigs ) ;
}
2021-07-26 11:31:09 +00:00
void RemoteStore : : addMultipleToStore (
Source & source ,
RepairFlag repair ,
CheckSigsFlag checkSigs )
{
if ( GET_PROTOCOL_MINOR ( getConnection ( ) - > daemonVersion ) > = 32 ) {
auto conn ( getConnection ( ) ) ;
conn - > to
2023-05-26 15:07:25 +00:00
< < WorkerProto : : Op : : AddMultipleToStore
2021-07-26 11:31:09 +00:00
< < repair
< < ! checkSigs ;
conn . withFramedSink ( [ & ] ( Sink & sink ) {
source . drainInto ( sink ) ;
} ) ;
} else
Store : : addMultipleToStore ( source , repair , checkSigs ) ;
}
2022-02-25 15:00:00 +00:00
StorePath RemoteStore : : addTextToStore (
std : : string_view name ,
std : : string_view s ,
const StorePathSet & references ,
RepairFlag repair )
2006-11-30 18:35:50 +00:00
{
2020-09-17 18:19:15 +00:00
StringSource source ( s ) ;
2023-04-17 23:02:45 +00:00
return addCAToStore ( source , name , TextIngestionMethod { } , htSHA256 , references , repair ) - > path ;
2006-11-30 18:35:50 +00:00
}
2020-10-08 15:36:51 +00:00
void RemoteStore : : registerDrvOutput ( const Realisation & info )
{
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : RegisterDrvOutput ;
2021-05-07 09:06:17 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 31 ) {
conn - > to < < info . id . to_string ( ) ;
conn - > to < < std : : string ( info . outPath . to_string ( ) ) ;
} else {
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , info ) ;
2021-05-07 09:06:17 +00:00
}
2020-10-08 15:36:51 +00:00
conn . processStderr ( ) ;
}
2021-10-27 09:36:51 +00:00
void RemoteStore : : queryRealisationUncached ( const DrvOutput & id ,
Callback < std : : shared_ptr < const Realisation > > callback ) noexcept
2020-10-08 15:36:51 +00:00
{
2022-05-30 11:27:13 +00:00
try {
auto conn ( getConnection ( ) ) ;
2021-12-06 10:45:18 +00:00
2022-05-30 11:27:13 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 27 ) {
warn ( " the daemon is too old to support content-addressed derivations, please upgrade it to 2.4 " ) ;
return callback ( nullptr ) ;
}
2021-12-06 10:45:18 +00:00
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryRealisation ;
2022-05-30 11:27:13 +00:00
conn - > to < < id . to_string ( ) ;
conn . processStderr ( ) ;
2021-10-27 09:36:51 +00:00
2022-05-30 11:27:13 +00:00
auto real = [ & ] ( ) - > std : : shared_ptr < const Realisation > {
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 31 ) {
2023-05-26 15:07:25 +00:00
auto outPaths = WorkerProto : : Serialise < std : : set < StorePath > > : : read (
2023-04-17 17:40:46 +00:00
* this , * conn ) ;
2022-05-30 11:27:13 +00:00
if ( outPaths . empty ( ) )
return nullptr ;
return std : : make_shared < const Realisation > ( Realisation { . id = id , . outPath = * outPaths . begin ( ) } ) ;
} else {
2023-05-26 15:07:25 +00:00
auto realisations = WorkerProto : : Serialise < std : : set < Realisation > > : : read (
2023-04-17 17:40:46 +00:00
* this , * conn ) ;
2022-05-30 11:27:13 +00:00
if ( realisations . empty ( ) )
return nullptr ;
return std : : make_shared < const Realisation > ( * realisations . begin ( ) ) ;
}
} ( ) ;
2021-10-27 09:36:51 +00:00
callback ( std : : shared_ptr < const Realisation > ( real ) ) ;
} catch ( . . . ) { return callback . rethrow ( ) ; }
2020-10-08 15:36:51 +00:00
}
2022-03-08 18:50:46 +00:00
void RemoteStore : : copyDrvsFromEvalStore (
const std : : vector < DerivedPath > & paths ,
std : : shared_ptr < Store > evalStore )
2006-11-30 18:35:50 +00:00
{
2021-07-22 20:50:48 +00:00
if ( evalStore & & evalStore . get ( ) ! = this ) {
/* The remote doesn't have a way to access evalStore, so copy
the . drvs . */
RealisedPath : : Set drvPaths2 ;
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-15 22:39:04 +00:00
for ( const auto & i : paths ) {
std : : visit ( overloaded {
[ & ] ( const DerivedPath : : Opaque & bp ) {
// Do nothing, path is hopefully there already
} ,
[ & ] ( const DerivedPath : : Built & bp ) {
drvPaths2 . insert ( bp . drvPath - > getBaseStorePath ( ) ) ;
} ,
} , i . raw ( ) ) ;
}
2021-07-22 20:50:48 +00:00
copyClosure ( * evalStore , * this , drvPaths2 ) ;
}
2022-03-08 18:50:46 +00:00
}
void RemoteStore : : buildPaths ( const std : : vector < DerivedPath > & drvPaths , BuildMode buildMode , std : : shared_ptr < Store > evalStore )
{
copyDrvsFromEvalStore ( drvPaths , evalStore ) ;
2021-07-19 13:43:08 +00:00
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : BuildPaths ;
2019-12-05 18:11:09 +00:00
assert ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 13 ) ;
2024-03-04 03:59:31 +00:00
WorkerProto : : write ( * this , * conn , drvPaths ) ;
2019-12-05 18:11:09 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 15 )
conn - > to < < buildMode ;
else
/* Old daemons did not take a 'buildMode' parameter, so we
need to validate it here on the client side . */
if ( buildMode ! = bmNormal )
throw Error ( " repairing or checking is not supported when building through the Nix daemon " ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
readInt ( conn - > from ) ;
2006-11-30 18:35:50 +00:00
}
Make `KeyedBuildResult`, `BuildResult` like before, and fix bug another way
In https://github.com/NixOS/nix/pull/6311#discussion_r834863823, I
realized since derivation goals' wanted outputs can "grow" due to
overlapping dependencies (See `DerivationGoal::addWantedOutputs`, called
by `Worker::makeDerivationGoalCommon`), the previous bug fix had an
unfortunate side effect of causing more pointless rebuilds.
In paticular, we have this situation:
1. Goal made from `DerivedPath::Built { foo, {a} }`.
2. Goal gives on on substituting, starts building.
3. Goal made from `DerivedPath::Built { foo, {b} }`, in fact is just
modified original goal.
4. Though the goal had gotten as far as building, so all outputs were
going to be produced, `addWantedOutputs` no longer knows that and so
the goal is flagged to be restarted.
This might sound far-fetched with input-addressed drvs, where we usually
basically have all our goals "planned out" before we start doing
anything, but with CA derivation goals and especially RFC 92, where *drv
resolution* means goals are created after some building is completed, it
is more likely to happen.
So the first thing to do was restore the clearing of `wantedOutputs` we
used to do, and then filter the outputs in `buildPathsWithResults` to
only get the ones we care about.
But fix also has its own side effect in that the `DerivedPath` in the
`BuildResult` in `DerivationGoal` cannot be trusted; it is merely the
*first* `DerivedPath` for which this goal was originally created.
To remedy this, I made `BuildResult` be like it was before, and instead
made `KeyedBuildResult` be a subclass wit the path. Only
`buildPathsWithResults` returns `KeyedBuildResult`s, everything else
just becomes like it was before, where the "key" is unambiguous from
context.
I think separating the "primary key" field(s) from the other fields is
good practical in general anyways. (I would like to do the same thing
for `ValidPathInfo`.) Among other things, it allows constructions like
`std::map<Key, ThingWithKey>` where doesn't contain duplicate keys and
just precludes the possibility of those duplicate keys being out of
sync.
We might leverage the above someday to overload `buildPathsWithResults`
to take a *set* of return a *map* per the above.
-----
Unfortunately, we need to avoid C++20 strictness on designated
initializers.
(BTW
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2287r1.html
this offers some new syntax for this use-case. Hopefully this will be
adopted and we can eventually use it.)
No having that yet, maybe it would be better to not make
`KeyedBuildResult` a subclass to just avoid this.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-03-25 01:26:07 +00:00
std : : vector < KeyedBuildResult > RemoteStore : : buildPathsWithResults (
2022-03-08 18:50:46 +00:00
const std : : vector < DerivedPath > & paths ,
BuildMode buildMode ,
std : : shared_ptr < Store > evalStore )
{
copyDrvsFromEvalStore ( paths , evalStore ) ;
std : : optional < ConnectionHandle > conn_ ( getConnection ( ) ) ;
auto & conn = * conn_ ;
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) > = 34 ) {
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : BuildPathsWithResults ;
2024-03-04 03:59:31 +00:00
WorkerProto : : write ( * this , * conn , paths ) ;
2022-03-08 18:50:46 +00:00
conn - > to < < buildMode ;
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
return WorkerProto : : Serialise < std : : vector < KeyedBuildResult > > : : read ( * this , * conn ) ;
2022-03-08 18:50:46 +00:00
} else {
// Avoid deadlock.
conn_ . reset ( ) ;
// Note: this throws an exception if a build/substitution
// fails, but meh.
buildPaths ( paths , buildMode , evalStore ) ;
Make `KeyedBuildResult`, `BuildResult` like before, and fix bug another way
In https://github.com/NixOS/nix/pull/6311#discussion_r834863823, I
realized since derivation goals' wanted outputs can "grow" due to
overlapping dependencies (See `DerivationGoal::addWantedOutputs`, called
by `Worker::makeDerivationGoalCommon`), the previous bug fix had an
unfortunate side effect of causing more pointless rebuilds.
In paticular, we have this situation:
1. Goal made from `DerivedPath::Built { foo, {a} }`.
2. Goal gives on on substituting, starts building.
3. Goal made from `DerivedPath::Built { foo, {b} }`, in fact is just
modified original goal.
4. Though the goal had gotten as far as building, so all outputs were
going to be produced, `addWantedOutputs` no longer knows that and so
the goal is flagged to be restarted.
This might sound far-fetched with input-addressed drvs, where we usually
basically have all our goals "planned out" before we start doing
anything, but with CA derivation goals and especially RFC 92, where *drv
resolution* means goals are created after some building is completed, it
is more likely to happen.
So the first thing to do was restore the clearing of `wantedOutputs` we
used to do, and then filter the outputs in `buildPathsWithResults` to
only get the ones we care about.
But fix also has its own side effect in that the `DerivedPath` in the
`BuildResult` in `DerivationGoal` cannot be trusted; it is merely the
*first* `DerivedPath` for which this goal was originally created.
To remedy this, I made `BuildResult` be like it was before, and instead
made `KeyedBuildResult` be a subclass wit the path. Only
`buildPathsWithResults` returns `KeyedBuildResult`s, everything else
just becomes like it was before, where the "key" is unambiguous from
context.
I think separating the "primary key" field(s) from the other fields is
good practical in general anyways. (I would like to do the same thing
for `ValidPathInfo`.) Among other things, it allows constructions like
`std::map<Key, ThingWithKey>` where doesn't contain duplicate keys and
just precludes the possibility of those duplicate keys being out of
sync.
We might leverage the above someday to overload `buildPathsWithResults`
to take a *set* of return a *map* per the above.
-----
Unfortunately, we need to avoid C++20 strictness on designated
initializers.
(BTW
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2287r1.html
this offers some new syntax for this use-case. Hopefully this will be
adopted and we can eventually use it.)
No having that yet, maybe it would be better to not make
`KeyedBuildResult` a subclass to just avoid this.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-03-25 01:26:07 +00:00
std : : vector < KeyedBuildResult > results ;
2022-03-08 18:50:46 +00:00
for ( auto & path : paths ) {
std : : visit (
overloaded {
[ & ] ( const DerivedPath : : Opaque & bo ) {
Make `KeyedBuildResult`, `BuildResult` like before, and fix bug another way
In https://github.com/NixOS/nix/pull/6311#discussion_r834863823, I
realized since derivation goals' wanted outputs can "grow" due to
overlapping dependencies (See `DerivationGoal::addWantedOutputs`, called
by `Worker::makeDerivationGoalCommon`), the previous bug fix had an
unfortunate side effect of causing more pointless rebuilds.
In paticular, we have this situation:
1. Goal made from `DerivedPath::Built { foo, {a} }`.
2. Goal gives on on substituting, starts building.
3. Goal made from `DerivedPath::Built { foo, {b} }`, in fact is just
modified original goal.
4. Though the goal had gotten as far as building, so all outputs were
going to be produced, `addWantedOutputs` no longer knows that and so
the goal is flagged to be restarted.
This might sound far-fetched with input-addressed drvs, where we usually
basically have all our goals "planned out" before we start doing
anything, but with CA derivation goals and especially RFC 92, where *drv
resolution* means goals are created after some building is completed, it
is more likely to happen.
So the first thing to do was restore the clearing of `wantedOutputs` we
used to do, and then filter the outputs in `buildPathsWithResults` to
only get the ones we care about.
But fix also has its own side effect in that the `DerivedPath` in the
`BuildResult` in `DerivationGoal` cannot be trusted; it is merely the
*first* `DerivedPath` for which this goal was originally created.
To remedy this, I made `BuildResult` be like it was before, and instead
made `KeyedBuildResult` be a subclass wit the path. Only
`buildPathsWithResults` returns `KeyedBuildResult`s, everything else
just becomes like it was before, where the "key" is unambiguous from
context.
I think separating the "primary key" field(s) from the other fields is
good practical in general anyways. (I would like to do the same thing
for `ValidPathInfo`.) Among other things, it allows constructions like
`std::map<Key, ThingWithKey>` where doesn't contain duplicate keys and
just precludes the possibility of those duplicate keys being out of
sync.
We might leverage the above someday to overload `buildPathsWithResults`
to take a *set* of return a *map* per the above.
-----
Unfortunately, we need to avoid C++20 strictness on designated
initializers.
(BTW
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2287r1.html
this offers some new syntax for this use-case. Hopefully this will be
adopted and we can eventually use it.)
No having that yet, maybe it would be better to not make
`KeyedBuildResult` a subclass to just avoid this.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-03-25 01:26:07 +00:00
results . push_back ( KeyedBuildResult {
{
. status = BuildResult : : Substituted ,
} ,
/* .path = */ bo ,
2022-03-09 11:25:35 +00:00
} ) ;
2022-03-08 18:50:46 +00:00
} ,
[ & ] ( const DerivedPath : : Built & bfd ) {
Make `KeyedBuildResult`, `BuildResult` like before, and fix bug another way
In https://github.com/NixOS/nix/pull/6311#discussion_r834863823, I
realized since derivation goals' wanted outputs can "grow" due to
overlapping dependencies (See `DerivationGoal::addWantedOutputs`, called
by `Worker::makeDerivationGoalCommon`), the previous bug fix had an
unfortunate side effect of causing more pointless rebuilds.
In paticular, we have this situation:
1. Goal made from `DerivedPath::Built { foo, {a} }`.
2. Goal gives on on substituting, starts building.
3. Goal made from `DerivedPath::Built { foo, {b} }`, in fact is just
modified original goal.
4. Though the goal had gotten as far as building, so all outputs were
going to be produced, `addWantedOutputs` no longer knows that and so
the goal is flagged to be restarted.
This might sound far-fetched with input-addressed drvs, where we usually
basically have all our goals "planned out" before we start doing
anything, but with CA derivation goals and especially RFC 92, where *drv
resolution* means goals are created after some building is completed, it
is more likely to happen.
So the first thing to do was restore the clearing of `wantedOutputs` we
used to do, and then filter the outputs in `buildPathsWithResults` to
only get the ones we care about.
But fix also has its own side effect in that the `DerivedPath` in the
`BuildResult` in `DerivationGoal` cannot be trusted; it is merely the
*first* `DerivedPath` for which this goal was originally created.
To remedy this, I made `BuildResult` be like it was before, and instead
made `KeyedBuildResult` be a subclass wit the path. Only
`buildPathsWithResults` returns `KeyedBuildResult`s, everything else
just becomes like it was before, where the "key" is unambiguous from
context.
I think separating the "primary key" field(s) from the other fields is
good practical in general anyways. (I would like to do the same thing
for `ValidPathInfo`.) Among other things, it allows constructions like
`std::map<Key, ThingWithKey>` where doesn't contain duplicate keys and
just precludes the possibility of those duplicate keys being out of
sync.
We might leverage the above someday to overload `buildPathsWithResults`
to take a *set* of return a *map* per the above.
-----
Unfortunately, we need to avoid C++20 strictness on designated
initializers.
(BTW
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2287r1.html
this offers some new syntax for this use-case. Hopefully this will be
adopted and we can eventually use it.)
No having that yet, maybe it would be better to not make
`KeyedBuildResult` a subclass to just avoid this.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2022-03-25 01:26:07 +00:00
KeyedBuildResult res {
{
. status = BuildResult : : Built
} ,
/* .path = */ bfd ,
2022-03-09 11:25:35 +00:00
} ;
2022-03-08 18:50:46 +00:00
OutputPathMap outputs ;
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-15 22:39:04 +00:00
auto drvPath = resolveDerivedPath ( * evalStore , * bfd . drvPath ) ;
auto drv = evalStore - > readDerivation ( drvPath ) ;
2022-05-04 05:44:32 +00:00
const auto outputHashes = staticOutputHashes ( * evalStore , drv ) ; // FIXME: expensive
2023-01-11 23:57:18 +00:00
auto built = resolveDerivedPath ( * this , bfd , & * evalStore ) ;
for ( auto & [ output , outputPath ] : built ) {
2022-05-04 05:44:32 +00:00
auto outputHash = get ( outputHashes , output ) ;
if ( ! outputHash )
2022-03-08 18:50:46 +00:00
throw Error (
" the derivation '%s' doesn't have an output named '%s' " ,
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-15 22:39:04 +00:00
printStorePath ( drvPath ) , output ) ;
2022-05-04 05:44:32 +00:00
auto outputId = DrvOutput { * outputHash , output } ;
2023-03-17 14:33:48 +00:00
if ( experimentalFeatureSettings . isEnabled ( Xp : : CaDerivations ) ) {
2022-03-08 18:50:46 +00:00
auto realisation =
queryRealisation ( outputId ) ;
if ( ! realisation )
2023-01-02 16:35:48 +00:00
throw MissingRealisation ( outputId ) ;
2023-04-14 22:18:32 +00:00
res . builtOutputs . emplace ( output , * realisation ) ;
2022-03-08 18:50:46 +00:00
} else {
res . builtOutputs . emplace (
2023-04-14 22:18:32 +00:00
output ,
2022-03-08 18:50:46 +00:00
Realisation {
. id = outputId ,
2023-01-11 23:57:18 +00:00
. outPath = outputPath ,
2022-03-08 18:50:46 +00:00
} ) ;
}
}
results . push_back ( res ) ;
}
} ,
path . raw ( ) ) ;
}
return results ;
}
}
2006-11-30 18:35:50 +00:00
2019-12-05 18:11:09 +00:00
BuildResult RemoteStore : : buildDerivation ( const StorePath & drvPath , 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
BuildMode buildMode )
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : BuildDerivation < < printStorePath ( drvPath ) ;
2019-12-05 18:11:09 +00:00
writeDerivation ( conn - > to , * this , drv ) ;
conn - > to < < buildMode ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2024-03-04 03:59:31 +00:00
return WorkerProto : : Serialise < BuildResult > : : read ( * this , * conn ) ;
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
void RemoteStore : : ensurePath ( const StorePath & path )
2006-11-30 18:35:50 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : EnsurePath < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
readInt ( conn - > from ) ;
2006-11-30 18:35:50 +00:00
}
2019-12-05 18:11:09 +00:00
void RemoteStore : : addTempRoot ( const StorePath & path )
2006-12-02 16:41:36 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : AddTempRoot < < printStorePath ( path ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
readInt ( conn - > from ) ;
2006-12-02 16:41:36 +00:00
}
2019-03-14 12:50:07 +00:00
Roots RemoteStore : : findRoots ( bool censor )
2006-12-05 01:31:45 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : FindRoots ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2017-03-01 12:52:54 +00:00
size_t count = readNum < size_t > ( conn - > from ) ;
2006-12-05 01:31:45 +00:00
Roots result ;
while ( count - - ) {
2016-02-23 14:00:59 +00:00
Path link = readString ( conn - > from ) ;
2019-12-05 18:11:09 +00:00
auto target = parseStorePath ( readString ( conn - > from ) ) ;
result [ std : : move ( target ) ] . emplace ( link ) ;
2006-12-05 01:31:45 +00:00
}
return result ;
}
2008-06-18 09:34:17 +00:00
void RemoteStore : : collectGarbage ( const GCOptions & options , GCResults & results )
2006-12-05 02:18:46 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2012-07-30 21:13:25 +00:00
2016-07-27 13:03:20 +00:00
conn - > to
2023-05-26 15:07:25 +00:00
< < WorkerProto : : Op : : CollectGarbage < < options . action ;
2023-04-17 17:40:46 +00:00
WorkerProto : : write ( * this , * conn , options . pathsToDelete ) ;
2019-12-05 18:11:09 +00:00
conn - > to < < options . ignoreLiveness
2016-07-27 13:03:20 +00:00
< < options . maxFreed
2009-11-20 17:12:38 +00:00
/* removed options */
2016-07-27 13:03:20 +00:00
< < 0 < < 0 < < 0 ;
2012-07-30 21:13:25 +00:00
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2012-07-30 21:13:25 +00:00
2016-02-23 14:00:59 +00:00
results . paths = readStrings < PathSet > ( conn - > from ) ;
results . bytesFreed = readLongLong ( conn - > from ) ;
readLongLong ( conn - > from ) ; // obsolete
2016-04-19 16:50:15 +00:00
{
auto state_ ( Store : : state . lock ( ) ) ;
state_ - > pathInfoCache . clear ( ) ;
}
2006-12-05 02:18:46 +00:00
}
2014-09-01 20:21:42 +00:00
void RemoteStore : : optimiseStore ( )
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : OptimiseStore ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-02-23 14:00:59 +00:00
readInt ( conn - > from ) ;
2014-09-01 20:21:42 +00:00
}
2010-05-04 10:45:10 +00:00
2016-04-08 16:16:53 +00:00
2017-06-28 16:11:01 +00:00
bool RemoteStore : : verifyStore ( bool checkContents , RepairFlag repair )
2015-06-01 21:20:11 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : VerifyStore < < checkContents < < repair ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2017-03-01 12:52:54 +00:00
return readInt ( conn - > from ) ;
2015-06-01 21:20:11 +00:00
}
2016-02-23 15:40:16 +00:00
2019-12-05 18:11:09 +00:00
void RemoteStore : : addSignatures ( const StorePath & storePath , const StringSet & sigs )
2016-04-05 13:30:22 +00:00
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : AddSignatures < < printStorePath ( storePath ) < < sigs ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2016-04-05 13:30:22 +00:00
readInt ( conn - > from ) ;
}
2021-04-05 13:48:18 +00:00
void RemoteStore : : queryMissing ( const std : : vector < DerivedPath > & targets ,
2019-12-05 18:11:09 +00:00
StorePathSet & willBuild , StorePathSet & willSubstitute , StorePathSet & unknown ,
2020-07-30 11:10:49 +00:00
uint64_t & downloadSize , uint64_t & narSize )
2017-04-06 16:40:19 +00:00
{
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2017-04-06 16:40:19 +00:00
if ( GET_PROTOCOL_MINOR ( conn - > daemonVersion ) < 19 )
// Don't hold the connection handle in the fallback case
// to prevent a deadlock.
goto fallback ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : QueryMissing ;
2024-03-04 03:59:31 +00:00
WorkerProto : : write ( * this , * conn , targets ) ;
2018-10-16 21:36:15 +00:00
conn . processStderr ( ) ;
2023-04-17 17:40:46 +00:00
willBuild = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
willSubstitute = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
unknown = WorkerProto : : Serialise < StorePathSet > : : read ( * this , * conn ) ;
2017-04-06 16:40:19 +00:00
conn - > from > > downloadSize > > narSize ;
return ;
}
fallback :
return Store : : queryMissing ( targets , willBuild , willSubstitute ,
unknown , downloadSize , narSize ) ;
}
2022-01-17 18:45:21 +00:00
void RemoteStore : : addBuildLog ( const StorePath & drvPath , std : : string_view log )
{
auto conn ( getConnection ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : AddBuildLog < < drvPath . to_string ( ) ;
2022-01-17 18:45:21 +00:00
StringSource source ( log ) ;
conn . withFramedSink ( [ & ] ( Sink & sink ) {
source . drainInto ( sink ) ;
} ) ;
readInt ( conn - > from ) ;
}
2022-01-25 20:14:27 +00:00
std : : optional < std : : string > RemoteStore : : getVersion ( )
{
auto conn ( getConnection ( ) ) ;
return conn - > daemonNixVersion ;
}
2017-05-02 12:18:46 +00:00
void RemoteStore : : connect ( )
{
2018-10-16 21:36:15 +00:00
auto conn ( getConnection ( ) ) ;
2017-05-02 12:18:46 +00:00
}
2018-08-30 21:28:47 +00:00
unsigned int RemoteStore : : getProtocol ( )
{
auto conn ( connections - > get ( ) ) ;
return conn - > daemonVersion ;
}
2022-12-26 20:21:08 +00:00
std : : optional < TrustedFlag > RemoteStore : : isTrustedClient ( )
{
auto conn ( getConnection ( ) ) ;
return conn - > remoteTrustsUs ;
}
2018-08-30 21:28:47 +00:00
2017-09-14 16:10:38 +00:00
void RemoteStore : : flushBadConnections ( )
{
connections - > flushBad ( ) ;
}
2016-02-23 15:40:16 +00:00
RemoteStore : : Connection : : ~ Connection ( )
{
try {
to . flush ( ) ;
} catch ( . . . ) {
ignoreException ( ) ;
}
}
2020-09-07 09:26:09 +00:00
void RemoteStore : : narFromPath ( const StorePath & path , Sink & sink )
{
auto conn ( connections - > get ( ) ) ;
2023-05-26 15:07:25 +00:00
conn - > to < < WorkerProto : : Op : : NarFromPath < < printStorePath ( path ) ;
2020-09-07 09:26:09 +00:00
conn - > processStderr ( ) ;
copyNAR ( conn - > from , sink ) ;
}
ref < FSAccessor > RemoteStore : : getFSAccessor ( )
{
return make_ref < RemoteFSAccessor > ( ref < Store > ( shared_from_this ( ) ) ) ;
}
2016-02-23 15:40:16 +00:00
2017-08-28 16:49:42 +00:00
static Logger : : Fields readFields ( Source & from )
{
Logger : : Fields fields ;
size_t size = readInt ( from ) ;
for ( size_t n = 0 ; n < size ; n + + ) {
auto type = ( decltype ( Logger : : Field : : type ) ) readInt ( from ) ;
if ( type = = Logger : : Field : : tInt )
fields . push_back ( readNum < uint64_t > ( from ) ) ;
else if ( type = = Logger : : Field : : tString )
fields . push_back ( readString ( from ) ) ;
else
throw Error ( " got unsupported field type %x from Nix daemon " , ( int ) type ) ;
}
return fields ;
}
2020-08-27 12:48:08 +00:00
std : : exception_ptr RemoteStore : : Connection : : processStderr ( Sink * sink , Source * source , bool flush )
2006-12-03 02:08:13 +00:00
{
2020-08-27 12:48:08 +00:00
if ( flush )
to . flush ( ) ;
2017-08-28 16:49:42 +00:00
while ( true ) {
auto msg = readNum < uint64_t > ( from ) ;
2007-02-21 17:34:02 +00:00
if ( msg = = STDERR_WRITE ) {
2022-02-25 15:00:00 +00:00
auto s = readString ( from ) ;
2007-02-21 16:34:00 +00:00
if ( ! sink ) throw Error ( " no sink " ) ;
2016-05-04 13:46:25 +00:00
( * sink ) ( s ) ;
2007-02-21 16:34:00 +00:00
}
2017-08-28 16:49:42 +00:00
2007-02-21 17:34:02 +00:00
else if ( msg = = STDERR_READ ) {
if ( ! source ) throw Error ( " no source " ) ;
2017-03-01 12:52:54 +00:00
size_t len = readNum < size_t > ( from ) ;
2020-12-02 13:10:56 +00:00
auto buf = std : : make_unique < char [ ] > ( len ) ;
2020-12-02 13:00:43 +00:00
writeString ( { ( const char * ) buf . get ( ) , source - > read ( buf . get ( ) , len ) } , to ) ;
2011-12-14 23:30:06 +00:00
to . flush ( ) ;
2007-02-21 17:34:02 +00:00
}
2017-08-28 16:49:42 +00:00
else if ( msg = = STDERR_ERROR ) {
2020-10-07 15:13:54 +00:00
if ( GET_PROTOCOL_MINOR ( daemonVersion ) > = 26 ) {
return std : : make_exception_ptr ( readError ( from ) ) ;
} else {
2022-02-25 15:00:00 +00:00
auto error = readString ( from ) ;
2020-10-07 15:13:54 +00:00
unsigned int status = readInt ( from ) ;
return std : : make_exception_ptr ( Error ( status , error ) ) ;
}
2017-08-28 16:49:42 +00:00
}
else if ( msg = = STDERR_NEXT )
2016-09-21 14:11:01 +00:00
printError ( chomp ( readString ( from ) ) ) ;
2017-08-28 16:49:42 +00:00
else if ( msg = = STDERR_START_ACTIVITY ) {
auto act = readNum < ActivityId > ( from ) ;
2017-08-28 17:13:24 +00:00
auto lvl = ( Verbosity ) readInt ( from ) ;
2017-08-28 16:49:42 +00:00
auto type = ( ActivityType ) readInt ( from ) ;
auto s = readString ( from ) ;
auto fields = readFields ( from ) ;
auto parent = readNum < ActivityId > ( from ) ;
2017-08-28 17:13:24 +00:00
logger - > startActivity ( act , lvl , type , s , fields , parent ) ;
2017-08-28 16:49:42 +00:00
}
else if ( msg = = STDERR_STOP_ACTIVITY ) {
auto act = readNum < ActivityId > ( from ) ;
logger - > stopActivity ( act ) ;
}
else if ( msg = = STDERR_RESULT ) {
auto act = readNum < ActivityId > ( from ) ;
auto type = ( ResultType ) readInt ( from ) ;
auto fields = readFields ( from ) ;
logger - > result ( act , type , fields ) ;
}
else if ( msg = = STDERR_LAST )
break ;
else
throw Error ( " got unknown message type %x from Nix daemon " , msg ) ;
2006-12-03 02:08:13 +00:00
}
2018-10-16 21:36:15 +00:00
return nullptr ;
2006-12-03 02:08:13 +00:00
}
2023-04-17 16:14:15 +00:00
void RemoteStore : : ConnectionHandle : : withFramedSink ( std : : function < void ( Sink & sink ) > fun )
2018-01-15 02:20:22 +00:00
{
2020-09-17 15:36:16 +00:00
( * this ) - > to . flush ( ) ;
std : : exception_ptr ex ;
2021-07-26 11:31:09 +00:00
/* Handle log messages / exceptions from the remote on a separate
thread . */
2020-09-17 15:36:16 +00:00
std : : thread stderrThread ( [ & ] ( )
{
try {
2024-03-04 06:40:17 +00:00
ReceiveInterrupts receiveInterrupts ;
2020-09-17 15:36:16 +00:00
processStderr ( nullptr , nullptr , false ) ;
} catch ( . . . ) {
ex = std : : current_exception ( ) ;
}
} ) ;
Finally joinStderrThread ( [ & ] ( )
{
if ( stderrThread . joinable ( ) ) {
stderrThread . join ( ) ;
if ( ex ) {
try {
std : : rethrow_exception ( ex ) ;
} catch ( . . . ) {
ignoreException ( ) ;
}
}
}
} ) ;
{
2020-09-17 20:01:35 +00:00
FramedSink sink ( ( * this ) - > to , ex ) ;
2020-09-17 15:36:16 +00:00
fun ( sink ) ;
sink . flush ( ) ;
}
stderrThread . join ( ) ;
if ( ex )
std : : rethrow_exception ( ex ) ;
}
2006-11-30 18:35:50 +00:00
}