Skip to content

Commit

Permalink
addNuGetDeps: support loading JSON or TOML lockfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Dec 5, 2024
1 parent 3611f5d commit 669f208
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 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 either be a nix value or a path to a nix, toml, or json 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
49 changes: 39 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,53 @@
}:

{
/**
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="; })
]
```
A nix, toml, or json 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 ".nix" x then
import x
else if lib.hasSuffix ".toml" x then
lib.importTOML x
else
lib.importJSON x
);

deps = loadDeps nugetDeps;

finalPackage = finalAttrs.finalPackage;

Expand Down

0 comments on commit 669f208

Please sign in to comment.