Skip to content

Commit

Permalink
nixos service: add ipv6 support. Better control of port sharing.
Browse files Browse the repository at this point in the history
 use-case: ipv6 addr can be used in multi-instances mode so that instances
 on same machine can be included each other topology's, while still
 sharing ipv4 port.
  • Loading branch information
jbgi authored and jutaro committed May 13, 2021
1 parent fb5fe76 commit 631ba47
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions nix/nixos/cardano-node-service.nix
Expand Up @@ -56,12 +56,13 @@ let
"--config ${realNodeConfigFile}"
"--database-path ${instanceDbPath}"
"--topology ${topology}"
] ++ (lib.optionals (!cfg.systemdSocketActivation) [
] ++ (lib.optionals (!cfg.systemdSocketActivation) ([
"--host-addr ${cfg.hostAddr}"
"--port ${toString cfg.port}"
"--port ${toString (cfg.port + i)}"
"--socket-path ${cfg.socketPath}"
])
++ consensusParams.${cfg.nodeConfig.Protocol} ++ cfg.extraArgs ++ cfg.rtsArgs;
] ++ lib.optional (cfg.ipv6HostAddr != null)
"--host-ipv6-addr ${cfg.ipv6HostAddr}"
)) ++ consensusParams.${cfg.nodeConfig.Protocol} ++ cfg.extraArgs ++ cfg.rtsArgs;
in ''
choice() { i=$1; shift; eval "echo \''${$((i + 1))}"; }
echo "Starting ${exec}: ${concatStringsSep "\"\n echo \"" cmd}"
Expand Down Expand Up @@ -258,6 +259,14 @@ in {
'';
};

ipv6HostAddr = mkOption {
type = types.nullOr types.str;
default = "::1";
description = ''
The ipv6 host address to bind to. Set to null to disable.
'';
};

stateDir = mkOption {
type = types.str;
default = "/var/lib/cardano-node";
Expand Down Expand Up @@ -333,6 +342,27 @@ in {
'';
};

shareIpv4port = mkOption {
type = types.bool;
default = cfg.systemdSocketActivation;
description = ''
Should instances on same machine share ipv4 port.
Default: true if systemd activated socket. Otherwise always false.
If false use port increments starting from `port`.
'';
};

shareIpv6port = mkOption {
type = types.bool;
default = false;
description = ''
Should instances on same machine share ipv6 port.
Only works with systemd socket.
Default: false.
If false use port increments starting from `port`.
'';
};

nodeId = mkOption {
type = types.int;
default = 0;
Expand All @@ -356,7 +386,7 @@ in {
# type = types.functionTo (types.listOf types.attrs);
default = _: [];
description = ''
Static routes to peers, specific to a given instance (when multiple instances are used)
Static routes to peers, specific to a given instance (when multiple instances are used).
'';
};

Expand Down Expand Up @@ -481,7 +511,8 @@ in {
wantedBy = [ "sockets.target" ];
partOf = [ "${n}.service" ];
socketConfig = {
ListenStream = [ "${cfg.hostAddr}:${toString cfg.port}" ]
ListenStream = [ "${cfg.hostAddr}:${toString (if cfg.shareIpv4port then cfg.port else cfg.port + i)}" ]
++ optional (cfg.ipv6HostAddr != null) "[${cfg.ipv6HostAddr}]:${toString (if cfg.shareIpv6port then cfg.port else cfg.port + i)}"
++ [(if (i == 0) then cfg.socketPath else "${runtimeDir}-${toString i}/node.socket")];
ReusePort = "yes";
SocketMode = "0660";
Expand Down

0 comments on commit 631ba47

Please sign in to comment.