buildDotnetModule: pass runtimeId whenever possible and disable trimming when not allowed.

This fixes up some build errors
This commit is contained in:
mdarocha 2023-06-21 17:06:26 +02:00
parent d6b7d2bac8
commit c51141d997
5 changed files with 30 additions and 6 deletions

View file

@ -92,7 +92,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` is used for specifying the dotnet project file, relative to the source root. These usually have `.sln` or `.csproj` file extensions. This can be a list of multiple projects as well. Most of the time dotnet can figure this location out by itself, so this should only be set if necessary.
* `projectFile` is used for specifying the dotnet project file, relative to the source root. These have `.sln` (entire solution) or `.csproj` (single project) file extensions. This can be a list of multiple projects as well. When omitted, will attempt to find and build the solution (`.sln`). If running into problems, make sure to set it to a file (or a list of files) with the `.csproj` extension - building applications as entire solutions is not fully supported by the .NET CLI.
* `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.
@ -108,13 +108,13 @@ To package Dotnet applications, you can use `buildDotnetModule`. This has simila
* `executables` is used to specify which executables get wrapped to `$out/bin`, relative to `$out/lib/$pname`. If this is unset, all executables generated will get installed. If you do not want to install any, set this to `[]`. This gets done in the `preFixup` phase.
* `runtimeDeps` is used to wrap libraries into `LD_LIBRARY_PATH`. This is how dotnet usually handles runtime dependencies.
* `buildType` is used to change the type of build. Possible values are `Release`, `Debug`, etc. By default, this is set to `Release`.
* `selfContainedBuild` allows to enable the [self-contained](https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained) build flag. By default, it is set to false and generated applications have a dependency on the selected dotnet runtime. If enabled, the dotnet runtime is bundled into the executable and the built app has no dependency on Dotnet.
* `selfContainedBuild` allows to enable the [self-contained](https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained) build flag. By default, it is set to false and generated applications have a dependency on the selected dotnet runtime. If enabled, the dotnet runtime is bundled into the executable and the built app has no dependency on .NET.
* `useAppHost` will enable creation of a binary executable that runs the .NET application using the specified root. More info in [Microsoft docs](https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-framework-dependent). Enabled by default.
* `useDotnetFromEnv` will change the binary wrapper so that it uses the .NET from the environment. The runtime specified by `dotnet-runtime` is given as a fallback in case no .NET is installed in the user's environment. This is most useful for .NET global tools and LSP servers, which often extend the .NET CLI and their runtime should match the users' .NET runtime.
* `dotnet-sdk` is useful in cases where you need to change what dotnet SDK is being used. You can also set this to the result of `dotnetSdkPackages.combinePackages`, if the project uses multiple SDKs to build.
* `dotnet-runtime` is useful in cases where you need to change what dotnet runtime is being used. This can be either a regular dotnet runtime, or an aspnetcore.
* `dotnet-test-sdk` is useful in cases where unit tests expect a different dotnet SDK. By default, this is set to the `dotnet-sdk` attribute.
* `testProjectFile` is useful in cases where the regular project file does not contain the unit tests. It gets restored and build, but not installed. You may need to regenerate your nuget lockfile after setting this.
* `testProjectFile` is useful in cases where the regular project file does not contain the unit tests. It gets restored and build, but not installed. You may need to regenerate your nuget lockfile after setting this. Note that if set, only tests from this project are executed.
* `disabledTests` is used to disable running specific unit tests. This gets passed as: `dotnet test --filter "FullyQualifiedName!={}"`, to ensure compatibility with all unit test frameworks.
* `dotnetRestoreFlags` can be used to pass flags to `dotnet restore`.
* `dotnetBuildFlags` can be used to pass flags to `dotnet build`.

View file

@ -44,7 +44,7 @@ in
name = "dotnet-check-hook";
propagatedBuildInputs = [ dotnet-test-sdk ];
substitutions = {
inherit buildType libraryPath;
inherit buildType runtimeId libraryPath;
disabledTests = lib.optionalString (disabledTests != [])
(let
escapedNames = lib.lists.map (n: lib.replaceStrings [","] ["%2C"] n) disabledTests;

View file

@ -15,7 +15,7 @@ dotnetBuildHook() {
fi
if [ "${selfContainedBuild-}" ]; then
dotnetBuildFlags+=(--runtime "@runtimeId@" "-p:SelfContained=true")
dotnetBuildFlags+=("-p:SelfContained=true")
else
dotnetBuildFlags+=("-p:SelfContained=false")
fi
@ -30,6 +30,12 @@ dotnetBuildHook() {
dotnetBuild() {
local -r project="${1-}"
runtimeIdFlags=()
if [[ "$project" == *.csproj ]] || [ "${selfContainedBuild-}" ]; then
runtimeIdFlags+=("--runtime @runtimeId@")
fi
env dotnet build ${project-} \
-maxcpucount:$maxCpuFlag \
-p:BuildInParallel=$parallelBuildFlag \
@ -38,6 +44,7 @@ dotnetBuildHook() {
--configuration "@buildType@" \
--no-restore \
${versionFlag-} \
${runtimeIdFlags[@]} \
${dotnetBuildFlags[@]} \
${dotnetFlags[@]}
}

View file

@ -17,6 +17,11 @@ dotnetCheckHook() {
fi
for project in ${testProjectFile[@]-${projectFile[@]}}; do
runtimeIdFlags=()
if [[ "$project" == *.csproj ]]; then
runtimeIdFlags=("--runtime @runtimeId@")
fi
env "LD_LIBRARY_PATH=@libraryPath@" \
dotnet test "$project" \
-maxcpucount:$maxCpuFlag \
@ -26,6 +31,7 @@ dotnetCheckHook() {
--no-build \
--logger "console;verbosity=normal" \
${disabledTestsFlag-} \
${runtimeIdFlags[@]} \
"${dotnetTestFlags[@]}" \
"${dotnetFlags[@]}"
done

View file

@ -7,9 +7,12 @@ dotnetInstallHook() {
runHook preInstall
if [ "${selfContainedBuild-}" ]; then
dotnetInstallFlags+=(--runtime "@runtimeId@" "--self-contained")
dotnetInstallFlags+=("--self-contained")
else
dotnetInstallFlags+=("--no-self-contained")
# https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained
# Trimming is only available for self-contained build, so force disable it here
dotnetInstallFlags+=("-p:PublishTrimmed=false")
fi
if [ "${useAppHost-}" ]; then
@ -18,12 +21,19 @@ dotnetInstallHook() {
dotnetPublish() {
local -r project="${1-}"
runtimeIdFlags=()
if [[ "$project" == *.csproj ]] || [ "${selfContainedBuild-}" ]; then
runtimeIdFlags+=("--runtime @runtimeId@")
fi
env dotnet publish ${project-} \
-p:ContinuousIntegrationBuild=true \
-p:Deterministic=true \
--output "$out/lib/${pname}" \
--configuration "@buildType@" \
--no-build \
${runtimeIdFlags[@]} \
${dotnetInstallFlags[@]} \
${dotnetFlags[@]}
}
@ -36,6 +46,7 @@ dotnetInstallHook() {
--output "$out/share" \
--configuration "@buildType@" \
--no-build \
--runtime "@runtimeId@" \
${dotnetPackFlags[@]} \
${dotnetFlags[@]}
}