DotNET: Difference between revisions
create systemd service from an ASP.NET-based package |
Lord-Valen (talk | contribs) m Use the correct command name. |
||
(9 intermediate revisions by 6 users not shown) | |||
Line 4: | Line 4: | ||
Example build file: | Example build file: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix" line="1" start="1"> | ||
{ | { | ||
, dotnetCorePackages | buildDotnetModule, | ||
, | dotnetCorePackages, | ||
}: | }: | ||
buildDotnetModule | buildDotnetModule { | ||
pname = " | pname = "hello"; | ||
version = " | version = "0.1"; | ||
src = | src = ./.; | ||
projectFile = " | projectFile = "Hello/Hello.csproj"; | ||
dotnet-sdk = dotnetCorePackages.sdk_8_0; | dotnet-sdk = dotnetCorePackages.sdk_8_0; | ||
dotnet-runtime = dotnetCorePackages.runtime_8_0; | dotnet-runtime = dotnetCorePackages.runtime_8_0; | ||
nugetDeps = ./deps. | nugetDeps = ./deps.json; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If the <code>fetch-deps</code> script isn't working for whatever reason, you can manually run <code>nuget-to- | If the <code>fetch-deps</code> script isn't working for whatever reason, you can manually run <code>nuget-to-json</code>: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="shell-session"> | ||
dotnet restore --packages=packageDir ./SomeProject.csproj | $ dotnet restore --packages=packageDir ./SomeProject.csproj | ||
nuget-to- | $ nuget-to-json packageDir > deps.json | ||
rm -r packageDir | $ rm -r packageDir | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 54: | Line 43: | ||
== Packaging ASP.NET projects == | == 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 | 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. | ||
> Request finished HTTP/2 GET https://my.app/css/site.css - 404 0 | <blockquote> | ||
Request finished HTTP/2 GET https://my.app/css/site.css - 404 0 | |||
</blockquote> | |||
The situation can be fixed by setting | The situation can be fixed by setting <code>WEBROOT</code> environment variable to the package path. | ||
An example of systemd + ASP.NET 8 service: | An example of systemd + ASP.NET 8 service: | ||
<syntaxhighlight lang="nix"> | |||
# myapp package needs to be imported; and added to `environment.systemPackages` | # myapp package needs to be imported; and added to `environment.systemPackages` | ||
# | # the variable myapp is used below | ||
systemd.services.my-app = { | systemd.services.my-app = { | ||
Line 73: | Line 64: | ||
wants = [ "network-online.target" ]; | wants = [ "network-online.target" ]; | ||
serviceConfig = { | 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"; }; | User = "myapp"; # must be created using users.users.myapp = { isSystemUser = true; group = "myapp"; }; | ||
Group = "myapp"; # must be created using users.groups.myapp = {}; | Group = "myapp"; # must be created using users.groups.myapp = {}; | ||
Line 110: | Line 102: | ||
}; | }; | ||
</syntaxhighlight> | |||
[ACME] | |||
See also: setting up SSL certificates using [[ACME]] | |||
== .NET location: Not found == | == .NET location: Not found == | ||
If running a .NET-build executable you get the above error, make sure the DOTNET_ROOT environment variable is set: | 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 = { | environment.sessionVariables = { | ||
DOTNET_ROOT = "${pkgs.dotnet-sdk}"; | 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 | See : https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#net-sdk-and-cli-environment-variables | ||
Line 133: | Line 126: | ||
Wontfix: The project will build only on Windows. | 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. | |||
== NativeAOT == | == NativeAOT == | ||
Line 187: | Line 202: | ||
== Example: Running Rider with dotnet & PowerShell == | == Example: Running Rider with dotnet & PowerShell == | ||
Rider has better compatibility when run in FHS mode | |||
Rider package | Rider package<syntaxhighlight lang="nix"> | ||
< | |||
pkgs.jetbrains.rider | pkgs.jetbrains.rider | ||
</ | </syntaxhighlight>rider-fhs.nix<syntaxhighlight lang="nix"> | ||
{ pkgs ? import <nixpkgs> {} }: | |||
(pkgs.buildFHSEnv { | |||
name = "rider-env"; | |||
targetPkgs = pkgs: (with pkgs; [ | |||
dotnetCorePackages.dotnet_8.sdk | |||
name = " | dotnetCorePackages.dotnet_8.aspnetcore | ||
powershell | powershell | ||
]; | ]); | ||
} | multiPkgs = pkgs: (with pkgs; [ | ||
</ | ]); | ||
runScript = "nohup rider &"; | |||
}).env | |||
</syntaxhighlight><syntaxhighlight lang="nix"> | |||
< | nix-shell ./rider-fhs.nix | ||
nix-shell ./ | </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. <syntaxhighlight> | ||
</ | run-rider = "nix-shell ~/nix/rider-fhs.nix"; | ||
</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. | |||
== Example: multi-SDK installation with local workload installation enabled == | == Example: multi-SDK installation with local workload installation enabled == | ||
Line 238: | Line 244: | ||
* https://learn.microsoft.com/en-us/dotnet/core/introduction | * https://learn.microsoft.com/en-us/dotnet/core/introduction | ||
[[Category: | |||
[[Category:Languages]] |