2020-11-22 17:35:39 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
eachGeth = config.services.geth;
|
|
|
|
|
|
|
|
gethOpts = { config, lib, name, ...}: {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Go Ethereum Node");
|
2020-11-22 17:35:39 +01:00
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 30303;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Port number Go Ethereum will be listening on, both TCP and UDP.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
http = {
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Go Ethereum HTTP API");
|
2020-11-22 17:35:39 +01:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Listen address of Go Ethereum HTTP API.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8545;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Port number of Go Ethereum HTTP API.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
apis = mkOption {
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "APIs to enable over WebSocket";
|
2020-11-22 17:35:39 +01:00
|
|
|
example = ["net" "eth"];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
websocket = {
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Go Ethereum WebSocket API");
|
2020-11-22 17:35:39 +01:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Listen address of Go Ethereum WebSocket API.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8546;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Port number of Go Ethereum WebSocket API.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
apis = mkOption {
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "APIs to enable over WebSocket";
|
2020-11-22 17:35:39 +01:00
|
|
|
example = ["net" "eth"];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-22 13:29:45 +02:00
|
|
|
authrpc = {
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Go Ethereum Auth RPC API");
|
2022-08-22 13:29:45 +02:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = lib.mdDoc "Listen address of Go Ethereum Auth RPC API.";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8551;
|
|
|
|
description = lib.mdDoc "Port number of Go Ethereum Auth RPC API.";
|
|
|
|
};
|
|
|
|
|
|
|
|
vhosts = mkOption {
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
default = ["localhost"];
|
|
|
|
description = lib.mdDoc "List of virtual hostnames from which to accept requests.";
|
|
|
|
example = ["localhost" "geth.example.org"];
|
|
|
|
};
|
|
|
|
|
|
|
|
jwtsecret = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
|
|
|
description = lib.mdDoc "Path to a JWT secret for authenticated RPC endpoint.";
|
|
|
|
example = "/var/run/geth/jwtsecret";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-22 17:35:39 +01:00
|
|
|
metrics = {
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Go Ethereum prometheus metrics");
|
2020-11-22 17:35:39 +01:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Listen address of Go Ethereum metrics service.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 6060;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Port number of Go Ethereum metrics service.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
network = mkOption {
|
|
|
|
type = types.nullOr (types.enum [ "goerli" "rinkeby" "yolov2" "ropsten" ]);
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "The network to connect to. Mainnet (null) is the default ethereum network.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
syncmode = mkOption {
|
2021-08-02 20:35:41 +02:00
|
|
|
type = types.enum [ "snap" "fast" "full" "light" ];
|
|
|
|
default = "snap";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Blockchain sync mode.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
gcmode = mkOption {
|
|
|
|
type = types.enum [ "full" "archive" ];
|
|
|
|
default = "full";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Blockchain garbage collection mode.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
maxpeers = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 50;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Maximum peers to connect to.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Additional arguments passed to Go Ethereum.";
|
2020-11-22 17:35:39 +01:00
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.go-ethereum.geth;
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = literalExpression "pkgs.go-ethereum.geth";
|
2020-11-22 17:35:39 +01:00
|
|
|
type = types.package;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Package to use as Go Ethereum node.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.geth = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule gethOpts);
|
|
|
|
default = {};
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Specification of one or more geth instances.";
|
2020-11-22 17:35:39 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf (eachGeth != {}) {
|
|
|
|
|
|
|
|
environment.systemPackages = flatten (mapAttrsToList (gethName: cfg: [
|
|
|
|
cfg.package
|
|
|
|
]) eachGeth);
|
|
|
|
|
2022-08-22 13:29:45 +02:00
|
|
|
systemd.services = mapAttrs' (gethName: cfg: let
|
|
|
|
stateDir = "goethereum/${gethName}/${if (cfg.network == null) then "mainnet" else cfg.network}";
|
|
|
|
dataDir = "/var/lib/${stateDir}";
|
|
|
|
in (
|
2020-11-22 17:35:39 +01:00
|
|
|
nameValuePair "geth-${gethName}" (mkIf cfg.enable {
|
|
|
|
description = "Go Ethereum node (${gethName})";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
Restart = "always";
|
2022-08-22 13:29:45 +02:00
|
|
|
StateDirectory = stateDir;
|
2020-11-22 17:35:39 +01:00
|
|
|
|
|
|
|
# Hardening measures
|
|
|
|
PrivateTmp = "true";
|
|
|
|
ProtectSystem = "full";
|
|
|
|
NoNewPrivileges = "true";
|
|
|
|
PrivateDevices = "true";
|
|
|
|
MemoryDenyWriteExecute = "true";
|
|
|
|
};
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
${cfg.package}/bin/geth \
|
|
|
|
--nousb \
|
|
|
|
--ipcdisable \
|
|
|
|
${optionalString (cfg.network != null) ''--${cfg.network}''} \
|
|
|
|
--syncmode ${cfg.syncmode} \
|
|
|
|
--gcmode ${cfg.gcmode} \
|
|
|
|
--port ${toString cfg.port} \
|
|
|
|
--maxpeers ${toString cfg.maxpeers} \
|
|
|
|
${if cfg.http.enable then ''--http --http.addr ${cfg.http.address} --http.port ${toString cfg.http.port}'' else ""} \
|
|
|
|
${optionalString (cfg.http.apis != null) ''--http.api ${lib.concatStringsSep "," cfg.http.apis}''} \
|
|
|
|
${if cfg.websocket.enable then ''--ws --ws.addr ${cfg.websocket.address} --ws.port ${toString cfg.websocket.port}'' else ""} \
|
|
|
|
${optionalString (cfg.websocket.apis != null) ''--ws.api ${lib.concatStringsSep "," cfg.websocket.apis}''} \
|
|
|
|
${optionalString cfg.metrics.enable ''--metrics --metrics.addr ${cfg.metrics.address} --metrics.port ${toString cfg.metrics.port}''} \
|
2022-08-22 13:29:45 +02:00
|
|
|
--authrpc.addr ${cfg.authrpc.address} --authrpc.port ${toString cfg.authrpc.port} --authrpc.vhosts ${lib.concatStringsSep "," cfg.authrpc.vhosts} \
|
|
|
|
${if (cfg.authrpc.jwtsecret != "") then ''--authrpc.jwtsecret ${cfg.authrpc.jwtsecret}'' else ''--authrpc.jwtsecret ${dataDir}/geth/jwtsecret''} \
|
2020-11-22 17:35:39 +01:00
|
|
|
${lib.escapeShellArgs cfg.extraArgs} \
|
2022-08-22 13:29:45 +02:00
|
|
|
--datadir ${dataDir}
|
2020-11-22 17:35:39 +01:00
|
|
|
'';
|
|
|
|
}))) eachGeth;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|