summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/blockchain/ethereum/erigon.nix
blob: 3d80f33ff2148672b3cd1224a83e2a1eb6618e58 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let

  cfg = config.services.erigon;

  settingsFormat = pkgs.formats.toml { };
  configFile = settingsFormat.generate "config.toml" cfg.settings;
in
{

  options = {
    services.erigon = {
      enable = lib.mkEnableOption "Ethereum implementation on the efficiency frontier";

      package = lib.mkPackageOption pkgs "erigon" { };

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

      secretJwtPath = lib.mkOption {
        type = lib.types.path;
        description = ''
          Path to the secret jwt used for the http api authentication.
        '';
        default = "";
        example = "config.age.secrets.ERIGON_JWT.path";
      };

      settings = lib.mkOption {
        description = ''
          Configuration for Erigon
          Refer to <https://github.com/ledgerwatch/erigon#usage> for details on supported values.
        '';

        type = settingsFormat.type;

        example = {
          datadir = "/var/lib/erigon";
          chain = "mainnet";
          http = true;
          "http.port" = 8545;
          "http.api" = [
            "eth"
            "debug"
            "net"
            "trace"
            "web3"
            "erigon"
          ];
          ws = true;
          port = 30303;
          "authrpc.port" = 8551;
          "torrent.port" = 42069;
          "private.api.addr" = "localhost:9090";
          "log.console.verbosity" = 3; # info
        };

        defaultText = lib.literalExpression ''
          {
            datadir = "/var/lib/erigon";
            chain = "mainnet";
            http = true;
            "http.port" = 8545;
            "http.api" = ["eth" "debug" "net" "trace" "web3" "erigon"];
            ws = true;
            port = 30303;
            "authrpc.port" = 8551;
            "torrent.port" = 42069;
            "private.api.addr" = "localhost:9090";
            "log.console.verbosity" = 3; # info
          }
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    # Default values are the same as in the binary, they are just written here for convenience.
    services.erigon.settings = {
      datadir = lib.mkDefault "/var/lib/erigon";
      chain = lib.mkDefault "mainnet";
      http = lib.mkDefault true;
      "http.port" = lib.mkDefault 8545;
      "http.api" = lib.mkDefault [
        "eth"
        "debug"
        "net"
        "trace"
        "web3"
        "erigon"
      ];
      ws = lib.mkDefault true;
      port = lib.mkDefault 30303;
      "authrpc.port" = lib.mkDefault 8551;
      "torrent.port" = lib.mkDefault 42069;
      "private.api.addr" = lib.mkDefault "localhost:9090";
      "log.console.verbosity" = lib.mkDefault 3; # info
    };

    systemd.services.erigon = {
      description = "Erigon ethereum implemenntation";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        LoadCredential = "ERIGON_JWT:${cfg.secretJwtPath}";
        ExecStart = "${cfg.package}/bin/erigon --config ${configFile} --authrpc.jwtsecret=%d/ERIGON_JWT ${lib.escapeShellArgs cfg.extraArgs}";
        DynamicUser = true;
        Restart = "on-failure";
        StateDirectory = "erigon";
        CapabilityBoundingSet = "";
        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"
        ];
      };
    };
  };
}