blob: 84b4eb1c1098245049dbe654873dc777139ee8dd [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 {
Samuel Shuert180ff192023-12-09 15:16:55 -05008 domain = "grafana.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 =
Samuel Shuert180ff192023-12-09 15:16:55 -050025 "https://login.clicks.codes/realms/master/protocol/openid-connect/auth";
TheCodedProfa7cc4e92023-10-10 19:29:06 -040026 token_url =
Samuel Shuert180ff192023-12-09 15:16:55 -050027 "https://login.clicks.codes/realms/master/protocol/openid-connect/token";
TheCodedProfa7cc4e92023-10-10 19:29:06 -040028 api_url =
Samuel Shuert180ff192023-12-09 15:16:55 -050029 "https://login.clicks.codes/realms/master/protocol/openid-connect/userinfo";
TheCodedProfa7cc4e92023-10-10 19:29:06 -040030 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
Skyler Grey915067d2023-12-03 13:46:53 +000052 systemd.services.grafana.requires = [ "postgresql.service" ];
53
TheCodedProfa7cc4e92023-10-10 19:29:06 -040054 sops.secrets.clicks_grafana_client_secret = {
55 mode = "0600";
Skyler Greybcb46d32023-11-10 20:48:38 +000056 owner = config.users.users.root.name;
57 group = config.users.users.root.group;
Samuel Shuertf68685d2023-10-28 20:07:56 -040058 sopsFile = ../../secrets/grafana.json;
TheCodedProfa7cc4e92023-10-10 19:29:06 -040059 format = "json";
60 };
Skyler Greyfe1740c2023-10-21 01:24:18 +000061} (let isDerived = base != null;
62in if isDerived then
63 let
64 generators = lib.generators;
65 cfg = config.services.grafana;
66 settingsFormatIni = pkgs.formats.ini {
67 listToValue =
68 lib.concatMapStringsSep " " (generators.mkValueStringDefault { });
69 mkKeyValue = generators.mkKeyValueDefault {
70 mkValueString = v:
71 if v == null then "" else generators.mkValueStringDefault { } v;
72 } "=";
73 };
74 grafana_cfgfile = settingsFormatIni.generate "config.ini" cfg.settings;
75 in {
76 scalpel.trafos."grafana.ini" = {
77 source = toString grafana_cfgfile;
78 matchers."client_secret".secret =
79 config.sops.secrets.clicks_grafana_client_secret.path;
80 owner = config.users.users.grafana.name;
Skyler Greybcb46d32023-11-10 20:48:38 +000081 group = config.users.users.root.name;
Skyler Greyfe1740c2023-10-21 01:24:18 +000082 mode = "0400";
83 };
TheCodedProfa7cc4e92023-10-10 19:29:06 -040084
Skyler Greyfe1740c2023-10-21 01:24:18 +000085 systemd.services.grafana.serviceConfig.ExecStart = lib.mkForce
86 (pkgs.writeShellScript "grafana-start" ''
87 set -o errexit -o pipefail -o nounset -o errtrace
88 shopt -s inherit_errexit
TheCodedProfa7cc4e92023-10-10 19:29:06 -040089
Skyler Greyfe1740c2023-10-21 01:24:18 +000090 exec ${cfg.package}/bin/grafana-server -homepath ${cfg.dataDir} -config ${
91 config.scalpel.trafos."grafana.ini".destination
92 }
93 '');
94 systemd.services.grafana.restartTriggers = [ grafana_cfgfile ];
95 }
96else
97 { })