1{ config, lib, pkgs, ... }:
2
3with lib;
4let
5 cfg = config.services.notbit;
6 varDir = "/var/lib/notbit";
7
8 sendmail = pkgs.stdenv.mkDerivation {
9 name = "notbit-wrapper";
10 buildInputs = [ pkgs.makeWrapper ];
11 propagatedBuildInputs = [ pkgs.notbit ];
12 buildCommand = ''
13 mkdir -p $out/bin
14 makeWrapper ${pkgs.notbit}/bin/notbit-sendmail $out/bin/notbit-system-sendmail \
15 --set XDG_RUNTIME_DIR ${varDir}
16 '';
17 };
18 opts = "${optionalString cfg.allowPrivateAddresses "-L"} ${optionalString cfg.noBootstrap "-b"} ${optionalString cfg.specifiedPeersOnly "-e"}";
19 peers = concatStringsSep " " (map (str: "-P \"${str}\"") cfg.peers);
20 listen = if cfg.listenAddress == [] then "-p ${toString cfg.port}" else
21 concatStringsSep " " (map (addr: "-a \"${addr}:${toString cfg.port}\"") cfg.listenAddress);
22in
23
24with lib;
25{
26
27 ### configuration
28
29 options = {
30
31 services.notbit = {
32
33 enable = mkOption {
34 type = types.bool;
35 default = false;
36 description = ''
37 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.
38 '';
39 };
40
41 port = mkOption {
42 type = types.int;
43 default = 8444;
44 description = "The port which the daemon listens for other bitmessage clients";
45 };
46
47 nice = mkOption {
48 type = types.int;
49 default = 10;
50 description = "Set the nice level for the notbit daemon";
51 };
52
53 listenAddress = mkOption {
54 type = types.listOf types.str;
55 default = [ ];
56 example = [ "localhost" "myhostname" ];
57 description = "The addresses which notbit will use to listen for incoming connections. These addresses are advertised to connecting clients.";
58 };
59
60 peers = mkOption {
61 type = types.listOf types.str;
62 default = [ ];
63 example = [ "bitmessage.org:8877" ];
64 description = "The initial set of peers notbit will connect to.";
65 };
66
67 specifiedPeersOnly = mkOption {
68 type = types.bool;
69 default = false;
70 description = "If true, notbit will only connect to peers specified by the peers option.";
71 };
72
73 allowPrivateAddresses = mkOption {
74 type = types.bool;
75 default = false;
76 description = "If true, notbit will allow connections to to RFC 1918 addresses.";
77 };
78
79 noBootstrap = mkOption {
80 type = types.bool;
81 default = false;
82 description = "If true, notbit will not bootstrap an initial peerlist from bitmessage.org servers";
83 };
84
85 };
86
87 };
88
89 ### implementation
90
91 config = mkIf cfg.enable {
92
93 environment.systemPackages = [ sendmail ];
94
95 systemd.services.notbit = {
96 description = "Notbit daemon";
97 after = [ "network.target" ];
98 wantedBy = [ "multi-user.target" ];
99 path = [ pkgs.notbit ];
100 environment = { XDG_RUNTIME_DIR = varDir; };
101
102 postStart = ''
103 [ ! -f "${varDir}/addr" ] && notbit-keygen > ${varDir}/addr
104 chmod 0640 ${varDir}/{addr,notbit/notbit-ipc.lock}
105 chmod 0750 ${varDir}/notbit/{,notbit-ipc}
106 '';
107
108 serviceConfig = {
109 Type = "forking";
110 ExecStart = "${pkgs.notbit}/bin/notbit -d ${listen} ${peers} ${opts}";
111 User = "notbit";
112 Group = "notbit";
113 UMask = "0077";
114 WorkingDirectory = varDir;
115 Nice = cfg.nice;
116 };
117 };
118
119 users.extraUsers.notbit = {
120 group = "notbit";
121 description = "Notbit daemon user";
122 home = varDir;
123 createHome = true;
124 uid = config.ids.uids.notbit;
125 };
126
127 users.extraGroups.notbit.gid = config.ids.gids.notbit;
128 };
129
130}