1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.alerta;
9
10 alertaConf = pkgs.writeTextFile {
11 name = "alertad.conf";
12 text = ''
13 DATABASE_URL = '${cfg.databaseUrl}'
14 DATABASE_NAME = '${cfg.databaseName}'
15 LOG_FILE = '${cfg.logDir}/alertad.log'
16 LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
17 CORS_ORIGINS = [ ${lib.concatMapStringsSep ", " (s: "\"" + s + "\"") cfg.corsOrigins} ];
18 AUTH_REQUIRED = ${if cfg.authenticationRequired then "True" else "False"}
19 SIGNUP_ENABLED = ${if cfg.signupEnabled then "True" else "False"}
20 ${cfg.extraConfig}
21 '';
22 };
23in
24{
25 options.services.alerta = {
26 enable = lib.mkEnableOption "alerta";
27
28 port = lib.mkOption {
29 type = lib.types.port;
30 default = 5000;
31 description = "Port of Alerta";
32 };
33
34 bind = lib.mkOption {
35 type = lib.types.str;
36 default = "0.0.0.0";
37 description = "Address to bind to. The default is to bind to all addresses";
38 };
39
40 logDir = lib.mkOption {
41 type = lib.types.path;
42 description = "Location where the logfiles are stored";
43 default = "/var/log/alerta";
44 };
45
46 databaseUrl = lib.mkOption {
47 type = lib.types.str;
48 description = "URL of the MongoDB or PostgreSQL database to connect to";
49 default = "mongodb://localhost";
50 };
51
52 databaseName = lib.mkOption {
53 type = lib.types.str;
54 description = "Name of the database instance to connect to";
55 default = "monitoring";
56 };
57
58 corsOrigins = lib.mkOption {
59 type = lib.types.listOf lib.types.str;
60 description = "List of URLs that can access the API for Cross-Origin Resource Sharing (CORS)";
61 default = [
62 "http://localhost"
63 "http://localhost:5000"
64 ];
65 };
66
67 authenticationRequired = lib.mkOption {
68 type = lib.types.bool;
69 description = "Whether users must authenticate when using the web UI or command-line tool";
70 default = false;
71 };
72
73 signupEnabled = lib.mkOption {
74 type = lib.types.bool;
75 description = "Whether to prevent sign-up of new users via the web UI";
76 default = true;
77 };
78
79 extraConfig = lib.mkOption {
80 description = "These lines go into alertad.conf verbatim.";
81 default = "";
82 type = lib.types.lines;
83 };
84 };
85
86 config = lib.mkIf cfg.enable {
87 systemd.tmpfiles.settings."10-alerta".${cfg.logDir}.d = {
88 user = "alerta";
89 group = "alerta";
90 };
91
92 systemd.services.alerta = {
93 description = "Alerta Monitoring System";
94 wantedBy = [ "multi-user.target" ];
95 after = [ "networking.target" ];
96 environment = {
97 ALERTA_SVR_CONF_FILE = alertaConf;
98 };
99 serviceConfig = {
100 ExecStart = "${pkgs.alerta-server}/bin/alertad run --port ${toString cfg.port} --host ${cfg.bind}";
101 User = "alerta";
102 Group = "alerta";
103 };
104 };
105
106 environment.systemPackages = [ pkgs.alerta ];
107
108 users.users.alerta = {
109 uid = config.ids.uids.alerta;
110 description = "Alerta user";
111 };
112
113 users.groups.alerta = {
114 gid = config.ids.gids.alerta;
115 };
116 };
117}