libstore: fix race condition when creating state directories

Running (parallel?) nix in nix can lead to multiple instances trying
to create the state directories and failing on the createSymlink step,
because the link already exists.

`replaceSymlink` is already idempotent, so let's use that.

See also:
- https://github.com/NixOS/nix/pull/13368
- https://github.com/NixOS/nix/issues/2706

Change-Id: I7fadd0ce3c1ffcebc9d281c00e5b49c12af3d50b
This commit is contained in:
Wolfgang Walther
2025-06-25 16:01:51 +00:00
parent 9a59106c17
commit 34696c65a2
+1 -1
View File
@@ -119,7 +119,7 @@ LocalStore::LocalStore(LocalStoreConfig config)
Path gcRootsDir = config_.stateDir + "/gcroots";
if (!pathExists(gcRootsDir)) {
createDirs(gcRootsDir);
createSymlink(profilesDir, gcRootsDir + "/profiles");
replaceSymlink(profilesDir, gcRootsDir + "/profiles");
}
for (auto & perUserDir : {profilesDir + "/per-user", gcRootsDir + "/per-user"}) {