Skip to content
This repository was archived by the owner on Jan 25, 2025. It is now read-only.

Commit 56402fe

Browse files
committed
nix: improve module
1 parent ede0c52 commit 56402fe

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

nixos-modules/zia-server.nix

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@
22

33
let
44
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;
87
in
98
{
109
options = {
1110
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" { };
1812

1913
servers = lib.mkOption {
20-
type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: {
14+
type = lib.types.attrsOf (lib.types.submodule {
2115
options = {
22-
enable = lib.mkEnableOption "Zia Server.";
16+
enable = lib.mkEnableOption "Zia Server";
2317
listen = {
2418
addr = lib.mkOption {
2519
type = lib.types.str;
@@ -28,7 +22,7 @@ in
2822
};
2923
port = lib.mkOption {
3024
type = lib.types.port;
31-
description = "The port zia shuld be listening on.";
25+
description = "The port zia should be listening on.";
3226
default = null;
3327
};
3428
};
@@ -39,7 +33,7 @@ in
3933
};
4034
mode = lib.mkOption {
4135
type = lib.types.enum [ "ws" ];
42-
description = "The mode zia sould be listening with.";
36+
description = "The mode zia should be listening with.";
4337
default = "ws";
4438
};
4539
openFirewall = lib.mkOption {
@@ -48,32 +42,31 @@ in
4842
description = "Whether to open ports in the firewall for the server.";
4943
};
5044
};
51-
}));
45+
});
5246
};
5347
};
5448
};
5549

5650
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);
5952

6053
systemd.services = lib.mapAttrs'
6154
(name: conf: lib.nameValuePair (ziaServerName name) {
62-
description = "Zia Server - ${ziaServerName name}";
55+
description = "Zia Server (${name})";
6356

6457
wantedBy = [ "multi-user.target" ];
6558
after = [ "network.target" ];
6659

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+
6766
serviceConfig = {
68-
ExecStart = "${cfg.package}/bin/zia-server";
67+
ExecStart = lib.getExe cfg.package;
6968
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}";
7770
};
7871
})
7972
enabledServers;

0 commit comments

Comments
 (0)