DotNET: Difference between revisions

Lostmsu (talk | contribs)
m Undo revision 13402 by Lostmsu (talk)
Tag: Undo
Railwhale (talk | contribs)
group into sections, add intro text from wikipedia
 
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
.NET packages can be built with <code>buildDotnetModule</code>
More information about <code>buildDotnetModule</code> can be found in the [https://nixos.org/manual/nixpkgs/unstable/#dotnet nixpkgs manual]
Example build file:
<syntaxhighlight lang="nix">
{ fetchFromGitHub
, dotnetCorePackages
, buildDotnetModule
}:
buildDotnetModule rec {
  pname = "some_program";
  version = "some_version";
  src = fetchFromGitHub {
    owner = "some_owner";
    repo = pname;
    rev = "v${version}";
    sha256 = ""; # can be figured out from the first build attempt
  };
  projectFile = "SomeProject/SomeProject.csproj";
  dotnet-sdk = dotnetCorePackages.sdk_8_0;
  dotnet-runtime = dotnetCorePackages.runtime_8_0;
  nugetDeps = null; # only if there are NuGet dependencies, real file will be genned later
  meta = with lib; {
    homepage = "some_homepage";
    description = "some_description";
    license = licenses.mit;
  };
}
</syntaxhighlight>
Note that the above package will not build the first time. After adding the above definition to `all-packages.nix`, you
can run the package-specific `fetch-deps` script, which will generate a file containing all the nuget dependencies of the
package. Build the script with <code>nix-build -A some-package.fetch-deps</code>, might need to run <code>./result deps.nix</code>, copy that generated file (the location will be printed by the script) and set the <code>nugetDeps</code> attribute in <code>buildDotnetModule</code> to point to that generated file (ie. <code>nugetDeps = ./deps.nix</code>).
After that the package will build normally. Remember to re-run <code>fetch-deps</code> every time the package is updated.
== Building non-.NET Core packages ==
Keep in mind that building non-.NET Core projects (ie. projects that don't build using the <code>dotnet</code> CLI tool) is not well supported. For those projects, you have to work on a custom derivation or override the <code>buildDotnetModule</code> build steps.
== .NET location: Not found ==
If running a .NET-build executable you get the above error, make sure the DOTNET_ROOT environment variable is set:
<syntaxHighlight lang=nix>
environment.sessionVariables = {
  DOTNET_ROOT = "${pkgs.dotnet-sdk}";
};
</syntaxHighlight>
See : https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#net-sdk-and-cli-environment-variables
== TargetFramework value was not recognized ==


From [https://en.wikipedia.org/wiki/.NET Wikipedia]
<blockquote>
<blockquote>
 
The .NET platform (formerly named .NET Core) is a free and open-source, managed computer software framework for Windows, Linux, and macOS operating systems. It is a cross-platform successor to the .NET Framework. The project is mainly developed by Microsoft employees by way of the .NET Foundation and is today released under an MIT License.
error NETSDK1013: The TargetFramework value 'net6.0-windows' was not recognized. It may be misspelled. If not, then the TargetFrameworkIdentifier and/or TargetFrameworkVersion properties must be specified explicitly.
 
</blockquote>
</blockquote>
Wontfix: The project will build only on Windows.


== NativeAOT ==
== NativeAOT ==
Line 118: Line 57:




== Example: Running Rider with dotnet & PowerShell ==
== Packaging ==
 
.NET packages can be built with <code>buildDotnetModule</code>
 
More information about <code>buildDotnetModule</code> can be found in the [https://nixos.org/manual/nixpkgs/unstable/#dotnet nixpkgs manual]
Example build file:
 
<syntaxhighlight lang="nix" line="1" start="1">
{
  buildDotnetModule,
  dotnetCorePackages,
}:
 
buildDotnetModule {
  pname = "hello";
  version = "0.1";
 
  src = ./.;
 
  projectFile = "Hello/Hello.csproj";
  dotnet-sdk = dotnetCorePackages.sdk_8_0;
  dotnet-runtime = dotnetCorePackages.runtime_8_0;
  nugetDeps = ./deps.json;
}
 
</syntaxhighlight>
 
If the <code>fetch-deps</code> script isn't working for whatever reason, you can manually run <code>nuget-to-json</code>:
<syntaxhighlight lang="shell-session">
$ dotnet restore --packages=packageDir ./SomeProject.csproj
$ nuget-to-json packageDir > deps.json
$ rm -r packageDir
</syntaxhighlight>
 
Remember to build and run the <code>fetch-deps</code> script after NuGet packages are updated, or building the derivation will fail.
 
=== Building non-.NET Core packages ===
 
Keep in mind that building projects which don't use the .NET SDK (formerly the .NET Core SDK) and its <code>dotnet</code> CLI tool isn't supported.
For those projects, you'll have to heavily customise the <code>buildDotnetModule</code> build steps, or write a custom derivation.
 
Projects which target .NET Standard or .NET Framework (incl. Mono), but still use the new project structure and SDK, work as expected.
Just remember to add `mono` to `buildInputs` and generate a wrapper script in `postInstall`.
 
=== Packaging ASP.NET projects ===
 
Currently building ASP.NET project as Nix package produces a website that does not work correctly out of the box because the executable can not find <code>wwwroot</code>, so all the static assets won't load with 404.
 
<blockquote>
Request finished HTTP/2 GET https://my.app/css/site.css - 404 0
</blockquote>
 
The situation can be fixed by setting <code>WEBROOT</code> environment variable to the package path.
 
An example of systemd + ASP.NET 8 service:


Rider package
<syntaxhighlight lang="nix">
# myapp package needs to be imported; and added to `environment.systemPackages`
# the variable myapp is used below


<syntaxHighlight lang=nix>
systemd.services.my-app = {
pkgs.jetbrains.rider
  enable = true;
</syntaxHighlight>
  description = "Runs my.app";
  wantedBy = [ "multi-user.target" ];
  after = [ "network-online.target" ];
  wants = [ "network-online.target" ];
  serviceConfig = {
    # allow binding to privileged ports - when you want to expose Kestrel directly without reverse proxy
    AmbientCapabilities = "CAP_NET_BIND_SERVICE";
    User = "myapp"; # must be created using users.users.myapp = { isSystemUser = true; group = "myapp"; };
    Group = "myapp"; # must be created using users.groups.myapp = {};
    Restart = "always";
    ExecStart = "${myapp}/bin/myapp";
    StateDirectory = "myapp";
    StateDirectoryMode = "0750";
    WorkingDirectory = "/var/lib/myapp";
    # EnvironmentFile = "/var/lib/myapp/env";
  };
  environment = {
    WEBROOT = "${myapp}/lib/myapp/wwwroot"; # IMPORTANT, required to pick up static assets
 
    DOTNET_ENVIRONMENT = "Production";
 
    # the following are examples
    ConnectionStrings__DefaultConnection = "Host=/var/run/postgresql;Database=myapp";
 
    # Kestrel + HTTPS; must setup https://wiki.nixos.org/wiki/ACME
    Kestrel__Endpoints__Https__Url = "https://my.app";
    Kestrel__Endpoints__Https__Certificate__Path = "/var/lib/acme/my.app/cert.pem";
    Kestrel__Endpoints__Https__Certificate__KeyPath = "/var/lib/acme/my.app/key.pem";
 
    Logging__LogLevel__Default = "Information";
    Logging__LogLevel__Microsoft__AspNetCore = "Warning"; # this does not actually work, not sure how to fix
 
    Authentication__Google__ClientId = "xxxyyyzzz.apps.googleusercontent.com";
    Authentication__Microsoft__ClientId = "aaaaaa-0000-aaaa-0000-aaaaaaaaaa";
    # secrets must be placed in /var/lib/myapp/appsettings.json
 
    # TODO email
 
    # TODO Stripe
    Stripe__Currency = "USD";
  };
};
 
</syntaxhighlight>
 
See also: setting up SSL certificates using [[ACME]]
 
 
== Examples ==


dotnet.nix
=== Running Rider with dotnet & PowerShell ===
Rider has better compatibility when run in FHS mode


<syntaxHighlight lang=nix>
Rider package<syntaxhighlight lang="nix">
with import <nixpkgs> {};
pkgs.jetbrains.rider
</syntaxhighlight>rider-fhs.nix<syntaxhighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:


mkShell {
(pkgs.buildFHSEnv {
   name = "dotnet-env";
   name = "rider-env";
   packages = [
   targetPkgs = pkgs: (with pkgs; [
    (with dotnetCorePackages; combinePackages [
    dotnetCorePackages.dotnet_8.sdk
      sdk_6_0
     dotnetCorePackages.dotnet_8.aspnetcore
      sdk_7_0
      sdk_8_0
     ])
     powershell
     powershell
   ];
   ]);
}
  multiPkgs = pkgs: (with pkgs; [
</syntaxHighlight>
  ]);
  runScript = "nohup rider &";
}).env
</syntaxhighlight>


To execute Rider
<syntaxhighlight lang="nix">
nix-shell ./rider-fhs.nix
</syntaxhighlight>


<syntaxHighlight lang=bash>
This can be added as an alias to your shell if you update the reference to an absolute address, such as location within your home directory.
nix-shell ./dotnet.nix --run 'nohup rider &'
</syntaxHighlight>


This can be added as an alias to your shell if you update the reference to an absolute address, such as location within your home directory. e.g. `~/nix/dotnet.nix`
<syntaxhighlight>
run-rider = "nix-shell ~/nix/rider-fhs.nix";
</syntaxhighlight>


== Example: multi-SDK installation with local workload installation enabled ==
=== Multi-SDK installation with local workload installation enabled ===


By default, workload installation will fail on NixOS, as dotnet will attempt to save it to $DOTNET_ROOT, which is inside the read-only Nix store.
By default, workload installation will fail on NixOS, as dotnet will attempt to save it to $DOTNET_ROOT, which is inside the read-only Nix store.


Please visit the [https://discourse.nixos.org/t/dotnet-maui-workload/20370/10 forum] for an example of a multi-SDK installation with workload changed to install to home directory.
Please visit the [https://discourse.nixos.org/t/dotnet-maui-workload/20370/10 forum] for an example of a multi-SDK installation with workload changed to install to home directory.
== Troubleshooting ==
=== .NET location: Not found ===
If running a .NET-build executable you get the above error, make sure the DOTNET_ROOT environment variable is set:
<syntaxhighlight lang="nix">
environment.sessionVariables = {
  DOTNET_ROOT = "${pkgs.dotnet-sdk}/share/dotnet/";
};
</syntaxhighlight>
See : https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#net-sdk-and-cli-environment-variables
=== TargetFramework value was not recognized ===
<blockquote>
error NETSDK1013: The TargetFramework value 'net6.0-windows' was not recognized. It may be misspelled. If not, then the TargetFrameworkIdentifier and/or TargetFrameworkVersion properties must be specified explicitly.
</blockquote>
Wontfix: The project will build only on Windows.
=== Unable to find package ===
<blockquote>
error NU1101: Unable to find package runtime.any.System.Collections. No packages exist with this id in source(s): nugetSource
</blockquote>
Unsure what specific situations cause this, probably has something to do with .NET Standard libraries.
The workaround is modifying the bits that generate nuget-deps.nix:
<syntaxhighlight lang="sh">
dotnet restore --packages=packageDir --use-current-runtime ./SomeProject.csproj
nuget-to-nix packageDir >deps.nix
rm -r packageDir
</syntaxhighlight>
The new parameter <code>--use-current-runtime</code> requires .NET SDK 8+. I believe what it does is explicitly adding packages missing in this runtime vs .NET Standard to packageDir.
If this still does not work, it might indicate a good time to update target frameworks and dependencies.


== See also ==
== See also ==
Line 163: Line 258:
* [https://nixos.org/manual/nixpkgs/unstable/#dotnet dotnet in the nixpkgs manual]
* [https://nixos.org/manual/nixpkgs/unstable/#dotnet dotnet in the nixpkgs manual]
* [https://github.com/search?q=repo%3ANixOS%2Fnixpkgs%20buildDotnetModule&type=code buildDotnetModule references in nixpkgs]
* [https://github.com/search?q=repo%3ANixOS%2Fnixpkgs%20buildDotnetModule&type=code buildDotnetModule references in nixpkgs]
* [https://www.reddit.com/r/NixOS_dotnet NixOS.NET community on Reddit]
* [https://discord.gg/pTpq7Qfs NixOS.NET community on Discord]
* [https://sgt.hootr.club/molten-matter/dotnet-on-nix/ The journey of packaging a .NET app on Nix]
* [https://sgt.hootr.club/molten-matter/dotnet-on-nix/ The journey of packaging a .NET app on Nix]
* https://en.wikipedia.org/wiki/.NET_Framework - The old, windows-only version of .NET. Newer versions (ie. .NET Core) are multiplatform.
* https://en.wikipedia.org/wiki/.NET_Framework - The old, windows-only version of .NET. Newer versions (ie. .NET Core) are multiplatform.
** https://en.wikipedia.org/wiki/Mono_(software) is the deprecated open source implementation of the DotNET compiler and runtime. It has transformed into .NET Core.
** https://en.wikipedia.org/wiki/Mono_(software) is the open source reimplementation of .NET Framework. Its runtime/JIT has been merged into .NET Core, and now it only receives bugfixes.
* https://learn.microsoft.com/en-us/dotnet/core/introduction
* https://learn.microsoft.com/en-us/dotnet/core/introduction


[[Category: Development]]
 
[[Category:Languages]]