Home Explore Blog Models CI



nixpkgs

5th chunk of `doc/languages-frameworks/dotnet.section.md`
3a4de345be8bc7a899873294bb6a3ff70f33e916a1ec2d180000000100000825
Dotnet global tools are standard .NET binaries, just made available through a special
NuGet package. Therefore, they can be built and packaged like every .NET application,
using `buildDotnetModule`.

If however the source is not available or difficult to build, the
`buildDotnetGlobalTool` helper can be used, which will package the tool
straight from its NuGet package.

This helper has the same arguments as `buildDotnetModule`, with a few differences:

* `pname` and `version` are required, and will be used to find the NuGet package of the tool
* `nugetName` can be used to override the NuGet package name that will be downloaded, if it's different from `pname`
* `nugetHash` is the hash of the fetched NuGet package. `nugetSha256` is also supported, but not recommended. Set this to `lib.fakeHash` for the first build, and it will error out, giving you the proper hash. Also remember to update it during version updates (it will not error out if you just change the version while having a fetched package in `/nix/store`)
* `dotnet-runtime` is set to `dotnet-sdk` by default. When changing this, remember that .NET tools fetched from NuGet require an SDK.

Here is an example of packaging `pbm`, an unfree binary without source available:
```nix
{ buildDotnetGlobalTool, lib }:

buildDotnetGlobalTool {
  pname = "pbm";
  version = "1.3.1";

  nugetHash = "sha256-ZG2HFyKYhVNVYd2kRlkbAjZJq88OADe3yjxmLuxXDUo=";

  meta = {
    homepage = "https://cmd.petabridge.com/index.html";
    changelog = "https://cmd.petabridge.com/articles/RELEASE_NOTES.html";
    license = lib.licenses.unfree;
    platforms = lib.platforms.linux;
  };
}
```
## 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.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`.)

Title: Packaging Dotnet Global Tools with `buildDotnetGlobalTool` and Managing NuGet Dependencies
Summary
This chunk details how to package .NET global tools in NixOS, especially when source code is not available or difficult to build. It introduces the `buildDotnetGlobalTool` helper, which packages tools directly from NuGet packages. Key arguments for this helper include `pname`, `version`, `nugetHash` (with a tip to use `lib.fakeHash` for initial discovery), and an optional `nugetName`. It also notes that `dotnet-runtime` defaults to `dotnet-sdk` for these tools. An example demonstrates packaging the `pbm` tool. Finally, the text explains how to generate and update NuGet dependencies for a project using a `fetch-deps` script, outlining the steps to initialize a lockfile (`deps.json`).