Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/unify service args #78

Merged
merged 3 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,19 @@ which aren't there is typically and an `extraArgs` array that can be passed to t
```nix
services.geth.mainnet = {
enable = true;
openFirewall = true;
service.supplementaryGroups = [users.groups.keys.name];
};

services.geth.goerli = {
enable = true;
network = "goerli";
dataDir = "/data/ethereum/goerli/geth";
authrpc.jwtsecret = sops.secrets.geth_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
openFirewall = true;
args = {
network = "goerli";
dataDir = "/data/ethereum/goerli/geth";
authrpc.jwtsecret = sops.secrets.geth_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
}
};
```

Expand All @@ -196,18 +200,41 @@ services.geth.goerli = {
```nix
services.prysm.beacon.mainnet = {
enable = true;
jwt-secret = secrets.prysm_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
args = {
jwt-secret = secrets.prysm_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
};
};

services.prysm.beacon.goerli = {
enable = true;
network = "goerli";
dataDir = "/data/ethereum/goerli/prysm-beacon";
jwt-secret = secrets.prysm_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
checkpoint.sync-url = "https://goerli.checkpoint-sync.ethpandaops.io";
genesis.beacon-api-url = "https://goerli.checkpoint-sync.ethpandaops.io";
args = {
network = "goerli";
dataDir = "/data/ethereum/goerli/prysm-beacon";
jwt-secret = secrets.prysm_jwt_secret.path;
service.supplementaryGroups = [users.groups.keys.name];
checkpoint.sync-url = "https://goerli.checkpoint-sync.ethpandaops.io";
genesis.beacon-api-url = "https://goerli.checkpoint-sync.ethpandaops.io";
};
};
```

### Erigon

```nix
services.erigon.sepolia = {
enable = true;
openFirewall = true;
args = {
chain = "sepolia";
datadir = "/data/ethereum/sepolia/erigon";
http = {
enable = true;
addr = "0.0.0.0";
api = ["eth" "erigon" "engine" "sealer" "net"];
vhosts = ["localhost" "dione"];
};
};
};
```

Expand Down
19 changes: 0 additions & 19 deletions modules/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,8 @@
(path: opt: mkFlag {inherit path opt args pathReducer;})
opts
);

flag = name: pred:
if pred
then "--${name}"
else "";
optionalArg = name: pred: value:
if pred
then "--${name} ${toString value}"
else "";
arg = name: value: (optionalArg name true value);

joinArgs = args: let
flattened = flatten args;
filtered = builtins.filter (arg: arg != "") flattened;
in
concatStringsSep " \\\n" filtered;
in {
flags = {
inherit mkFlag mkFlags defaultPathReducer dotPathReducer;
};
script = {
inherit flag arg optionalArg joinArgs;
};
}
60 changes: 34 additions & 26 deletions modules/prysm/beacon.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
}: let
inherit (lib) literalExpression mkEnableOption mkIf mkOption types optionalString;
inherit (lib) mdDoc flatten nameValuePair zipAttrsWith mapAttrs' filterAttrsRecursive mapAttrsToList filterAttrs;
inherit (lib.lists) optionals;
inherit (lib.lists) optionals findFirst;
inherit (builtins) concatStringsSep;

settingsFormat = pkgs.formats.yaml {};

Expand All @@ -16,7 +17,7 @@
options = {
enable = mkEnableOption (mdDoc "Ethereum Beacon Chain Node from Prysmatic Labs");

settings = {
args = {
datadir = mkOption {
type = types.nullOr types.path;
default = null;
Expand Down Expand Up @@ -117,7 +118,7 @@
description = mdDoc "Enable the pprof HTTP server.";
};

pprofhost = mkOption {
pprofaddr = mkOption {
type = types.str;
default = "127.0.0.1";
description = mdDoc "pprof HTTP server listening interface.";
Expand All @@ -130,12 +131,6 @@
};
};

extraSettings = mkOption {
type = settingsFormat.type;
default = {};
description = mdDoc "Additional settings to pass to Prysm Beacon Chain.";
};

extraArgs = mkOption {
type = types.listOf types.str;
description = mdDoc "Additional arguments to pass to Prysm Beacon Chain.";
Expand Down Expand Up @@ -208,8 +203,8 @@ in {
(mapAttrsToList
(
beaconName: cfg:
lib.lists.optionals (cfg.settings.datadir != null) [
"d ${cfg.settings.datadir} 0700 prysm-beacon-${beaconName} prysm-beacon-${beaconName} - -"
lib.lists.optionals (cfg.args.datadir != null) [
"d ${cfg.args.datadir} 0700 prysm-beacon-${beaconName} prysm-beacon-${beaconName} - -"
]
)
eachBeacon);
Expand All @@ -221,7 +216,7 @@ in {
mapAttrsToList
(
_: cfg:
with cfg.settings; {
with cfg.args; {
allowedUDPPorts = [p2p-udp-port];
allowedTCPPorts =
[rpc-port p2p-tcp-port]
Expand All @@ -241,8 +236,8 @@ in {
stateDir = "prysm-beacon-${beaconName}";
datadir = "/var/lib/${stateDir}";

inherit (import ../lib.nix {inherit lib pkgs;}) script;
inherit (script) flag arg optionalArg joinArgs;
modulesLib = import ../lib.nix {inherit lib pkgs;};
inherit (modulesLib.flags) mkFlags;
in
cfg:
nameValuePair "prysm-beacon-${beaconName}" (mkIf cfg.enable {
Expand All @@ -251,8 +246,8 @@ in {
after = ["network.target"];

unitConfig = {
RequiresMountsFor = optionals (cfg.settings.datadir != null) [
cfg.settings.datadir
RequiresMountsFor = optionals (cfg.args.datadir != null) [
cfg.args.datadir
];
};

Expand All @@ -265,8 +260,8 @@ in {
SupplementaryGroups = cfg.service.supplementaryGroups;

# bind custom data dir to /var/lib/... if provided
BindPaths = lib.lists.optionals (cfg.settings.datadir != null) [
"${cfg.settings.datadir}:${datadir}"
BindPaths = lib.lists.optionals (cfg.args.datadir != null) [
"${cfg.args.datadir}:${datadir}"
];

# Hardening measures
Expand All @@ -293,17 +288,30 @@ in {
# MemoryDenyWriteExecute = "true"; causes a library loading error
};

script = with cfg; let
# filter null values and merge with extra settings
settings = lib.recursiveUpdate (filterAttrsRecursive (_: v: v != null) cfg.settings) cfg.extraSettings;
# generate the yaml config file
configFile = settingsFormat.generate "config.yaml" settings;
script = let
# filter out certain args which need to be treated differently
specialArgs = ["network" "datadir"];
isNormalArg = name: (findFirst (a: a == name) null specialArgs) == null;

filteredOpts = filterAttrs (n: v: isNormalArg n) beaconOpts.options.args;

# generate flags
flags = mkFlags {
opts = filteredOpts;
args = cfg.args;
};

networkFlag =
if cfg.args.network != null
then "--${cfg.args.network} \\"
else "";
in ''
${cfg.package}/bin/beacon-chain \
--accept-terms-of-use \
--${settings.network} \
--config-file ${configFile} \
${lib.escapeShellArgs extraArgs}
${concatStringsSep " \\\n" flags} \
${networkFlag}
--datadir ${datadir} \
${lib.escapeShellArgs cfg.extraArgs}
'';
})
)
Expand Down