about summary refs log tree commit diff
path: root/pkgs/stdenv
diff options
context:
space:
mode:
authoradisbladis <adisbladis@gmail.com>2023-12-12 10:55:58 +1300
committerGitHub <noreply@github.com>2023-12-12 10:55:58 +1300
commit6aa414bcfd647445819fb82a2eca1eb9fd2ed900 (patch)
treef14942866c471a38bbcfd1ccf41e8a026f040274 /pkgs/stdenv
parentdba565f62d169c33b699775a28225367d52af70d (diff)
parent3b13bd5c841fc77b92d2c627001414f839d57514 (diff)
Merge pull request #269782 from adisbladis/stdenv-meta-tolist-license-allocations
stdenv: Avoid some list allocations in check-meta when checking licenses
Diffstat (limited to 'pkgs/stdenv')
-rw-r--r--pkgs/stdenv/generic/check-meta.nix29
1 files changed, 21 insertions, 8 deletions
diff --git a/pkgs/stdenv/generic/check-meta.nix b/pkgs/stdenv/generic/check-meta.nix
index f2d9233b6fddf..0bcdf69773f8b 100644
--- a/pkgs/stdenv/generic/check-meta.nix
+++ b/pkgs/stdenv/generic/check-meta.nix
@@ -22,11 +22,15 @@ let
     optionals
     remove
     unknownModule
+    isAttrs
+    isString
   ;
 
   inherit (lib.lists)
     any
     toList
+    isList
+    elem
   ;
 
   # If we're in hydra, we can dispense with the more verbose error
@@ -59,11 +63,15 @@ let
   hasLicense = attrs:
     attrs ? meta.license;
 
-  hasAllowlistedLicense = assert areLicenseListsValid; attrs:
-    hasLicense attrs && any (l: builtins.elem l allowlist) (toList attrs.meta.license);
+  hasListedLicense = assert areLicenseListsValid; list: attrs:
+    length list > 0 && hasLicense attrs && (
+      if isList attrs.meta.license then any (l: elem l list) attrs.meta.license
+      else elem attrs.meta.license list
+    );
 
-  hasBlocklistedLicense = assert areLicenseListsValid; attrs:
-    hasLicense attrs && any (l: builtins.elem l blocklist) (toList attrs.meta.license);
+  hasAllowlistedLicense = attrs: hasListedLicense allowlist attrs;
+
+  hasBlocklistedLicense = attrs: hasListedLicense blocklist attrs;
 
   allowBroken = config.allowBroken
     || builtins.getEnv "NIXPKGS_ALLOW_BROKEN" == "1";
@@ -71,11 +79,16 @@ let
   allowUnsupportedSystem = config.allowUnsupportedSystem
     || builtins.getEnv "NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM" == "1";
 
-  isUnfree = licenses: any (l: !l.free or true) licenses;
+  isUnfree = licenses:
+    if isAttrs licenses then !licenses.free or true
+    # TODO: Returning false in the case of a string is a bug that should be fixed.
+    # In a previous implementation of this function the function body
+    # was `licenses: lib.lists.any (l: !l.free or true) licenses;`
+    # which always evaluates to `!true` for strings.
+    else if isString licenses then false
+    else lib.lists.any (l: !l.free or true) licenses;
 
-  hasUnfreeLicense = attrs:
-    hasLicense attrs &&
-    isUnfree (toList attrs.meta.license);
+  hasUnfreeLicense = attrs: hasLicense attrs && isUnfree attrs.meta.license;
 
   hasNoMaintainers = attrs:
     attrs ? meta.maintainers && (length attrs.meta.maintainers) == 0;