Skip to content

Commit bb1f22a

Browse files
committed
refactor(libstore): minimize NIX_WITH_S3_SUPPORT scope to auth only
Move S3 URL parsing, store configuration, and public bucket support outside of NIX_WITH_S3_SUPPORT guards. Only AWS credential resolution remains gated, allowing builds with withAWS = false to: - Parse s3:// URLs - Register S3 store types - Access public S3 buckets (via HTTPS conversion) - Use S3-compatible services without authentication The setupForS3() function now always performs URL conversion, with authentication code conditionally compiled based on NIX_WITH_S3_SUPPORT. The aws-creds.cc file (only code using AWS CRT SDK) is now conditionally compiled by meson.
1 parent 1f71030 commit bb1f22a

File tree

9 files changed

+40
-67
lines changed

9 files changed

+40
-67
lines changed

src/libstore-tests/s3-binary-cache-store.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "nix/store/s3-binary-cache-store.hh"
2+
#include "nix/store/http-binary-cache-store.hh"
3+
#include "nix/store/filetransfer.hh"
4+
#include "nix/store/s3-url.hh"
25

3-
#if NIX_WITH_S3_SUPPORT
4-
5-
# include "nix/store/http-binary-cache-store.hh"
6-
# include "nix/store/filetransfer.hh"
7-
# include "nix/store/s3-url.hh"
8-
9-
# include <gtest/gtest.h>
6+
#include <gtest/gtest.h>
107

118
namespace nix {
129

@@ -126,5 +123,3 @@ TEST(S3BinaryCacheStore, parameterFiltering)
126123
}
127124

128125
} // namespace nix
129-
130-
#endif

src/libstore-tests/s3-url.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#include "nix/store/s3-url.hh"
22
#include "nix/util/tests/gmock-matchers.hh"
33

4-
#if NIX_WITH_S3_SUPPORT
5-
6-
# include <gtest/gtest.h>
7-
# include <gmock/gmock.h>
4+
#include <gtest/gtest.h>
5+
#include <gmock/gmock.h>
86

97
namespace nix {
108

@@ -228,5 +226,3 @@ INSTANTIATE_TEST_SUITE_P(
228226
[](const ::testing::TestParamInfo<S3ToHttpsConversionTestCase> & info) { return info.param.description; });
229227

230228
} // namespace nix
231-
232-
#endif

src/libstore/filetransfer.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include "nix/util/signals.hh"
99

1010
#include "store-config-private.hh"
11+
#include "nix/store/s3-url.hh"
1112
#include <optional>
1213
#if NIX_WITH_S3_SUPPORT
1314
# include "nix/store/aws-creds.hh"
14-
# include "nix/store/s3-url.hh"
1515
#endif
1616

1717
#ifdef __linux__
@@ -820,10 +820,7 @@ struct curlFileTransfer : public FileTransfer
820820
void enqueueItem(std::shared_ptr<TransferItem> item)
821821
{
822822
if (item->request.data && item->request.uri.scheme() != "http" && item->request.uri.scheme() != "https"
823-
#if NIX_WITH_S3_SUPPORT
824-
&& item->request.uri.scheme() != "s3"
825-
#endif
826-
)
823+
&& item->request.uri.scheme() != "s3")
827824
throw nix::Error("uploading to '%s' is not supported", item->request.uri.to_string());
828825

829826
{
@@ -839,16 +836,11 @@ struct curlFileTransfer : public FileTransfer
839836

840837
void enqueueFileTransfer(const FileTransferRequest & request, Callback<FileTransferResult> callback) override
841838
{
842-
/* Ugly hack to support s3:// URIs. */
839+
/* Handle s3:// URIs by converting to HTTPS and optionally adding auth */
843840
if (request.uri.scheme() == "s3") {
844-
#if NIX_WITH_S3_SUPPORT
845-
// New curl-based S3 implementation
846841
auto modifiedRequest = request;
847842
modifiedRequest.setupForS3();
848843
enqueueItem(std::make_shared<TransferItem>(*this, std::move(modifiedRequest), std::move(callback)));
849-
#else
850-
throw nix::Error("cannot download '%s' because Nix is not built with S3 support", request.uri.to_string());
851-
#endif
852844
return;
853845
}
854846

@@ -876,14 +868,16 @@ ref<FileTransfer> makeFileTransfer()
876868
return makeCurlFileTransfer();
877869
}
878870

879-
#if NIX_WITH_S3_SUPPORT
880871
void FileTransferRequest::setupForS3()
881872
{
882873
auto parsedS3 = ParsedS3URL::parse(uri.parsed());
883-
// Update the request URI to use HTTPS
874+
// Update the request URI to use HTTPS (works without AWS SDK)
884875
uri = parsedS3.toHttpsUrl();
885-
// This gets used later in a curl setopt
876+
877+
#if NIX_WITH_S3_SUPPORT
878+
// Auth-specific code only compiled when AWS support is available
886879
awsSigV4Provider = "aws:amz:" + parsedS3.region.value_or("us-east-1") + ":s3";
880+
887881
// check if the request already has pre-resolved credentials
888882
std::optional<std::string> sessionToken;
889883
if (usernameAuth) {
@@ -908,8 +902,11 @@ void FileTransferRequest::setupForS3()
908902
}
909903
if (sessionToken)
910904
headers.emplace_back("x-amz-security-token", *sessionToken);
911-
}
905+
#else
906+
// When built without AWS support, just try as public bucket
907+
debug("S3 request without authentication (built without AWS support)");
912908
#endif
909+
}
913910

