git.iko.soy/typeable/nixpkgs
on master help sign in
lib/meta: add getLicenseFromSpdxId function

Move function spdxLicense, internally used in yarn2nix to lib/meta.nix, and rename to getLicenseFromSpdxId A similar function is implemented in poetry2nix, but the one originally in yarn2nix seems beter. since it falls back to an license-like attrset for mismatched case instead of a plain string

commit60950f739e0fd046e94c5ef86c146b311d39a2a5 authorShamrock Lee <44064051+ShamrockLee@users.noreply.github.com> dateMon 11 Oct 2021, 07:08 (5 years ago) parent53edfe1
3 files changed+42−12
lib/default.nix+1−1 lib/meta.nix+27 pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix+14−11
lib/default.nix+1−1
@@ -105,7 +105,7 @@ let
105105 makeScope makeScopeWithSplicing;
106106 inherit (self.meta) addMetaAttrs dontDistribute setName updateName
107107 appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
108− hiPrioSet;
108+ hiPrioSet getLicenseFromSpdxId;
109109 inherit (self.sources) pathType pathIsDirectory cleanSourceFilter
110110 cleanSource sourceByRegex sourceFilesBySuffices
111111 commitIdFromGitRepo cleanSourceWith pathHasContext
lib/meta.nix+27
@@ -99,4 +99,31 @@ rec {
9999 availableOn = platform: pkg:
100100 lib.any (platformMatch platform) pkg.meta.platforms &&
101101 lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
102+
103+ /* Get the corresponding attribute in lib.licenses
104+ from the SPDX ID.
105+ For SPDX IDs, see
106+ https://spdx.org/licenses
107+
108+ Type:
109+ getLicenseFromSpdxId :: str -> AttrSet
110+
111+ Example:
112+ lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit
113+ => true
114+ lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit
115+ => true
116+ lib.getLicenseFromSpdxId "MY LICENSE"
117+ => trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE
118+ => { shortName = "MY LICENSE"; }
119+ */
120+ getLicenseFromSpdxId =
121+ let
122+ spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls)
123+ (lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses)));
124+ in licstr:
125+ spdxLicenses.${ lib.toLower licstr } or (
126+ lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
127+ { shortName = licstr; }
128+ );
102129}
pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix+14−11
@@ -1,6 +1,7 @@
11{ pkgs ? import <nixpkgs> {}
22, nodejs ? pkgs.nodejs
33, yarn ? pkgs.yarn
4+, allowAliases ? pkgs.config.allowAliases or true
45}:
56
67let
@@ -9,6 +10,14 @@ let
910 compose = f: g: x: f (g x);
1011 id = x: x;
1112 composeAll = builtins.foldl' compose id;
13+
14+ # https://docs.npmjs.com/files/package.json#license
15+ # TODO: support expression syntax (OR, AND, etc)
16+ getLicenseFromSpdxId = licstr:
17+ if licstr == "UNLICENSED" then
18+ lib.licenses.unfree
19+ else
20+ lib.getLicenseFromSpdxId licstr;
1221in rec {
1322 # Export yarn again to make it easier to find out which yarn was used.
1423 inherit yarn;
@@ -30,16 +39,7 @@ in rec {
3039 non-null = builtins.filter (x: x != null) parts;
3140 in builtins.concatStringsSep "-" non-null;
3241
33− # https://docs.npmjs.com/files/package.json#license
34− # TODO: support expression syntax (OR, AND, etc)
35− spdxLicense = licstr:
36− if licstr == "UNLICENSED" then
37− lib.licenses.unfree
38− else
39− lib.findFirst
40− (l: l ? spdxId && l.spdxId == licstr)
41− { shortName = licstr; }
42− (builtins.attrValues lib.licenses);
42+ inherit getLicenseFromSpdxId;
4343
4444 # Generates the yarn.nix from the yarn.lock file
4545 mkYarnNix = { yarnLock, flags ? [] }:
@@ -369,7 +369,7 @@ in rec {
369369 description = packageJSON.description or "";
370370 homepage = packageJSON.homepage or "";
371371 version = packageJSON.version or "";
372− license = if packageJSON ? license then spdxLicense packageJSON.license else "";
372+ license = if packageJSON ? license then getLicenseFromSpdxId packageJSON.license else "";
373373 } // (attrs.meta or {});
374374 });
375375
@@ -437,4 +437,7 @@ in rec {
437437
438438 patchShebangs $out
439439 '';
440+} // lib.optionalAttrs allowAliases {
441+ # Aliases
442+ spdxLicense = getLicenseFromSpdxId; # added 2021-12-01
440443}