summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/blockchain/ethereum/geth.nix
blob: f58ca9d9819a9d8acd8057faf3f7d1084d46b535 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
{
  config,
  lib,
  pkgs,
  ...
}:
let
  eachGeth = config.services.geth;

  gethOpts =
    {
      config,
      lib,
      name,
      ...
    }:
    {

      options = {

        enable = lib.mkEnableOption "Go Ethereum Node";

        port = lib.mkOption {
          type = lib.types.port;
          default = 30303;
          description = "Port number Go Ethereum will be listening on, both TCP and UDP.";
        };

        http = {
          enable = lib.mkEnableOption "Go Ethereum HTTP API";
          address = lib.mkOption {
            type = lib.types.str;
            default = "127.0.0.1";
            description = "Listen address of Go Ethereum HTTP API.";
          };

          port = lib.mkOption {
            type = lib.types.port;
            default = 8545;
            description = "Port number of Go Ethereum HTTP API.";
          };

          apis = lib.mkOption {
            type = lib.types.nullOr (lib.types.listOf lib.types.str);
            default = null;
            description = "APIs to enable over WebSocket";
            example = [
              "net"
              "eth"
            ];
          };
        };

        websocket = {
          enable = lib.mkEnableOption "Go Ethereum WebSocket API";
          address = lib.mkOption {
            type = lib.types.str;
            default = "127.0.0.1";
            description = "Listen address of Go Ethereum WebSocket API.";
          };

          port = lib.mkOption {
            type = lib.types.port;
            default = 8546;
            description = "Port number of Go Ethereum WebSocket API.";
          };

          apis = lib.mkOption {
            type = lib.types.nullOr (lib.types.listOf lib.types.str);
            default = null;
            description = "APIs to enable over WebSocket";
            example = [
              "net"
              "eth"
            ];
          };
        };

        authrpc = {
          enable = lib.mkEnableOption "Go Ethereum Auth RPC API";
          address = lib.mkOption {
            type = lib.types.str;
            default = "127.0.0.1";
            description = "Listen address of Go Ethereum Auth RPC API.";
          };

          port = lib.mkOption {
            type = lib.types.port;
            default = 8551;
            description = "Port number of Go Ethereum Auth RPC API.";
          };

          vhosts = lib.mkOption {
            type = lib.types.nullOr (lib.types.listOf lib.types.str);
            default = [ "localhost" ];
            description = "List of virtual hostnames from which to accept requests.";
            example = [
              "localhost"
              "geth.example.org"
            ];
          };

          jwtsecret = lib.mkOption {
            type = lib.types.str;
            default = "";
            description = "Path to a JWT secret for authenticated RPC endpoint.";
            example = "/var/run/geth/jwtsecret";
          };
        };

        metrics = {
          enable = lib.mkEnableOption "Go Ethereum prometheus metrics";
          address = lib.mkOption {
            type = lib.types.str;
            default = "127.0.0.1";
            description = "Listen address of Go Ethereum metrics service.";
          };

          port = lib.mkOption {
            type = lib.types.port;
            default = 6060;
            description = "Port number of Go Ethereum metrics service.";
          };
        };

        network = lib.mkOption {
          type = lib.types.nullOr (
            lib.types.enum [
              "holesky"
              "sepolia"
            ]
          );
          default = null;
          description = "The network to connect to. Mainnet (null) is the default ethereum network.";
        };

        syncmode = lib.mkOption {
          type = lib.types.enum [
            "snap"
            "fast"
            "full"
            "light"
          ];
          default = "snap";
          description = "Blockchain sync mode.";
        };

        gcmode = lib.mkOption {
          type = lib.types.enum [
            "full"
            "archive"
          ];
          default = "full";
          description = "Blockchain garbage collection mode.";
        };

        maxpeers = lib.mkOption {
          type = lib.types.int;
          default = 50;
          description = "Maximum peers to connect to.";
        };

        extraArgs = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          description = "Additional arguments passed to Go Ethereum.";
          default = [ ];
        };

        package = lib.mkPackageOption pkgs [ "go-ethereum" "geth" ] { };
      };
    };
in

{

  ###### interface

  options = {
    services.geth = lib.mkOption {
      type = lib.types.attrsOf (lib.types.submodule gethOpts);
      default = { };
      description = "Specification of one or more geth instances.";
    };
  };

  ###### implementation

  config = lib.mkIf (eachGeth != { }) {

    environment.systemPackages = lib.flatten (
      lib.mapAttrsToList (gethName: cfg: [
        cfg.package
      ]) eachGeth
    );

    systemd.services = lib.mapAttrs' (
      gethName: cfg:
      let
        stateDir = "goethereum/${gethName}/${if (cfg.network == null) then "mainnet" else cfg.network}";
        dataDir = "/var/lib/${stateDir}";
      in
      (lib.nameValuePair "geth-${gethName}" (
        lib.mkIf cfg.enable {
          description = "Go Ethereum node (${gethName})";
          wantedBy = [ "multi-user.target" ];
          after = [ "network.target" ];

          serviceConfig = {
            ExecStart =
              let
                args = lib.cli.toCommandLineShellGNU { } {
                  inherit (cfg)
                    syncmode
                    gcmode
                    port
                    maxpeers
                    ;
                  nousb = true;
                  ipcdisable = true;
                  datadir = dataDir;
                  ${cfg.network} = true;

                  http = cfg.http.enable;
                  "http.addr" = if cfg.http.enable then cfg.http.address else null;
                  "http.port" = if cfg.http.enable then cfg.http.port else null;
                  "http.api" = if cfg.http.apis != null then lib.concatStringsSep "," cfg.http.apis else null;

                  ws = cfg.websocket.enable;
                  "ws.addr" = if cfg.websocket.enable then cfg.websocket.address else null;
                  "ws.port" = if cfg.websocket.enable then cfg.websocket.port else null;
                  "ws.api" = if cfg.websocket.apis != null then lib.concatStringsSep "," cfg.websocket.apis else null;

                  metrics = cfg.metrics.enable;
                  "metrics.addr" = if cfg.metrics.enable then cfg.metrics.address else null;
                  "metrics.port" = if cfg.metrics.enable then cfg.metrics.port else null;

                  "authrpc.addr" = cfg.authrpc.address;
                  "authrpc.port" = cfg.authrpc.port;
                  "authrpc.vhosts" = lib.concatStringsSep "," cfg.authrpc.vhosts;
                  "authrpc.jwtsecret" =
                    if cfg.authrpc.jwtsecret != "" then cfg.authrpc.jwtsecret else "${dataDir}/geth/jwtsecret";
                };
              in
              "${lib.getExe cfg.package} ${args} ${lib.escapeShellArgs cfg.extraArgs}";

            DynamicUser = true;
            Restart = "always";
            StateDirectory = stateDir;

            # Hardening measures
            PrivateTmp = "true";
            ProtectSystem = "full";
            NoNewPrivileges = "true";
            PrivateDevices = "true";
            MemoryDenyWriteExecute = "true";
          };
        }
      ))
    ) eachGeth;

  };

}