From 520895b1dac1b82be9f571df3981a15cfada2968 Mon Sep 17 00:00:00 2001
From: regnat <rg@regnat.ovh>
Date: Thu, 17 Sep 2020 13:36:58 +0200
Subject: [PATCH] Fix garbage collection of CA derivations

Fix #4026
---
 src/libstore/gc.cc         | 9 ++++++---
 tests/content-addressed.sh | 3 +++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index e6cbc525d..08b53c702 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -574,9 +574,12 @@ bool LocalStore::canReachRoot(GCState & state, StorePathSet & visited, const Sto
     /* If keep-derivations is set and this is a derivation, then
        don't delete the derivation if any of the outputs are alive. */
     if (state.gcKeepDerivations && path.isDerivation()) {
-        for (auto & i : queryDerivationOutputs(path))
-            if (isValidPath(i) && queryPathInfo(i)->deriver == path)
-                incoming.insert(i);
+        for (auto & [name, maybeOutPath] : queryPartialDerivationOutputMap(path))
+            if (maybeOutPath &&
+                isValidPath(*maybeOutPath) &&
+                queryPathInfo(*maybeOutPath)->deriver == path
+                )
+                incoming.insert(*maybeOutPath);
     }
 
     /* If keep-outputs is set, then don't delete this path if there
diff --git a/tests/content-addressed.sh b/tests/content-addressed.sh
index ae9e3c59e..0ae2852d2 100644
--- a/tests/content-addressed.sh
+++ b/tests/content-addressed.sh
@@ -15,3 +15,6 @@ out1=$(nix-build "${commonArgs[@]}" ./content-addressed.nix --arg seed 1)
 out2=$(nix-build "${commonArgs[@]}" ./content-addressed.nix --arg seed 2)
 
 test $out1 == $out2
+
+nix-instantiate --experimental-features ca-derivations ./content-addressed.nix -A rootCA --arg seed 5
+nix-collect-garbage --experimental-features ca-derivations --option keep-derivations true
-- 
GitLab