about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/monitoring/prometheus/sachet.nix
blob: 3deb29aeb222eaa6c37372fb23b791dd456054f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.prometheus.sachet;
  configFile = pkgs.writeText "sachet.yml" (builtins.toJSON cfg.configuration);
in
{
  options = {
    services.prometheus.sachet = {
      enable = mkEnableOption "Sachet, an SMS alerting tool for the Prometheus Alertmanager";

      configuration = mkOption {
        type = types.nullOr types.attrs;
        default = null;
        example = literalExpression ''
          {
            providers = {
              twilio = {
                # environment variables gets expanded at runtime
                account_sid = "$TWILIO_ACCOUNT";
                auth_token = "$TWILIO_TOKEN";
              };
            };
            templates = [ ./some-template.tmpl ];
            receivers = [{
              name = "pager";
              provider = "twilio";
              to = [ "+33123456789" ];
              text = "{{ template \"message\" . }}";
            }];
          }
        '';
        description = ''
          Sachet's configuration as a nix attribute set.
        '';
      };

      address = mkOption {
        type = types.str;
        default = "localhost";
        description = ''
          The address Sachet will listen to.
        '';
      };

      port = mkOption {
        type = types.port;
        default = 9876;
        description = ''
          The port Sachet will listen to.
        '';
      };

    };
  };

  config = mkIf cfg.enable {
    assertions = singleton {
      assertion = cfg.configuration != null;
      message = "Cannot enable Sachet without a configuration.";
    };

    systemd.services.sachet = {
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "network-online.target" ];
      script = ''
        ${pkgs.envsubst}/bin/envsubst -i "${configFile}" > /tmp/sachet.yaml
        exec ${pkgs.prometheus-sachet}/bin/sachet -config /tmp/sachet.yaml -listen-address ${cfg.address}:${builtins.toString cfg.port}
      '';

      serviceConfig = {
        Restart = "always";

        ProtectSystem = "strict";
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;

        DynamicUser = true;
        PrivateTmp = true;
        WorkingDirectory = "/tmp/";
      };
    };
  };
}