dotnet-purifier full guide

Detailed setup, dependency management, build commands, and troubleshooting. Start with the overview for the initial commands; use the sections below for the complete setup and behavior. dotnet-purifier is classified at maturity tier 3 (see tier assessment for details).

Contents

Setup and activation

Use the overview’s Quickstart for the initial command sequence. The setup details below apply to that workflow.

Commit the lockfiles that the lock command writes, including the build tool’s own lockfile when it has one. Keep using the project’s usual build, test and editor commands with this setup. Nix provides the locked dependencies; the development commands above run outside the Nix sandbox. No project Nix files are needed for this workflow.

Sandboxed builds are an optional next step.

The required wrapper is dotnet-purifier dotnet; pass your usual tool arguments after it. With no purifier lockfile, it warns on standard error and runs the ordinary tool with impure resolution.

Install these packages through your usual Nix configuration.

Version compatibility

Tool or language Packaged defaults Tested versions
.NET SDK 10.0.403 —

Operating systems and architectures

OS CPU architecture Packaged toolchain Recorded workflow
macOS ARM64 Yes Not recorded
Linux ARM64 Yes Not recorded
Linux x86-64 Yes Tested

Choosing versions

Component Selected by
Build tool / package manager: .NET SDK global.json selects the project SDK; the configured default SDK major applies when it is absent.
global.json

Requirements

Development loop

Run dotnet through the purifier, with the locked packages:

nix shell 'github:nix-purifiers/dotnet-purifier'
dotnet-purifier dotnet build
dotnet-purifier dotnet test
dotnet-purifier dotnet run \
  --project src/App

Every restore reads only the locked packages, with the locked SDK and the runtime of every target framework of the build, so dotnet test runs each framework’s tests. dotnet tool restore and dotnet workload restore read the locked packages too.

To add or upgrade a package, edit the project file, or use the SDK’s own commands with the network, then relock:

nix shell 'github:nix-purifiers/dotnet-purifier'
# every package
dotnet-purifier update
dotnet-purifier update \
  Newtonsoft.Json
# after editing a project file
dotnet-purifier lock

update runs dotnet package update on each project, refreshes any packages.lock.json, and relocks. lock keeps the versions the project files name, and a project in NuGet’s lock mode keeps the resolution its packages.lock.json records. Where that file is missing or no longer matches the project, lock writes it.

For an external build, every command takes --lockfile <path>, and the environment variable DOTNET_PURIFIER_LOCKFILE names it too. The option --tool-lockfile <directory>, or the variable DOTNET_PURIFIER_TOOL_LOCKFILE, names the directory of the packages.lock.json files. With either, lock and update write NuGet’s lockfiles there and leave the files in the tree as they are. A lock into a directory that does not exist yet starts from the files in the tree. The dev shell of the flake that calls the builder can set both variables to the paths it passes to the builder.

dotnet-purifier is a command that runs the SDK, rather than an MSBuild plugin. A lock run controls the NuGet package folder, the SDK and the workloads of every restore and build it starts, and the development loop runs dotnet through the same command.

To publish the packages Nix built:

nix shell 'github:nix-purifiers/dotnet-purifier'
dotnet-purifier publish \
  --source https://api.nuget.org/v3/index.json --api-key \
  "$NUGET_API_KEY"

Using locked dependencies

dotnet-purifier keeps .NET’s development workflow on locked, Nix-resolved dependencies. Use the normal build and test commands in your working tree; building the project itself in the Nix sandbox is an optional next step.

Working offline

nix shell 'github:nix-purifiers/dotnet-purifier'
dotnet-purifier prefetch

prefetch builds the locked packages, the SDK with every runtime the build targets and the libraries a self-contained runtime loads. It keeps them with garbage-collector roots in the .dotnet-purifier/ directory next to the lockfile. It installs the packages into your NuGet package folder, restores the local tools and the workloads, and restores every project. Afterwards dotnet-purifier dotnet uses those store paths without evaluating Nix, so the development loop works with the network cut off. When the lock changes, the next dotnet-purifier dotnet prefetches again.

Language tooling integration

After a prefetch, the packages are in your NuGet package folder and every project is restored, so a language server that reads a project through MSBuild (the Roslyn language server, csharp-ls or OmniSharp) works with no network. Run it with the locked SDK, which prefetch links at .dotnet-purifier/sdk next to the lockfile: put .dotnet-purifier/sdk/bin first on its PATH and set DOTNET_ROOT to .dotnet-purifier/sdk/share/dotnet. Navigation into a package’s code decompiles it locally; source files that SourceLink downloads need the network.

Nix builds

Once the native development workflow is working, you can also build and test the whole project in the Nix sandbox. For this step, keep a flake or another Nix build definition in the project’s repository, or maintain an external packaging repository that points at its sources and lockfiles. The builder consumes the same lockfiles as the development workflow.

This builds the project in App.csproj. Each project is named after its project file. The overlay overlays.default adds one attribute to a package set, the scope dotnet-purifier, which holds the builder buildDotnetProject and the command.

To package the project in a flake, which can live in the project’s repository or in another one, call the builder in the flake’s outputs:

