Skip to content

Commit

Permalink
buildDotnetModule: add support for using combinePackages as dotnet-sdk
Browse files Browse the repository at this point in the history
This allows packages that require several dotnet versions to build (like
BeatSaberModManager) to properly depend on the dotnet-sdk specific deps.
This in turns avoids having to regenerate the deps of those packages
after each dotnet-sdk update.

This also changes nuget-to-nix to accept a file with a list of
exclusions instead of a folder.
  • Loading branch information
mdarocha committed Mar 19, 2023
1 parent 26ff0e4 commit d093086
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/languages-frameworks/dotnet.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ To package Dotnet applications, you can use `buildDotnetModule`. This has simila
* `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.
* `dotnet-sdk` is useful in cases where you need to change what dotnet SDK is being used.
* `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.
Expand Down
17 changes: 12 additions & 5 deletions pkgs/build-support/dotnet/build-dotnet-module/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ let
# this contains all the nuget packages that are implicitly referenced by the dotnet
# build system. having them as separate deps allows us to avoid having to regenerate
# a packages dependencies when the dotnet-sdk version changes
sdkDeps = dotnet-sdk.packages;
sdkDeps = lib.lists.flatten [ dotnet-sdk.packages ];

sdkSource = mkNugetSource {
name = "dotnet-sdk-${dotnet-sdk.version}-source";
deps = [ sdkDeps ];
sdkSource = let
version = dotnet-sdk.version or (lib.concatStringsSep "-" dotnet-sdk.versions);
in mkNugetSource {
name = "dotnet-sdk-${version}-source";
deps = sdkDeps;
};

nuget-source = symlinkJoin {
Expand Down Expand Up @@ -271,7 +273,12 @@ stdenvNoCC.mkDerivation (args // {
echo "Writing lockfile..."
echo -e "# This file was automatically generated by passthru.fetch-deps.\n# Please dont edit it manually, your changes might get overwritten!\n" > "$depsFile"
nuget-to-nix "$tmp/nuget_pkgs" "${sdkDeps}" >> "$depsFile"
excluded_sources="${lib.concatStringsSep " " sdkDeps}"
for excluded_source in ''${excluded_sources[@]}; do
ls "$excluded_source" >> "$tmp/excluded_list"
done
nuget-to-nix "$tmp/nuget_pkgs" "$tmp/excluded_list" >> "$depsFile"
echo "Succesfully wrote lockfile to $depsFile"
'';
} // args.passthru or { };
Expand Down
7 changes: 4 additions & 3 deletions pkgs/build-support/dotnet/nuget-to-nix/nuget-to-nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ export PATH="@binPath@"
export LC_ALL=C

if [ $# -eq 0 ]; then
>&2 echo "Usage: $0 <packages directory> [path to excluded package source] > deps.nix"
>&2 echo "Usage: $0 <packages directory> [path to a file with a list of excluded packages] > deps.nix"
exit 1
fi

pkgs=$1
tmp=$(realpath "$(mktemp -td nuget-to-nix.XXXXXX)")
trap 'rm -r "$tmp"' EXIT
excluded_source=$(realpath "${2:-$tmp/empty}")

excluded_list=$(realpath "${2:-/dev/null}")

export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1
Expand All @@ -37,7 +38,7 @@ for package in *; do
for version in *; do
id=$(xq -r .package.metadata.id "$version/$package".nuspec)

if [[ -e "$excluded_source/$id.$version".nupkg ]]; then
if grep -qxF "$id.$version.nupkg" "$excluded_list"; then
continue
fi

Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/compilers/dotnet/build-dotnet.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ let
sdk = ".NET SDK ${version}";
};

packageDeps = mkNugetDeps {
packageDeps = if type == "sdk" then mkNugetDeps {
name = "${pname}-${version}-deps";
nugetDeps = packages;
};
} else null;

in
stdenv.mkDerivation (finalAttrs: rec {
Expand Down
15 changes: 10 additions & 5 deletions pkgs/development/compilers/dotnet/combine-packages.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
packages:
dotnetPackages:
{ buildEnv, makeWrapper, lib }:
# TODO: Rethink how we determine and/or get the CLI.
# Possible options raised in #187118:
# 1. A separate argument for the CLI (as suggested by IvarWithoutBones
# 2. Use the highest version SDK for the CLI (as suggested by GGG)
# 3. Something else?
let cli = builtins.head packages;
let cli = builtins.head dotnetPackages;
in
assert lib.assertMsg ((builtins.length packages) > 0)
assert lib.assertMsg ((builtins.length dotnetPackages) > 0)
''You must include at least one package, e.g
`with dotnetCorePackages; combinePackages [
sdk_3_1 aspnetcore_5_0
];`'' ;
buildEnv {
name = "dotnet-core-combined";
paths = packages;
paths = dotnetPackages;
pathsToLink = [ "/host" "/packs" "/sdk" "/sdk-manifests" "/shared" "/templates" ];
ignoreCollisions = true;
nativeBuildInputs = [
Expand All @@ -29,6 +29,11 @@ assert lib.assertMsg ((builtins.length packages) > 0)
--prefix LD_LIBRARY_PATH : ${cli.icu}/lib
'';
passthru = {
inherit (cli) icu packages;
inherit (cli) icu;

versions = lib.catAttrs "version" dotnetPackages;
packages = lib.remove null (lib.catAttrs "packages" dotnetPackages);
};

inherit (cli) meta;
}

0 comments on commit d093086

Please sign in to comment.