diff --git a/lib/default.nix b/lib/default.nix index 63a31101eee7..0ff3a3980745 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -123,7 +123,8 @@ let inherit (self.derivations) lazyDerivation optionalDrvAttr; inherit (self.meta) addMetaAttrs dontDistribute setName updateName appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio - hiPrioSet getLicenseFromSpdxId getLicenseFromSpdxIdOr getExe getExe'; + hiPrioSet licensesSpdx getLicenseFromSpdxId getLicenseFromSpdxIdOr + getExe getExe'; inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile packagesFromDirectoryRecursive; inherit (self.sources) cleanSourceFilter diff --git a/lib/meta.nix b/lib/meta.nix index 65d7bf99191a..57f3a46d0698 100644 --- a/lib/meta.nix +++ b/lib/meta.nix @@ -7,6 +7,7 @@ let inherit (lib) matchAttrs any all isDerivation getBin assertMsg; + inherit (lib.attrsets) mapAttrs' filterAttrs; inherit (builtins) isString match typeOf; in @@ -286,11 +287,39 @@ rec { ((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) && all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []); + /** + Mapping of SPDX ID to the attributes in lib.licenses. + + For SPDX IDs, see https://spdx.org/licenses. + Note that some SPDX licenses might be missing. + + # Examples + :::{.example} + ## `lib.meta.licensesSpdx` usage example + + ```nix + lib.licensesSpdx.MIT == lib.licenses.mit + => true + lib.licensesSpdx."MY LICENSE" + => error: attribute 'MY LICENSE' missing + ``` + + ::: + */ + licensesSpdx = + mapAttrs' + (_key: license: { + name = license.spdxId; + value = license; + }) + (filterAttrs (_key: license: license ? spdxId) lib.licenses); + /** Get the corresponding attribute in lib.licenses from the SPDX ID or warn and fallback to `{ shortName = ; }`. - For SPDX IDs, see https://spdx.org/licenses + For SPDX IDs, see https://spdx.org/licenses. + Note that some SPDX licenses might be missing. # Type @@ -325,7 +354,8 @@ rec { Get the corresponding attribute in lib.licenses from the SPDX ID or fallback to the given default value. - For SPDX IDs, see https://spdx.org/licenses + For SPDX IDs, see https://spdx.org/licenses. + Note that some SPDX licenses might be missing. # Inputs @@ -361,10 +391,12 @@ rec { */ getLicenseFromSpdxIdOr = let - spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls) - (lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses))); + lowercaseLicenses = lib.mapAttrs' (name: value: { + name = lib.toLower name; + inherit value; + }) licensesSpdx; in licstr: default: - spdxLicenses.${ lib.toLower licstr } or default; + lowercaseLicenses.${ lib.toLower licstr } or default; /** Get the path to the main program of a package based on meta.mainProgram