914911
std::future<FileTransferResult> FileTransfer::enqueueFileTransfer(const FileTransferRequest & request)
915912
{

src/libstore/include/nix/store/filetransfer.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#if NIX_WITH_S3_SUPPORT
1616
# include "nix/store/aws-creds.hh"
1717
#endif
18+
#include "nix/store/s3-url.hh"
1819

1920
namespace nix {
2021

@@ -132,10 +133,10 @@ struct FileTransferRequest
132133
return data ? "upload" : "download";
133134
}
134135

135-
#if NIX_WITH_S3_SUPPORT
136136
private:
137137
friend struct curlFileTransfer;
138138
void setupForS3();
139+
#if NIX_WITH_S3_SUPPORT
139140
std::optional<std::string> awsSigV4Provider;
140141
#endif
141142
};

src/libstore/include/nix/store/s3-binary-cache-store.hh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
///@file
33

44
#include "nix/store/config.hh"
5-
6-
#if NIX_WITH_S3_SUPPORT
7-
8-
# include "nix/store/http-binary-cache-store.hh"
5+
#include "nix/store/http-binary-cache-store.hh"
96

107
namespace nix {
118

@@ -77,5 +74,3 @@ public:
7774
};
7875

7976
} // namespace nix
80-
81-
#endif

src/libstore/include/nix/store/s3-url.hh

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#pragma once
22
///@file
33
#include "nix/store/config.hh"
4+
#include "nix/util/url.hh"
5+
#include "nix/util/util.hh"
46

5-
#if NIX_WITH_S3_SUPPORT
6-
7-
# include "nix/util/url.hh"
8-
# include "nix/util/util.hh"
9-
10-
# include <optional>
11-
# include <string>
12-
# include <variant>
13-
# include <vector>
7+
#include <optional>
8+
#include <string>
9+
#include <variant>
10+
#include <vector>
1411

1512
namespace nix {
1613

@@ -56,5 +53,3 @@ struct ParsedS3URL
5653
};
5754

5855
} // namespace nix
59-
60-
#endif

src/libstore/meson.build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ config_priv_h = configure_file(
265265
subdir('nix-meson-build-support/common')
266266

267267
sources = files(
268-
'aws-creds.cc',
269268
'binary-cache-store.cc',
270269
'build-result.cc',
271270
'build/derivation-builder.cc',
@@ -344,6 +343,11 @@ sources = files(
344343
'worker-protocol.cc',
345344
)
346345

346+
# AWS credentials code requires AWS CRT, so only compile when enabled
347+
if curl_s3_store_opt.enabled()
348+
sources += files('aws-creds.cc')
349+
endif
350+
347351
subdir('include/nix/store')
348352

349353
if host_machine.system() == 'linux'

src/libstore/s3-binary-cache-store.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "nix/store/s3-binary-cache-store.hh"
22

3-
#if NIX_WITH_S3_SUPPORT
3+
#include <cassert>
44

5-
# include <cassert>
6-
7-
# include "nix/store/s3-binary-cache-store.hh"
8-
# include "nix/store/http-binary-cache-store.hh"
9-
# include "nix/store/store-registration.hh"
5+
#include "nix/store/http-binary-cache-store.hh"
6+
#include "nix/store/store-registration.hh"
107

118
namespace nix {
129

@@ -45,5 +42,3 @@ std::string S3BinaryCacheStoreConfig::doc()
4542
static RegisterStoreImplementation<S3BinaryCacheStoreConfig> registerS3BinaryCacheStore;
4643

4744
} // namespace nix
48-
49-
#endif

src/libstore/s3-url.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#include "nix/store/s3-url.hh"
2+
#include "nix/util/error.hh"
3+
#include "nix/util/split.hh"
4+
#include "nix/util/strings-inline.hh"
25

3-
#if NIX_WITH_S3_SUPPORT
4-
5-
# include "nix/util/error.hh"
6-
# include "nix/util/split.hh"
7-
# include "nix/util/strings-inline.hh"
8-
9-
# include <ranges>
10-
# include <string_view>
6+
#include <ranges>
7+
#include <string_view>
118

129
using namespace std::string_view_literals;
1310

@@ -117,5 +114,3 @@ ParsedURL ParsedS3URL::toHttpsUrl() const
117114
}
118115

119116
} // namespace nix
120-
121-
#endif

0 commit comments

Comments
 (0)