4646#include " store-config-private.hh"
4747#include " build/derivation-check.hh"
4848
49+ #if NIX_WITH_CURL_S3
50+ # include " nix/store/aws-creds.hh"
51+ # include " nix/store/s3-url.hh"
52+ # include " nix/util/url.hh"
53+ #endif
54+
4955namespace nix {
5056
5157struct NotDeterministic : BuildError
@@ -290,6 +296,15 @@ class DerivationBuilderImpl : public DerivationBuilder, public DerivationBuilder
290296 */
291297 virtual void startChild ();
292298
299+ #if NIX_WITH_CURL_S3
300+ /* *
301+ * Pre-resolve AWS credentials for S3 URLs in builtin:fetchurl.
302+ * This should be called before forking to ensure credentials are available in child.
303+ * Returns the credentials if successfully resolved, or std::nullopt otherwise.
304+ */
305+ std::optional<AwsCredentials> preResolveAwsCredentials ();
306+ #endif
307+
293308private:
294309
295310 /* *
@@ -339,10 +354,20 @@ class DerivationBuilderImpl : public DerivationBuilder, public DerivationBuilder
339354 */
340355 void writeBuilderFile (const std::string & name, std::string_view contents);
341356
357+ /* *
358+ * Arguments passed to runChild().
359+ */
360+ struct RunChildArgs
361+ {
362+ #if NIX_WITH_CURL_S3
363+ std::optional<AwsCredentials> awsCredentials;
364+ #endif
365+ };
366+
342367 /* *
343368 * Run the builder's process.
344369 */
345- void runChild ();
370+ void runChild (RunChildArgs args );
346371
347372 /* *
348373 * Move the current process into the chroot, if any. Called early
@@ -920,11 +945,43 @@ void DerivationBuilderImpl::openSlave()
920945 throw SysError (" cannot pipe standard error into log file" );
921946}
922947
948+ #if NIX_WITH_CURL_S3
949+ std::optional<AwsCredentials> DerivationBuilderImpl::preResolveAwsCredentials ()
950+ {
951+ if (drv.isBuiltin () && drv.builder == " builtin:fetchurl" ) {
952+ auto url = drv.env .find (" url" );
953+ if (url != drv.env .end ()) {
954+ try {
955+ auto parsedUrl = parseURL (url->second );
956+ if (parsedUrl.scheme == " s3" ) {
957+ debug (" Pre-resolving AWS credentials for S3 URL in builtin:fetchurl" );
958+ auto s3Url = ParsedS3URL::parse (parsedUrl);
959+
960+ // Use the preResolveAwsCredentials from aws-creds
961+ auto credentials = nix::preResolveAwsCredentials (s3Url);
962+ debug (" Successfully pre-resolved AWS credentials in parent process" );
963+ return credentials;
964+ }
965+ } catch (const std::exception & e) {
966+ debug (" Error pre-resolving S3 credentials: %s" , e.what ());
967+ }
968+ }
969+ }
970+ return std::nullopt ;
971+ }
972+ #endif
973+
923974void DerivationBuilderImpl::startChild ()
924975{
925- pid = startProcess ([&]() {
976+ RunChildArgs args{
977+ #if NIX_WITH_CURL_S3
978+ .awsCredentials = preResolveAwsCredentials (),
979+ #endif
980+ };
981+
982+ pid = startProcess ([this , args = std::move (args)]() {
926983 openSlave ();
927- runChild ();
984+ runChild (std::move (args) );
928985 });
929986}
930987
@@ -1181,7 +1238,7 @@ void DerivationBuilderImpl::writeBuilderFile(const std::string & name, std::stri
11811238 chownToBuilder (fd.get (), path);
11821239}
11831240
1184- void DerivationBuilderImpl::runChild ()
1241+ void DerivationBuilderImpl::runChild (RunChildArgs args )
11851242{
11861243 /* Warning: in the child we should absolutely not make any SQLite
11871244 calls! */
@@ -1198,6 +1255,9 @@ void DerivationBuilderImpl::runChild()
11981255 BuiltinBuilderContext ctx{
11991256 .drv = drv,
12001257 .tmpDirInSandbox = tmpDirInSandbox (),
1258+ #if NIX_WITH_CURL_S3
1259+ .awsCredentials = args.awsCredentials ,
1260+ #endif
12011261 };
12021262
12031263 if (drv.isBuiltin () && drv.builder == " builtin:fetchurl" ) {
0 commit comments