Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.meta.licensesSpdx: mapping from SPDX ID to compatible licenses #333953

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ let
inherit (self.derivations) lazyDerivation optionalDrvAttr;
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet getLicenseFromSpdxId getLicenseFromSpdxIdOr getExe getExe';
hiPrioSet licensesSpdx getLicenseFromSpdxId getLicenseFromSpdxIdOr
getExe getExe';
inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile
packagesFromDirectoryRecursive;
inherit (self.sources) cleanSourceFilter
Expand Down
42 changes: 37 additions & 5 deletions lib/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

let
inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
inherit (lib.attrsets) mapAttrs' filterAttrs;
inherit (builtins) isString match typeOf;

in
Expand Down Expand Up @@ -286,11 +287,39 @@ rec {
((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);

/**
Mapping of SPDX ID to the attributes in lib.licenses.

For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.

# Examples
:::{.example}
## `lib.meta.licensesSpdx` usage example

```nix
lib.licensesSpdx.MIT == lib.licenses.mit
=> true
lib.licensesSpdx."MY LICENSE"
=> error: attribute 'MY LICENSE' missing
```

:::
*/
licensesSpdx =
mapAttrs'
(_key: license: {
name = license.spdxId;
value = license;
})
(filterAttrs (_key: license: license ? spdxId) lib.licenses);

/**
Get the corresponding attribute in lib.licenses from the SPDX ID
or warn and fallback to `{ shortName = <license string>; }`.

For SPDX IDs, see https://spdx.org/licenses
For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.

# Type

Expand Down Expand Up @@ -325,7 +354,8 @@ rec {
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
For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.

# Inputs

Expand Down Expand Up @@ -361,10 +391,12 @@ rec {
*/
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)));
lowercaseLicenses = lib.mapAttrs' (name: value: {
name = lib.toLower name;
inherit value;
}) licensesSpdx;
in licstr: default:
spdxLicenses.${ lib.toLower licstr } or default;
lowercaseLicenses.${ lib.toLower licstr } or default;

/**
Get the path to the main program of a package based on meta.mainProgram
Expand Down