DotNET: Difference between revisions

Lostmsu (talk | contribs)
added Discord and Reddit communities
Lostmsu (talk | contribs)
create systemd service from an ASP.NET-based package
Line 52: Line 52:
Just remember to add `mono` to `buildInputs` and generate a wrapper script in `postInstall`.
Just remember to add `mono` to `buildInputs` and generate a wrapper script in `postInstall`.


== Building ASP.NET packages ==
== Packaging ASP.NET projects ==


Currently building ASP.NET packages produces website that does not work correctly out of the box because the executable can not find ContentRoot and wwwroot, so all the static assets won't load.
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 `wwwroot`, so all the static assets won't load with 404.
 
> Request finished HTTP/2 GET https://my.app/css/site.css - 404 0
 
The situation can be fixed by setting `WEBROOT` environment variable to the package path.
 
An example of systemd + ASP.NET 8 service:
 
```nix
# myapp package needs to be imported; and added to `environment.systemPackages`
# it is used below
 
systemd.services.my-app = {
  enable = true;
  description = "Runs my.app";
  wantedBy = [ "multi-user.target" ];
  after = [ "network-online.target" ];
  wants = [ "network-online.target" ];
  serviceConfig = {
    AmbientCapabilities = "CAP_NET_BIND_SERVICE"; # allow binding to privileged ports - when you want to expose Kestrel directly without reverse proxy
    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";
  };
};
 
```
[ACME](https://wiki.nixos.org/wiki/ACME)


== .NET location: Not found ==
== .NET location: Not found ==