Dotnet run - error while loading shared libraries: libstdc++.so.6

I’m attempting to build & run a .NET Core 3.1 app GitHub - bramford/fsharp-grpc-greeter: An F# + ASP.NET Core implementation of the "Greeter" gRPC example from the official .NET gRPC documentation on NixOS 22.05. I used the supplied dotnet nix-shell script documented in the manual, with some extra library packages:

with import <nixpkgs> {};

mkShell {
  name = "dotnet-env";
  packages = [
    glibc
    icu.dev
    openssl
    stdenv.cc.cc.lib
    (with dotnetCorePackages; combinePackages [
      sdk_3_1
      sdk_6_0
    ])
  ];
}

The compile failed initially due to the nuget-managed protoc so I added this to my system after some googling:

  system.activationScripts.ldso = lib.stringAfter [ "usrbinenv" ] ''
            mkdir -m 0755 -p /lib64
            ln -sfn ${pkgs.glibc.out}/lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2.tmp
            mv -f /lib64/ld-linux-x86-64.so.2.tmp /lib64/ld-linux-x86-64.so.2 # atomically replace
          '';

dotnet run in GrpcGreeter fails

Now in nix-shell, compilation works with dotnet build. However, dotnet run while in GrpcGreeter fails with:

/home/bramford/git/github.com/bramford/fsharp-grpc-greeter/GrpcGreeter/bin/Debug/netcoreapp3.1/GrpcGreeter: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

Using a buildFHSUserEnv shell also fails to run

I’ve also tried using nix-shell with buildFHSUserEnv as per fhs-shell.nix:

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSUserEnv {
  name = "dotnet";

  targetPkgs = pkgs: [
    pkgs.glibc
    pkgs.icu.dev
    pkgs.openssl
    pkgs.stdenv.cc.cc.lib
    (with pkgs.dotnetCorePackages; combinePackages [
      sdk_3_1
      sdk_6_0
    ])
  ];

}).env

But it fails during dotnet run with:

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 [/home/bramford/git/github.com/bramford/fsharp-grpc-greeter/GrpcGreeter/bin/Debug/netcoreapp3.1/].
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 Core runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=nixos.22.11-x64
dotnet-chrootenv:bramford@droop:~/git/github.com/bramford/fsharp-grpc-greeter/GrpcGreeter$ ^C
dotnet-chrootenv:bramford@droop:~/git/github.com/bramford/fsharp-grpc-greeter/GrpcGreeter$
exit