{config, ...}: let
  inherit (config.services) loki;
in {
  services.loki = {
    enable = true;
    configuration = {
      auth_enabled = false;
      server.http_listen_port = 3100;
      schema_config.configs = [
        {
          from = "2024-10-12";
          object_store = "filesystem";
          store = "tsdb";
          schema = "v13";
          index = {
            prefix = "index_";
            period = "24h";
          };
        }
      ];
      storage_config."filesystem".directory = "/tmp/loki/chunks";
      common = {
        ring = {
          instance_addr = "127.0.0.1";
          kvstore.store = "inmemory";
        };
        replication_factor = 1;
        path_prefix = "/tmp/loki";
      };
      limits_config = {
        ingestion_rate_strategy = "local";
        ingestion_rate_mb = 24;
        ingestion_burst_size_mb = 36;
      };
    };
  };
  services.prometheus.scrapeConfigs = [{
    job_name = "loki";
    static_configs = [
      {
        targets = ["localhost:3100"];
      }
    ];
  }];
  services.promtail = {
    enable = true;
    # https://grafana.com/docs/loki/latest/send-data/promtail/configuration/
    configuration = {
      server = {
        http_listen_port = 9080;
        grpc_listen_port = 0;
      };
      clients = [
        {url = "http://localhost:3100/loki/api/v1/push";}
      ];
      scrape_configs = [
        {
          job_name = "system";
          journal = {
            path = "/var/log/journal/";
          };
          relabel_configs = [
            {
              source_labels = ["__journal_message"];
              target_label = "message";
              regex = "(.+)";
            }
            {
              source_labels = ["__journal__systemd_unit"];
              target_label = "systemd_unit";
              regex = "(.+)";
            }
            {
              source_labels = ["__journal__systemd_user_unit"];
              target_label = "systemd_user_unit";
              regex = "(.+)";
            }
            {
              source_labels = ["__journal__transport"];
              target_label = "transport";
              regex = "(.+)";
            }
            {
              source_labels = ["__journal__priority_keyword"];
              target_label = "severity";
              regex = "(.+)";
            }
          ];
        }
      ];
    };
  };
  services.grafana.provision.datasources.settings.datasources = [{
    name = "Loki";
    type = "loki";
    url = "http://localhost:${toString loki.configuration.server.http_listen_port}";
  }];
}
