1{ config, lib, pkgs, ... }:
2with lib;
3let
4 cfg = config.services.mainsail;
5 moonraker = config.services.moonraker;
6in
7{
8 options.services.mainsail = {
9 enable = mkEnableOption (lib.mdDoc "a modern and responsive user interface for Klipper");
10
11 package = mkOption {
12 type = types.package;
13 description = lib.mdDoc "Mainsail package to be used in the module";
14 default = pkgs.mainsail;
15 defaultText = literalExpression "pkgs.mainsail";
16 };
17
18 hostName = mkOption {
19 type = types.str;
20 default = "localhost";
21 description = lib.mdDoc "Hostname to serve mainsail on";
22 };
23
24 nginx = mkOption {
25 type = types.submodule
26 (import ../web-servers/nginx/vhost-options.nix { inherit config lib; });
27 default = { };
28 example = literalExpression ''
29 {
30 serverAliases = [ "mainsail.''${config.networking.domain}" ];
31 }
32 '';
33 description = lib.mdDoc "Extra configuration for the nginx virtual host of mainsail.";
34 };
35 };
36
37 config = mkIf cfg.enable {
38 services.nginx = {
39 enable = true;
40 upstreams.mainsail-apiserver.servers."${moonraker.address}:${toString moonraker.port}" = { };
41 virtualHosts."${cfg.hostName}" = mkMerge [
42 cfg.nginx
43 {
44 root = mkForce "${cfg.package}/share/mainsail";
45 locations = {
46 "/" = {
47 index = "index.html";
48 tryFiles = "$uri $uri/ /index.html";
49 };
50 "/index.html".extraConfig = ''
51 add_header Cache-Control "no-store, no-cache, must-revalidate";
52 '';
53 "/websocket" = {
54 proxyWebsockets = true;
55 proxyPass = "http://mainsail-apiserver/websocket";
56 };
57 "~ ^/(printer|api|access|machine|server)/" = {
58 proxyWebsockets = true;
59 proxyPass = "http://mainsail-apiserver$request_uri";
60 };
61 };
62 }
63 ];
64 };
65 };
66}