From af411d49d1d4ff759c7c7fa6420c5fb3d88028d1 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Thu, 5 Dec 2024 18:59:01 +0000 Subject: [PATCH] addNuGetDeps: support loading JSON lockfiles In addition to loading nix lockfiles --- doc/languages-frameworks/dotnet.section.md | 2 +- .../dotnet/add-nuget-deps/default.nix | 47 +++++++++++++++---- pkgs/test/dotnet/nuget-deps/default.nix | 3 +- pkgs/test/dotnet/nuget-deps/nuget-deps.json | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 pkgs/test/dotnet/nuget-deps/nuget-deps.json diff --git a/doc/languages-frameworks/dotnet.section.md b/doc/languages-frameworks/dotnet.section.md index 3de2bafb5cf32..9fb020e87172f 100644 --- a/doc/languages-frameworks/dotnet.section.md +++ b/doc/languages-frameworks/dotnet.section.md @@ -93,7 +93,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 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` takes a derivation, a list of derivations, or a list of `fetchNupkg` arguments. This can be a nix value, path to a JSON file or path to a nix file. A `deps.nix` file can be generated using the script attached to `passthru.fetch-deps`. All `nugetDeps` packages will be added to `buildInputs`. ::: {.note} For more detail about managing the `deps.nix` file, see [Generating and updating NuGet dependencies](#generating-and-updating-nuget-dependencies) ::: diff --git a/pkgs/build-support/dotnet/add-nuget-deps/default.nix b/pkgs/build-support/dotnet/add-nuget-deps/default.nix index bf3df815af8a5..bd065bf9e73bf 100644 --- a/pkgs/build-support/dotnet/add-nuget-deps/default.nix +++ b/pkgs/build-support/dotnet/add-nuget-deps/default.nix @@ -11,6 +11,23 @@ }: { + /** + A list of nuget packages. + + Should be a list of derivations or arguments to be applied to `fetchNupkg`. + + ```nix + [ + # Arguments for use fetchNupkg + { pname = "FosterFramework"; version = "0.1.15-alpha"; hash = "sha256-lM6eYgOGjl1fx6WFD7rnRi/YAQieM0mx60h0p5dr+l8="; } + + # Or a derivation + (fetchNupkg { pname = "Microsoft.AspNetCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-QbUQXjCzr8j8u/5X0af9jE++EugdoxMhT08F49MZX74="; }) + ] + ``` + + Either a JSON or nix file with the same structure can also be used. + */ nugetDeps, overrideFetchAttrs ? x: { }, }: @@ -18,17 +35,27 @@ fnOrAttrs: finalAttrs: 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; } + callNupkg = pkg: if lib.isDerivation pkg then pkg else fetchNupkg pkg; + + loadDeps = + x: + if x == null then + [ ] + else if lib.isDerivation x then + [ x ] + else if builtins.isList x then + builtins.map callNupkg x else - [ ]; + loadDeps ( + if builtins.isFunction x then + callPackage x { fetchNuGet = fetchNupkg; } + else if lib.hasSuffix ".json" x then + lib.importJSON x + else + import x + ); + + deps = loadDeps nugetDeps; finalPackage = finalAttrs.finalPackage; diff --git a/pkgs/test/dotnet/nuget-deps/default.nix b/pkgs/test/dotnet/nuget-deps/default.nix index bf0e1445e93eb..31471ee00463e 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 0000000000000..fe51488c7066f --- /dev/null +++ b/pkgs/test/dotnet/nuget-deps/nuget-deps.json @@ -0,0 +1 @@ +[]