Skip to content

Commit

Permalink
addNuGetDeps: support loading JSON lockfiles
Browse files Browse the repository at this point in the history
In addition to loading nix lockfiles
  • Loading branch information
MattSturgeon committed Dec 5, 2024
1 parent 3611f5d commit af411d4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 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 @@ -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)
:::
Expand Down
47 changes: 37 additions & 10 deletions pkgs/build-support/dotnet/add-nuget-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,51 @@
}:

{
/**
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: { },
}:
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;

Expand Down
3 changes: 2 additions & 1 deletion pkgs/test/dotnet/nuget-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}
1 change: 1 addition & 0 deletions pkgs/test/dotnet/nuget-deps/nuget-deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit af411d4

Please sign in to comment.