Skip to content

Cannot execute dotnet generated binary files on NixOS Sup

The dotnet linker cannot find the dotnet root on a NixOS installation.

To reproduce:

Test.csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

</Project>

Program.cs

using System;

class TestClass
{
    static void Main(string[] args)
    {
        Console.WriteLine(args.Length);
    }
}
dotnet build

Executing dotnet run works fine but ./bin/Debug/net5.0/Test crashes with the following error:

A fatal error occurred. The required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/tmp/test/bin/Debug/net5.0/].
If this is a framework-dependent application, install the runtime in the global location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [/etc/dotnet/install_location].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=nixos.21.11-x64&apphost_version=5.0.12

Setting the DOTNET_ROOT environment variable to the path given by whereis dotnet fixes the issue.

Maybe a possible fix is to add ${pkgs.dotnet-sdk_5} to the environment.variables somewhere.