1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +01:00

Merge pull request #14219 from lovesegfault/eval-copy-less

libstore: Avoid copying derivations to the store if they are already valid
This commit is contained in:
John Ericson 2025-10-13 16:36:40 +00:00 committed by GitHub
commit 0f85ef3677
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 77 additions and 17 deletions

View file

@ -83,6 +83,7 @@ sources = files(
'store-reference.cc', 'store-reference.cc',
'uds-remote-store.cc', 'uds-remote-store.cc',
'worker-protocol.cc', 'worker-protocol.cc',
'write-derivation.cc',
) )
include_dirs = [ include_directories('.') ] include_dirs = [ include_directories('.') ]

View file

@ -0,0 +1,57 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "nix/util/tests/gmock-matchers.hh"
#include "nix/store/derivations.hh"
#include "nix/store/dummy-store-impl.hh"
#include "nix/store/tests/libstore.hh"
namespace nix {
namespace {
class WriteDerivationTest : public LibStoreTest
{
protected:
WriteDerivationTest(ref<DummyStoreConfig> config_)
: LibStoreTest(config_->openDummyStore())
, config(std::move(config_))
{
config->readOnly = false;
}
WriteDerivationTest()
: WriteDerivationTest(make_ref<DummyStoreConfig>(DummyStoreConfig::Params{}))
{
}
ref<DummyStoreConfig> config;
};
static Derivation makeSimpleDrv()
{
Derivation drv;
drv.name = "simple-derivation";
drv.platform = "system";
drv.builder = "foo";
drv.args = {"bar", "baz"};
drv.env = StringPairs{{"BIG_BAD", "WOLF"}};
return drv;
}
} // namespace
TEST_F(WriteDerivationTest, addToStoreFromDumpCalledOnce)
{
auto drv = makeSimpleDrv();
auto path1 = writeDerivation(*store, drv, NoRepair);
config->readOnly = true;
auto path2 = writeDerivation(*store, drv, NoRepair);
EXPECT_EQ(path1, path2);
EXPECT_THAT(
[&] { writeDerivation(*store, drv, Repair); },
::testing::ThrowsMessage<Error>(testing::HasSubstrIgnoreANSIMatcher(
"operation 'addToStoreFromDump' is not supported by store 'dummy://'")));
}
} // namespace nix

View file

@ -115,15 +115,15 @@ StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repa
held during a garbage collection). */ held during a garbage collection). */
auto suffix = std::string(drv.name) + drvExtension; auto suffix = std::string(drv.name) + drvExtension;
auto contents = drv.unparse(store, false); auto contents = drv.unparse(store, false);
return readOnly || settings.readOnlyMode ? store.makeFixedOutputPathFromCA( auto hash = hashString(HashAlgorithm::SHA256, contents);
suffix, auto ca = TextInfo{.hash = hash, .references = references};
TextInfo{ auto path = store.makeFixedOutputPathFromCA(suffix, ca);
.hash = hashString(HashAlgorithm::SHA256, contents),
.references = std::move(references), if (readOnly || settings.readOnlyMode || (store.isValidPath(path) && !repair))
}) return path;
: ({
StringSource s{contents}; StringSource s{contents};
store.addToStoreFromDump( auto path2 = store.addToStoreFromDump(
s, s,
suffix, suffix,
FileSerialisationMethod::Flat, FileSerialisationMethod::Flat,
@ -131,7 +131,9 @@ StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repa
HashAlgorithm::SHA256, HashAlgorithm::SHA256,
references, references,
repair); repair);
}); assert(path2 == path);
return path;
} }
namespace { namespace {