let
  dataDir = "/var/lib/loki";
in
{
  services.loki = {
    enable = true;
    configuration = {
      auth_enabled = false;

      server.http_listen_port = 3100;

      ingester = {
        lifecycler = {
          address = "0.0.0.0";
          ring = {
            kvstore.store = "inmemory";
            replication_factor = 1;
          };
          final_sleep = "0s";
        };
        chunk_idle_period = "1h";
        max_chunk_age = "1h";
        chunk_target_size = 1024 * 1024; # 1 MiB
        chunk_retain_period = "30s";
      };

      schema_config = {
        configs = [
          {
            from = "2024-01-01";
            store = "tsdb";
            object_store = "filesystem";
            schema = "v13";
            index = {
              prefix = "_index";
              period = "24h";
            };
          }
        ];
      };

      storage_config = {
        filesystem.directory = "${dataDir}/chunks";

        tsdb_shipper = {
          active_index_directory = "${dataDir}/tsdb-shipper-active";
          cache_location = "${dataDir}/tsdb-shipper-cache";
          cache_ttl = "24h";
        };
      };

      limits_config = {
        reject_old_samples = true;
        reject_old_samples_max_age = "168h"; # 1 week
      };

      table_manager = {
        retention_deletes_enabled = false;
        retention_period = "0s";
      };

      compactor = {
        working_directory = dataDir;
        compactor_ring.kvstore.store = "inmemory";
      };
    };
  };
}