From f18c39f46d343b4cd13d5b8f57000a18f34572a0 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sun, 9 Feb 2025 17:30:04 +0100 Subject: [PATCH] libstore: asyncify writeDerivation Change-Id: Ia4e25dbc84e522b27c5257564febbd1ccf4df8a3 --- lix/libexpr/primops.cc | 2 +- lix/libstore/build/derivation-goal.cc | 2 +- lix/libstore/daemon.cc | 2 +- lix/libstore/derivations.cc | 8 +++++--- lix/libstore/derivations.hh | 4 +++- lix/libstore/store-api.cc | 2 +- lix/nix/derivation-add.cc | 4 ++-- lix/nix/develop.cc | 2 +- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 961687185..1dc6fa2a3 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -1109,7 +1109,7 @@ drvName, Bindings * attrs, Value & v) } /* Write the resulting term into the Nix store directory. */ - auto drvPath = writeDerivation(*state.ctx.store, drv, state.ctx.repair); + auto drvPath = state.aio.blockOn(writeDerivation(*state.ctx.store, drv, state.ctx.repair)); auto drvPathS = state.ctx.store->printStorePath(drvPath); printMsg(lvlChatty, "instantiated '%1%' -> '%2%'", drvName, drvPathS); diff --git a/lix/libstore/build/derivation-goal.cc b/lix/libstore/build/derivation-goal.cc index ecd9bfadc..be7bb8f6f 100644 --- a/lix/libstore/build/derivation-goal.cc +++ b/lix/libstore/build/derivation-goal.cc @@ -604,7 +604,7 @@ try { assert(attempt); Derivation drvResolved { std::move(*attempt) }; - auto pathResolved = writeDerivation(worker.store, drvResolved); + auto pathResolved = TRY_AWAIT(writeDerivation(worker.store, drvResolved)); auto msg = fmt("resolved derivation: '%s' -> '%s'", worker.store.printStorePath(drvPath), diff --git a/lix/libstore/daemon.cc b/lix/libstore/daemon.cc index 491d38550..af3e9624e 100644 --- a/lix/libstore/daemon.cc +++ b/lix/libstore/daemon.cc @@ -645,7 +645,7 @@ static void performOp(AsyncIoRoot & aio, TunnelLogger * logger, ref store Derivation drv2; static_cast(drv2) = drv; - drvPath = writeDerivation(*store, Derivation { drv2 }); + drvPath = aio.blockOn(writeDerivation(*store, Derivation { drv2 })); } auto res = aio.blockOn(store->buildDerivation(drvPath, drv, buildMode)); diff --git a/lix/libstore/derivations.cc b/lix/libstore/derivations.cc index 5b1e189f0..5e814943d 100644 --- a/lix/libstore/derivations.cc +++ b/lix/libstore/derivations.cc @@ -134,9 +134,9 @@ bool BasicDerivation::isBuiltin() const } -StorePath writeDerivation(Store & store, +kj::Promise> writeDerivation(Store & store, const Derivation & drv, RepairFlag repair, bool readOnly) -{ +try { auto references = drv.inputSrcs; for (auto & i : drv.inputDrvs.map) references.insert(i.first); @@ -145,9 +145,11 @@ StorePath writeDerivation(Store & store, held during a garbage collection). */ auto suffix = std::string(drv.name) + drvExtension; auto contents = drv.unparse(store, false); - return readOnly || settings.readOnlyMode + co_return readOnly || settings.readOnlyMode ? store.computeStorePathForText(suffix, contents, references) : store.addTextToStore(suffix, contents, references, repair); +} catch (...) { + co_return result::current_exception(); } diff --git a/lix/libstore/derivations.hh b/lix/libstore/derivations.hh index 909c701b2..73db36676 100644 --- a/lix/libstore/derivations.hh +++ b/lix/libstore/derivations.hh @@ -2,6 +2,7 @@ ///@file #include "lix/libstore/path.hh" +#include "lix/libutil/result.hh" #include "lix/libutil/types.hh" #include "lix/libutil/hash.hh" #include "lix/libstore/content-address.hh" @@ -11,6 +12,7 @@ #include "lix/libutil/comparator.hh" #include "lix/libutil/variant-wrapper.hh" +#include #include #include @@ -382,7 +384,7 @@ class Store; /** * Write a derivation to the Nix store, and return its path. */ -StorePath writeDerivation(Store & store, +kj::Promise> writeDerivation(Store & store, const Derivation & drv, RepairFlag repair = NoRepair, bool readOnly = false); diff --git a/lix/libstore/store-api.cc b/lix/libstore/store-api.cc index 4fda8d4ac..99028c640 100644 --- a/lix/libstore/store-api.cc +++ b/lix/libstore/store-api.cc @@ -1433,7 +1433,7 @@ try { // resolved derivation, so we need to get it first auto resolvedDrv = drv.tryResolve(*this); if (resolvedDrv) - co_return writeDerivation(*this, *resolvedDrv, NoRepair, true); + co_return TRY_AWAIT(writeDerivation(*this, *resolvedDrv, NoRepair, true)); } co_return path; diff --git a/lix/nix/derivation-add.cc b/lix/nix/derivation-add.cc index 2019f18c5..965ce1f08 100644 --- a/lix/nix/derivation-add.cc +++ b/lix/nix/derivation-add.cc @@ -32,11 +32,11 @@ struct CmdAddDerivation : MixDryRun, StoreCommand auto drv = Derivation::fromJSON(*store, json); - auto drvPath = writeDerivation(*store, drv, NoRepair, /* read only */ dryRun); + auto drvPath = aio().blockOn(writeDerivation(*store, drv, NoRepair, /* read only */ dryRun)); drv.checkInvariants(*store, drvPath); - writeDerivation(*store, drv, NoRepair, dryRun); + aio().blockOn(writeDerivation(*store, drv, NoRepair, dryRun)); logger->cout("%s", store->printStorePath(drvPath)); } diff --git a/lix/nix/develop.cc b/lix/nix/develop.cc index 5fa365ab1..163d4995f 100644 --- a/lix/nix/develop.cc +++ b/lix/nix/develop.cc @@ -256,7 +256,7 @@ try { } } - auto shellDrvPath = writeDerivation(*evalStore, drv); + auto shellDrvPath = TRY_AWAIT(writeDerivation(*evalStore, drv)); /* Build the derivation. */ TRY_AWAIT(store->buildPaths(