addNuGetDeps: support loading JSON lockfiles

In addition to loading nix lockfiles
This commit is contained in:
Matt Sturgeon 2024-12-05 18:59:01 +00:00 committed by GGG
parent 3cc4563eb2
commit bccae9e7c0
4 changed files with 77 additions and 32 deletions

View File

@ -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: 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. * `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} ::: {.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`. * `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`. * `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. * `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: Here is an example `default.nix`, using some of the previously discussed arguments:
```nix ```nix
@ -148,7 +148,7 @@ in buildDotnetModule rec {
src = ./.; src = ./.;
projectFile = "src/project.sln"; 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. 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} ## 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. 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. 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`.) (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 ```bash
$ dotnet restore --packages out $ dotnet restore --packages out
Determining projects to restore... 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. the packages inside the `out` directory.
```bash ```bash
$ nuget-to-nix out > deps.nix $ nuget-to-json out > deps.json
``` ```
Which `nuget-to-nix` will generate an output similar to below Which `nuget-to-json` will generate an output similar to below
```nix ```json
{ 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="; }) "pname": "Avalonia",
(fetchNuGet { pname = "Microsoft.NET.ILLink.Tasks"; version = "8.0.1"; hash = "sha256-SopZpGaZ48/8dpUwDFDM3ix+g1rP4Yqs1PGuzRp+K7c="; }) "version": "11.1.3",
(fetchNuGet { pname = "Microsoft.NETCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-jajBI5GqG2IIcsIMgxTHfXbMapoXrZGl/EEhShwYq7w="; }) "hash": "sha256-kz+k/vkuWoL0XBvRT8SadMOmmRCFk9W/J4k/IM6oYX0="
(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="; }) "pname": "Avalonia.Angle.Windows.Natives",
(fetchNuGet { pname = "Sledge.Formats.Map"; version = "1.1.5"; hash = "sha256-hkYJ2iWIz7vhPWlDOw2fvTenlh+4/D/37Z71tCEwnK8="; }) "version": "2.1.22045.20230930",
(fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.5.0"; hash = "sha256-qdSTIFgf2htPS+YhLGjAGiLN8igCYJnCCo6r78+Q+c8="; }) "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 If you ever need to update the dependencies of a package, you instead do

View File

@ -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, nugetDeps,
overrideFetchAttrs ? x: { }, overrideFetchAttrs ? x: { },
}: }:
@ -20,16 +39,17 @@ let
attrs = if builtins.isFunction fnOrAttrs then fnOrAttrs finalAttrs else fnOrAttrs; attrs = if builtins.isFunction fnOrAttrs then fnOrAttrs finalAttrs else fnOrAttrs;
deps = deps =
if (nugetDeps != null) then if nugetDeps == null then
if lib.isDerivation nugetDeps then [ ]
[ nugetDeps ] else if lib.isDerivation nugetDeps then
else if lib.isList nugetDeps then [ nugetDeps ]
nugetDeps else if lib.isList nugetDeps then
else nugetDeps
assert (lib.isPath nugetDeps); else if lib.hasSuffix ".nix" nugetDeps then
callPackage nugetDeps { fetchNuGet = fetchNupkg; } assert (lib.isPath nugetDeps);
callPackage nugetDeps { fetchNuGet = fetchNupkg; }
else else
[ ]; builtins.map fetchNupkg (lib.importJSON nugetDeps);
finalPackage = finalAttrs.finalPackage; finalPackage = finalAttrs.finalPackage;
@ -84,7 +104,7 @@ attrs
in in
writeShellScript "${finalPackage.name}-fetch-deps" '' writeShellScript "${finalPackage.name}-fetch-deps" ''
set -eu set -euo pipefail
echo 'fetching dependencies for' ${lib.escapeShellArg finalPackage.name} >&2 echo 'fetching dependencies for' ${lib.escapeShellArg finalPackage.name} >&2

View File

@ -38,7 +38,8 @@ mapAttrs
) )
{ {
"null" = null; "null" = null;
"file" = ./nuget-deps.nix; "nix-file" = ./nuget-deps.nix;
"json-file" = ./nuget-deps.json;
"derivation" = emptyDirectory; "derivation" = emptyDirectory;
"list" = [ emptyDirectory ]; "list" = [ emptyDirectory ];
} }

View File

@ -0,0 +1 @@
[]