{ pkgs, ... }:

{
  environment.etc."xiu/config.toml".text = ''
    [rtmp]
    enabled = true
    port = 1935
    gop_num = 0

    [rtmp.pull]
    enabled = false
    address = "127.0.0.1"
    port = 1935

    [[rtmp.push]]
    enabled = false
    address = "127.0.0.1"
    port = 1936

    [rtmp.auth]
    pull_enabled = false
    push_enabled = false
    algorithm = "simple"

    [rtsp]
    enabled = false
    port = 8554
    relay_enabled = false

    [rtsp.auth]
    pull_enabled = false
    push_enabled = false
    algorithm = "simple"

    [webrtc]
    enabled = false
    port = 8900

    [webrtc.auth]
    pull_enabled = false
    push_enabled = false
    algorithm = "simple"

    [httpflv]
    enabled = false
    port = 8081

    [httpflv.auth]
    pull_enabled = false
    algorithm = "simple"

    [hls]
    enabled = true
    port = 10023
    need_record = false

    [hls.auth]
    pull_enabled = false
    algorithm = "simple"

    [log]
    level = "info"

    [log.file]
    enabled = false
    rotate = "day"
    path = "/var/log/xiu"

    [authsecret]
    key = ""
    password = ""

    [httpapi]
    port = 8000

    [httpnotify]
    enabled = false
    on_publish = ""
    on_unpublish = ""
    on_play = ""
    on_stop = ""
  '';

  systemd.services.xiu = {
    description = "XIU live media server";
    wantedBy = [ "multi-user.target" ];
    after = [ "network.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.xiu}/bin/xiu --config /etc/xiu/config.toml";
      DynamicUser = true;
      StateDirectory = "xiu";
      WorkingDirectory = "/var/lib/xiu";
      Restart = "on-failure";
      RestartSec = "5s";
    };
  };

  networking.firewall.allowedTCPPorts = [
    1935
    10023
  ];
}
