1{ config, lib, pkgs, ... }:
2
3let
4 inherit (lib) mkEnableOption mkIf mkOption types;
5
6 cfg = config.services.tempo;
7
8 settingsFormat = pkgs.formats.yaml {};
9in {
10 options.services.tempo = {
11 enable = mkEnableOption "Grafana Tempo";
12
13 settings = mkOption {
14 type = settingsFormat.type;
15 default = {};
16 description = ''
17 Specify the configuration for Tempo in Nix.
18
19 See https://grafana.com/docs/tempo/latest/configuration/ for available options.
20 '';
21 };
22
23 configFile = mkOption {
24 type = types.nullOr types.path;
25 default = null;
26 description = ''
27 Specify a path to a configuration file that Tempo should use.
28 '';
29 };
30
31 extraFlags = mkOption {
32 type = types.listOf types.str;
33 default = [];
34 example = lib.literalExpression
35 ''
36 [ "-config.expand-env=true" ]
37 '';
38 description = ''
39 Additional flags to pass to the `ExecStart=` in `tempo.service`.
40 '';
41 };
42 };
43
44 config = mkIf cfg.enable {
45 # for tempo-cli and friends
46 environment.systemPackages = [ pkgs.tempo ];
47
48 assertions = [{
49 assertion = (
50 (cfg.settings == {}) != (cfg.configFile == null)
51 );
52 message = ''
53 Please specify a configuration for Tempo with either
54 'services.tempo.settings' or
55 'services.tempo.configFile'.
56 '';
57 }];
58
59 systemd.services.tempo = {
60 description = "Grafana Tempo Service Daemon";
61 wantedBy = [ "multi-user.target" ];
62
63 serviceConfig = let
64 conf = if cfg.configFile == null
65 then settingsFormat.generate "config.yaml" cfg.settings
66 else cfg.configFile;
67 in
68 {
69 ExecStart = "${pkgs.tempo}/bin/tempo --config.file=${conf} ${lib.escapeShellArgs cfg.extraFlags}";
70 DynamicUser = true;
71 Restart = "always";
72 ProtectSystem = "full";
73 DevicePolicy = "closed";
74 NoNewPrivileges = true;
75 WorkingDirectory = "/var/lib/tempo";
76 StateDirectory = "tempo";
77 };
78 };
79 };
80}