maintainers/scripts/update.nix: refactor package collector

The `packagesWith` function expected an attrSet but `packagesWithUpdateScript`
could be passing it a derivation or a list when the attribute path
supplied by user through the `--argstr path` argument pointed to one.
It only worked because derivations are also attrSets and contain their
outputs as attributes, and did not work for lists at all.

Additionally, the improper handling would cause the `src` attribute
to be built in some rare cases (`mkYarnPackage` seems to trigger this).

Rewriting the `packagesWith` function to be inductive with a derivation
as a base case and attrSets and lists as inductive steps is much cleaner
and also fixes the unnecessary build.
This commit is contained in:
Jan Tojnar 2020-05-13 10:29:01 +02:00
parent fab2ee8c10
commit 3f3aeb7c85
No known key found for this signature in database
GPG key ID: 7FAB2A15F7A607A4

View file

@ -25,26 +25,29 @@ let
in in
[x] ++ nubOn f xs; [x] ++ nubOn f xs;
packagesWith = cond: return: set: packagesWithPath = relativePath: cond: return: pathContent:
nubOn (pkg: pkg.updateScript) let
(lib.flatten result = builtins.tryEval pathContent;
(lib.mapAttrsToList
(name: pkg: dedupResults = lst: nubOn (pkg: pkg.updateScript) (lib.concatLists lst);
let in
result = builtins.tryEval ( if result.success then
if lib.isDerivation pkg let
then lib.optional (cond name pkg) (return name pkg) pathContent = result.value;
else if pkg.recurseForDerivations or false || pkg.recurseForRelease or false in
then packagesWith cond return pkg if lib.isDerivation pathContent then
else [] lib.optional (cond relativePath pathContent) (return relativePath pathContent)
); else if lib.isAttrs pathContent then
in # If user explicitly points to an attrSet or it is marked for recursion, we recur.
if result.success then result.value if relativePath == [] || pathContent.recurseForDerivations or false || pathContent.recurseForRelease or false then
else [] dedupResults (lib.mapAttrsToList (name: elem: packagesWithPath (relativePath ++ [name]) cond return elem) pathContent)
) else []
set else if lib.isList pathContent then
) dedupResults (lib.imap0 (i: elem: packagesWithPath (relativePath ++ [i]) cond return elem) pathContent)
); else []
else [];
packagesWith = packagesWithPath [];
packagesWithUpdateScriptAndMaintainer = maintainer': packagesWithUpdateScriptAndMaintainer = maintainer':
let let
@ -54,7 +57,7 @@ let
else else
builtins.getAttr maintainer' lib.maintainers; builtins.getAttr maintainer' lib.maintainers;
in in
packagesWith (name: pkg: builtins.hasAttr "updateScript" pkg && packagesWith (relativePath: pkg: builtins.hasAttr "updateScript" pkg &&
(if builtins.hasAttr "maintainers" pkg.meta (if builtins.hasAttr "maintainers" pkg.meta
then (if builtins.isList pkg.meta.maintainers then (if builtins.isList pkg.meta.maintainers
then builtins.elem maintainer pkg.meta.maintainers then builtins.elem maintainer pkg.meta.maintainers
@ -63,19 +66,19 @@ let
else false else false
) )
) )
(name: pkg: pkg) (relativePath: pkg: pkg)
pkgs; pkgs;
packagesWithUpdateScript = path: packagesWithUpdateScript = path:
let let
attrSet = lib.attrByPath (lib.splitString "." path) null pkgs; pathContent = lib.attrByPath (lib.splitString "." path) null pkgs;
in in
if attrSet == null then if pathContent == null then
builtins.throw "Attribute path `${path}` does not exists." builtins.throw "Attribute path `${path}` does not exists."
else else
packagesWith (name: pkg: builtins.hasAttr "updateScript" pkg) packagesWith (relativePath: pkg: builtins.hasAttr "updateScript" pkg)
(name: pkg: pkg) (relativePath: pkg: pkg)
attrSet; pathContent;
packageByName = name: packageByName = name:
let let