{
  config,
  lib,
  ...
}: let
  lokiPort = 3030;
  promtailPort = 3031;
  path = "/data/loki";
in {
  services.loki = {
    enable = true;
    configuration = {
      server.http_listen_address = "0.0.0.0";
      server.http_listen_port = lokiPort;
      auth_enabled = false;

      ingester = {
        lifecycler = {
          address = "127.0.0.1";
          ring = {
            kvstore = {
              store = "inmemory";
            };
            replication_factor = 1;
          };
        };
        chunk_idle_period = "1h";
        max_chunk_age = "1h";
        chunk_target_size = 999999;
        chunk_retain_period = "30s";
        max_transfer_retries = 0;
      };
      schema_config = {
        configs = [
          {
            from = "2023-10-28";
            store = "tsdb";
            object_store = "filesystem";
            schema = "v12";
            index = {
              prefix = "index_";
              period = "24h";
            };
          }
        ];
      };

      storage_config = {
        tsdb_shipper = {
          active_index_directory = "${path}/tsdb-index";
          cache_location = "${path}/tsdb-cache";
          cache_ttl = "24h";
          shared_store = "filesystem";
        };

        filesystem = {
          directory = "${path}/chunks";
        };
      };

      limits_config = {
        reject_old_samples = true;
        reject_old_samples_max_age = "168h";
        split_queries_by_interval = "24h";
        max_query_parallelism = 100;
      };

      query_scheduler = {
        max_outstanding_requests_per_tenant = 4096;
      };

      frontend = {
        max_outstanding_per_tenant = 4096;
      };

      chunk_store_config = {
        max_look_back_period = "0s";
      };

      table_manager = {
        retention_deletes_enabled = true;
        retention_period = "48h";
      };

      compactor = {
        working_directory = "${path}";
        shared_store = "filesystem";
        compactor_ring = {
          kvstore = {
            store = "inmemory";
          };
        };
      };

      ruler = {
        enable_api = true;
        enable_alertmanager_v2 = true;
        ring.kvstore.store = "inmemory";
        rule_path = "${path}/rules-temp";
        alertmanager_url = "http://127.0.0.1:${toString config.services.prometheus.alertmanager.port}";
        storage = {
          type = "local";
          local.directory = "${path}/rules";
        };
      };
    };
  };
}
