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