buildDotnetModule: restore for all platforms in fetch-deps

This makes buildDotnetModule restore nuget dependencies for the
platforms set in meta.platforms. This should help with generating
lockfiles for platforms other than the host machine.

Co-authored-by: mdarocha <git@mdarocha.pl>
This commit is contained in:
Ivar Scholten 2022-08-18 23:24:38 +02:00
parent 0d8a23bfcd
commit efd92e7fd7
No known key found for this signature in database
GPG Key ID: E22887AD87C33E3C

View File

@ -1,4 +1,20 @@
{ lib, stdenvNoCC, linkFarmFromDrvs, callPackage, nuget-to-nix, writeShellScript, makeWrapper, fetchurl, xml2, dotnetCorePackages, dotnetPackages, mkNugetSource, mkNugetDeps, cacert, srcOnly, symlinkJoin, coreutils }:
{ lib
, stdenvNoCC
, callPackage
, linkFarmFromDrvs
, dotnetCorePackages
, dotnetPackages
, mkNugetSource
, mkNugetDeps
, srcOnly
, writeShellScript
, writeText
, makeWrapper
, nuget-to-nix
, cacert
, symlinkJoin
, coreutils
}:
{ name ? "${args.pname}-${args.version}"
, pname ? name
@ -135,44 +151,73 @@ in stdenvNoCC.mkDerivation (args // {
inherit nuget-source;
fetch-deps = let
exclusions = dotnet-sdk.passthru.packages { fetchNuGet = attrs: attrs.pname; };
# Because this list is rather long its put in its own store path to maintain readability of the generated script
exclusions = writeText "nuget-package-exclusions" (lib.concatStringsSep "\n" (dotnet-sdk.passthru.packages { fetchNuGet = attrs: attrs.pname; }));
runtimeIds = map (system: dotnet-sdk.systemToDotnetRid system) (args.meta.platforms or dotnet-sdk.meta.platforms);
# Derivations may set flags such as `--runtime <rid>` based on the host platform to avoid restoring/building nuget dependencies they dont have or dont need.
# This introduces an issue; In this script we loop over all platforms from `meta` and add the RID flag for it, as to fetch all required dependencies.
# The script would inherit the RID flag from the derivation based on the platform building the script, and set the flag for any iteration we do over the RIDs.
# That causes conflicts. To circumvent it we remove all occurances of the flag.
flags =
let
hasRid = flag: lib.any (v: v) (map (rid: lib.hasInfix rid flag) (lib.attrValues dotnet-sdk.runtimeIdentifierMap));
in
builtins.filter (flag: !(hasRid flag)) (dotnetFlags ++ dotnetRestoreFlags);
in writeShellScript "fetch-${pname}-deps" ''
set -euo pipefail
export PATH="${lib.makeBinPath [ coreutils dotnet-sdk nuget-to-nix ]}"
cd "$(dirname "''${BASH_SOURCE[0]}")"
case "''${1-}" in
--help|-h)
echo "usage: $0 <output path> [--help]"
echo " <output path> The path to write the lockfile to"
echo " --help Show this help message"
exit
;;
esac
export HOME=$(mktemp -d)
deps_file="''${1:-/tmp/${pname}-deps.nix}"
deps_file="$(realpath "''${1:-$(mktemp -t "XXXXXX-${pname}-deps.nix")}")"
export HOME=$(mktemp -td "XXXXXX-${pname}-home")
mkdir -p "$HOME/nuget_pkgs"
store_src="${srcOnly args}"
src="$(mktemp -d /tmp/${pname}.XXX)"
src="$(mktemp -td "XXXXXX-${pname}-src")"
cp -rT "$store_src" "$src"
chmod -R +w "$src"
trap "rm -rf $src $HOME" EXIT
pushd "$src"
cd "$src"
echo "Restoring project..."
export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
mkdir -p "$HOME/nuget_pkgs"
for project in "${lib.concatStringsSep "\" \"" ((lib.toList projectFile) ++ lib.optionals (testProjectFile != "") (lib.toList testProjectFile))}"; do
dotnet restore "$project" \
${lib.optionalString (!enableParallelBuilding) "--disable-parallel"} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--packages "$HOME/nuget_pkgs" \
${lib.optionalString (dotnetRestoreFlags != []) (builtins.toString dotnetRestoreFlags)} \
${lib.optionalString (dotnetFlags != []) (builtins.toString dotnetFlags)}
for rid in "${lib.concatStringsSep "\" \"" runtimeIds}"; do
for project in "${lib.concatStringsSep "\" \"" ((lib.toList projectFile) ++ lib.optionals (testProjectFile != "") (lib.toList testProjectFile))}"; do
dotnet restore "$project" \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--packages "$HOME/nuget_pkgs" \
--runtime "$rid" \
${lib.optionalString (!enableParallelBuilding) "--disable-parallel"} \
${lib.optionalString (flags != []) (toString flags)}
done
done
echo "${lib.concatStringsSep "\n" exclusions}" > "$HOME/package_exclusions"
echo "Succesfully restored project"
echo "Writing lockfile..."
nuget-to-nix "$HOME/nuget_pkgs" "$HOME/package_exclusions" > "$deps_file"
echo -e "# This file was automatically generated by passthru.fetch-deps.\n# Please dont edit it manually, your changes might get overwritten!\n" > "$deps_file"
nuget-to-nix "$HOME/nuget_pkgs" "${exclusions}" >> "$deps_file"
echo "Succesfully wrote lockfile to: $deps_file"
'';
} // args.passthru or {};
meta = {
platforms = dotnet-sdk.meta.platforms;
} // args.meta or {};
})