# flake.nix
{
  inputs.dotnet-purifier.url =
    "github:nix-purifiers/dotnet-purifier";
  inputs.nixpkgs.follows = "dotnet-purifier/nixpkgs";

  outputs =
    { nixpkgs, dotnet-purifier, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ dotnet-purifier.overlays.default ];
      };
      build = pkgs.dotnet-purifier.buildDotnetProject {
        src = ./.;
      };
    in
    {
      packages.${system}.default = build.packages.App;
      checks.${system}.tests = build.tests;
    };
}

The builder returns

The builder takes src and these optional arguments:

A flake can instead keep the build external. It keeps the lockfile outside src and passes it as lockFile, and it can keep its own copies of the packages.lock.json files in a directory it passes as packagesLock. The build then uses them in place of the files in src. This builds a project whose author does not lock it, or whose committed packages.lock.json is stale or written by another version of the SDK.

The builder takes what it needs from the package set, such as dotnetCorePackages and stdenv, so buildDotnetProject.override replaces any of it, and dotnet-purifier.overrideScope replaces what the whole scope shares. lib.dotnet-purifier.readLock { src, lockFile, packagesLock } reads the lock in the shape every purifier shares.

One-off builds without a project Nix file

For an ad hoc build, the builder can also be evaluated directly from the project root. Replace the example package name with one from your project:

nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/dotnet-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.dotnet-purifier.buildDotnetProject { src = ./.; })
.packages.App'

This uses the purifier’s pinned nixpkgs, matching the lock command above. To build with ambient <nixpkgs> from your Nix search path instead, replace import purifier.inputs.nixpkgs with import <nixpkgs>. Both forms need no Nix files in your project. The build tool and language versions must match the lockfile; with ambient nixpkgs, lock with the purifier from that package set or pass a compatible toolchain to the builder.

Supported scope

Supported language repositories

The lock records packages from these sources, each by URL and hash:

  • NuGet V3 feeds of the build’s nuget.config, with package source mapping. Each package’s URL is its feed’s flat-container URL.
  • Folder feeds inside the build’s tree, recorded by file and hash.
  • MSBuild SDK packages, such as Microsoft.Build.NoTargets.
  • Local tools of .config/dotnet-tools.json, which a build may run.
  • Runtime, host and targeting packs. The lock takes them from NuGet, so one lock serves both x86_64-linux and aarch64-linux.
  • SDK workloads, such as wasm-tools. The lock records their packs for both machines, and a Nix build installs them offline.
  • The folder feed inside the SDK (library-packs). The lock takes these packages from a feed when a feed serves them, so both machines get the same file.
  • The lock mode of NuGet. When a packages.lock.json records a package with the hash of its file, the lockfile reads the hash from it. NuGet hashes a signed package without its signature, so for signed packages the lockfile keeps the hash of the file. Every package on nuget.org is signed.

A lock run stops, naming the reason, on these:

  • Feeds outside NuGet V3, and folder feeds outside the build’s tree: the lock records a package by URL or by a file Nix can read.
  • Feeds that need credentials: the lock run cannot authenticate yet. A Nix build can fetch through fetchers.fetchurl.
  • Workloads that need tools outside NuGet. The Android workload needs the Android SDK and a JDK, which it finds on the machine; a build would have to provide them through nativeBuildInputs. The iOS, macOS and MAUI workloads need Xcode and build only on macOS.
  • Windows and macOS target frameworks. The purifier targets Linux.

Other limits: a lock is made with the nixpkgs SDK of the major version global.json names (10 when there is none), and a Nix build uses the SDK of that major version from your nixpkgs, warning when the patch version differs. Workload tools run interactively on NixOS with programs.nix-ld.

Target systems

  • x86_64-linux
  • aarch64-linux

Troubleshooting

Message Fix
… does not match the build: a project, solution, .props or .targets file … changed dotnet-purifier lock, then commit the lockfile. A Nix build can pass allowStaleLock = true instead.
… was edited after dotnet-purifier lock wrote it dotnet-purifier lock --force.
… has format … or … lacks …, so it is not a lockfile dotnet-purifier lock wrote dotnet-purifier lock --force.
the lock does not hold <package>: run dotnet-purifier lock A restore asked for a package the lock does not hold; relock.
… dotnet publish failed. If NuGet could not find a package … (in a Nix build) The same, from a Nix build; relock.
… has the hash …, and the lock records …: the package changed A package of a folder feed in the tree changed; relock.
… no longer records …, whose hash … reads from it A packages.lock.json changed; relock.

Examples

For day-to-day work on these projects, use the native-tool setup and commands in Quickstart and Development loop. The Nix expressions below show optional sandboxed packaging of those projects.

FluentValidation is a .NET validation library with a solution of packable libraries, a benchmark executable and a test project. To lock it, build it with Nix and work on it with the locked packages, clone it and run these commands in the clone:

nix shell 'github:nix-purifiers/dotnet-purifier'
git clone https://github.com/FluentValidation/FluentValidation
cd FluentValidation
dotnet-purifier lock
nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/dotnet-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.dotnet-purifier.buildDotnetProject { src = ./.; })
.packages.FluentValidation'
dotnet-purifier dotnet test

The build gives the package of the main library. Its tests are the attribute tests of the same builder result, which runs them on every framework the test project targets.

FluentValidation does not commit a dotnet-purifier lockfile. To keep the lockfile in a flake of your own instead, run lock with --lockfile <path> and pass the file to the builder as lockFile, with FluentValidation as the src of the flake.