From efd92e7fd779a5636489621f0bafc309cecf59db Mon Sep 17 00:00:00 2001 From: Ivar Scholten Date: Thu, 18 Aug 2022 23:24:38 +0200 Subject: 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 --- .../dotnet/build-dotnet-module/default.nix | 87 ++++++++++++++++------ 1 file changed, 66 insertions(+), 21 deletions(-) (limited to 'pkgs/build-support/dotnet/build-dotnet-module/default.nix') diff --git a/pkgs/build-support/dotnet/build-dotnet-module/default.nix b/pkgs/build-support/dotnet/build-dotnet-module/default.nix index bc50f1bd090..2a166089bae 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/default.nix +++ b/pkgs/build-support/dotnet/build-dotnet-module/default.nix @@ -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 ` 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]}")" + export PATH="${lib.makeBinPath [ coreutils dotnet-sdk nuget-to-nix ]}" - export HOME=$(mktemp -d) - deps_file="''${1:-/tmp/${pname}-deps.nix}" + case "''${1-}" in + --help|-h) + echo "usage: $0 [--help]" + echo " The path to write the lockfile to" + echo " --help Show this help message" + exit + ;; + esac + + 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 {}; }) -- cgit 1.4.1