Skip to content

Commit

Permalink
feat(nixos/cardano-node-service): declare state and runtime directory
Browse files Browse the repository at this point in the history
this gives endusers more control, fix #5098
  • Loading branch information
clemenscodes committed Dec 2, 2024
1 parent ef5f0a9 commit 50591b4
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions nix/nixos/cardano-node-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ with lib; with builtins;
let
cfg = config.services.cardano-node;
envConfig = cfg.environments.${cfg.environment};
runtimeDir = i : if cfg.runtimeDir i == null then cfg.stateDir i else "/run/${cfg.runtimeDir i}";
runtimeDir = i : if cfg.runtimeDir i == null then cfg.stateDir i else "${cfg.runDirBase}${lib.removePrefix cfg.runDirBase (cfg.runtimeDir i)}";
suffixDir = base: i: "${base}${optionalString (i != 0) "-${toString i}"}";
nullOrStr = types.nullOr types.str;
funcToOr = t: types.either t (types.functionTo t);
Expand Down Expand Up @@ -333,21 +333,37 @@ in {
'';
};

stateDirBase = mkOption {
type = types.str;
default = "/var/lib/";
description = ''
Base directory to store blockchain data, for each instance.
'';
};

stateDir = mkOption {
type = funcToOr types.str;
default = "/var/lib/cardano-node";
default = "${cfg.stateDirBase}cardano-node";
apply = x : if (builtins.isFunction x) then x else i: x;
description = ''
Directory to store blockchain data, for each instance.
'';
};

runDirBase = mkOption {
type = types.str;
default = "/run/";
description = ''
Base runtime directory, for each instance.
'';
};

runtimeDir = mkOption {
type = funcToOr nullOrStr;
default = suffixDir "cardano-node";
apply = x : if builtins.isFunction x then x else if x == null then _: null else suffixDir x;
default = i: ''${cfg.runDirBase}${suffixDir "cardano-node" i}'';
apply = x : if builtins.isFunction x then x else if x == null then _: null else "${cfg.runDirBase}${suffixDir "cardano-node" x}";
description = ''
Runtime directory relative to /run, for each instance
Runtime directory relative to ${cfg.runDirBase}, for each instance
'';
};

Expand Down Expand Up @@ -705,8 +721,6 @@ in {
};

config = mkIf cfg.enable ( let
stateDirBase = "/var/lib/";
runDirBase = "/run/";
lmdbPaths = filter (x: x != null) (map (e: cfg.lmdbDatabasePath e) (builtins.genList lib.trivial.id cfg.instances));
genInstanceConf = f: listToAttrs (if cfg.instances > 1
then genList (i: let n = "cardano-node-${toString i}"; in nameValuePair n (f n i)) cfg.instances
Expand Down Expand Up @@ -745,11 +759,11 @@ in {
ExecReload = mkIf (cfg.useSystemdReload && cfg.useNewTopology) "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
Restart = "always";
RuntimeDirectory = lib.mkIf (!cfg.systemdSocketActivation)
(lib.removePrefix runDirBase (runtimeDir i));
(lib.removePrefix cfg.runDirBase (runtimeDir i));
WorkingDirectory = cfg.stateDir i;
# This assumes /var/lib/ is a prefix of cfg.stateDir.
# This assumes cfg.stateDirBase is a prefix of cfg.stateDir.
# This is checked as an assertion below.
StateDirectory = lib.removePrefix stateDirBase (cfg.stateDir i);
StateDirectory = lib.removePrefix cfg.stateDirBase (cfg.stateDir i);
NonBlocking = lib.mkIf cfg.systemdSocketActivation true;
# time to sleep before restarting a service
RestartSec = 1;
Expand All @@ -765,8 +779,7 @@ in {
++ optional (cfg.ipv6HostAddr i != null) "[${cfg.ipv6HostAddr i}]:${toString (if cfg.shareIpv6port then cfg.port else cfg.port + i)}"
++ (cfg.additionalListenStream i)
++ [(cfg.socketPath i)];
RuntimeDirectory = lib.removePrefix runDirBase
(cfg.runtimeDir i);
RuntimeDirectory = lib.removePrefix cfg.runDirBase (cfg.runtimeDir i);
NoDelay = "yes";
ReusePort = "yes";
SocketMode = "0660";
Expand All @@ -788,17 +801,17 @@ in {
User = "cardano-node";
Group = "cardano-node";
ExecStart = "${pkgs.coreutils}/bin/echo Starting ${toString cfg.instances} cardano-node instances";
WorkingDirectory = "/var/lib/cardano-node";
StateDirectory = "cardano-node";
WorkingDirectory = cfg.stateDir i;
StateDirectory = lib.removePrefix cfg.stateDirBase (cfg.stateDir i);
};
};
}
{
assertions = [
{
assertion = builtins.all (i : lib.hasPrefix stateDirBase (cfg.stateDir i))
assertion = builtins.all (i : lib.hasPrefix cfg.stateDirBase (cfg.stateDir i))
(builtins.genList lib.trivial.id cfg.instances);
message = "The option services.cardano-node.stateDir should have ${stateDirBase}
message = "The option services.cardano-node.stateDir should have ${cfg.stateDirBase}
as a prefix, for each instance!";
}
{
Expand Down

0 comments on commit 50591b4

Please sign in to comment.