blob: 6c63f755545aca6bd382b0f8c97a246ea704eaea [file] [log] [blame]
Skyler Grey2ca6ccd2023-10-14 22:56:43 +00001{ lib, config, base, pkgs, helpers, ... }:
Skyler Greyfe1740c2023-10-21 01:24:18 +00002lib.recursiveUpdate {
Skyler Grey6f0f43d2023-05-03 15:01:05 +00003 services.grafana = {
4 enable = true;
5
6 settings = {
7 server = rec {
8 domain = "logs.clicks.codes";
Skyler Greya78aa672023-05-20 13:48:18 +02009 root_url = "https://${domain}";
Skyler Grey6f0f43d2023-05-03 15:01:05 +000010 http_port = 9052;
11 enable_gzip = true;
12 };
13 analytics.reporting_enabled = false;
TheCodedProfa7cc4e92023-10-10 19:29:06 -040014 "auth.generic_oauth" = {
15 enabled = true;
16 name = "Clicks OAuth";
17 allow_sign_up = true;
18 client_id = "grafana";
19 client_secret = "!!client_secret!!";
20 scopes = "openid email profile offline_access roles";
21 email_attribute_path = "email";
22 login_attribute_path = "login";
23 name_attribute_path = "name";
24 auth_url =
25 "https://login.clicks.codes/realms/clicks/protocol/openid-connect/auth";
26 token_url =
27 "https://login.clicks.codes/realms/clicks/protocol/openid-connect/token";
28 api_url =
29 "https://login.clicks.codes/realms/clicks/protocol/openid-connect/userinfo";
30 role_attribute_path =
Skyler Greyfe1740c2023-10-21 01:24:18 +000031 "contains(resource_access.grafana.roles[*], 'server_admin') && 'GrafanaAdmin' || contains(resource_access.grafana.roles[*], 'admin') && 'Admin' || contains(resource_access.grafana.roles[*], 'editor') && 'Editor' || 'Viewer'";
TheCodedProfa7cc4e92023-10-10 19:29:06 -040032 allow_assign_grafana_admin = true;
33 auto_login = true;
34 };
35 "auth.basic".enabled = false;
36 auth.disable_login_form = true;
Skyler Grey6f0f43d2023-05-03 15:01:05 +000037 };
Skyler Greya78aa672023-05-20 13:48:18 +020038
39 provision.datasources.settings.datasources = [{
40 name = "clicks-postgresql";
41 type = "postgres";
42 access = "proxy";
43
44 url = "postgres://localhost:${toString config.services.postgresql.port}";
45 user = "clicks_grafana";
TheCodedProfa7cc4e92023-10-10 19:29:06 -040046 password =
47 "$__file{${config.sops.secrets.clicks_grafana_db_password.path}}";
Skyler Greya78aa672023-05-20 13:48:18 +020048 # defined in postgres.nix
49 }];
Skyler Grey6f0f43d2023-05-03 15:01:05 +000050 };
TheCodedProfa7cc4e92023-10-10 19:29:06 -040051
52 sops.secrets.clicks_grafana_client_secret = {
53 mode = "0600";
54 owner = "root";
55 group = "nobody";
56 sopsFile = ../secrets/grafana.json;
57 format = "json";
58 };
Skyler Greyfe1740c2023-10-21 01:24:18 +000059} (let isDerived = base != null;
60in if isDerived then
61 let
62 generators = lib.generators;
63 cfg = config.services.grafana;
64 settingsFormatIni = pkgs.formats.ini {
65 listToValue =
66 lib.concatMapStringsSep " " (generators.mkValueStringDefault { });
67 mkKeyValue = generators.mkKeyValueDefault {
68 mkValueString = v:
69 if v == null then "" else generators.mkValueStringDefault { } v;
70 } "=";
71 };
72 grafana_cfgfile = settingsFormatIni.generate "config.ini" cfg.settings;
73 in {
74 scalpel.trafos."grafana.ini" = {
75 source = toString grafana_cfgfile;
76 matchers."client_secret".secret =
77 config.sops.secrets.clicks_grafana_client_secret.path;
78 owner = config.users.users.grafana.name;
79 group = "nobody";
80 mode = "0400";
81 };
TheCodedProfa7cc4e92023-10-10 19:29:06 -040082
Skyler Greyfe1740c2023-10-21 01:24:18 +000083 systemd.services.grafana.serviceConfig.ExecStart = lib.mkForce
84 (pkgs.writeShellScript "grafana-start" ''
85 set -o errexit -o pipefail -o nounset -o errtrace
86 shopt -s inherit_errexit
TheCodedProfa7cc4e92023-10-10 19:29:06 -040087
Skyler Greyfe1740c2023-10-21 01:24:18 +000088 exec ${cfg.package}/bin/grafana-server -homepath ${cfg.dataDir} -config ${
89 config.scalpel.trafos."grafana.ini".destination
90 }
91 '');
92 systemd.services.grafana.restartTriggers = [ grafana_cfgfile ];
93 }
94else
95 { })