Merge pull request #141856 from ShamrockLee/lib-spdx-license

lib/meta: add getLicenseFromSpdxId function (resumed)
This commit is contained in:
Silvan Mosberger 2021-12-07 18:37:13 +01:00 committed by GitHub
commit d2c50190b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -105,7 +105,7 @@ let
makeScope makeScopeWithSplicing; makeScope makeScopeWithSplicing;
inherit (self.meta) addMetaAttrs dontDistribute setName updateName inherit (self.meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet; hiPrioSet getLicenseFromSpdxId;
inherit (self.sources) pathType pathIsDirectory cleanSourceFilter inherit (self.sources) pathType pathIsDirectory cleanSourceFilter
cleanSource sourceByRegex sourceFilesBySuffices cleanSource sourceByRegex sourceFilesBySuffices
commitIdFromGitRepo cleanSourceWith pathHasContext commitIdFromGitRepo cleanSourceWith pathHasContext

View File

@ -99,4 +99,31 @@ rec {
availableOn = platform: pkg: availableOn = platform: pkg:
lib.any (platformMatch platform) pkg.meta.platforms && lib.any (platformMatch platform) pkg.meta.platforms &&
lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []); lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
/* Get the corresponding attribute in lib.licenses
from the SPDX ID.
For SPDX IDs, see
https://spdx.org/licenses
Type:
getLicenseFromSpdxId :: str -> AttrSet
Example:
lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit
=> true
lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit
=> true
lib.getLicenseFromSpdxId "MY LICENSE"
=> trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE
=> { shortName = "MY LICENSE"; }
*/
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 (
lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
{ shortName = licstr; }
);
} }

View File

@ -1,6 +1,7 @@
{ pkgs ? import <nixpkgs> {} { pkgs ? import <nixpkgs> {}
, nodejs ? pkgs.nodejs , nodejs ? pkgs.nodejs
, yarn ? pkgs.yarn , yarn ? pkgs.yarn
, allowAliases ? pkgs.config.allowAliases or true
}: }:
let let
@ -9,6 +10,14 @@ let
compose = f: g: x: f (g x); compose = f: g: x: f (g x);
id = x: x; id = x: x;
composeAll = builtins.foldl' compose id; composeAll = builtins.foldl' compose id;
# https://docs.npmjs.com/files/package.json#license
# TODO: support expression syntax (OR, AND, etc)
getLicenseFromSpdxId = licstr:
if licstr == "UNLICENSED" then
lib.licenses.unfree
else
lib.getLicenseFromSpdxId licstr;
in rec { in rec {
# Export yarn again to make it easier to find out which yarn was used. # Export yarn again to make it easier to find out which yarn was used.
inherit yarn; inherit yarn;
@ -30,16 +39,7 @@ in rec {
non-null = builtins.filter (x: x != null) parts; non-null = builtins.filter (x: x != null) parts;
in builtins.concatStringsSep "-" non-null; in builtins.concatStringsSep "-" non-null;
# https://docs.npmjs.com/files/package.json#license inherit getLicenseFromSpdxId;
# TODO: support expression syntax (OR, AND, etc)
spdxLicense = licstr:
if licstr == "UNLICENSED" then
lib.licenses.unfree
else
lib.findFirst
(l: l ? spdxId && l.spdxId == licstr)
{ shortName = licstr; }
(builtins.attrValues lib.licenses);
# Generates the yarn.nix from the yarn.lock file # Generates the yarn.nix from the yarn.lock file
mkYarnNix = { yarnLock, flags ? [] }: mkYarnNix = { yarnLock, flags ? [] }:
@ -369,7 +369,7 @@ in rec {
description = packageJSON.description or ""; description = packageJSON.description or "";
homepage = packageJSON.homepage or ""; homepage = packageJSON.homepage or "";
version = packageJSON.version or ""; version = packageJSON.version or "";
license = if packageJSON ? license then spdxLicense packageJSON.license else ""; license = if packageJSON ? license then getLicenseFromSpdxId packageJSON.license else "";
} // (attrs.meta or {}); } // (attrs.meta or {});
}); });
@ -437,4 +437,7 @@ in rec {
patchShebangs $out patchShebangs $out
''; '';
} // lib.optionalAttrs allowAliases {
# Aliases
spdxLicense = getLicenseFromSpdxId; # added 2021-12-01
} }