about summary refs log tree commit diff
path: root/pkgs/build-support
diff options
context:
space:
mode:
authormdarocha <git@mdarocha.pl>2023-06-20 17:19:30 +0200
committermdarocha <git@mdarocha.pl>2023-06-20 17:20:51 +0200
commitabf6081bc2aeb6746044b3528bb5f5dd5b3f0d84 (patch)
treef7eac8b7d218ebe79d53a03a303f7cd9166defc9 /pkgs/build-support
parent6c639e869c1fa420c707724eeba107a5017df0d2 (diff)
buildDotnetModule: add useDotnetFromEnv option
This causes an alternative wrapper to be used, that takes the dotnet
runtime from the environment.
Diffstat (limited to 'pkgs/build-support')
-rw-r--r--pkgs/build-support/dotnet/build-dotnet-module/default.nix7
-rw-r--r--pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix6
-rw-r--r--pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh13
3 files changed, 22 insertions, 4 deletions
diff --git a/pkgs/build-support/dotnet/build-dotnet-module/default.nix b/pkgs/build-support/dotnet/build-dotnet-module/default.nix
index bfd249a872b13..a9c49d1e526eb 100644
--- a/pkgs/build-support/dotnet/build-dotnet-module/default.nix
+++ b/pkgs/build-support/dotnet/build-dotnet-module/default.nix
@@ -75,7 +75,10 @@
 , buildType ? "Release"
   # If set to true, builds the application as a self-contained - removing the runtime dependency on dotnet
 , selfContainedBuild ? false
-  # Whether to explicitly enable UseAppHost when building
+  # Whether to use an alternative wrapper, that executes the application DLL using the dotnet runtime from the user environment. `dotnet-runtime` is provided as a default in case no .NET is installed
+  # This is useful for .NET tools and applications that may need to run under different .NET runtimes
+, useDotnetFromEnv ? false
+  # Whether to explicitly enable UseAppHost when building. This is redundant if useDotnetFromEnv is enabledz
 , useAppHost ? true
   # The dotnet SDK to use.
 , dotnet-sdk ? dotnetCorePackages.sdk_6_0
@@ -159,7 +162,7 @@ stdenvNoCC.mkDerivation (args // {
   # gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
   dontWrapGApps = args.dontWrapGApps or true;
 
-  inherit selfContainedBuild useAppHost;
+  inherit selfContainedBuild useAppHost useDotnetFromEnv;
 
   passthru = {
     inherit nuget-source;
diff --git a/pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix b/pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix
index a72f0291a872b..99d9644f8b701 100644
--- a/pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix
+++ b/pkgs/build-support/dotnet/build-dotnet-module/hooks/default.nix
@@ -1,4 +1,7 @@
 { lib
+, stdenv
+, which
+, coreutils
 , callPackage
 , makeSetupHook
 , makeWrapper
@@ -67,6 +70,9 @@ in
       substitutions = {
         dotnetRuntime = dotnet-runtime;
         runtimeDeps = libraryPath;
+        shell = stdenv.shell;
+        which = "${which}/bin/which";
+        dirname = "${coreutils}/bin/dirname";
       };
     } ./dotnet-fixup-hook.sh) { };
 }
diff --git a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh
index 27885238ef7bf..70728e4321ede 100644
--- a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh
+++ b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh
@@ -5,13 +5,22 @@ makeWrapperArgs=( "${derivationMakeWrapperArgs[@]}" )
 # First argument is the executable you want to wrap,
 # the second is the destination for the wrapper.
 wrapDotnetProgram() {
+    local dotnetRootFlags=()
+
     if [ ! "${selfContainedBuild-}" ]; then
-        local -r dotnetRootFlag=("--set" "DOTNET_ROOT" "@dotnetRuntime@")
+        if [ "${useDotnetFromEnv-}" ]; then
+            # if dotnet CLI is available, set DOTNET_ROOT based on it. Otherwise set to default .NET runtime
+            dotnetRootFlags+=("--run" 'command -v dotnet &>/dev/null && export DOTNET_ROOT="$(@dirname@ "$(@dirname@ "$(@which@ dotnet)")")" || export DOTNET_ROOT="@dotnetRuntime@"')
+            dotnetRootFlags+=("--suffix" "PATH" ":" "@dotnetRuntime@/bin")
+        else
+            dotnetRootFlags+=("--set" "DOTNET_ROOT" "@dotnetRuntime@")
+            dotnetRootFlags+=("--prefix" "PATH" ":" "@dotnetRuntime@/bin")
+        fi
     fi
 
     makeWrapper "$1" "$2" \
         --suffix "LD_LIBRARY_PATH" : "@runtimeDeps@" \
-        "${dotnetRootFlag[@]}" \
+        "${dotnetRootFlags[@]}" \
         "${gappsWrapperArgs[@]}" \
         "${makeWrapperArgs[@]}"