summaryrefslogtreecommitdiff
path: root/module.nix
blob: f06b88476be85a32dc8e2be3e89f52e936c3fb22 (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, pkgs, lib, system, self, ... }:
let cfg = config.services.nuogai;
in with lib; {
  options.services.nuogai = {
    enable = mkEnableOption "Enables the nuogaı Discord Bot";
    ports = listToAttrs
      (map (flip attrsets.nameValuePair (mkOption { type = types.port; })) [
        "nuigui"
        "serial-predicate-engine"
        "toadua"
      ]);
    tokenPath = mkOption { type = types.path; };
  };
  config = mkIf cfg.enable {
    fonts.fonts = [ self.packages.${system}.toaqScript ];
    systemd.services = lib.mapAttrs (k: v:
      {
        wants = [ "network-online.target" ];
      } // (v self.packages.${system}.${k})) {
        nuogai = pkg: {
          description = "Toaq Discord bot";
          wantedBy = [ "multi-user.target" ];
          wants = [ "nuigui.service" "serial-predicate-engine.service" ];
          environment = {
            NUI_PORT = toString cfg.ports.nuigui;
            SPE_PORT = toString cfg.ports.serial-predicate-engine;
            TOA_PORT = toString cfg.ports.toadua;
          };
          script = ''
            export TOKEN=$(cat ${cfg.tokenPath})
            ${pkg}/bin/nuogai
          '';
        };
        nuigui = pkg: {
          serviceConfig.WorkingDirectory = pkg;
          serviceConfig.ExecStart = "${pkg}/bin/nuigui";
          environment.PORT = toString cfg.ports.nuigui;
        };
        serial-predicate-engine = pkg: {
          serviceConfig.WorkingDirectory = pkg;
          serviceConfig.ExecStart = "${pkg}/bin/serial-predicate-engine";
          environment.PORT = toString cfg.ports.serial-predicate-engine;
        };
      };
  };
}