From a6d02a52140017e5d10ad702620cdc6829b2ee91 Mon Sep 17 00:00:00 2001 From: Evgeny Zemtsov Date: Thu, 9 Dec 2021 18:19:59 +0100 Subject: [PATCH] buildDotnetModule: support optional nupkg packing --- doc/languages-frameworks/dotnet.section.md | 6 +++++- .../build-dotnet-module/default.nix | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/doc/languages-frameworks/dotnet.section.md b/doc/languages-frameworks/dotnet.section.md index f3d9fb875734..b7907014cf48 100644 --- a/doc/languages-frameworks/dotnet.section.md +++ b/doc/languages-frameworks/dotnet.section.md @@ -73,6 +73,7 @@ To package Dotnet applications, you can use `buildDotnetModule`. This has simila * `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. * `nugetDeps` has to be used to specify the NuGet dependency file. Unfortunately, these cannot be deterministically fetched without a lockfile. This file should be generated using `nuget-to-nix` tool, which is available in nixpkgs. +* `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`. * `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 `[]`. * `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`. @@ -83,8 +84,9 @@ To package Dotnet applications, you can use `buildDotnetModule`. This has simila * `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`. -* `dotnetTestFlags` can be used to pass flags to `dotnet test`. +* `dotnetTestFlags` can be used to pass flags to `dotnet test`. Used only if `doCheck` is set to `true`. * `dotnetInstallFlags` can be used to pass flags to `dotnet install`. +* `dotnetPackFlags` can be used to pass flags to `dotnet pack`. Used only if `packNupkg` is set to `true`. * `dotnetFlags` can be used to pass flags to all of the above phases. Here is an example `default.nix`, using some of the previously discussed arguments: @@ -107,6 +109,8 @@ buildDotnetModule rec { executables = [ "foo" ]; # This wraps "$out/lib/$pname/foo" to `$out/bin/foo`. executables = []; # Don't install any executables. + packNupkg = true; # This packs the project as "foo-0.1.nupkg" at `$out/share`. + runtimeDeps = [ ffmpeg ]; # This will wrap ffmpeg's library path into `LD_LIBRARY_PATH`. } ``` diff --git a/pkgs/build-support/build-dotnet-module/default.nix b/pkgs/build-support/build-dotnet-module/default.nix index 5178e08a9c73..637924b056a3 100644 --- a/pkgs/build-support/build-dotnet-module/default.nix +++ b/pkgs/build-support/build-dotnet-module/default.nix @@ -14,6 +14,8 @@ , dotnetTestFlags ? [] # Flags to pass to `dotnet install`. , dotnetInstallFlags ? [] +# Flags to pass to `dotnet pack`. +, dotnetPackFlags ? [] # Flags to pass to dotnet in all phases. , dotnetFlags ? [] @@ -21,6 +23,8 @@ # Unfortunately, dotnet has no method for doing this automatically. # If unset, all executables in the projects root will get installed. This may cause bloat! , executables ? null +# Packs a 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`. +, packNupkg ? false # The packages project file, which contains instructions on how to compile it. This can be an array of multiple project files as well. , projectFile ? null # The NuGet dependency file. This locks all NuGet dependency versions, as otherwise they cannot be deterministically fetched. @@ -167,7 +171,18 @@ let "''${dotnetInstallFlags[@]}" \ "''${dotnetFlags[@]}" done - '' + (if executables != null then '' + '' + (lib.optionalString packNupkg '' + for project in ''${projectFile[@]}; do + dotnet pack "$project" \ + -p:ContinuousIntegrationBuild=true \ + -p:Deterministic=true \ + --output $out/share \ + --configuration "$buildType" \ + --no-build \ + "''${dotnetPackFlags[@]}" \ + "''${dotnetFlags[@]}" + done + '') + (if executables != null then '' for executable in $executables; do execPath="$out/lib/${args.pname}/$executable"