summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYueh-Shun Li <shamrocklee@posteo.net>2024-06-26 08:42:44 +0800
committerYueh-Shun Li <shamrocklee@posteo.net>2024-06-30 02:59:58 +0800
commit10d2e6c906ab535232549e23546aa29fd6f9cf76 (patch)
tree98017ab8963b280d80f28632388700d5a5823cad
parentlib.getLicenseFromSpdxId: improve documentation (diff)
downloadnixpkgs-10d2e6c906ab535232549e23546aa29fd6f9cf76.tar.gz
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.
-rw-r--r--lib/default.nix2
-rw-r--r--lib/meta.nix52
-rw-r--r--lib/tests/misc.nix20
3 files changed, 68 insertions, 6 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 9c6f886c9ee4..939cb1306464 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -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
diff --git a/lib/meta.nix b/lib/meta.nix
index 8e13c048a68c..65d7bf99191a 100644
--- a/lib/meta.nix
+++ b/lib/meta.nix
@@ -315,16 +315,58 @@ 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
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 408ea5416293..ffff5d91f105 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -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;