diff --git a/doc/languages-frameworks/dotnet.section.md b/doc/languages-frameworks/dotnet.section.md index 4c245a7544e..0927ce9146f 100644 --- a/doc/languages-frameworks/dotnet.section.md +++ b/doc/languages-frameworks/dotnet.section.md @@ -71,7 +71,7 @@ The `dotnetCorePackages.sdk` contains both a runtime and the full sdk of a given To package Dotnet applications, you can use `buildDotnetModule`. This has similar arguments to `stdenv.mkDerivation`, with the following additions: -* `projectFile` has to be used for specifying the dotnet project file relative to the source root. These usually have `.sln` or `.csproj` file extensions. This can be an array of multiple projects as well. +* `projectFile` is used for specifying the dotnet project file relative to the source root. These usually have `.sln` or `.csproj` file extensions. This can be an array of multiple projects as well. * `nugetDeps` takes either a path to a `deps.nix` file, or a derivation. The `deps.nix` file can be generated using the script attached to `passthru.fetch-deps`. This file can also be generated manually using `nuget-to-nix` tool, which is available in nixpkgs. If the argument is a derivation, it will be used directly and assume it has the same output as `mkNugetDeps`. * `packNupkg` is used to pack project as a `nupkg`, and installs it to `$out/share`. If set to `true`, the derivation can be used as a dependency for another dotnet project by adding it to `projectReferences`. * `projectReferences` can be used to resolve `ProjectReference` project items. Referenced projects can be packed with `buildDotnetModule` by setting the `packNupkg = true` attribute and passing a list of derivations to `projectReferences`. Since we are sharing referenced projects as NuGets they must be added to csproj/fsproj files as `PackageReference` as well. diff --git a/pkgs/build-support/dotnet/build-dotnet-module/default.nix b/pkgs/build-support/dotnet/build-dotnet-module/default.nix index b321c8eb10c..569ad2615e6 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/default.nix +++ b/pkgs/build-support/dotnet/build-dotnet-module/default.nix @@ -82,8 +82,6 @@ , ... } @ args: -assert projectFile == null -> throw "Defining the `projectFile` attribute is required. This is usually an `.csproj`, or `.sln` file."; - # TODO: Automatically generate a dependency file when a lockfile is present. # This file is unfortunately almost never present, as Microsoft recommands not to push this in upstream repositories. assert nugetDeps == null -> throw "Defining the `nugetDeps` attribute is required, as to lock the NuGet dependencies. This file can be generated by running the `passthru.fetch-deps` script."; @@ -159,8 +157,6 @@ stdenvNoCC.mkDerivation (args // { # 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. @@ -171,6 +167,7 @@ stdenvNoCC.mkDerivation (args // { in builtins.filter (flag: !(hasRid flag)) (dotnetFlags ++ dotnetRestoreFlags); + runtimeIds = map (system: dotnet-sdk.systemToDotnetRid system) (args.meta.platforms or dotnet-sdk.meta.platforms); in writeShellScript "fetch-${pname}-deps" '' set -euo pipefail @@ -202,15 +199,27 @@ stdenvNoCC.mkDerivation (args // { export DOTNET_NOLOGO=1 export DOTNET_CLI_TELEMETRY_OPTOUT=1 + declare -a projectFiles=( ${toString (lib.toList projectFile)} ) + declare -a testProjectFiles=( ${toString (lib.toList testProjectFile)} ) + + dotnetRestore() { + local -r project="''${1-}" + local -r rid="''${2-}" + + 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)} + } + 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)} + (( ''${#projectFiles[@]} == 0 )) && dotnetRestore "" "$rid" + + for project in ''${projectFiles[@]-} ''${testProjectFiles[@]-}; do + dotnetRestore "$project" "$rid" done done diff --git a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-build-hook.sh b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-build-hook.sh index 3e6f2bd8465..9199b8d02a8 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-build-hook.sh +++ b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-build-hook.sh @@ -24,19 +24,25 @@ dotnetBuildHook() { local -r versionFlag="-p:Version=${version-}" fi + dotnetBuild() { + local -r project="${1-}" + env dotnet build ${project-} \ + -maxcpucount:$maxCpuFlag \ + -p:BuildInParallel=$parallelBuildFlag \ + -p:ContinuousIntegrationBuild=true \ + -p:Deterministic=true \ + -p:UseAppHost=true \ + --configuration "@buildType@" \ + --no-restore \ + ${versionFlag-} \ + ${dotnetBuildFlags[@]} \ + ${dotnetFlags[@]} + } + + (( "${#projectFile[@]}" == 0 )) && dotnetBuild + for project in ${projectFile[@]} ${testProjectFile[@]-}; do - env \ - dotnet build "$project" \ - -maxcpucount:$maxCpuFlag \ - -p:BuildInParallel=$parallelBuildFlag \ - -p:ContinuousIntegrationBuild=true \ - -p:Deterministic=true \ - -p:UseAppHost=true \ - --configuration "@buildType@" \ - --no-restore \ - ${versionFlag-} \ - ${dotnetBuildFlags[@]} \ - ${dotnetFlags[@]} + dotnetBuild "$project" done runHook postBuild diff --git a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-configure-hook.sh b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-configure-hook.sh index 5528f0e07a3..10067cf0882 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-configure-hook.sh +++ b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-configure-hook.sh @@ -13,15 +13,21 @@ dotnetConfigureHook() { local -r parallelFlag="--disable-parallel" fi + dotnetRestore() { + local -r project="${1-}" + env dotnet restore ${project-} \ + -p:ContinuousIntegrationBuild=true \ + -p:Deterministic=true \ + --source "@nugetSource@/lib" \ + ${parallelFlag-} \ + ${dotnetRestoreFlags[@]} \ + ${dotnetFlags[@]} + } + + (( "${#projectFile[@]}" == 0 )) && dotnetRestore + for project in ${projectFile[@]} ${testProjectFile[@]-}; do - env \ - dotnet restore "$project" \ - -p:ContinuousIntegrationBuild=true \ - -p:Deterministic=true \ - --source "@nugetSource@/lib" \ - ${parallelFlag-} \ - ${dotnetRestoreFlags[@]} \ - ${dotnetFlags[@]} + dotnetRestore "$project" done runHook postConfigure diff --git a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-install-hook.sh b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-install-hook.sh index fd88ea32ec0..217e79e41b4 100644 --- a/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-install-hook.sh +++ b/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-install-hook.sh @@ -12,31 +12,47 @@ dotnetInstallHook() { dotnetInstallFlags+=("--no-self-contained") fi - for project in ${projectFile[@]}; do - env \ - dotnet publish "$project" \ - -p:ContinuousIntegrationBuild=true \ - -p:Deterministic=true \ - -p:UseAppHost=true \ - --output "$out/lib/${pname}" \ - --configuration "@buildType@" \ - --no-build \ - ${dotnetInstallFlags[@]} \ - ${dotnetFlags[@]} - done + dotnetPublish() { + local -r project="${1-}" + env dotnet publish ${project-} \ + -p:ContinuousIntegrationBuild=true \ + -p:Deterministic=true \ + -p:UseAppHost=true \ + --output "$out/lib/${pname}" \ + --configuration "@buildType@" \ + --no-build \ + ${dotnetInstallFlags[@]} \ + ${dotnetFlags[@]} + } + + dotnetPack() { + local -r project="${1-}" + env dotnet pack ${project-} \ + -p:ContinuousIntegrationBuild=true \ + -p:Deterministic=true \ + --output "$out/share" \ + --configuration "@buildType@" \ + --no-build \ + ${dotnetPackFlags[@]} \ + ${dotnetFlags[@]} + } + + if (( "${#projectFile[@]}" == 0 )); then + dotnetPublish + else + for project in ${projectFile[@]}; do + dotnetPublish "$project" + done + fi if [[ "${packNupkg-}" ]]; then - for project in ${projectFile[@]}; do - env \ - dotnet pack "$project" \ - -p:ContinuousIntegrationBuild=true \ - -p:Deterministic=true \ - --output "$out/share" \ - --configuration "@buildType@" \ - --no-build \ - ${dotnetPackFlags[@]} \ - ${dotnetFlags[@]} - done + if (( "${#projectFile[@]}" == 0 )); then + dotnetPack + else + for project in ${projectFile[@]}; do + dotnetPack "$project" + done + fi fi runHook postInstall