summary refs log tree commit diff
path: root/nixos/modules/services/networking/notbit.nix
blob: a96e181cb808f241a7383400925f3fa36e9e0b80 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.notbit;
  varDir = "/var/lib/notbit";
  
  sendmail = pkgs.stdenv.mkDerivation {
    name = "notbit-wrapper";
    buildInputs = [ pkgs.makeWrapper ];
    propagatedBuildInputs = [ pkgs.notbit ];
    buildCommand = ''
      mkdir -p $out/bin
      makeWrapper ${pkgs.notbit}/bin/notbit-sendmail $out/bin/notbit-system-sendmail \
        --set XDG_RUNTIME_DIR ${varDir}
    '';
  };
  opts = "${optionalString cfg.allowPrivateAddresses "-L"} ${optionalString cfg.noBootstrap "-b"} ${optionalString cfg.specifiedPeersOnly "-e"}";
  peers = concatStringsSep " " (map (str: "-P \"${str}\"") cfg.peers);
  listen = if cfg.listenAddress == [] then "-p ${toString cfg.port}" else
    concatStringsSep " " (map (addr: "-a \"${addr}:${toString cfg.port}\"") cfg.listenAddress);
in

with lib;
{

  ### configuration

  options = {

    services.notbit = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Enables the notbit daemon and provides a sendmail binary named `notbit-system-sendmail` for sending mail over the system instance of notbit. Users must be in the notbit group in order to send mail over the system notbit instance. Currently mail recipt is not supported.
        '';
      };

      port = mkOption {
        type = types.int;
        default = 8444;
        description = "The port which the daemon listens for other bitmessage clients";
      };

      nice = mkOption {
        type = types.int;
        default = 10;
        description = "Set the nice level for the notbit daemon";
      };

      listenAddress = mkOption {
        type = types.listOf types.str;
        default = [ ];
        example = [ "localhost" "myhostname" ];
        description = "The addresses which notbit will use to listen for incoming connections. These addresses are advertised to connecting clients.";
      };

      peers = mkOption {
        type = types.listOf types.str;
        default = [ ];
        example = [ "bitmessage.org:8877" ];
        description = "The initial set of peers notbit will connect to.";
      };

      specifiedPeersOnly = mkOption {
        type = types.bool;
        default = false;
        description = "If true, notbit will only connect to peers specified by the peers option.";
      };

      allowPrivateAddresses = mkOption {
        type = types.bool;
        default = false;
        description = "If true, notbit will allow connections to to RFC 1918 addresses.";
      };

      noBootstrap = mkOption {
        type = types.bool;
        default = false;
        description = "If true, notbit will not bootstrap an initial peerlist from bitmessage.org servers";
      };

    };

  };

  ### implementation

  config = mkIf cfg.enable {

    environment.systemPackages = [ sendmail ];

    systemd.services.notbit = {
      description = "Notbit daemon";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      path = [ pkgs.notbit ];
      environment = { XDG_RUNTIME_DIR = varDir; };

      postStart = ''
        [ ! -f "${varDir}/addr" ] && notbit-keygen > ${varDir}/addr
        chmod 0640 ${varDir}/{addr,notbit/notbit-ipc.lock}
        chmod 0750 ${varDir}/notbit/{,notbit-ipc}
      '';

      serviceConfig = {
        Type = "forking";
        ExecStart = "${pkgs.notbit}/bin/notbit -d ${listen} ${peers} ${opts}";
        User = "notbit";
        Group = "notbit";
        UMask = "0077";
        WorkingDirectory = varDir;
        Nice = cfg.nice;
      };
    };

    users.extraUsers.notbit = {
      group = "notbit";
      description = "Notbit daemon user";
      home = varDir;
      createHome = true;
      uid = config.ids.uids.notbit;
    };

    users.extraGroups.notbit.gid = config.ids.gids.notbit;
  };

}