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