about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/blockchain/ethereum/lighthouse.nix
blob: 863e737d908ae058dbaae82946e084a993067299 (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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{ config, lib, pkgs, ... }:

with lib;
let

  cfg = config.services.lighthouse;
in {

  options = {
    services.lighthouse = {
      beacon = mkOption {
        description = lib.mdDoc "Beacon node";
        default = {};
        type = types.submodule {
          options = {
            enable = lib.mkEnableOption (lib.mdDoc "Lightouse Beacon node");

            dataDir = mkOption {
              type = types.str;
              default = "/var/lib/lighthouse-beacon";
              description = lib.mdDoc ''
                Directory where data will be stored. Each chain will be stored under it's own specific subdirectory.
              '';
            };

            address = mkOption {
              type = types.str;
              default = "0.0.0.0";
              description = lib.mdDoc ''
                Listen address of Beacon node.
              '';
            };

            port = mkOption {
              type = types.port;
              default = 9000;
              description = lib.mdDoc ''
                Port number the Beacon node will be listening on.
              '';
            };

            openFirewall = mkOption {
              type = types.bool;
              default = false;
              description = lib.mdDoc ''
                Open the port in the firewall
              '';
            };

            disableDepositContractSync = mkOption {
              type = types.bool;
              default = false;
              description = lib.mdDoc ''
                Explicitly disables syncing of deposit logs from the execution node.
                This overrides any previous option that depends on it.
                Useful if you intend to run a non-validating beacon node.
              '';
            };

            execution = {
              address = mkOption {
                type = types.str;
                default = "127.0.0.1";
                description = lib.mdDoc ''
                  Listen address for the execution layer.
                '';
              };

              port = mkOption {
                type = types.port;
                default = 8551;
                description = lib.mdDoc ''
                  Port number the Beacon node will be listening on for the execution layer.
                '';
              };

              jwtPath = mkOption {
                type = types.str;
                default = "";
                description = lib.mdDoc ''
                  Path for the jwt secret required to connect to the execution layer.
                '';
              };
            };

            http = {
              enable = lib.mkEnableOption (lib.mdDoc "Beacon node http api");
              port = mkOption {
                type = types.port;
                default = 5052;
                description = lib.mdDoc ''
                  Port number of Beacon node RPC service.
                '';
              };

              address = mkOption {
                type = types.str;
                default = "127.0.0.1";
                description = lib.mdDoc ''
                  Listen address of Beacon node RPC service.
                '';
              };
            };

            metrics = {
              enable = lib.mkEnableOption (lib.mdDoc "Beacon node prometheus metrics");
              address = mkOption {
                type = types.str;
                default = "127.0.0.1";
                description = lib.mdDoc ''
                  Listen address of Beacon node metrics service.
                '';
              };

              port = mkOption {
                type = types.port;
                default = 5054;
                description = lib.mdDoc ''
                  Port number of Beacon node metrics service.
                '';
              };
            };

            extraArgs = mkOption {
              type = types.str;
              description = lib.mdDoc ''
                Additional arguments passed to the lighthouse beacon command.
              '';
              default = "";
              example = "";
            };
          };
        };
      };

      validator = mkOption {
        description = lib.mdDoc "Validator node";
        default = {};
        type = types.submodule {
          options = {
            enable = mkOption {
              type = types.bool;
              default = false;
              description = lib.mdDoc "Enable Lightouse Validator node.";
            };

            dataDir = mkOption {
              type = types.str;
              default = "/var/lib/lighthouse-validator";
              description = lib.mdDoc ''
                Directory where data will be stored. Each chain will be stored under it's own specific subdirectory.
              '';
            };

            beaconNodes = mkOption {
              type = types.listOf types.str;
              default = ["http://localhost:5052"];
              description = lib.mdDoc ''
                Beacon nodes to connect to.
              '';
            };

            metrics = {
              enable = lib.mkEnableOption (lib.mdDoc "Validator node prometheus metrics");
              address = mkOption {
                type = types.str;
                default = "127.0.0.1";
                description = lib.mdDoc ''
                  Listen address of Validator node metrics service.
                '';
              };

              port = mkOption {
                type = types.port;
                default = 5056;
                description = lib.mdDoc ''
                  Port number of Validator node metrics service.
                '';
              };
            };

            extraArgs = mkOption {
              type = types.str;
              description = lib.mdDoc ''
                Additional arguments passed to the lighthouse validator command.
              '';
              default = "";
              example = "";
            };
          };
        };
      };

      network = mkOption {
        type = types.enum [ "mainnet" "prater" "goerli" "gnosis" "kiln" "ropsten" "sepolia" ];
        default = "mainnet";
        description = lib.mdDoc ''
          The network to connect to. Mainnet is the default ethereum network.
        '';
      };

      extraArgs = mkOption {
        type = types.str;
        description = lib.mdDoc ''
          Additional arguments passed to every lighthouse command.
        '';
        default = "";
        example = "";
      };
    };
  };

  config = mkIf (cfg.beacon.enable || cfg.validator.enable) {

    environment.systemPackages = [ pkgs.lighthouse ] ;

    networking.firewall = mkIf cfg.beacon.enable {
      allowedTCPPorts = mkIf cfg.beacon.openFirewall [ cfg.beacon.port ];
      allowedUDPPorts = mkIf cfg.beacon.openFirewall [ cfg.beacon.port ];
    };


    systemd.services.lighthouse-beacon = mkIf cfg.beacon.enable {
      description = "Lighthouse beacon node (connect to P2P nodes and verify blocks)";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      script = ''
        # make sure the chain data directory is created on first run
        mkdir -p ${cfg.beacon.dataDir}/${cfg.network}

        ${pkgs.lighthouse}/bin/lighthouse beacon_node \
          --disable-upnp \
          ${lib.optionalString cfg.beacon.disableDepositContractSync "--disable-deposit-contract-sync"} \
          --port ${toString cfg.beacon.port} \
          --listen-address ${cfg.beacon.address} \
          --network ${cfg.network} \
          --datadir ${cfg.beacon.dataDir}/${cfg.network} \
          --execution-endpoint http://${cfg.beacon.execution.address}:${toString cfg.beacon.execution.port} \
          --execution-jwt ''${CREDENTIALS_DIRECTORY}/LIGHTHOUSE_JWT \
          ${lib.optionalString cfg.beacon.http.enable '' --http --http-address ${cfg.beacon.http.address} --http-port ${toString cfg.beacon.http.port}''} \
          ${lib.optionalString cfg.beacon.metrics.enable '' --metrics --metrics-address ${cfg.beacon.metrics.address} --metrics-port ${toString cfg.beacon.metrics.port}''} \
          ${cfg.extraArgs} ${cfg.beacon.extraArgs}
      '';
      serviceConfig = {
        LoadCredential = "LIGHTHOUSE_JWT:${cfg.beacon.execution.jwtPath}";
        DynamicUser = true;
        Restart = "on-failure";
        StateDirectory = "lighthouse-beacon";
        ReadWritePaths = [ cfg.beacon.dataDir ];
        NoNewPrivileges = true;
        PrivateTmp = true;
        ProtectHome = true;
        ProtectClock = true;
        ProtectProc = "noaccess";
        ProcSubset = "pid";
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        RestrictSUIDSGID = true;
        RestrictRealtime = true;
        RestrictNamespaces = true;
        LockPersonality = true;
        RemoveIPC = true;
        SystemCallFilter = [ "@system-service" "~@privileged" ];
      };
    };

    systemd.services.lighthouse-validator = mkIf cfg.validator.enable {
      description = "Lighthouse validtor node (manages validators, using data obtained from the beacon node via a HTTP API)";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      script = ''
        # make sure the chain data directory is created on first run
        mkdir -p ${cfg.validator.dataDir}/${cfg.network}

        ${pkgs.lighthouse}/bin/lighthouse validator_client \
          --network ${cfg.network} \
          --beacon-nodes ${lib.concatStringsSep "," cfg.validator.beaconNodes} \
          --datadir ${cfg.validator.dataDir}/${cfg.network} \
          ${optionalString cfg.validator.metrics.enable ''--metrics --metrics-address ${cfg.validator.metrics.address} --metrics-port ${toString cfg.validator.metrics.port}''} \
          ${cfg.extraArgs} ${cfg.validator.extraArgs}
      '';

      serviceConfig = {
        Restart = "on-failure";
        StateDirectory = "lighthouse-validator";
        ReadWritePaths = [ cfg.validator.dataDir ];
        CapabilityBoundingSet = "";
        DynamicUser = true;
        NoNewPrivileges = true;
        PrivateTmp = true;
        ProtectHome = true;
        ProtectClock = true;
        ProtectProc = "noaccess";
        ProcSubset = "pid";
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        RestrictSUIDSGID = true;
        RestrictRealtime = true;
        RestrictNamespaces = true;
        LockPersonality = true;
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        SystemCallFilter = [ "@system-service" "~@privileged" ];
      };
    };
  };
}