From d04697aee8fff61578cf3480506f13a4747892d3 Mon Sep 17 00:00:00 2001 From: mib Date: Sun, 11 Aug 2024 19:06:54 +0200 Subject: [PATCH 1/2] lib.meta.licensesSpdx: mapping from SPDX ID to licenses --- lib/meta.nix | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/meta.nix b/lib/meta.nix index 65d7bf99191a93a..ddd062f38c47afb 100644 --- a/lib/meta.nix +++ b/lib/meta.nix @@ -286,6 +286,32 @@ 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 + + # 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 = + lib.attrsets.mapAttrs' + (_key: license: { + name = license.spdxId; + value = license; + }) + (lib.attrsets.filterAttrs (_key: license: license ? spdxId) lib.licenses); + /** Get the corresponding attribute in lib.licenses from the SPDX ID or warn and fallback to `{ shortName = ; }`. @@ -361,10 +387,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 From f55a4b99ef73df3b17ae5e951198949a880cf3ab Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sun, 25 Aug 2024 00:57:44 +0200 Subject: [PATCH 2/2] lib.meta: Minor SPDX license function improvements - Expose `lib.licensesSpdx` - Create bindings for the needed internal functions - Mention that some SPDX licenses might be missing (in the future I hope we can autogenerate the Nixpkgs license list from some SPDX endpoint --- lib/default.nix | 3 ++- lib/meta.nix | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 63a31101eee7f08..0ff3a39807450aa 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 ddd062f38c47afb..57f3a46d0698ed4 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 @@ -289,7 +290,8 @@ rec { /** Mapping of SPDX ID to the attributes in lib.licenses. - For SPDX IDs, see https://spdx.org/licenses + For SPDX IDs, see https://spdx.org/licenses. + Note that some SPDX licenses might be missing. # Examples :::{.example} @@ -305,18 +307,19 @@ rec { ::: */ licensesSpdx = - lib.attrsets.mapAttrs' + mapAttrs' (_key: license: { name = license.spdxId; value = license; }) - (lib.attrsets.filterAttrs (_key: license: license ? spdxId) lib.licenses); + (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 @@ -351,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