diff --git a/doc/languages-frameworks/dotnet.section.md b/doc/languages-frameworks/dotnet.section.md index 41f7f380f3ef..23e3be414573 100644 --- a/doc/languages-frameworks/dotnet.section.md +++ b/doc/languages-frameworks/dotnet.section.md @@ -99,9 +99,9 @@ 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 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`. For compatibility, if the argument is a list of derivations, they will be added to `buildInputs`. +* `nugetDeps` should be a path to a JSON file, a path to a nix file (deprecated), a derivation, or a list of derivations. A `deps.json` file can be generated using the script attached to `passthru.fetch-deps`, which is the preferred method. All `nugetDeps` packages are added to `buildInputs`. ::: {.note} -For more detail about managing the `deps.nix` file, see [Generating and updating NuGet dependencies](#generating-and-updating-nuget-dependencies) +For more detail about managing the `deps.json` file, see [Generating and updating NuGet dependencies](#generating-and-updating-nuget-dependencies) ::: * `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 `buildInputs`. @@ -133,7 +133,7 @@ For more detail about managing the `deps.nix` file, see [Generating and updating * `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. -When packaging a new application, you need to fetch its dependencies. Create an empty `deps.nix`, set `nugetDeps = ./deps.nix`, then run `nix-build -A package.fetch-deps` to generate a script that will build the lockfile for you. +When packaging a new application, you need to fetch its dependencies. Create an empty `deps.json`, set `nugetDeps = ./deps.json`, then run `nix-build -A package.fetch-deps` to generate a script that will build the lockfile for you. Here is an example `default.nix`, using some of the previously discussed arguments: ```nix @@ -148,7 +148,7 @@ in buildDotnetModule rec { src = ./.; projectFile = "src/project.sln"; - nugetDeps = ./deps.nix; # see "Generating and updating NuGet dependencies" section for details + nugetDeps = ./deps.json; # see "Generating and updating NuGet dependencies" section for details buildInputs = [ referencedProject ]; # `referencedProject` must contain `nupkg` in the folder structure. @@ -225,7 +225,7 @@ buildDotnetGlobalTool { ## Generating and updating NuGet dependencies {#generating-and-updating-nuget-dependencies} When writing a new expression, you can use the generated `fetch-deps` script to initialise the lockfile. -After setting `nugetDeps` to the desired location of the lockfile (e.g. `./deps.nix`), +After setting `nugetDeps` to the desired location of the lockfile (e.g. `./deps.json`), build the script with `nix-build -A package.fetch-deps` and then run the result. (When the root attr is your package, it's simply `nix-build -A fetch-deps`.) @@ -236,31 +236,54 @@ the upstream repository and you are inside it. ```bash $ dotnet restore --packages out Determining projects to restore... - Restored /home/lychee/Celeste64/Celeste64.csproj (in 1.21 sec). + Restored /home/ggg/git-credential-manager/src/shared/Git-Credential-Manager/Git-Credential-Manager.csproj (in 1.21 sec). ``` -Next, use `nuget-to-nix` tool provided in nixpkgs to generate a lockfile to `deps.nix` from +Next, use `nuget-to-json` tool provided in nixpkgs to generate a lockfile to `deps.json` from the packages inside the `out` directory. ```bash -$ nuget-to-nix out > deps.nix +$ nuget-to-json out > deps.json ``` -Which `nuget-to-nix` will generate an output similar to below -```nix -{ fetchNuGet }: [ - (fetchNuGet { pname = "FosterFramework"; version = "0.1.15-alpha"; hash = "sha256-lM6eYgOGjl1fx6WFD7rnRi/YAQieM0mx60h0p5dr+l8="; }) - (fetchNuGet { pname = "Microsoft.AspNetCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-QbUQXjCzr8j8u/5X0af9jE++EugdoxMhT08F49MZX74="; }) - (fetchNuGet { pname = "Microsoft.NET.ILLink.Tasks"; version = "8.0.1"; hash = "sha256-SopZpGaZ48/8dpUwDFDM3ix+g1rP4Yqs1PGuzRp+K7c="; }) - (fetchNuGet { pname = "Microsoft.NETCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-jajBI5GqG2IIcsIMgxTHfXbMapoXrZGl/EEhShwYq7w="; }) - (fetchNuGet { pname = "SharpGLTF.Core"; version = "1.0.0-alpha0031"; hash = "sha256-Bs4baD5wNIH6wAbGK4Xaem0i3luQkOQs37izBWdFx1I="; }) - (fetchNuGet { pname = "SharpGLTF.Runtime"; version = "1.0.0-alpha0031"; hash = "sha256-TwJO6b8ubmwBQh6NyHha8+JT5zHDJ4dROBbsEbUaa1M="; }) - (fetchNuGet { pname = "Sledge.Formats"; version = "1.2.2"; hash = "sha256-0Ddhuwpu3wwIzA4NuPaEVdMkx6tUukh8uKD6nKoxFPg="; }) - (fetchNuGet { pname = "Sledge.Formats.Map"; version = "1.1.5"; hash = "sha256-hkYJ2iWIz7vhPWlDOw2fvTenlh+4/D/37Z71tCEwnK8="; }) - (fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.5.0"; hash = "sha256-qdSTIFgf2htPS+YhLGjAGiLN8igCYJnCCo6r78+Q+c8="; }) +Which `nuget-to-json` will generate an output similar to below +```json +[ + { + "pname": "Avalonia", + "version": "11.1.3", + "hash": "sha256-kz+k/vkuWoL0XBvRT8SadMOmmRCFk9W/J4k/IM6oYX0=" + }, + { + "pname": "Avalonia.Angle.Windows.Natives", + "version": "2.1.22045.20230930", + "hash": "sha256-RxPcWUT3b/+R3Tu5E5ftpr5ppCLZrhm+OTsi0SwW3pc=" + }, + { + "pname": "Avalonia.BuildServices", + "version": "0.0.29", + "hash": "sha256-WPHRMNowRnYSCh88DWNBCltWsLPyOfzXGzBqLYE7tRY=" + }, + // ... + { + "pname": "System.Runtime.CompilerServices.Unsafe", + "version": "6.0.0", + "hash": "sha256-bEG1PnDp7uKYz/OgLOWs3RWwQSVYm+AnPwVmAmcgp2I=" + }, + { + "pname": "System.Security.Cryptography.ProtectedData", + "version": "4.5.0", + "hash": "sha256-Z+X1Z2lErLL7Ynt2jFszku6/IgrngO3V1bSfZTBiFIc=" + }, + { + "pname": "Tmds.DBus.Protocol", + "version": "0.16.0", + "hash": "sha256-vKYEaa1EszR7alHj48R8G3uYArhI+zh2ZgiBv955E98=" + } ] + ``` -Finally, you move the `deps.nix` file to the appropriate location to be used by `nugetDeps`, then you're all set! +Finally, you move the `deps.json` file to the appropriate location to be used by `nugetDeps`, then you're all set! If you ever need to update the dependencies of a package, you instead do diff --git a/pkgs/build-support/dotnet/add-nuget-deps/default.nix b/pkgs/build-support/dotnet/add-nuget-deps/default.nix index fb349679e579..b47a5b427cb4 100644 --- a/pkgs/build-support/dotnet/add-nuget-deps/default.nix +++ b/pkgs/build-support/dotnet/add-nuget-deps/default.nix @@ -12,6 +12,25 @@ }: { + /** + A list of nuget packages. + + Should be a JSON file with arguments to `fetchNupkg`: + + ```json + [ + { + "pname": "AsyncIO", + "version": "0.1.69", + "hash": "sha256-JQKq/U71NQTfPuUqj7z5bALe+d7G1o3GcI8kvVDxy6o=" + } + ] + ``` + + (to generate this file, use the script generated by `passthru.fetch-deps`) + + Or a derivation (or list of derivations) containing nuget packages. + */ nugetDeps, overrideFetchAttrs ? x: { }, }: @@ -20,16 +39,17 @@ let attrs = if builtins.isFunction fnOrAttrs then fnOrAttrs finalAttrs else fnOrAttrs; deps = - if (nugetDeps != null) then - if lib.isDerivation nugetDeps then - [ nugetDeps ] - else if lib.isList nugetDeps then - nugetDeps - else - assert (lib.isPath nugetDeps); - callPackage nugetDeps { fetchNuGet = fetchNupkg; } + if nugetDeps == null then + [ ] + else if lib.isDerivation nugetDeps then + [ nugetDeps ] + else if lib.isList nugetDeps then + nugetDeps + else if lib.hasSuffix ".nix" nugetDeps then + assert (lib.isPath nugetDeps); + callPackage nugetDeps { fetchNuGet = fetchNupkg; } else - [ ]; + builtins.map fetchNupkg (lib.importJSON nugetDeps); finalPackage = finalAttrs.finalPackage; @@ -84,7 +104,7 @@ attrs in writeShellScript "${finalPackage.name}-fetch-deps" '' - set -eu + set -euo pipefail echo 'fetching dependencies for' ${lib.escapeShellArg finalPackage.name} >&2 diff --git a/pkgs/test/dotnet/nuget-deps/default.nix b/pkgs/test/dotnet/nuget-deps/default.nix index bf0e1445e93e..31471ee00463 100644 --- a/pkgs/test/dotnet/nuget-deps/default.nix +++ b/pkgs/test/dotnet/nuget-deps/default.nix @@ -38,7 +38,8 @@ mapAttrs ) { "null" = null; - "file" = ./nuget-deps.nix; + "nix-file" = ./nuget-deps.nix; + "json-file" = ./nuget-deps.json; "derivation" = emptyDirectory; "list" = [ emptyDirectory ]; } diff --git a/pkgs/test/dotnet/nuget-deps/nuget-deps.json b/pkgs/test/dotnet/nuget-deps/nuget-deps.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/pkgs/test/dotnet/nuget-deps/nuget-deps.json @@ -0,0 +1 @@ +[]