|
2 | 2 |
|
3 | 3 | let |
4 | 4 | cfg = config.services.zia-server; |
5 | | - ziaServerName = name: "zia-server" + "-" + name; |
6 | | - enabledServers = lib.filterAttrs (name: conf: conf.enable) config.services.zia-server.servers; |
7 | | - |
| 5 | + ziaServerName = name: "zia-server-${name}"; |
| 6 | + enabledServers = lib.filterAttrs (_: conf: conf.enable) config.services.zia-server.servers; |
8 | 7 | in |
9 | 8 | { |
10 | 9 | options = { |
11 | 10 | services.zia-server = { |
12 | | - package = lib.mkOption { |
13 | | - type = lib.types.package; |
14 | | - default = pkgs.zia-server; |
15 | | - defaultText = lib.literalExpression "pkgs.zia-server"; |
16 | | - description = "Which Zia Server derivation to use."; |
17 | | - }; |
| 11 | + package = lib.mkPackageOption pkgs "zia-server" { }; |
18 | 12 |
|
19 | 13 | servers = lib.mkOption { |
20 | | - type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: { |
| 14 | + type = lib.types.attrsOf (lib.types.submodule { |
21 | 15 | options = { |
22 | | - enable = lib.mkEnableOption "Zia Server."; |
| 16 | + enable = lib.mkEnableOption "Zia Server"; |
23 | 17 | listen = { |
24 | 18 | addr = lib.mkOption { |
25 | 19 | type = lib.types.str; |
|
28 | 22 | }; |
29 | 23 | port = lib.mkOption { |
30 | 24 | type = lib.types.port; |
31 | | - description = "The port zia shuld be listening on."; |
| 25 | + description = "The port zia should be listening on."; |
32 | 26 | default = null; |
33 | 27 | }; |
34 | 28 | }; |
|
39 | 33 | }; |
40 | 34 | mode = lib.mkOption { |
41 | 35 | type = lib.types.enum [ "ws" ]; |
42 | | - description = "The mode zia sould be listening with."; |
| 36 | + description = "The mode zia should be listening with."; |
43 | 37 | default = "ws"; |
44 | 38 | }; |
45 | 39 | openFirewall = lib.mkOption { |
|
48 | 42 | description = "Whether to open ports in the firewall for the server."; |
49 | 43 | }; |
50 | 44 | }; |
51 | | - })); |
| 45 | + }); |
52 | 46 | }; |
53 | 47 | }; |
54 | 48 | }; |
55 | 49 |
|
56 | 50 | config = lib.mkIf (enabledServers != { }) { |
57 | | - environment.systemPackages = [ cfg.package ]; |
58 | | - networking.firewall.allowedTCPPorts = lib.mapAttrsToList (_: conf: conf.listen.port) enabledServers; |
| 51 | + networking.firewall.allowedTCPPorts = lib.mkMerge (lib.mapAttrsToList (_: conf: if conf.openFirewall then [ conf.listen.port ] else [ ]) enabledServers); |
59 | 52 |
|
60 | 53 | systemd.services = lib.mapAttrs' |
61 | 54 | (name: conf: lib.nameValuePair (ziaServerName name) { |
62 | | - description = "Zia Server - ${ziaServerName name}"; |
| 55 | + description = "Zia Server (${name})"; |
63 | 56 |
|
64 | 57 | wantedBy = [ "multi-user.target" ]; |
65 | 58 | after = [ "network.target" ]; |
66 | 59 |
|
| 60 | + environment = { |
| 61 | + ZIA_LISTEN_ADDR = "${if (lib.hasInfix ":" conf.listen.addr) then "[${conf.listen.addr}]" else conf.listen.addr}:${toString conf.listen.port}"; |
| 62 | + ZIA_UPSTREAM = conf.upstream; |
| 63 | + ZIA_MODE = conf.mode; |
| 64 | + }; |
| 65 | + |
67 | 66 | serviceConfig = { |
68 | | - ExecStart = "${cfg.package}/bin/zia-server"; |
| 67 | + ExecStart = lib.getExe cfg.package; |
69 | 68 | DynamicUser = true; |
70 | | - User = "zia-server"; |
71 | | - |
72 | | - Environment = [ |
73 | | - "ZIA_LISTEN_ADDR=${if (lib.hasInfix ":" conf.listen.addr) then "[${conf.listen.addr}]" else conf.listen.addr}:${toString conf.listen.port}" |
74 | | - "ZIA_UPSTREAM=${conf.upstream}" |
75 | | - "ZIA_MODE=${conf.mode}" |
76 | | - ]; |
| 69 | + User = "zia-server-${name}"; |
77 | 70 | }; |
78 | 71 | }) |
79 | 72 | enabledServers; |
|
0 commit comments