@@ -139,9 +139,18 @@ with haskellLib;
139139 # to: tests.mypackage.unit-tests
140140 #
141141 collectComponents = group: packageSel: haskellPackages:
142− (lib.mapAttrs (_: package: package.components.${group} // { recurseForDerivations = true; })
143− (lib.filterAttrs (name: package: (package.isHaskell or false) && packageSel package) haskellPackages))
144− // { recurseForDerivations = true; };
142+ let packageToComponents = name: package:
143+ # look for the components with this group if there are any
144+ let components = package.components.${group} or {};
145+ # set recurseForDerivations unless it's a derivation itself (e.g. the "library" component) or an empty set
146+ in if lib.isDerivation components || components == {}
147+ then components
148+ else pkgs.recurseIntoAttrs components;
149+ packageFilter = name: package: (package.isHaskell or false) && packageSel package;
150+ filteredPkgs = lib.filterAttrs packageFilter haskellPackages;
151+ # at this point we can filter out packages that don't have any of the given kind of component
152+ packagesByComponent = lib.filterAttrs (_: components: components != {}) (lib.mapAttrs packageToComponents filteredPkgs);
153+ in pkgs.recurseIntoAttrs packagesByComponent;
145154
146155 # Equivalent to collectComponents with (_: true) as selection function.
147156 # Useful for pre-filtered package-set.