Skip to content

Commit 50591b4

Browse files
committed
feat(nixos/cardano-node-service): declare state and runtime directory
this gives endusers more control, fix #5098
1 parent ef5f0a9 commit 50591b4

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

nix/nixos/cardano-node-service.nix

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ with lib; with builtins;
77
let
88
cfg = config.services.cardano-node;
99
envConfig = cfg.environments.${cfg.environment};
10-
runtimeDir = i : if cfg.runtimeDir i == null then cfg.stateDir i else "/run/${cfg.runtimeDir i}";
10+
runtimeDir = i : if cfg.runtimeDir i == null then cfg.stateDir i else "${cfg.runDirBase}${lib.removePrefix cfg.runDirBase (cfg.runtimeDir i)}";
1111
suffixDir = base: i: "${base}${optionalString (i != 0) "-${toString i}"}";
1212
nullOrStr = types.nullOr types.str;
1313
funcToOr = t: types.either t (types.functionTo t);
@@ -333,21 +333,37 @@ in {
333333
'';
334334
};
335335

336+
stateDirBase = mkOption {
337+
type = types.str;
338+
default = "/var/lib/";
339+
description = ''
340+
Base directory to store blockchain data, for each instance.
341+
'';
342+
};
343+
336344
stateDir = mkOption {
337345
type = funcToOr types.str;
338-
default = "/var/lib/cardano-node";
346+
default = "${cfg.stateDirBase}cardano-node";
339347
apply = x : if (builtins.isFunction x) then x else i: x;
340348
description = ''
341349
Directory to store blockchain data, for each instance.
342350
'';
343351
};
344352

353+
runDirBase = mkOption {
354+
type = types.str;
355+
default = "/run/";
356+
description = ''
357+
Base runtime directory, for each instance.
358+
'';
359+
};
360+
345361
runtimeDir = mkOption {
346362
type = funcToOr nullOrStr;
347-
default = suffixDir "cardano-node";
348-
apply = x : if builtins.isFunction x then x else if x == null then _: null else suffixDir x;
363+
default = i: ''${cfg.runDirBase}${suffixDir "cardano-node" i}'';
364+
apply = x : if builtins.isFunction x then x else if x == null then _: null else "${cfg.runDirBase}${suffixDir "cardano-node" x}";
349365
description = ''
350-
Runtime directory relative to /run, for each instance
366+
Runtime directory relative to ${cfg.runDirBase}, for each instance
351367
'';
352368
};
353369

@@ -705,8 +721,6 @@ in {
705721
};
706722

707723
config = mkIf cfg.enable ( let
708-
stateDirBase = "/var/lib/";
709-
runDirBase = "/run/";
710724
lmdbPaths = filter (x: x != null) (map (e: cfg.lmdbDatabasePath e) (builtins.genList lib.trivial.id cfg.instances));
711725
genInstanceConf = f: listToAttrs (if cfg.instances > 1
712726
then genList (i: let n = "cardano-node-${toString i}"; in nameValuePair n (f n i)) cfg.instances
@@ -745,11 +759,11 @@ in {
745759
ExecReload = mkIf (cfg.useSystemdReload && cfg.useNewTopology) "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
746760
Restart = "always";
747761
RuntimeDirectory = lib.mkIf (!cfg.systemdSocketActivation)
748-
(lib.removePrefix runDirBase (runtimeDir i));
762+
(lib.removePrefix cfg.runDirBase (runtimeDir i));
749763
WorkingDirectory = cfg.stateDir i;
750-
# This assumes /var/lib/ is a prefix of cfg.stateDir.
764+
# This assumes cfg.stateDirBase is a prefix of cfg.stateDir.
751765
# This is checked as an assertion below.
752-
StateDirectory = lib.removePrefix stateDirBase (cfg.stateDir i);
766+
StateDirectory = lib.removePrefix cfg.stateDirBase (cfg.stateDir i);
753767
NonBlocking = lib.mkIf cfg.systemdSocketActivation true;
754768
# time to sleep before restarting a service
755769
RestartSec = 1;
@@ -765,8 +779,7 @@ in {
765779
++ optional (cfg.ipv6HostAddr i != null) "[${cfg.ipv6HostAddr i}]:${toString (if cfg.shareIpv6port then cfg.port else cfg.port + i)}"
766780
++ (cfg.additionalListenStream i)
767781
++ [(cfg.socketPath i)];
768-
RuntimeDirectory = lib.removePrefix runDirBase
769-
(cfg.runtimeDir i);
782+
RuntimeDirectory = lib.removePrefix cfg.runDirBase (cfg.runtimeDir i);
770783
NoDelay = "yes";
771784
ReusePort = "yes";
772785
SocketMode = "0660";
@@ -788,17 +801,17 @@ in {
788801
User = "cardano-node";
789802
Group = "cardano-node";
790803
ExecStart = "${pkgs.coreutils}/bin/echo Starting ${toString cfg.instances} cardano-node instances";
791-
WorkingDirectory = "/var/lib/cardano-node";
792-
StateDirectory = "cardano-node";
804+
WorkingDirectory = cfg.stateDir i;
805+
StateDirectory = lib.removePrefix cfg.stateDirBase (cfg.stateDir i);
793806
};
794807
};
795808
}
796809
{
797810
assertions = [
798811
{
799-
assertion = builtins.all (i : lib.hasPrefix stateDirBase (cfg.stateDir i))
812+
assertion = builtins.all (i : lib.hasPrefix cfg.stateDirBase (cfg.stateDir i))
800813
(builtins.genList lib.trivial.id cfg.instances);
801-
message = "The option services.cardano-node.stateDir should have ${stateDirBase}
814+
message = "The option services.cardano-node.stateDir should have ${cfg.stateDirBase}
802815
as a prefix, for each instance!";
803816
}
804817
{

0 commit comments

Comments
 (0)