lib.getLicenseFromSpdxIdOr: init

Add lib.meta.getLicenseFromSpdxIdOr as a variant of
lib.meta.getLicenseFromSpdxId that explicitly state the default
(fallback) value if there's no license matching the given SPDX ID.
This commit is contained in:
Yueh-Shun Li 2024-06-26 08:42:44 +08:00
parent 319736f69f
commit 10d2e6c906
3 changed files with 68 additions and 6 deletions

View File

@ -120,7 +120,7 @@ let
inherit (self.derivations) lazyDerivation optionalDrvAttr;
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet getLicenseFromSpdxId getExe getExe';
hiPrioSet getLicenseFromSpdxId getLicenseFromSpdxIdOr getExe getExe';
inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile
packagesFromDirectoryRecursive;
inherit (self.sources) cleanSourceFilter

View File

@ -315,15 +315,57 @@ rec {
:::
*/
getLicenseFromSpdxId =
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)));
in licstr:
spdxLicenses.${ lib.toLower licstr } or (
licstr:
getLicenseFromSpdxIdOr licstr (
lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
{ shortName = licstr; }
);
/**
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
# Inputs
`licstr`
: 1\. SPDX ID string to find a matching license
`default`
: 2\. Fallback value when a match is not found
# Type
```
getLicenseFromSpdxIdOr :: str -> Any -> Any
```
# Examples
:::{.example}
## `lib.meta.getLicenseFromSpdxIdOr` usage example
```nix
lib.getLicenseFromSpdxIdOr "MIT" null == lib.licenses.mit
=> true
lib.getLicenseFromSpdxId "mIt" null == lib.licenses.mit
=> true
lib.getLicenseFromSpdxIdOr "MY LICENSE" lib.licenses.free == lib.licenses.free
=> true
lib.getLicenseFromSpdxIdOr "MY LICENSE" null
=> null
lib.getLicenseFromSpdxIdOr "MY LICENSE" (builtins.throw "No SPDX ID matches MY LICENSE")
=> error: No SPDX ID matches MY LICENSE
```
:::
*/
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)));
in licstr: default:
spdxLicenses.${ lib.toLower licstr } or default;
/**
Get the path to the main program of a package based on meta.mainProgram

View File

@ -58,6 +58,7 @@ let
genList
getExe
getExe'
getLicenseFromSpdxIdOr
groupBy
groupBy'
hasAttrByPath
@ -2307,6 +2308,25 @@ runTests {
getExe' { type = "derivation"; } "dir/executable"
);
testGetLicenseFromSpdxIdOrExamples = {
expr = [
(getLicenseFromSpdxIdOr "MIT" null)
(getLicenseFromSpdxIdOr "mIt" null)
(getLicenseFromSpdxIdOr "MY LICENSE" lib.licenses.free)
(getLicenseFromSpdxIdOr "MY LICENSE" null)
];
expected = [
lib.licenses.mit
lib.licenses.mit
lib.licenses.free
null
];
};
testGetLicenseFromSpdxIdOrThrow = testingThrow (
getLicenseFromSpdxIdOr "MY LICENSE" (throw "No SPDX ID matches MY LICENSE")
);
testPlatformMatch = {
expr = meta.platformMatch { system = "x86_64-linux"; } "x86_64-linux";
expected = true;