From 2ec675e3941d77a55ff13f278407fe78f7bb03a4 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Tue, 13 Sep 2022 21:02:43 +0300 Subject: [PATCH] feat: add license extraction for nodejs and rust --- examples/nodejs_prettier/flake.nix | 2 +- examples/rust_ripgrep/flake.nix | 2 +- src/lib/default.nix | 17 +++++++++++++++++ .../nodejs/builders/granular/default.nix | 4 ++++ .../nodejs/translators/package-lock/default.nix | 5 ++++- .../nodejs/translators/yarn-lock/default.nix | 5 ++++- .../builders/build-rust-package/default.nix | 8 ++++++++ src/subsystems/rust/builders/crane/default.nix | 6 ++++++ .../rust/translators/cargo-lock/default.nix | 10 ++++++++++ 9 files changed, 55 insertions(+), 4 deletions(-) diff --git a/examples/nodejs_prettier/flake.nix b/examples/nodejs_prettier/flake.nix index 37001a55..29d8e0be 100644 --- a/examples/nodejs_prettier/flake.nix +++ b/examples/nodejs_prettier/flake.nix @@ -1,6 +1,6 @@ { inputs = { - dream2nix.url = "github:nix-community/dream2nix"; + dream2nix.url = "path:../.."; src.url = "github:prettier/prettier/2.4.1"; src.flake = false; }; diff --git a/examples/rust_ripgrep/flake.nix b/examples/rust_ripgrep/flake.nix index 03608ef4..5ce38c21 100644 --- a/examples/rust_ripgrep/flake.nix +++ b/examples/rust_ripgrep/flake.nix @@ -1,6 +1,6 @@ { inputs = { - dream2nix.url = "github:nix-community/dream2nix"; + dream2nix.url = "path:../.."; src.url = "github:BurntSushi/ripgrep/13.0.0"; src.flake = false; }; diff --git a/src/lib/default.nix b/src/lib/default.nix index 5cc5c9a0..16fb7b34 100644 --- a/src/lib/default.nix +++ b/src/lib/default.nix @@ -34,6 +34,7 @@ traceJ modules warnIfIfd + parseSpdxId ; inherit @@ -322,5 +323,21 @@ (ifdWarningEnabled && module.type == "ifd") (ifdWarnMsg module) val; + + idToLicenseKey = + l.mapAttrs' + (n: v: l.nameValuePair (l.toLower (v.spdxId or v.fullName or n)) n) + l.licenses; + # Parses a string like "Unlicense OR MIT" to `["unlicense" "mit"]` + # TODO: this does not parse `AND` or `WITH` or paranthesis, so it is + # pretty hacky in how it works. But for most cases this should be okay. + parseSpdxId = _id: let + # some spdx ids might have paranthesis around them + id = l.removePrefix "(" (l.removeSuffix ")" _id); + licenseStrings = l.map l.toLower (l.splitString " OR " id); + _licenses = l.map (string: idToLicenseKey.${string} or null) licenseStrings; + licenses = l.filter (license: license != null) _licenses; + in + licenses; in dlib diff --git a/src/subsystems/nodejs/builders/granular/default.nix b/src/subsystems/nodejs/builders/granular/default.nix index 7b9afb43..dd6a27bd 100644 --- a/src/subsystems/nodejs/builders/granular/default.nix +++ b/src/subsystems/nodejs/builders/granular/default.nix @@ -216,6 +216,10 @@ inherit pname; + meta = { + licenses = l.map (name: l.licenses.${name}) subsystemAttrs.licenses; + }; + passthru.dependencies = passthruDeps; passthru.devShell = import ./devShell.nix { diff --git a/src/subsystems/nodejs/translators/package-lock/default.nix b/src/subsystems/nodejs/translators/package-lock/default.nix index 3f2a4958..80e25748 100644 --- a/src/subsystems/nodejs/translators/package-lock/default.nix +++ b/src/subsystems/nodejs/translators/package-lock/default.nix @@ -170,7 +170,10 @@ subsystemName = "nodejs"; - subsystemAttrs = {nodejsVersion = b.toString args.nodejs;}; + subsystemAttrs = { + nodejsVersion = b.toString args.nodejs; + licenses = dlib.parseSpdxId (packageJson.license or ""); + }; # functions serializePackages = inputData: let diff --git a/src/subsystems/nodejs/translators/yarn-lock/default.nix b/src/subsystems/nodejs/translators/yarn-lock/default.nix index 55cf506f..724b1ed0 100644 --- a/src/subsystems/nodejs/translators/yarn-lock/default.nix +++ b/src/subsystems/nodejs/translators/yarn-lock/default.nix @@ -140,7 +140,10 @@ subsystemName = "nodejs"; - subsystemAttrs = {nodejsVersion = b.toString args.nodejs;}; + subsystemAttrs = { + nodejsVersion = b.toString args.nodejs; + licenses = dlib.parseSpdxId (packageJson.license or ""); + }; keys = { yarnName = rawObj: finalObj: diff --git a/src/subsystems/rust/builders/build-rust-package/default.nix b/src/subsystems/rust/builders/build-rust-package/default.nix index 4ef2b32c..c221bf6d 100644 --- a/src/subsystems/rust/builders/build-rust-package/default.nix +++ b/src/subsystems/rust/builders/build-rust-package/default.nix @@ -3,6 +3,7 @@ build = { lib, + dlib, pkgs, ... } @ topArgs: { @@ -60,6 +61,13 @@ ${replacePaths} ${utils.writeCargoLock} ''; + + meta = { + licenses = + l.map + (name: l.licenses.${name}) + subsystemAttrs.licenses.${pname}.${version}; + }; }); mkShellForPkg = pkg: diff --git a/src/subsystems/rust/builders/crane/default.nix b/src/subsystems/rust/builders/crane/default.nix index 141c97b3..6f9f44e8 100644 --- a/src/subsystems/rust/builders/crane/default.nix +++ b/src/subsystems/rust/builders/crane/default.nix @@ -112,6 +112,12 @@ ${utils.writeCargoLock} ''; passthru = {dependencies = deps;}; + meta = { + licenses = + l.map + (name: l.licenses.${name}) + subsystemAttrs.licenses.${pname}.${version}; + }; }; in produceDerivation diff --git a/src/subsystems/rust/translators/cargo-lock/default.nix b/src/subsystems/rust/translators/cargo-lock/default.nix index 965bb388..87b74549 100644 --- a/src/subsystems/rust/translators/cargo-lock/default.nix +++ b/src/subsystems/rust/translators/cargo-lock/default.nix @@ -251,6 +251,16 @@ in { gitDeps = l.filter (dep: (getSourceTypeFrom dep) == "git") parsedDeps; in l.unique (l.map (dep: parseGitSource dep) gitDeps); + licenses = l.foldl' l.recursiveUpdate {} ( + l.map + ( + package: let + pkg = package.value.package; + licenses = dlib.parseSpdxId (pkg.license or ""); + in {${pkg.name}.${pkg.version} = licenses;} + ) + cargoPackages + ); }; defaultPackage = package.name;