1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.zeronet;
5
6 zConfFile = pkgs.writeTextFile {
7 name = "zeronet.conf";
8
9 text = ''
10 [global]
11 data_dir = ${cfg.dataDir}
12 log_dir = ${cfg.logDir}
13 '' + lib.optionalString (cfg.port != null) ''
14 ui_port = ${toString cfg.port}
15 '' + cfg.extraConfig;
16 };
17in with lib; {
18 options.services.zeronet = {
19 enable = mkEnableOption "zeronet";
20
21 dataDir = mkOption {
22 type = types.path;
23 default = "/var/lib/zeronet";
24 example = "/home/okina/zeronet";
25 description = "Path to the zeronet data directory.";
26 };
27
28 logDir = mkOption {
29 type = types.path;
30 default = "/var/log/zeronet";
31 example = "/home/okina/zeronet/log";
32 description = "Path to the zeronet log directory.";
33 };
34
35 port = mkOption {
36 type = types.nullOr types.int;
37 default = null;
38 example = 15441;
39 description = "Optional zeronet port.";
40 };
41
42 tor = mkOption {
43 type = types.bool;
44 default = false;
45 description = "Use TOR for all zeronet traffic.";
46 };
47
48 extraConfig = mkOption {
49 type = types.lines;
50 default = "";
51
52 description = ''
53 Extra configuration. Contents will be added verbatim to the
54 configuration file at the end.
55 '';
56 };
57 };
58
59 config = mkIf cfg.enable {
60 services.tor = mkIf cfg.tor {
61 enable = true;
62 controlPort = 9051;
63 extraConfig = "CookieAuthentication 1";
64 };
65
66 systemd.services.zeronet = {
67 description = "zeronet";
68 after = [ "network.target" (optionalString cfg.tor "tor.service") ];
69 wantedBy = [ "multi-user.target" ];
70
71 preStart = ''
72 # Ensure folder exists or create it and permissions are correct
73 mkdir -p ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
74 chmod 750 ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
75 chown zeronet:zeronet ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
76 '';
77
78 serviceConfig = {
79 PermissionsStartOnly = true;
80 PrivateTmp = "yes";
81 User = "zeronet";
82 Group = "zeronet";
83 ExecStart = "${pkgs.zeronet}/bin/zeronet --config_file ${zConfFile}";
84 };
85 };
86
87 users = {
88 groups.zeronet.gid = config.ids.gids.zeronet;
89
90 users.zeronet = {
91 description = "zeronet service user";
92 home = cfg.dataDir;
93 createHome = true;
94 group = "zeronet";
95 extraGroups = mkIf cfg.tor [ "tor" ];
96 uid = config.ids.uids.zeronet;
97 };
98 };
99 };
100
101 meta.maintainers = with maintainers; [ chiiruno ];
102}