diff options
author | Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> | 2021-10-11 07:08:40 +0800 |
---|---|---|
committer | Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> | 2021-12-02 18:33:48 +0800 |
commit | 60950f739e0fd046e94c5ef86c146b311d39a2a5 (patch) | |
tree | 663908ad601fd8f21f3c73ca5ef95478436af9e7 | |
parent | 53edfe1d1c51c38e2adc4d8eb37a7a2657e3fe01 (diff) |
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
-rw-r--r-- | lib/default.nix | 2 | ||||
-rw-r--r-- | lib/meta.nix | 27 | ||||
-rw-r--r-- | pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix | 25 |
3 files changed, 42 insertions, 12 deletions
diff --git a/lib/default.nix b/lib/default.nix index 68d73220fa9a..626a751cb10a 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -105,7 +105,7 @@ let makeScope makeScopeWithSplicing; inherit (self.meta) addMetaAttrs dontDistribute setName updateName appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio - hiPrioSet; + hiPrioSet getLicenseFromSpdxId; inherit (self.sources) pathType pathIsDirectory cleanSourceFilter cleanSource sourceByRegex sourceFilesBySuffices commitIdFromGitRepo cleanSourceWith pathHasContext diff --git a/lib/meta.nix b/lib/meta.nix index bc04394dcf0b..bc3387646f26 100644 --- a/lib/meta.nix +++ b/lib/meta.nix @@ -99,4 +99,31 @@ rec { availableOn = platform: pkg: lib.any (platformMatch platform) pkg.meta.platforms && 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; } + ); } diff --git a/pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix b/pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix index eadb34062f3e..b0bf715e66e8 100644 --- a/pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix +++ b/pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix @@ -1,6 +1,7 @@ { pkgs ? import <nixpkgs> {} , nodejs ? pkgs.nodejs , yarn ? pkgs.yarn +, allowAliases ? pkgs.config.allowAliases or true }: let @@ -9,6 +10,14 @@ let compose = f: g: x: f (g x); id = x: x; 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 { # Export yarn again to make it easier to find out which yarn was used. inherit yarn; @@ -30,16 +39,7 @@ in rec { non-null = builtins.filter (x: x != null) parts; in builtins.concatStringsSep "-" non-null; - # https://docs.npmjs.com/files/package.json#license - # 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); + inherit getLicenseFromSpdxId; # Generates the yarn.nix from the yarn.lock file mkYarnNix = { yarnLock, flags ? [] }: @@ -369,7 +369,7 @@ in rec { description = packageJSON.description or ""; homepage = packageJSON.homepage 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 {}); }); @@ -437,4 +437,7 @@ in rec { patchShebangs $out ''; +} // lib.optionalAttrs allowAliases { + # Aliases + spdxLicense = getLicenseFromSpdxId; # added 2021-12-01 } |