# Auto-generated using compose2nix v0.3.1.
{
  pkgs,
  lib,
  config,
  ...
}:

{
  # Containers
  virtualisation.oci-containers.containers."gerbil" = {
    image = "fosrl/gerbil:1.0.0";
    volumes = [
      "/etc/pangolin/config:/var/config:rw"
    ];
    ports = [
      "51820:51820/udp"
      "443:443/tcp"
      "222:222/tcp"
      "80:80/tcp"
    ];
    cmd = [
      "--reachableAt=http://gerbil:3003"
      "--generateAndSaveKeyTo=/var/config/key"
      "--remoteConfig=http://pangolin:3001/api/v1/gerbil/get-config"
      "--reportBandwidthTo=http://pangolin:3001/api/v1/gerbil/receive-bandwidth"
    ];
    dependsOn = [
      "pangolin"
    ];
    log-driver = "journald";
    extraOptions = [
      "--cap-add=NET_ADMIN"
      # "--cap-add=SYS_MODULE"
      "--network-alias=gerbil"
      "--network=pangolin"
    ];
  };
  systemd.services."docker-gerbil" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    after = [
      "docker-network-pangolin.service"
    ];
    requires = [
      "docker-network-pangolin.service"
    ];
    partOf = [
      "docker-compose-pangolin-root.target"
    ];
    wantedBy = [
      "docker-compose-pangolin-root.target"
    ];
  };
  virtualisation.oci-containers.containers."pangolin" = {
    image = "fosrl/pangolin:1.5.1";
    volumes = [
      "/etc/pangolin/config:/app/config:rw"
    ];
    log-driver = "journald";
    extraOptions = [
      "--health-cmd=[\"curl\", \"-f\", \"http://localhost:3001/api/v1/\"]"
      "--health-interval=3s"
      "--health-retries=15"
      "--health-timeout=3s"
      "--network-alias=pangolin"
      "--network=pangolin"
    ];
  };

  systemd.services."docker-pangolin" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    after = [
      "docker-network-pangolin.service"
      "pangolin-config-sync.service"
    ];
    requires = [
      "docker-network-pangolin.service"
      "pangolin-config-sync.service"
    ];
    partOf = [
      "docker-compose-pangolin-root.target"
    ];
    wantedBy = [
      "docker-compose-pangolin-root.target"
    ];
  };

  virtualisation.oci-containers.containers."traefik" = {
    image = "traefik:v3.4.0";
    environment = {
      "CLOUDFLARE_DNS_API_TOKEN" = config.secretsSpec.api.cloudflare;
    };
    volumes = [
      "/etc/pangolin/config/letsencrypt:/letsencrypt:rw"
      "/etc/pangolin/config/traefik:/etc/traefik:ro"
    ];
    cmd = [ "--configFile=/etc/traefik/traefik_config.yml" ];
    dependsOn = [
      "gerbil"
      "pangolin"
    ];
    log-driver = "journald";
    extraOptions = [
      "--network=container:gerbil"
    ];
  };
  systemd.services."docker-traefik" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    partOf = [
      "docker-compose-pangolin-root.target"
    ];
    wantedBy = [
      "docker-compose-pangolin-root.target"
    ];
  };

  # Networks
  systemd.services."docker-network-pangolin" = {
    path = [ pkgs.docker ];
    serviceConfig = {
      Type = "oneshot";
      RemainAfterExit = true;
      ExecStop = "docker network rm -f pangolin";
    };
    script = ''
      docker network inspect pangolin || docker network create pangolin --driver=bridge
    '';
    partOf = [ "docker-compose-pangolin-root.target" ];
    wantedBy = [ "docker-compose-pangolin-root.target" ];
  };

  # Root service
  # When started, this will automatically create all resources and start
  # the containers. When stopped, this will teardown all resources.
  systemd.targets."docker-compose-pangolin-root" = {
    unitConfig = {
      Description = "Root target generated by compose2nix.";
    };
    wantedBy = [ "multi-user.target" ];
  };
}
