@@ -8,6 +8,20 @@ let
88 promUser = "prometheus";
99 promGroup = "prometheus";
1010
11+ stateDir =
12+ if cfg.stateDir != null
13+ then cfg.stateDir
14+ else
15+ if cfg.dataDir != null
16+ then
17+ # This assumes /var/lib/ is a prefix of cfg.dataDir.
18+ # This is checked as an assertion below.
19+ removePrefix stateDirBase cfg.dataDir
20+ else "prometheus";
21+ stateDirBase = "/var/lib/";
22+ workingDir = stateDirBase + stateDir;
23+ workingDir2 = stateDirBase + cfg2.stateDir;
24+
1125 # Get a submodule without any embedded metadata:
1226 _filter = x: filterAttrs (k: v: k != "_module") x;
1327
@@ -52,7 +66,7 @@ let
5266 in promtoolCheck "check-config" "prometheus.yml" yml;
5367
5468 cmdlineArgs = cfg.extraFlags ++ [
55− "-storage.local.path=/var/lib/prometheus/metrics"
69+ "-storage.local.path=${workingDir}/metrics"
5670 "-config.file=${prometheusYml}"
5771 "-web.listen-address=${cfg.listenAddress}"
5872 "-alertmanager.notification-queue-capacity=${toString cfg.alertmanagerNotificationQueueCapacity}"
@@ -86,7 +100,7 @@ let
86100 in prom2toolCheck "check config" "prometheus.yml" yml;
87101
88102 cmdlineArgs2 = cfg2.extraFlags ++ [
89− "--storage.tsdb.path=/var/lib/prometheus2/data/"
103+ "--storage.tsdb.path=${workingDir2}/data/"
90104 "--config.file=${prometheus2Yml}"
91105 "--web.listen-address=${cfg2.listenAddress}"
92106 "--alertmanager.notification-queue-capacity=${toString cfg2.alertmanagerNotificationQueueCapacity}"
@@ -446,6 +460,25 @@ in {
446460 '';
447461 };
448462
463+ dataDir = mkOption {
464+ type = types.nullOr types.path;
465+ default = null;
466+ description = ''
467+ Directory to store Prometheus metrics data.
468+ This option is deprecated, please use <option>services.prometheus.stateDir</option>.
469+ '';
470+ };
471+
472+ stateDir = mkOption {
473+ type = types.nullOr types.str;
474+ default = null;
475+ description = ''
476+ Directory below <literal>${stateDirBase}</literal> to store Prometheus metrics data.
477+ This directory will be created automatically using systemd's StateDirectory mechanism.
478+ Defaults to <literal>prometheus</literal>.
479+ '';
480+ };
481+
449482 extraFlags = mkOption {
450483 type = types.listOf types.str;
451484 default = [];
@@ -560,6 +593,16 @@ in {
560593 '';
561594 };
562595
596+ stateDir = mkOption {
597+ type = types.str;
598+ default = "prometheus2";
599+ description = ''
600+ Directory below <literal>${stateDirBase}</literal> to store Prometheus metrics data.
601+ This directory will be created automatically using systemd's StateDirectory mechanism.
602+ Defaults to <literal>prometheus2</literal>.
603+ '';
604+ };
605+
563606 extraFlags = mkOption {
564607 type = types.listOf types.str;
565608 default = [];
@@ -659,6 +702,37 @@ in {
659702 };
660703 })
661704 (mkIf cfg.enable {
705+ warnings =
706+ optional (cfg.dataDir != null) ''
707+ The option services.prometheus.dataDir is deprecated, please use
708+ services.prometheus.stateDir.
709+ '';
710+ assertions = [
711+ {
712+ assertion = !(cfg.dataDir != null && cfg.stateDir != null);
713+ message =
714+ "The options services.prometheus.dataDir and services.prometheus.stateDir" +
715+ " can't both be set at the same time! It's recommended to only set the latter" +
716+ " since the former is deprecated.";
717+ }
718+ {
719+ assertion = cfg.dataDir != null -> hasPrefix stateDirBase cfg.dataDir;
720+ message =
721+ "The option services.prometheus.dataDir should have ${stateDirBase} as a prefix!";
722+ }
723+ {
724+ assertion = cfg.stateDir != null -> !hasPrefix "/" cfg.stateDir;
725+ message =
726+ "The option services.prometheus.stateDir shouldn't be an absolute directory." +
727+ " It should be a directory relative to ${stateDirBase}.";
728+ }
729+ {
730+ assertion = cfg2.stateDir != null -> !hasPrefix "/" cfg2.stateDir;
731+ message =
732+ "The option services.prometheus2.stateDir shouldn't be an absolute directory." +
733+ " It should be a directory relative to ${stateDirBase}.";
734+ }
735+ ];
662736 systemd.services.prometheus = {
663737 wantedBy = [ "multi-user.target" ];
664738 after = [ "network.target" ];
@@ -668,8 +742,8 @@ in {
668742 concatStringsSep " \\\n " cmdlineArgs);
669743 User = promUser;
670744 Restart = "always";
671− WorkingDirectory = /var/lib/prometheus;
672− StateDirectory = "prometheus";
745+ WorkingDirectory = workingDir;
746+ StateDirectory = stateDir;
673747 };
674748 };
675749 })
@@ -683,8 +757,8 @@ in {
683757 concatStringsSep " \\\n " cmdlineArgs2);
684758 User = promUser;
685759 Restart = "always";
686− WorkingDirectory = /var/lib/prometheus2;
687− StateDirectory = "prometheus2";
760+ WorkingDirectory = workingDir2;
761+ StateDirectory = cfg2.stateDir;
688762 };
689763 };
690764 })