aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2022-12-19 14:06:07 +0100
committerRobert Hensing <robert@roberthensing.nl>2022-12-24 14:39:30 +0100
commitaba6eb348e0ed8177da1e7f1df46ba00d20eab96 (patch)
tree50bb1337cafac6b3a1389d43fd033d97a13c09f9 /src
parent26c7602c390f8c511f326785b570918b2f468892 (diff)
libstore: Make sure that initNix has been called
Prevent bugs like https://github.com/cachix/cachix/pull/477
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/tests/libexprtests.hh1
-rw-r--r--src/libmain/shared.cc1
-rw-r--r--src/libstore/globals.cc14
-rw-r--r--src/libstore/globals.hh8
-rw-r--r--src/libstore/store-api.cc1
5 files changed, 25 insertions, 0 deletions
diff --git a/src/libexpr/tests/libexprtests.hh b/src/libexpr/tests/libexprtests.hh
index 4f6915882..4a03d4316 100644
--- a/src/libexpr/tests/libexprtests.hh
+++ b/src/libexpr/tests/libexprtests.hh
@@ -12,6 +12,7 @@ namespace nix {
class LibExprTest : public ::testing::Test {
public:
static void SetUpTestSuite() {
+ initLibStore();
initGC();
}
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index a58428762..99e3d6f56 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -235,6 +235,7 @@ void initNix()
#endif
preloadNSS();
+ initLibStore();
}
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc
index b7f55cae7..130c5b670 100644
--- a/src/libstore/globals.cc
+++ b/src/libstore/globals.cc
@@ -291,4 +291,18 @@ void initPlugins()
settings.pluginFiles.pluginsLoaded = true;
}
+static bool initLibStoreDone = false;
+
+void assertLibStoreInitialized() {
+ if (!initLibStoreDone) {
+ printError("The program must call nix::initNix() before calling any libstore library functions.");
+ abort();
+ };
+}
+
+void initLibStore() {
+ initLibStoreDone = true;
+}
+
+
}
diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh
index 274a15dd7..22458efcf 100644
--- a/src/libstore/globals.hh
+++ b/src/libstore/globals.hh
@@ -984,4 +984,12 @@ std::vector<Path> getUserConfigFiles();
extern const std::string nixVersion;
+/* NB: This is not sufficient. You need to call initNix() */
+void initLibStore();
+
+/* It's important to initialize before doing _anything_, which is why we
+ call upon the programmer to handle this correctly. However, we only add
+ this in a key locations, so as not to litter the code. */
+void assertLibStoreInitialized();
+
}
diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc
index 80b60ca1b..426230ca5 100644
--- a/src/libstore/store-api.cc
+++ b/src/libstore/store-api.cc
@@ -458,6 +458,7 @@ Store::Store(const Params & params)
: StoreConfig(params)
, state({(size_t) pathInfoCacheSize})
{
+ assertLibStoreInitialized();
}