From fce89f4c954869290d2cf179b1540776ea329ebc Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Mon, 6 Feb 2023 19:39:10 -0700 Subject: [PATCH] attic/nix_store: Only initialize Nix once --- attic/src/nix_store/bindings/nix.cpp | 11 ++++++++++- attic/src/nix_store/bindings/nix.hpp | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/attic/src/nix_store/bindings/nix.cpp b/attic/src/nix_store/bindings/nix.cpp index 40597c1..55367c0 100644 --- a/attic/src/nix_store/bindings/nix.cpp +++ b/attic/src/nix_store/bindings/nix.cpp @@ -10,6 +10,9 @@ #include "attic/src/nix_store/bindings/nix.hpp" +static std::mutex g_init_nix_mutex; +static bool g_init_nix_done = false; + static nix::StorePath store_path_from_rust(RBasePathSlice base_name) { std::string_view sv((const char *)base_name.data(), base_name.size()); return nix::StorePath(sv); @@ -76,7 +79,13 @@ RString CPathInfo::ca() { CNixStore::CNixStore() { std::map params; - nix::initNix(); + std::lock_guard lock(g_init_nix_mutex); + + if (!g_init_nix_done) { + nix::initNix(); + g_init_nix_done = true; + } + this->store = nix::openStore("auto", params); } diff --git a/attic/src/nix_store/bindings/nix.hpp b/attic/src/nix_store/bindings/nix.hpp index 8f50b1f..70d79cd 100644 --- a/attic/src/nix_store/bindings/nix.hpp +++ b/attic/src/nix_store/bindings/nix.hpp @@ -11,6 +11,7 @@ #pragma once #include #include +#include #include #include #include