aboutsummaryrefslogtreecommitdiff
path: root/module.nix
blob: 169db9fe73285a6ed1665300f9e93a4b1d53becc (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
{ config, lib, pkgs, ... }:

let uplcg-pkgs = import ./default.nix {inherit pkgs; };
in
{
  options.services.uplcg = with lib; {
    enable = mkOption {
      type = types.bool;
      default = false;
    };
    cards = mkOption {
      type = with types; nullOr (lazyAttrsOf (lazyAttrsOf (oneOf [
        (listOf str)
        bool
      ])));
      default = null;
    };
    cardsPath = mkOption {
      type = types.nullOr types.path;
      default = null;
    };
    port = mkOption {
      type = types.port;
      default = 4001;
    };
    domain = mkOption {
      type = types.str;
      default = "localhost";
    };
  };

  config =
    let cfg = config.services.uplcg;
    in lib.mkIf cfg.enable {
      systemd.services.uplcg = {
        description = "The Untitled Programming Language Card Game";
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];
        environment = {
          "UPLCG_HOSTNAME" = cfg.domain;
          "UPLCG_PORT" = builtins.toString cfg.port;
        };
        script = ''
            cd ${if cfg.cards != null || cfg.cardsPath != null
                 then uplcg-pkgs.client.override {
                   cards =
                     if cfg.cards == null
                     then cfg.cardsPath
                     else pkgs.writeText "uplcg-cards"
                       (lib.generators.toYAML {} cfg.cards);
                 }
                 else uplcg-pkgs.client
            }
            ${uplcg-pkgs.server}/bin/uplcg-server
          '';
      };
    };
}