From f820d2cf3c1df810ba1cfe12a77018fcb5a81043 Mon Sep 17 00:00:00 2001 From: David McFarland Date: Sat, 7 Sep 2024 22:36:21 -0300 Subject: [PATCH] dotnetCorePackages: split fetch-deps logic into addNuGetDeps --- .../dotnet/add-nuget-deps/default.nix | 86 ++++++++++++ .../fetch-deps.sh | 10 +- .../dotnet/build-dotnet-module/default.nix | 122 ++++++------------ pkgs/development/compilers/dotnet/default.nix | 1 + 4 files changed, 131 insertions(+), 88 deletions(-) create mode 100644 pkgs/build-support/dotnet/add-nuget-deps/default.nix rename pkgs/build-support/dotnet/{build-dotnet-module => add-nuget-deps}/fetch-deps.sh (80%) diff --git a/pkgs/build-support/dotnet/add-nuget-deps/default.nix b/pkgs/build-support/dotnet/add-nuget-deps/default.nix new file mode 100644 index 000000000000..afa68b0b92b5 --- /dev/null +++ b/pkgs/build-support/dotnet/add-nuget-deps/default.nix @@ -0,0 +1,86 @@ +{ + writeShellScript, + runtimeShell, + nix, + lib, + substituteAll, + nuget-to-nix, + cacert, + fetchNupkg, + callPackage, +}: + +{ + nugetDeps, + overrideFetchAttrs ? x: { }, +}: +fnOrAttrs: finalAttrs: +let + attrs = if builtins.isFunction fnOrAttrs then fnOrAttrs finalAttrs else fnOrAttrs; + + deps = + if (nugetDeps != null) then + if lib.isDerivation nugetDeps then + [ nugetDeps ] + else if lib.isList nugetDeps then + nugetDeps + else + assert (lib.isPath nugetDeps); + callPackage nugetDeps { fetchNuGet = fetchNupkg; } + else + [ ]; + + finalPackage = finalAttrs.finalPackage; + +in +attrs +// { + buildInputs = attrs.buildInputs or [ ] ++ deps; + + passthru = + attrs.passthru or { } + // { + nugetDeps = deps; + } + // lib.optionalAttrs (nugetDeps == null || lib.isPath nugetDeps) { + fetch-deps = + let + pkg' = finalPackage.overrideAttrs (old: { + buildInputs = attrs.buildInputs or [ ]; + keepNugetConfig = true; + dontBuild = true; + doCheck = false; + dontInstall = true; + doInstallCheck = false; + dontFixup = true; + doDist = false; + }); + + pkg'' = pkg'.overrideAttrs overrideFetchAttrs; + + drv = builtins.unsafeDiscardOutputDependency pkg''.drvPath; + + innerScript = substituteAll { + src = ./fetch-deps.sh; + isExecutable = true; + inherit cacert; + defaultDepsFile = + # Wire in the depsFile such that running the script with no args + # runs it agains the correct deps file by default. + # Note that toString is necessary here as it results in the path at + # eval time (i.e. to the file in your local Nixpkgs checkout) rather + # than the Nix store path of the path after it's been imported. + if lib.isPath nugetDeps && !lib.isStorePath nugetDeps then + toString nugetDeps + else + ''$(mktemp -t "${finalAttrs.pname or finalPackage.name}-deps-XXXXXX.nix")''; + nugetToNix = nuget-to-nix; + }; + + in + writeShellScript "${finalPackage.name}-fetch-deps" '' + NIX_BUILD_SHELL="${runtimeShell}" exec ${nix}/bin/nix-shell \ + --pure --run 'source "${innerScript}"' "${drv}" + ''; + }; +} diff --git a/pkgs/build-support/dotnet/build-dotnet-module/fetch-deps.sh b/pkgs/build-support/dotnet/add-nuget-deps/fetch-deps.sh similarity index 80% rename from pkgs/build-support/dotnet/build-dotnet-module/fetch-deps.sh rename to pkgs/build-support/dotnet/add-nuget-deps/fetch-deps.sh index ff971ce1e592..92c9ff9d6444 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/fetch-deps.sh +++ b/pkgs/build-support/dotnet/add-nuget-deps/fetch-deps.sh @@ -8,13 +8,9 @@ export TMPDIR="$tmp/.tmp" mkdir "$HOME" "$TMPDIR" cd "$tmp" -phases=" - ${prePhases[*]:-} - unpackPhase - patchPhase - ${preConfigurePhases[*]:-} - configurePhase -" genericBuild +export NIX_SSL_CERT_FILE=@cacert@/etc/ssl/certs/ca-bundle.crt + +genericBuild depsFile=$(realpath "${1:-@defaultDepsFile@}") tmpFile="$tmp"/deps.nix diff --git a/pkgs/build-support/dotnet/build-dotnet-module/default.nix b/pkgs/build-support/dotnet/build-dotnet-module/default.nix index 25dd4f777c77..34bac2c03686 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/default.nix +++ b/pkgs/build-support/dotnet/build-dotnet-module/default.nix @@ -7,12 +7,10 @@ writeShellScript, makeWrapper, dotnetCorePackages, - fetchNupkg, - nuget-to-nix, cacert, unzip, yq, - nix, + addNuGetDeps, }: let transformArgs = @@ -109,39 +107,9 @@ let dotnetFixupHook ; - _nugetDeps = - if (nugetDeps != null) then - if lib.isDerivation nugetDeps then - [ nugetDeps ] - else if lib.isList nugetDeps then - nugetDeps - else - assert (lib.isPath nugetDeps); - callPackage nugetDeps { fetchNuGet = fetchNupkg; } - else - [ ]; - - nugetDepsFile = if lib.isPath nugetDeps then nugetDeps else null; - inherit (dotnetCorePackages) systemToDotnetRid; in - # Not all args need to be passed through to mkDerivation - # TODO: We should probably filter out even more attrs - removeAttrs args [ - "nugetDeps" - "installPath" - "executables" - "projectFile" - "projectReferences" - "runtimeDeps" - "runtimeId" - "disabledTests" - "testProjectFile" - "buildType" - "selfContainedBuild" - "useDotnet" - "useAppHost" - ] + args // { dotnetInstallPath = installPath; dotnetExecutables = executables; @@ -167,6 +135,8 @@ let dotnetFlags packNupkg useDotnetFromEnv + nugetDeps + runtimeId ; nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [ @@ -183,7 +153,12 @@ let yq ]; - buildInputs = args.buildInputs or [ ] ++ [ dotnet-sdk.packages ] ++ _nugetDeps ++ projectReferences; + buildInputs = + args.buildInputs or [ ] + ++ [ + dotnet-sdk.packages + ] + ++ projectReferences; # Parse the version attr into a format acceptable for the Version msbuild property # The actual version attr is saved in InformationalVersion, which accepts an arbitrary string @@ -223,60 +198,45 @@ let # executables propagatedSandboxProfile = toString dotnet-runtime.__propagatedSandboxProfile; - passthru = - { - nugetDeps = _nugetDeps; - } - // lib.optionalAttrs (nugetDeps == null || lib.isPath nugetDeps) { - fetch-deps = - let - pkg = finalAttrs.finalPackage.overrideAttrs ( - old: - { - buildInputs = lib.subtractLists _nugetDeps old.buildInputs; - keepNugetConfig = true; - } - // lib.optionalAttrs (runtimeId == null) { - dotnetRuntimeIds = map (system: systemToDotnetRid system) platforms; - } - ); - - drv = builtins.unsafeDiscardOutputDependency pkg.drvPath; - - innerScript = substituteAll { - src = ./fetch-deps.sh; - isExecutable = true; - defaultDepsFile = - # Wire in the nugetDeps file such that running the script with no args - # runs it agains the correct deps file by default. - # Note that toString is necessary here as it results in the path at - # eval time (i.e. to the file in your local Nixpkgs checkout) rather - # than the Nix store path of the path after it's been imported. - if lib.isPath nugetDeps && !lib.isStorePath nugetDepsFile then - toString nugetDepsFile - else - ''$(mktemp -t "${finalAttrs.pname or finalAttrs.finalPackage.name}-deps-XXXXXX.nix")''; - nugetToNix = (nuget-to-nix.override { inherit dotnet-sdk; }); - }; - - in - writeShellScript "${finalAttrs.finalPackage.name}-fetch-deps" '' - NIX_BUILD_SHELL="${runtimeShell}" exec ${nix}/bin/nix-shell \ - --pure --run 'source "${innerScript}"' "${drv}" - ''; - } - // args.passthru or { }; - meta = (args.meta or { }) // { inherit platforms; }; }; + in fnOrAttrs: stdenvNoCC.mkDerivation ( finalAttrs: let - args = if lib.isFunction fnOrAttrs then fnOrAttrs (args // finalAttrs) else fnOrAttrs; + args = if lib.isFunction fnOrAttrs then fnOrAttrs (args' // finalAttrs) else fnOrAttrs; + args' = transformArgs finalAttrs args; + inherit (args') nugetDeps runtimeId meta; + args'' = removeAttrs args' [ + "nugetDeps" + "runtimeId" + "installPath" + "executables" + "projectFile" + "projectReferences" + "runtimeDeps" + "runtimeId" + "disabledTests" + "testProjectFile" + "buildType" + "selfContainedBuild" + "useDotnet" + "useAppHost" + ]; in - transformArgs finalAttrs args + if nugetDeps != null then + addNuGetDeps { + inherit nugetDeps; + overrideFetchAttrs = + a: + lib.optionalAttrs ((args'.runtimeId or null) == null) { + dotnetRuntimeIds = map (system: dotnetCorePackages.systemToDotnetRid system) meta.platforms; + }; + } args'' finalAttrs + else + args'' ) diff --git a/pkgs/development/compilers/dotnet/default.nix b/pkgs/development/compilers/dotnet/default.nix index 1a2fdc7f8dd1..0c6999471c48 100644 --- a/pkgs/development/compilers/dotnet/default.nix +++ b/pkgs/development/compilers/dotnet/default.nix @@ -56,6 +56,7 @@ makeScopeWithSplicing' { mkNugetSource = callPackage ../../../build-support/dotnet/make-nuget-source { }; mkNugetDeps = callPackage ../../../build-support/dotnet/make-nuget-deps { }; + addNuGetDeps = callPackage ../../../build-support/dotnet/add-nuget-deps { }; fetchNupkg = callPackage ../../../build-support/dotnet/fetch-nupkg { }; dotnet_8 = recurseIntoAttrs (callPackage ./8 { bootstrapSdk = dotnet_8_0.sdk_8_0_1xx; });