{
  config,
  pkgs,
  lib,
  ...
}:

let
  inherit (lib) types;

  cfg = config.services.pijul-srv-lite;
in
{
  options.services.pijul-srv-lite = {
    enable = lib.mkEnableOption "pijul-srv-lite";

    package = lib.mkOption {
      type = types.package;
      default =
        if lib.isDerivation pkgs.pijul-srv-lite.pijul-srv-lite then
          pkgs.pijul-srv-lite.pijul-srv-lite
        else
          pkgs.pijul-srv-lite;
      defaultText = lib.literalExpression "pkgs.pijul-srv-lite.pijul-srv-lite or pkgs.pijul-srv-lite";
      description = "pijul-srv-lite package to use.";
    };

    user = lib.mkOption {
      type = types.nonEmptyStr;
      default = "pijul-srv-lite";
      description = "User running pijul-srv-lite service.";
    };

    group = lib.mkOption {
      type = types.nonEmptyStr;
      default = "pijul-srv-lite";
      description = "Group running pijul-srv-lite service.";
    };

    bind-addr = lib.mkOption {
      type = with types; nullOr nonEmptyStr;
      default = null;
      description = "pijul-srv-lite bind address.";
      example = "127.0.0.1";
    };

    port = lib.mkOption {
      type = with types; nullOr port;
      default = null;
      description = "pijul-srv-lite daemon port.";
      example = 11988;
    };

    socket = lib.mkOption {
      type = with types; nullOr path;
      default = "${cfg.runtime-dir}/pijul-srv-lite.sock";
      description = "pijul-srv-lite socket path.";
    };

    remote-dir = lib.mkOption {
      type = types.path;
      description = ''
        The path to serve Pijul remotes from. Note that pijul-srv-lite can
        handle recursive searching to nest Pijul remotes, symlink from many
        places to one flat directory, put it under a specific users, & so on.
      '';
    };

    runtime-dir = lib.mkOption {
      type = types.path;
      default = "/run/pijul-srv-lite";
      description = "Runtime directory which holds application’s cache files.";
    };

    open-firewall = lib.mkOption {
      type = types.bool;
      default = false;
      description = "Open the firewall on all interfaces to the port set in the pijul-srv-lite configuration.";
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = lib.xor (cfg.socket != null) (cfg.bind-addr != null && cfg.port != null);
        message = ''
          pijul-srv-lite option must be: socket ⊕ (bind address ∧ port).
        '';
      }
    ];

    systemd.services.pijul-srv-lite = {
      description = "pijul-srv-lite daemon";
      wantedBy = [ "multi-user.target" ];
      wants = [
        "network.target"
        "local-fs.target"
      ];
      after = [
        "network.target"
        "local-fs.target"
        "systemd-tmpfiles-setup.service"
      ];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        ExecStart =
          let
            srvArgs = lib.escapeShellArgs (
              lib.optionals (cfg.socket != null) [
                "--socket"
                cfg.socket
              ]
              ++ lib.optionals (cfg.bind-addr != null && cfg.port != null) [
                "--bind-addr"
                cfg.bind-addr
                "--port"
                cfg.port
              ]
              ++ [ cfg.remote-dir ]
            );
          in
          "${lib.getExe cfg.package} ${srvArgs}";
      };
    };

    systemd.tmpfiles.settings."10-pijul-srv-lite".${cfg."runtime-dir"}.d = {
      inherit (cfg) user group;
      # Allow the service group to traverse the directory and connect to the
      # 0660 socket inside it.
      mode = "0770";
    };

    users = {
      users.${cfg.user} = lib.optionalAttrs (cfg.user == "pijul-srv-lite") {
        isSystemUser = true;
        group = cfg.group;
      };
      groups.${cfg.group} = { };
    };

    networking.firewall.allowedTCPPorts = lib.mkIf (cfg.open-firewall && cfg.port != null) [
      cfg.port
    ];
  };
}
