aboutsummaryrefslogtreecommitdiff
path: root/modules/dashboard/bacchus-dashboard.service.nix
blob: c14111bb574d088ac2b1149aff41d5b02c6a35b1 (plain) (blame)
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
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.bacchus-dashboard;

  rootHTML = import ./dashboard-template.nix cfg;
  indexHTMLFile = pkgs.writeText "index.html" rootHTML;

  pageDir = pkgs.stdenv.mkDerivation {
    pname = "dashboard-page";
    version = "0.0.0";
    unpackPhase = "true"; # no source
    installPhase = ''
      mkdir -p $out;
      cp ${indexHTMLFile} $out/index.html;
    '';
  };
in
{
  options.services.bacchus-dashboard = {
    enable = mkEnableOption "Enable dashboard";
    title = mkOption { type = types.str; default = "Dashboard"; };
    links = mkOption {
      type = types.listOf (types.attrs);
      default = [];
    };
    openFirewall = mkEnableOption "Open firewall ports";
    host = mkOption { type = types.str; default = "_"; };
    port = mkOption { type = types.int; default = 80; };
  };

  config = {
    services.nginx = mkIf cfg.enable {
      enable = true;
      virtualHosts."${cfg.host}" = {
        listen = [ { addr = toString cfg.port; } ];
        locations."/" = {
          root = pageDir;
          tryFiles = "$uri /index.html";
        };
      };
    };

    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 80 ];
  };
}