1{ config, lib, pkgs, ... }:
2
3with lib;
4let
5 cfg = config.services.pykms;
6 libDir = "/var/lib/pykms";
7
8in
9{
10 meta.maintainers = with lib.maintainers; [ peterhoeg ];
11
12 imports = [
13 (mkRemovedOptionModule [ "services" "pykms" "verbose" ] "Use services.pykms.logLevel instead")
14 ];
15
16 options = {
17 services.pykms = {
18 enable = mkOption {
19 type = types.bool;
20 default = false;
21 description = lib.mdDoc "Whether to enable the PyKMS service.";
22 };
23
24 listenAddress = mkOption {
25 type = types.str;
26 default = "0.0.0.0";
27 description = lib.mdDoc "The IP address on which to listen.";
28 };
29
30 port = mkOption {
31 type = types.port;
32 default = 1688;
33 description = lib.mdDoc "The port on which to listen.";
34 };
35
36 openFirewallPort = mkOption {
37 type = types.bool;
38 default = false;
39 description = lib.mdDoc "Whether the listening port should be opened automatically.";
40 };
41
42 memoryLimit = mkOption {
43 type = types.str;
44 default = "64M";
45 description = lib.mdDoc "How much memory to use at most.";
46 };
47
48 logLevel = mkOption {
49 type = types.enum [ "CRITICAL" "ERROR" "WARNING" "INFO" "DEBUG" "MININFO" ];
50 default = "INFO";
51 description = lib.mdDoc "How much to log";
52 };
53
54 extraArgs = mkOption {
55 type = types.listOf types.str;
56 default = [ ];
57 description = lib.mdDoc "Additional arguments";
58 };
59 };
60 };
61
62 config = mkIf cfg.enable {
63 networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewallPort [ cfg.port ];
64
65 systemd.services.pykms = {
66 description = "Python KMS";
67 after = [ "network.target" ];
68 wantedBy = [ "multi-user.target" ];
69 # python programs with DynamicUser = true require HOME to be set
70 environment.HOME = libDir;
71 serviceConfig = with pkgs; {
72 DynamicUser = true;
73 StateDirectory = baseNameOf libDir;
74 ExecStartPre = "${getBin pykms}/libexec/create_pykms_db.sh ${libDir}/clients.db";
75 ExecStart = lib.concatStringsSep " " ([
76 "${getBin pykms}/bin/server"
77 "--logfile=STDOUT"
78 "--loglevel=${cfg.logLevel}"
79 "--sqlite=${libDir}/clients.db"
80 ] ++ cfg.extraArgs ++ [
81 cfg.listenAddress
82 (toString cfg.port)
83 ]);
84 ProtectHome = "tmpfs";
85 WorkingDirectory = libDir;
86 SyslogIdentifier = "pykms";
87 Restart = "on-failure";
88 MemoryMax = cfg.memoryLimit;
89 };
90 };
91 };
92}