summary refs log tree commit diff
path: root/nixos/modules/services/networking/zeronet.nix
blob: 2377cb2c8f1195eac1a6cee660848dd567ab7182 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{ config, lib, pkgs, ... }:

let
  cfg = config.services.zeronet;

  zConfFile = pkgs.writeTextFile {
    name = "zeronet.conf";
    
    text = ''
      [global]
      data_dir = ${cfg.dataDir}
      log_dir = ${cfg.logDir}
    '' + lib.optionalString (cfg.port != null) ''
      ui_port = ${toString cfg.port}
    '' + cfg.extraConfig;
  };
in with lib; {
  options.services.zeronet = {
    enable = mkEnableOption "zeronet";

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/zeronet";
      example = "/home/okina/zeronet";
      description = "Path to the zeronet data directory.";
    };

    logDir = mkOption {
      type = types.path;
      default = "/var/log/zeronet";
      example = "/home/okina/zeronet/log";
      description = "Path to the zeronet log directory.";
    };

    port = mkOption {
      type = types.nullOr types.int;
      default = null;
      example = 15441;
      description = "Optional zeronet port.";
    };

    tor = mkOption {
      type = types.bool;
      default = false;
      description = "Use TOR for all zeronet traffic.";
    };

    extraConfig = mkOption {
      type = types.lines;
      default = "";

      description = ''
        Extra configuration. Contents will be added verbatim to the
        configuration file at the end.
      '';
    };
  };

  config = mkIf cfg.enable {
    services.tor = mkIf cfg.tor {
      enable = true;
      controlPort = 9051;
      extraConfig = "CookieAuthentication 1";
    };
    
    systemd.services.zeronet = {
      description = "zeronet";
      after = [ "network.target" (optionalString cfg.tor "tor.service") ];
      wantedBy = [ "multi-user.target" ];

      preStart = ''
        # Ensure folder exists or create it and permissions are correct
        mkdir -p ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
        chmod 750 ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
        chown zeronet:zeronet ${escapeShellArg cfg.dataDir} ${escapeShellArg cfg.logDir}
      '';

      serviceConfig = {
        PermissionsStartOnly = true;
        PrivateTmp = "yes";
        User = "zeronet";
        Group = "zeronet";
        ExecStart = "${pkgs.zeronet}/bin/zeronet --config_file ${zConfFile}";
      };
    };

    users = {
      groups.zeronet.gid = config.ids.gids.zeronet;

      users.zeronet = {
        description = "zeronet service user";
        home = cfg.dataDir;
        createHome = true;
        group = "zeronet";
        extraGroups = mkIf cfg.tor [ "tor" ];
        uid = config.ids.uids.zeronet;
      };
    };
  };

  meta.maintainers = with maintainers; [ chiiruno ];
}