aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules/services/misc/pykms.nix
blob: ab00086e591e1db5fc32d914b3daaa0eac69f73f (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.pykms;

in {
  meta.maintainers = with lib.maintainers; [ peterhoeg ];

  options = {
    services.pykms = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable the PyKMS service.";
      };

      listenAddress = mkOption {
        type = types.str;
        default = "0.0.0.0";
        description = "The IP address on which to listen.";
      };

      port = mkOption {
        type = types.int;
        default = 1688;
        description = "The port on which to listen.";
      };

      verbose = mkOption {
        type = types.bool;
        default = false;
        description = "Show verbose output.";
      };

      openFirewallPort = mkOption {
        type = types.bool;
        default = false;
        description = "Whether the listening port should be opened automatically.";
      };

      memoryLimit = mkOption {
        type = types.str;
        default = "64M";
        description = "How much memory to use at most.";
      };
    };
  };

  config = mkIf cfg.enable {
    networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewallPort [ cfg.port ];

    systemd.services.pykms = let
      home = "/var/lib/pykms";
    in {
      description = "Python KMS";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      # python programs with DynamicUser = true require HOME to be set
      environment.HOME = home;
      serviceConfig = with pkgs; {
        DynamicUser = true;
        StateDirectory = baseNameOf home;
        ExecStartPre = "${getBin pykms}/bin/create_pykms_db.sh ${home}/clients.db";
        ExecStart = lib.concatStringsSep " " ([
          "${getBin pykms}/bin/server.py"
          cfg.listenAddress
          (toString cfg.port)
        ] ++ lib.optional cfg.verbose "--verbose");
        WorkingDirectory = home;
        Restart = "on-failure";
        MemoryLimit = cfg.memoryLimit;
      };
    };
  };
}