aboutsummaryrefslogtreecommitdiff
path: root/default.nix
blob: 0f362dc4dca322dac4d14ca4c7313b26a9f090dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{config, lib, pkgs, ...}:
let
  picarones = import ./pkgs.nix { inherit pkgs; };
in
with lib;
{
  options.services.picarones = {
    enable = mkOption {
      default = false;
      example = true;
      type = types.bool;
      description = "'next slide please' as a service";
    };
    package = mkOption {
      description = "the server package to use";
      default = picarones.picarones-hs;
      type = types.package;
    };
    port = mkOption {
      description = "port at which the backend server should listen.";
      default = 9000;
      type = types.int;
    };
    frontend = {
      enable = mkOption {
        description = "Whether to setup nginx to deliver the picarones frontend";
        default = true;
        type = types.bool;
      };
      package = mkOption {
        description = "Which package provides the frontend app.";
        default = picarones.picarones-elm;
        type = types.package;
      };
      domain = mkOption {
        description = "Which domain nginx should listen on.";
        default = "";
        type = types.str;
      };
      proxyBackend = mkOption {
        description = "whether to set up a proxy for the backend using nginx (on the same domain).";
        default = false;
        type = types.bool;
      };
      config = mkOption {
        description = "extra options for nginx.";
        default = {};
        type = types.attrs;
      };
    };
  };

  config = let cfg = config.services.picarones; in {
    systemd.services.picarones-hs = mkIf cfg.enable {
      description = "picarones backend server";
      path = [ cfg.package ];
      wantedBy = [ "multi-user.target" ];
      script = ''
        ${cfg.package}/bin/picarones-hs
      '';
    };

    services.nginx = mkIf cfg.frontend.enable {
      enable = true;
      virtualHosts.${cfg.frontend.domain} = cfg.frontend.config // {
        root =
          # need to do it this (long) way since a user may change this value outside of this module
          let ssl = config.services.nginx.virtualHosts.${cfg.frontend.domain}.enableSSL;
          in (cfg.frontend.package.override ({
            baseurl = (if ssl then "https://" else "http://") + cfg.frontend.domain;
          } // (if cfg.frontend.proxyBackend then {
            serverurl = (if ssl then "wss" else "ws") + "://${cfg.frontend.domain}/websocket";
          } else {} ))).outPath;
        locations = mkIf cfg.frontend.proxyBackend {
          "/websocket" = {
            proxyPass = "http://localhost:9160";
            proxyWebsockets = true;
          };
        };
      };
    };
  };
}