mkNugetSource: fix bug in metadata generation

This improves the metadata generation, previously it would take any
"license" entry from the nuspec, and tried to match it to an spdx ID from
"lib.licenses".

Sometimes however licenses are provided in plain-text, which we
obviously cannot cleanly resolve. This resulted in in useless information
("LICENSE.txt") being written to "meta.license".
This commit is contained in:
= 2022-04-25 18:17:35 +02:00 committed by Jonathan Ringer
parent 2a88b7df51
commit 38419c65ce

View File

@ -1,30 +1,38 @@
{ dotnetPackages, lib, xml2, stdenvNoCC }:
{ name, description ? "", deps ? [] }:
let
_nuget-source = stdenvNoCC.mkDerivation rec {
inherit name;
meta.description = description;
{ name
, description ? ""
, deps ? []
}:
let
nuget-source = stdenvNoCC.mkDerivation rec {
inherit name;
meta.description = description;
nativeBuildInputs = [ dotnetPackages.Nuget xml2 ];
buildCommand = ''
export HOME=$(mktemp -d)
mkdir -p $out/{lib,share}
nuget sources Add -Name nixos -Source "$out/lib"
${ lib.concatMapStringsSep "\n" (dep:
''nuget init "${dep}" "$out/lib"''
) deps }
${lib.concatMapStringsSep "\n" (dep: ''
nuget init "${dep}" "$out/lib"
'') deps}
# Generates a list of all unique licenses' spdx ids.
# Generates a list of all licenses' spdx ids, if available.
# Note that this currently ignores any license provided in plain text (e.g. "LICENSE.txt")
find "$out/lib" -name "*.nuspec" -exec sh -c \
"xml2 < {} | grep "license=" | cut -d'=' -f2" \; | sort -u > $out/share/licenses
"NUSPEC=\$(xml2 < {}) && echo "\$NUSPEC" | grep license/@type=expression | tr -s \ '\n' | grep "license=" | cut -d'=' -f2" \
\; | sort -u > $out/share/licenses
'';
} // { # This is done because we need data from `$out` for `meta`. We have to use overrides as to not hit infinite recursion.
meta.licence = let
depLicenses = lib.splitString "\n" (builtins.readFile "${_nuget-source}/share/licenses");
getLicence = spdx: lib.filter (license: license.spdxId or null == spdx) (builtins.attrValues lib.licenses);
in (lib.flatten (lib.forEach depLicenses (spdx:
if (getLicence spdx) != [] then (getLicence spdx) else [] ++ lib.optional (spdx != "") spdx
)));
};
in _nuget-source
} // { # We need data from `$out` for `meta`, so we have to use overrides as to not hit infinite recursion.
meta.licence = let
depLicenses = lib.splitString "\n" (builtins.readFile "${nuget-source}/share/licenses");
in (lib.flatten (lib.forEach depLicenses (spdx:
if (spdx != "")
then lib.getLicenseFromSpdxId spdx
else []
)));
};
in nuget-source