From b85639c5890abf2eb3ab6757b036529a035b6235 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 8 Jul 2024 12:42:51 +1200 Subject: [PATCH] lib.licenses: refactor internal mkLicense to avoid future typo bugs This is a follow-up to https://github.com/NixOS/nixpkgs/pull/325380 where @emilazy discovered that we have some typos in field names. This refactoring wraps the implicit lib.licenses schema up in a factory function to ensure consistency. While a more proper type checker like the one we use for meta checks would be better I didn't want to depend on that in more places. In fact, we might want to make meta type checks more strict on the license field. --- lib/licenses.nix | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/licenses.nix b/lib/licenses.nix index c5d05437efc0..3a739783ea7d 100644 --- a/lib/licenses.nix +++ b/lib/licenses.nix @@ -1,25 +1,29 @@ { lib }: +let + inherit (lib) optionalAttrs; -lib.mapAttrs (lname: lset: let - defaultLicense = { - shortName = lname; - free = true; # Most of our licenses are Free, explicitly declare unfree additions as such! - deprecated = false; + mkLicense = lname: { + shortName ? lname, + # Most of our licenses are Free, explicitly declare unfree additions as such! + free ? true, + deprecated ? false, + spdxId ? null, + url ? null, + fullName ? null, + redistributable ? free + }@attrs: { + inherit shortName free deprecated redistributable; + } // optionalAttrs (attrs ? spdxId) { + inherit spdxId; + url = "https://spdx.org/licenses/${spdxId}.html"; + } // optionalAttrs (attrs ? url) { + inherit url; + } // optionalAttrs (attrs ? fullName) { + inherit fullName; }; - mkLicense = licenseDeclaration: let - applyDefaults = license: defaultLicense // license; - applySpdx = license: - if license ? spdxId - then license // { url = "https://spdx.org/licenses/${license.spdxId}.html"; } - else license; - applyRedistributable = license: { redistributable = license.free; } // license; - in lib.pipe licenseDeclaration [ - applyDefaults - applySpdx - applyRedistributable - ]; -in mkLicense lset) ({ +in +lib.mapAttrs mkLicense ({ /* License identifiers from spdx.org where possible. * If you cannot find your license here, then look for a similar license or * add it to this list. The URL mentioned above is a good source for inspiration.