|
| 1 | +{ pkgs |
| 2 | +, lib |
| 3 | +, name |
| 4 | +, config |
| 5 | +, ... |
| 6 | +}: |
| 7 | +let |
| 8 | + inherit (lib) types; |
| 9 | + yamlFormat = pkgs.formats.yaml { }; |
| 10 | +in |
| 11 | +{ |
| 12 | + options = { |
| 13 | + package = lib.mkPackageOption pkgs "grafana-loki" { }; |
| 14 | + |
| 15 | + httpAddress = lib.mkOption { |
| 16 | + type = types.str; |
| 17 | + description = "Which address to access loki from."; |
| 18 | + default = "localhost"; |
| 19 | + }; |
| 20 | + |
| 21 | + httpPort = lib.mkOption { |
| 22 | + type = types.port; |
| 23 | + description = "Which HTTP port to loki should listen on."; |
| 24 | + default = 3100; |
| 25 | + }; |
| 26 | + |
| 27 | + grpcPort = lib.mkOption { |
| 28 | + type = types.port; |
| 29 | + description = "Which gRPC port to run loki should listen on."; |
| 30 | + default = 9096; |
| 31 | + }; |
| 32 | + |
| 33 | + extraConfig = lib.mkOption { |
| 34 | + type = yamlFormat.type; |
| 35 | + default = { }; |
| 36 | + description = '' |
| 37 | + Specify the configuration for Loki in Nix. |
| 38 | +
|
| 39 | + See https://grafana.com/docs/loki/latest/configuration/ for available options. |
| 40 | + ''; |
| 41 | + }; |
| 42 | + |
| 43 | + extraFlags = lib.mkOption { |
| 44 | + type = types.listOf types.str; |
| 45 | + default = [ ]; |
| 46 | + example = lib.literalExpression '' |
| 47 | + [ "-config.expand-env=true" ] |
| 48 | + ''; |
| 49 | + description = '' |
| 50 | + Additional flags to pass to loki. |
| 51 | + ''; |
| 52 | + }; |
| 53 | + }; |
| 54 | + |
| 55 | + config = { |
| 56 | + outputs = { |
| 57 | + settings = { |
| 58 | + processes."${name}" = |
| 59 | + let |
| 60 | + lokiConfig = lib.recursiveUpdate |
| 61 | + { |
| 62 | + server = { |
| 63 | + http_listen_port = config.httpPort; |
| 64 | + grpc_listen_port = config.grpcPort; |
| 65 | + }; |
| 66 | + common = { |
| 67 | + instance_addr = config.httpAddress; |
| 68 | + path_prefix = "${config.dataDir}/"; |
| 69 | + storage = { |
| 70 | + filesystem = { |
| 71 | + chunks_directory = "${config.dataDir}/chunks"; |
| 72 | + rules_directory = "${config.dataDir}/rules"; |
| 73 | + }; |
| 74 | + }; |
| 75 | + replication_factor = 1; |
| 76 | + ring = { |
| 77 | + kvstore = { |
| 78 | + store = "inmemory"; |
| 79 | + }; |
| 80 | + }; |
| 81 | + }; |
| 82 | + schema_config = { |
| 83 | + configs = [ |
| 84 | + { |
| 85 | + from = "2020-10-24"; |
| 86 | + store = "tsdb"; |
| 87 | + object_store = "filesystem"; |
| 88 | + schema = "v13"; |
| 89 | + index = { |
| 90 | + prefix = "index_"; |
| 91 | + period = "24h"; |
| 92 | + }; |
| 93 | + } |
| 94 | + ]; |
| 95 | + }; |
| 96 | + } |
| 97 | + config.extraConfig; |
| 98 | + lokiConfigYaml = yamlFormat.generate "loki.yaml" lokiConfig; |
| 99 | + in |
| 100 | + { |
| 101 | + command = "${config.package}/bin/loki --config.file=${lokiConfigYaml} ${lib.escapeShellArgs config.extraFlags}"; |
| 102 | + readiness_probe = { |
| 103 | + http_get = { |
| 104 | + host = config.httpAddress; |
| 105 | + scheme = "http"; |
| 106 | + port = config.httpPort; |
| 107 | + path = "/ready"; |
| 108 | + }; |
| 109 | + initial_delay_seconds = 15; |
| 110 | + period_seconds = 10; |
| 111 | + timeout_seconds = 2; |
| 112 | + success_threshold = 1; |
| 113 | + failure_threshold = 5; |
| 114 | + }; |
| 115 | + availability = { |
| 116 | + restart = "on_failure"; |
| 117 | + max_restarts = 5; |
| 118 | + }; |
| 119 | + }; |
| 120 | + }; |
| 121 | + }; |
| 122 | + }; |
| 123 | +} |
0 commit comments