@@ -0,0 +1,801 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.thanos;
7+
8+ nullOpt = type: description: mkOption {
9+ type = types.nullOr type;
10+ default = null;
11+ inherit description;
12+ };
13+
14+ optionToArgs = opt: v : optional (v != null) ''--${opt}="${toString v}"'';
15+ flagToArgs = opt: v : optional v ''--${opt}'';
16+ listToArgs = opt: vs : map (v: ''--${opt}="${v}"'') vs;
17+ attrsToArgs = opt: kvs: mapAttrsToList (k: v: ''--${opt}=${k}=\"${v}\"'') kvs;
18+
19+ mkParamDef = type: default: description: mkParam type (description + ''
20+
21+ Defaults to <literal>${toString default}</literal> in Thanos
22+ when set to <literal>null</literal>.
23+ '');
24+
25+ mkParam = type: description: {
26+ toArgs = optionToArgs;
27+ option = nullOpt type description;
28+ };
29+
30+ mkFlagParam = description: {
31+ toArgs = flagToArgs;
32+ option = mkOption {
33+ type = types.bool;
34+ default = false;
35+ inherit description;
36+ };
37+ };
38+
39+ mkListParam = opt: description: {
40+ toArgs = _opt: listToArgs opt;
41+ option = mkOption {
42+ type = types.listOf types.str;
43+ default = [];
44+ inherit description;
45+ };
46+ };
47+
48+ mkAttrsParam = opt: description: {
49+ toArgs = _opt: attrsToArgs opt;
50+ option = mkOption {
51+ type = types.attrsOf types.str;
52+ default = {};
53+ inherit description;
54+ };
55+ };
56+
57+ mkStateDirParam = opt: default: description: {
58+ toArgs = _opt: stateDir: optionToArgs opt "/var/lib/${stateDir}";
59+ option = mkOption {
60+ type = types.str;
61+ inherit default;
62+ inherit description;
63+ };
64+ };
65+
66+ toYAML = name: attrs: pkgs.runCommandNoCC name {
67+ preferLocalBuild = true;
68+ json = builtins.toFile "${name}.json" (builtins.toJSON attrs);
69+ nativeBuildInputs = [ pkgs.remarshal ];
70+ } ''json2yaml -i $json -o $out'';
71+
72+ thanos = cmd: "${cfg.package}/bin/thanos ${cmd}" +
73+ (let args = cfg."${cmd}".arguments;
74+ in optionalString (length args != 0) (" \\\n " +
75+ concatStringsSep " \\\n " args));
76+
77+ argumentsOf = cmd: concatLists (collect isList
78+ (flip mapParamsRecursive params."${cmd}" (path: param:
79+ let opt = concatStringsSep "." path;
80+ v = getAttrFromPath path cfg."${cmd}";
81+ in param.toArgs opt v)));
82+
83+ mkArgumentsOption = cmd: mkOption {
84+ type = types.listOf types.str;
85+ default = argumentsOf cmd;
86+ description = ''
87+ Arguments to the <literal>thanos ${cmd}</literal> command.
88+
89+ Defaults to a list of arguments formed by converting the structured
90+ options of <option>services.thanos.${cmd}</option> to a list of arguments.
91+
92+ Overriding this option will cause none of the structured options to have
93+ any effect. So only set this if you know what you're doing!
94+ '';
95+ };
96+
97+ mapParamsRecursive =
98+ let noParam = attr: !(attr ? "toArgs" && attr ? "option");
99+ in mapAttrsRecursiveCond noParam;
100+
101+ paramsToOptions = mapParamsRecursive (_path: param: param.option);
102+
103+ params = {
104+
105+ log = {
106+
107+ log.level = mkParamDef (types.enum ["debug" "info" "warn" "error" "fatal"]) "info" ''
108+ Log filtering level.
109+ '';
110+
111+ log.format = mkParam types.str ''
112+ Log format to use.
113+ '';
114+ };
115+
116+ tracing = cfg: {
117+ tracing.config-file = {
118+ toArgs = _opt: path: optionToArgs "tracing.config-file" path;
119+ option = mkOption {
120+ type = with types; nullOr str;
121+ default = if cfg.tracing.config == null then null
122+ else toString (toYAML "tracing.yaml" cfg.tracing.config);
123+ defaultText = ''
124+ if config.services.thanos.<cmd>.tracing.config == null then null
125+ else toString (toYAML "tracing.yaml" config.services.thanos.<cmd>.tracing.config);
126+ '';
127+ description = ''
128+ Path to YAML file that contains tracing configuration.
129+ '';
130+ };
131+ };
132+
133+ tracing.config =
134+ {
135+ toArgs = _opt: _attrs: [];
136+ option = nullOpt types.attrs ''
137+ Tracing configuration.
138+
139+ When not <literal>null</literal> the attribute set gets converted to
140+ a YAML file and stored in the Nix store. The option
141+ <option>tracing.config-file</option> will default to its path.
142+
143+ If <option>tracing.config-file</option> is set this option has no effect.
144+ '';
145+ };
146+ };
147+
148+ common = cfg: params.log // params.tracing cfg // {
149+
150+ http-address = mkParamDef types.str "0.0.0.0:10902" ''
151+ Listen <literal>host:port</literal> for HTTP endpoints.
152+ '';
153+
154+ grpc-address = mkParamDef types.str "0.0.0.0:10901" ''
155+ Listen <literal>ip:port</literal> address for gRPC endpoints (StoreAPI).
156+
157+ Make sure this address is routable from other components.
158+ '';
159+
160+ grpc-server-tls-cert = mkParam types.str ''
161+ TLS Certificate for gRPC server, leave blank to disable TLS
162+ '';
163+
164+ grpc-server-tls-key = mkParam types.str ''
165+ TLS Key for the gRPC server, leave blank to disable TLS
166+ '';
167+
168+ grpc-server-tls-client-ca = mkParam types.str ''
169+ TLS CA to verify clients against.
170+
171+ If no client CA is specified, there is no client verification on server side.
172+ (tls.NoClientCert)
173+ '';
174+ };
175+
176+ objstore = cfg: {
177+
178+ objstore.config-file = {
179+ toArgs = _opt: path: optionToArgs "objstore.config-file" path;
180+ option = mkOption {
181+ type = with types; nullOr str;
182+ default = if cfg.objstore.config == null then null
183+ else toString (toYAML "objstore.yaml" cfg.objstore.config);
184+ defaultText = ''
185+ if config.services.thanos.<cmd>.objstore.config == null then null
186+ else toString (toYAML "objstore.yaml" config.services.thanos.<cmd>.objstore.config);
187+ '';
188+ description = ''
189+ Path to YAML file that contains object store configuration.
190+ '';
191+ };
192+ };
193+
194+ objstore.config =
195+ {
196+ toArgs = _opt: _attrs: [];
197+ option = nullOpt types.attrs ''
198+ Object store configuration.
199+
200+ When not <literal>null</literal> the attribute set gets converted to
201+ a YAML file and stored in the Nix store. The option
202+ <option>objstore.config-file</option> will default to its path.
203+
204+ If <option>objstore.config-file</option> is set this option has no effect.
205+ '';
206+ };
207+ };
208+
209+ sidecar = params.common cfg.sidecar // params.objstore cfg.sidecar // {
210+
211+ prometheus.url = mkParamDef types.str "http://localhost:9090" ''
212+ URL at which to reach Prometheus's API.
213+
214+ For better performance use local network.
215+ '';
216+
217+ tsdb.path = {
218+ toArgs = optionToArgs;
219+ option = mkOption {
220+ type = types.str;
221+ default = "/var/lib/${config.services.prometheus2.stateDir}/data";
222+ defaultText = "/var/lib/\${config.services.prometheus2.stateDir}/data";
223+ description = ''
224+ Data directory of TSDB.
225+ '';
226+ };
227+ };
228+
229+ reloader.config-file = mkParam types.str ''
230+ Config file watched by the reloader.
231+ '';
232+
233+ reloader.config-envsubst-file = mkParam types.str ''
234+ Output file for environment variable substituted config file.
235+ '';
236+
237+ reloader.rule-dirs = mkListParam "reloader.rule-dir" ''
238+ Rule directories for the reloader to refresh.
239+ '';
240+
241+ };
242+
243+ store = params.common cfg.store // params.objstore cfg.store // {
244+
245+ stateDir = mkStateDirParam "data-dir" "thanos-store" ''
246+ Data directory relative to <literal>/var/lib</literal>
247+ in which to cache remote blocks.
248+ '';
249+
250+ index-cache-size = mkParamDef types.str "250MB" ''
251+ Maximum size of items held in the index cache.
252+ '';
253+
254+ chunk-pool-size = mkParamDef types.str "2GB" ''
255+ Maximum size of concurrently allocatable bytes for chunks.
256+ '';
257+
258+ store.grpc.series-sample-limit = mkParamDef types.int 0 ''
259+ Maximum amount of samples returned via a single Series call.
260+
261+ <literal>0</literal> means no limit.
262+
263+ NOTE: for efficiency we take 120 as the number of samples in chunk (it
264+ cannot be bigger than that), so the actual number of samples might be
265+ lower, even though the maximum could be hit.
266+ '';
267+
268+ store.grpc.series-max-concurrency = mkParamDef types.int 20 ''
269+ Maximum number of concurrent Series calls.
270+ '';
271+
272+ sync-block-duration = mkParamDef types.str "3m" ''
273+ Repeat interval for syncing the blocks between local and remote view.
274+ '';
275+
276+ block-sync-concurrency = mkParamDef types.int 20 ''
277+ Number of goroutines to use when syncing blocks from object storage.
278+ '';
279+ };
280+
281+ query = params.common cfg.query // {
282+
283+ grpc-client-tls-secure = mkFlagParam ''
284+ Use TLS when talking to the gRPC server
285+ '';
286+
287+ grpc-client-tls-cert = mkParam types.str ''
288+ TLS Certificates to use to identify this client to the server
289+ '';
290+
291+ grpc-client-tls-key = mkParam types.str ''
292+ TLS Key for the client's certificate
293+ '';
294+
295+ grpc-client-tls-ca = mkParam types.str ''
296+ TLS CA Certificates to use to verify gRPC servers
297+ '';
298+
299+ grpc-client-server-name = mkParam types.str ''
300+ Server name to verify the hostname on the returned gRPC certificates.
301+ See <link xlink:href="https://tools.ietf.org/html/rfc4366#section-3.1"/>
302+ '';
303+
304+ web.route-prefix = mkParam types.str ''
305+ Prefix for API and UI endpoints.
306+
307+ This allows thanos UI to be served on a sub-path. This option is
308+ analogous to <option>web.route-prefix</option> of Promethus.
309+ '';
310+
311+ web.external-prefix = mkParam types.str ''
312+ Static prefix for all HTML links and redirect URLs in the UI query web
313+ interface.
314+
315+ Actual endpoints are still served on / or the
316+ <option>web.route-prefix</option>. This allows thanos UI to be served
317+ behind a reverse proxy that strips a URL sub-path.
318+ '';
319+
320+ web.prefix-header = mkParam types.str ''
321+ Name of HTTP request header used for dynamic prefixing of UI links and
322+ redirects.
323+
324+ This option is ignored if the option
325+ <literal>web.external-prefix</literal> is set.
326+
327+ Security risk: enable this option only if a reverse proxy in front of
328+ thanos is resetting the header.
329+
330+ The setting <literal>web.prefix-header="X-Forwarded-Prefix"</literal>
331+ can be useful, for example, if Thanos UI is served via Traefik reverse
332+ proxy with <literal>PathPrefixStrip</literal> option enabled, which
333+ sends the stripped prefix value in <literal>X-Forwarded-Prefix</literal>
334+ header. This allows thanos UI to be served on a sub-path.
335+ '';
336+
337+ query.timeout = mkParamDef types.str "2m" ''
338+ Maximum time to process query by query node.
339+ '';
340+
341+ query.max-concurrent = mkParamDef types.int 20 ''
342+ Maximum number of queries processed concurrently by query node.
343+ '';
344+
345+ query.replica-label = mkParam types.str ''
346+ Label to treat as a replica indicator along which data is
347+ deduplicated.
348+
349+ Still you will be able to query without deduplication using
350+ <literal>dedup=false</literal> parameter.
351+ '';
352+
353+ selector-labels = mkAttrsParam "selector-label" ''
354+ Query selector labels that will be exposed in info endpoint.
355+ '';
356+
357+ store.addresses = mkListParam "store" ''
358+ Addresses of statically configured store API servers.
359+
360+ The scheme may be prefixed with <literal>dns+</literal> or
361+ <literal>dnssrv+</literal> to detect store API servers through
362+ respective DNS lookups.
363+ '';
364+
365+ store.sd-files = mkListParam "store.sd-files" ''
366+ Path to files that contain addresses of store API servers. The path
367+ can be a glob pattern.
368+ '';
369+
370+ store.sd-interval = mkParamDef types.str "5m" ''
371+ Refresh interval to re-read file SD files. It is used as a resync fallback.
372+ '';
373+
374+ store.sd-dns-interval = mkParamDef types.str "30s" ''
375+ Interval between DNS resolutions.
376+ '';
377+
378+ store.unhealthy-timeout = mkParamDef types.str "5m" ''
379+ Timeout before an unhealthy store is cleaned from the store UI page.
380+ '';
381+
382+ query.auto-downsampling = mkFlagParam ''
383+ Enable automatic adjustment (step / 5) to what source of data should
384+ be used in store gateways if no
385+ <literal>max_source_resolution</literal> param is specified.
386+ '';
387+
388+ query.partial-response = mkFlagParam ''
389+ Enable partial response for queries if no
390+ <literal>partial_response</literal> param is specified.
391+ '';
392+
393+ query.default-evaluation-interval = mkParamDef types.str "1m" ''
394+ Set default evaluation interval for sub queries.
395+ '';
396+
397+ store.response-timeout = mkParamDef types.str "0ms" ''
398+ If a Store doesn't send any data in this specified duration then a
399+ Store will be ignored and partial data will be returned if it's
400+ enabled. <literal>0</literal> disables timeout.
401+ '';
402+ };
403+
404+ rule = params.common cfg.rule // params.objstore cfg.rule // {
405+
406+ labels = mkAttrsParam "label" ''
407+ Labels to be applied to all generated metrics.
408+
409+ Similar to external labels for Prometheus,
410+ used to identify ruler and its blocks as unique source.
411+ '';
412+
413+ stateDir = mkStateDirParam "data-dir" "thanos-rule" ''
414+ Data directory relative to <literal>/var/lib</literal>.
415+ '';
416+
417+ rule-files = mkListParam "rule-file" ''
418+ Rule files that should be used by rule manager. Can be in glob format.
419+ '';
420+
421+ eval-interval = mkParamDef types.str "30s" ''
422+ The default evaluation interval to use.
423+ '';
424+
425+ tsdb.block-duration = mkParamDef types.str "2h" ''
426+ Block duration for TSDB block.
427+ '';
428+
429+ tsdb.retention = mkParamDef types.str "48h" ''
430+ Block retention time on local disk.
431+ '';
432+
433+ alertmanagers.urls = mkListParam "alertmanagers.url" ''
434+ Alertmanager replica URLs to push firing alerts.
435+
436+ Ruler claims success if push to at least one alertmanager from
437+ discovered succeeds. The scheme may be prefixed with
438+ <literal>dns+</literal> or <literal>dnssrv+</literal> to detect
439+ Alertmanager IPs through respective DNS lookups. The port defaults to
440+ <literal>9093</literal> or the SRV record's value. The URL path is
441+ used as a prefix for the regular Alertmanager API path.
442+ '';
443+
444+ alertmanagers.send-timeout = mkParamDef types.str "10s" ''
445+ Timeout for sending alerts to alertmanager.
446+ '';
447+
448+ alert.query-url = mkParam types.str ''
449+ The external Thanos Query URL that would be set in all alerts 'Source' field.
450+ '';
451+
452+ alert.label-drop = mkListParam "alert.label-drop" ''
453+ Labels by name to drop before sending to alertmanager.
454+
455+ This allows alert to be deduplicated on replica label.
456+
457+ Similar Prometheus alert relabelling
458+ '';
459+
460+ web.route-prefix = mkParam types.str ''
461+ Prefix for API and UI endpoints.
462+
463+ This allows thanos UI to be served on a sub-path.
464+
465+ This option is analogous to <literal>--web.route-prefix</literal> of Promethus.
466+ '';
467+
468+ web.external-prefix = mkParam types.str ''
469+ Static prefix for all HTML links and redirect URLs in the UI query web
470+ interface.
471+
472+ Actual endpoints are still served on / or the
473+ <option>web.route-prefix</option>. This allows thanos UI to be served
474+ behind a reverse proxy that strips a URL sub-path.
475+ '';
476+
477+ web.prefix-header = mkParam types.str ''
478+ Name of HTTP request header used for dynamic prefixing of UI links and
479+ redirects.
480+
481+ This option is ignored if the option
482+ <option>web.external-prefix</option> is set.
483+
484+ Security risk: enable this option only if a reverse proxy in front of
485+ thanos is resetting the header.
486+
487+ The header <literal>X-Forwarded-Prefix</literal> can be useful, for
488+ example, if Thanos UI is served via Traefik reverse proxy with
489+ <literal>PathPrefixStrip</literal> option enabled, which sends the
490+ stripped prefix value in <literal>X-Forwarded-Prefix</literal>
491+ header. This allows thanos UI to be served on a sub-path.
492+ '';
493+
494+ query.addresses = mkListParam "query" ''
495+ Addresses of statically configured query API servers.
496+
497+ The scheme may be prefixed with <literal>dns+</literal> or
498+ <literal>dnssrv+</literal> to detect query API servers through
499+ respective DNS lookups.
500+ '';
501+
502+ query.sd-files = mkListParam "query.sd-files" ''
503+ Path to file that contain addresses of query peers.
504+ The path can be a glob pattern.
505+ '';
506+
507+ query.sd-interval = mkParamDef types.str "5m" ''
508+ Refresh interval to re-read file SD files. (used as a fallback)
509+ '';
510+
511+ query.sd-dns-interval = mkParamDef types.str "30s" ''
512+ Interval between DNS resolutions.
513+ '';
514+ };
515+
516+ compact = params.log // params.tracing cfg.compact // params.objstore cfg.compact // {
517+
518+ http-address = mkParamDef types.str "0.0.0.0:10902" ''
519+ Listen <literal>host:port</literal> for HTTP endpoints.
520+ '';
521+
522+ stateDir = mkStateDirParam "data-dir" "thanos-compact" ''
523+ Data directory relative to <literal>/var/lib</literal>
524+ in which to cache blocks and process compactions.
525+ '';
526+
527+ consistency-delay = mkParamDef types.str "30m" ''
528+ Minimum age of fresh (non-compacted) blocks before they are being
529+ processed. Malformed blocks older than the maximum of consistency-delay
530+ and 30m0s will be removed.
531+ '';
532+
533+ retention.resolution-raw = mkParamDef types.str "0d" ''
534+ How long to retain raw samples in bucket.
535+
536+ <literal>0d</literal> - disables this retention
537+ '';
538+
539+ retention.resolution-5m = mkParamDef types.str "0d" ''
540+ How long to retain samples of resolution 1 (5 minutes) in bucket.
541+
542+ <literal>0d</literal> - disables this retention
543+ '';
544+
545+ retention.resolution-1h = mkParamDef types.str "0d" ''
546+ How long to retain samples of resolution 2 (1 hour) in bucket.
547+
548+ <literal>0d</literal> - disables this retention
549+ '';
550+
551+ startAt = {
552+ toArgs = _opt: startAt: flagToArgs "wait" (startAt == null);
553+ option = nullOpt types.str ''
554+ When this option is set to a <literal>systemd.time</literal>
555+ specification the Thanos compactor will run at the specified period.
556+
557+ When this option is <literal>null</literal> the Thanos compactor service
558+ will run continuously. So it will not exit after all compactions have
559+ been processed but wait for new work.
560+ '';
561+ };
562+
563+ block-sync-concurrency = mkParamDef types.int 20 ''
564+ Number of goroutines to use when syncing block metadata from object storage.
565+ '';
566+
567+ compact.concurrency = mkParamDef types.int 1 ''
568+ Number of goroutines to use when compacting groups.
569+ '';
570+ };
571+
572+ downsample = params.log // params.tracing cfg.downsample // params.objstore cfg.downsample // {
573+
574+ stateDir = mkStateDirParam "data-dir" "thanos-downsample" ''
575+ Data directory relative to <literal>/var/lib</literal>
576+ in which to cache blocks and process downsamplings.
577+ '';
578+
579+ };
580+
581+ receive = params.common cfg.receive // params.objstore cfg.receive // {
582+
583+ remote-write.address = mkParamDef types.str "0.0.0.0:19291" ''
584+ Address to listen on for remote write requests.
585+ '';
586+
587+ stateDir = mkStateDirParam "tsdb.path" "thanos-receive" ''
588+ Data directory relative to <literal>/var/lib</literal> of TSDB.
589+ '';
590+
591+ labels = mkAttrsParam "labels" ''
592+ External labels to announce.
593+
594+ This flag will be removed in the future when handling multiple tsdb
595+ instances is added.
596+ '';
597+
598+ tsdb.retention = mkParamDef types.str "15d" ''
599+ How long to retain raw samples on local storage.
600+
601+ <literal>0d</literal> - disables this retention
602+ '';
603+ };
604+
605+ };
606+
607+ assertRelativeStateDir = cmd: {
608+ assertions = [
609+ {
610+ assertion = !hasPrefix "/" cfg."${cmd}".stateDir;
611+ message =
612+ "The option services.thanos.${cmd}.stateDir should not be an absolute directory." +
613+ " It should be a directory relative to /var/lib.";
614+ }
615+ ];
616+ };
617+
618+in {
619+
620+ options.services.thanos = {
621+
622+ package = mkOption {
623+ type = types.package;
624+ default = pkgs.thanos;
625+ defaultText = "pkgs.thanos";
626+ description = ''
627+ The thanos package that should be used.
628+ '';
629+ };
630+
631+ sidecar = paramsToOptions params.sidecar // {
632+ enable = mkEnableOption
633+ "the Thanos sidecar for Prometheus server";
634+ arguments = mkArgumentsOption "sidecar";
635+ };
636+
637+ store = paramsToOptions params.store // {
638+ enable = mkEnableOption
639+ "the Thanos store node giving access to blocks in a bucket provider.";
640+ arguments = mkArgumentsOption "store";
641+ };
642+
643+ query = paramsToOptions params.query // {
644+ enable = mkEnableOption
645+ ("the Thanos query node exposing PromQL enabled Query API " +
646+ "with data retrieved from multiple store nodes");
647+ arguments = mkArgumentsOption "query";
648+ };
649+
650+ rule = paramsToOptions params.rule // {
651+ enable = mkEnableOption
652+ ("the Thanos ruler service which evaluates Prometheus rules against" +
653+ " given Query nodes, exposing Store API and storing old blocks in bucket");
654+ arguments = mkArgumentsOption "rule";
655+ };
656+
657+ compact = paramsToOptions params.compact // {
658+ enable = mkEnableOption
659+ "the Thanos compactor which continuously compacts blocks in an object store bucket";
660+ arguments = mkArgumentsOption "compact";
661+ };
662+
663+ downsample = paramsToOptions params.downsample // {
664+ enable = mkEnableOption
665+ "the Thanos downsampler which continuously downsamples blocks in an object store bucket";
666+ arguments = mkArgumentsOption "downsample";
667+ };
668+
669+ receive = paramsToOptions params.receive // {
670+ enable = mkEnableOption
671+ ("the Thanos receiver which accept Prometheus remote write API requests " +
672+ "and write to local tsdb (EXPERIMENTAL, this may change drastically without notice)");
673+ arguments = mkArgumentsOption "receive";
674+ };
675+ };
676+
677+ config = mkMerge [
678+
679+ (mkIf cfg.sidecar.enable {
680+ assertions = [
681+ {
682+ assertion = config.services.prometheus2.enable;
683+ message =
684+ "Please enable services.prometheus2 when enabling services.thanos.sidecar.";
685+ }
686+ {
687+ assertion = !(config.services.prometheus2.globalConfig.external_labels == null ||
688+ config.services.prometheus2.globalConfig.external_labels == {});
689+ message =
690+ "services.thanos.sidecar requires uniquely identifying external labels " +
691+ "to be configured in the Prometheus server. " +
692+ "Please set services.prometheus2.globalConfig.external_labels.";
693+ }
694+ ];
695+ systemd.services.thanos-sidecar = {
696+ wantedBy = [ "multi-user.target" ];
697+ after = [ "network.target" "prometheus2.service" ];
698+ serviceConfig = {
699+ User = "prometheus";
700+ Restart = "always";
701+ ExecStart = thanos "sidecar";
702+ };
703+ };
704+ })
705+
706+ (mkIf cfg.store.enable (mkMerge [
707+ (assertRelativeStateDir "store")
708+ {
709+ systemd.services.thanos-store = {
710+ wantedBy = [ "multi-user.target" ];
711+ after = [ "network.target" ];
712+ serviceConfig = {
713+ DynamicUser = true;
714+ StateDirectory = cfg.store.stateDir;
715+ Restart = "always";
716+ ExecStart = thanos "store";
717+ };
718+ };
719+ }
720+ ]))
721+
722+ (mkIf cfg.query.enable {
723+ systemd.services.thanos-query = {
724+ wantedBy = [ "multi-user.target" ];
725+ after = [ "network.target" ];
726+ serviceConfig = {
727+ DynamicUser = true;
728+ Restart = "always";
729+ ExecStart = thanos "query";
730+ };
731+ };
732+ })
733+
734+ (mkIf cfg.rule.enable (mkMerge [
735+ (assertRelativeStateDir "rule")
736+ {
737+ systemd.services.thanos-rule = {
738+ wantedBy = [ "multi-user.target" ];
739+ after = [ "network.target" ];
740+ serviceConfig = {
741+ DynamicUser = true;
742+ StateDirectory = cfg.rule.stateDir;
743+ Restart = "always";
744+ ExecStart = thanos "rule";
745+ };
746+ };
747+ }
748+ ]))
749+
750+ (mkIf cfg.compact.enable (mkMerge [
751+ (assertRelativeStateDir "compact")
752+ {
753+ systemd.services.thanos-compact =
754+ let wait = cfg.compact.startAt == null; in {
755+ wantedBy = [ "multi-user.target" ];
756+ after = [ "network.target" ];
757+ serviceConfig = {
758+ Type = if wait then "simple" else "oneshot";
759+ Restart = if wait then "always" else "no";
760+ DynamicUser = true;
761+ StateDirectory = cfg.compact.stateDir;
762+ ExecStart = thanos "compact";
763+ };
764+ } // optionalAttrs (!wait) { inherit (cfg.compact) startAt; };
765+ }
766+ ]))
767+
768+ (mkIf cfg.downsample.enable (mkMerge [
769+ (assertRelativeStateDir "downsample")
770+ {
771+ systemd.services.thanos-downsample = {
772+ wantedBy = [ "multi-user.target" ];
773+ after = [ "network.target" ];
774+ serviceConfig = {
775+ DynamicUser = true;
776+ StateDirectory = cfg.downsample.stateDir;
777+ Restart = "always";
778+ ExecStart = thanos "downsample";
779+ };
780+ };
781+ }
782+ ]))
783+
784+ (mkIf cfg.receive.enable (mkMerge [
785+ (assertRelativeStateDir "receive")
786+ {
787+ systemd.services.thanos-receive = {
788+ wantedBy = [ "multi-user.target" ];
789+ after = [ "network.target" ];
790+ serviceConfig = {
791+ DynamicUser = true;
792+ StateDirectory = cfg.receive.stateDir;
793+ Restart = "always";
794+ ExecStart = thanos "receive";
795+ };
796+ };
797+ }
798+ ]))
799+
800+ ];
801+}