Merge pull request #333953 from mibmo/spdx-license-mapping

lib.meta.licensesSpdx: mapping from SPDX ID to compatible licenses
This commit is contained in:
Silvan Mosberger 2024-08-25 15:44:18 +02:00 committed by GitHub
commit 7954f9dfc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 6 deletions

View File

@ -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

View File

@ -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 = <license string>; }`.
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