NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.34k stars 14.3k forks source link

Unable to run an empty Avalonia .NET app from source using `dotnet run` #350147

Open lostmsu opened 1 month ago

lostmsu commented 1 month ago

Describe the bug

The app fails to start with

Unhandled exception. System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: 
libfontconfig.so.1: cannot open shared object file: No such file or directory
/nix/store/857lb4p516avw491s8i5pzglwrki8n36-dotnet-sdk-8.0.300/shared/Microsoft.NETCore.App/8.0.5/libSkiaSharp.so: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/libSkiaSharp.so: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/runtimes/linux-x64/native/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/nix/store/857lb4p516avw491s8i5pzglwrki8n36-dotnet-sdk-8.0.300/shared/Microsoft.NETCore.App/8.0.5/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/runtimes/linux-x64/native/libSkiaSharp: cannot open shared object file: No such file or directory
/nix/store/857lb4p516avw491s8i5pzglwrki8n36-dotnet-sdk-8.0.300/shared/Microsoft.NETCore.App/8.0.5/libSkiaSharp: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/libSkiaSharp: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/runtimes/linux-x64/native/liblibSkiaSharp: cannot open shared object file: No such file or directory
/nix/store/857lb4p516avw491s8i5pzglwrki8n36-dotnet-sdk-8.0.300/shared/Microsoft.NETCore.App/8.0.5/liblibSkiaSharp: cannot open shared object file: No such file or directory
/home/X/Projects/Env/Forever/Viewer/bin/Debug/net8.0/liblibSkiaSharp: cannot open shared object file: No such file or directory

   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor()
   --- End of inner exception stack trace ---
   at Avalonia.Skia.PlatformRenderInterface..ctor(Nullable`1 maxResourceBytes)
   at Avalonia.Skia.SkiaPlatform.Initialize(SkiaOptions options)
   at Avalonia.SkiaApplicationExtensions.<>c.<UseSkia>b__0_0()
   at Avalonia.AppBuilder.SetupUnsafe()
   at Avalonia.AppBuilder.Setup()
   at Avalonia.AppBuilder.SetupWithLifetime(IApplicationLifetime lifetime)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)

Steps To Reproduce

Steps to reproduce the behavior:

  1. install dotnetCorePackages.sdk_8_0
  2. dotnet new install "Avalonia.Templates"
  3. dotnet new avalonia.app -o MyApp
  4. in MyApp/ do dotnet run

Expected behavior

The app should start

Additional context

The default template pulls SkiaSharp.NativeAssets.Linux package, which has libSkiaSharp.so. It ends up in bin/Debug/net8.0/runtimes/linux-x64/native/libSkiaSharp.so, but can not be loaded.

Not quite sure what exactly is going on, as installing fontconfig NixOS package did not resolve the error at the top (libfontconfig.so.1: cannot open shared object file: No such file or directory). I suspect for a descent experience all native dependencies from NuGet need to be patched on download (might break building for other Linuxes) or on build (when bin/Debug is created).

EXTRA: the issue also reproduces with dotnet publish followed by dotnet MyApp.dll from publish/ output directory.

Notify maintainers

@kuznero @mdarocha

Metadata

 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.57, NixOS, 24.05 (Uakari), 24.05.5809.4eb33fe664af`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.8`
 - channels(root): `"home-manager-24.05.tar.gz, nixos-24.05"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Add a :+1: reaction to issues you find important.

AlienJust commented 1 month ago

Hi, Avalonia depends on some libs. You can use nix develop command with flake.nix like this:

{
  description = "A Nix-flake-based C# development environment";

  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz";

  outputs = {
    self,
    nixpkgs,
  }: let
    supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
    forEachSupportedSystem = f:
      nixpkgs.lib.genAttrs supportedSystems (system:
        f {
          pkgs = import nixpkgs {inherit system;};
        });
  in {
    devShells = forEachSupportedSystem ({pkgs}: {
      default = pkgs.mkShell {
        packages = with pkgs; [
          #dotnet-sdk_6
          #dotnet-sdk_7
          #dotnet-sdk_8
          (with dotnetCorePackages;
            combinePackages [
              sdk_6_0
              sdk_7_0
              sdk_8_0
            ])
          omnisharp-roslyn
          mono
          msbuild
          (pkgs.buildFHSEnv {
            name = "avalonia-fhs-shell";
            targetPkgs = pkgs:
              with pkgs;
                [
                  udev
                  alsa-lib
                  fontconfig
                  glew
                ]
                ++ (with pkgs.xorg; [
                  # Avalonia UI
                  libX11
                  libICE
                  libSM
                  libXi
                  libXcursor
                  libXext
                  libXrandr
                ]);
            #runScript = "zsh"; # same colors scheme of zsh confusing me
          })
        ];
      };
    });
  };
}

then enter shell: avalonia-fhs-shell then run: dotnet run

AlienJust commented 1 month ago

There also ILSpy.Avalonia package in nixpkgs See: https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/applications/misc/avalonia-ilspy/default.nix#L51

Upd. And even Avalonia 11 o_O https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/by-name/av/avalonia/package.nix

AlienJust commented 1 month ago

Or if You don't use flakes, then create file shell.nix:

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSEnv {
  name = "simple-x11-env";
  targetPkgs = pkgs: (with pkgs; [
    udev
    alsa-lib
    fontconfig
    glew
  ]) ++ (with pkgs.xorg; [
    # Avalonia UI
    libX11
    libICE
    libSM
    libXi
    libXcursor
    libXext
    libXrandr
  ]);
  multiPkgs = pkgs: (with pkgs; [
    udev
    alsa-lib
  ]);
  runScript = "bash";
}).env

then run nix-shell shell.nix before dotnet-run

lostmsu commented 1 week ago

@AlienJust is there something I could do to make it work out of the box? I mean contribute to Nix.

I mean it is interesting that there's Avalonia package, but it is also very surprising that installing it does not resolve the issue. I think the best solution would be to do something so that the libSkiaSharp.so that NuGet package brings somehow would be able to resolve installed libfontconfig without user doing anything.

P.S. not using flakes yet