<commit_before>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.hqplayerd;
  pkg = pkgs.hqplayerd;
  # XXX: This is hard-coded in the distributed binary, don't try to change it.
  stateDir = "/var/lib/hqplayer";
  configDir = "/etc/hqplayer";
in
{
  options = {
    services.hqplayerd = {
      enable = mkEnableOption "HQPlayer Embedded";

      licenseFile = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = ''
          Path to the HQPlayer license key file.

          Without this, the service will run in trial mode and restart every 30
          minutes.
        '';
      };

      auth = {
        username = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            Username used for HQPlayer's WebUI.

            Without this you will need to manually create the credentials after
            first start by going to http://your.ip/8088/auth
          '';
        };

        password = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            Password used for HQPlayer's WebUI.

            Without this you will need to manually create the credentials after
            first start by going to http://your.ip/8088/auth
          '';
        };
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Open TCP port 8088 in the firewall for the server.
        '';
      };

      user = mkOption {
        type = types.str;
        default = "hqplayer";
        description = ''
          User account under which hqplayerd runs.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "hqplayer";
        description = ''
          Group account under which hqplayerd runs.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = (cfg.auth.username != null -> cfg.auth.password != null)
                 && (cfg.auth.password != null -> cfg.auth.username != null);
        message = "You must set either both services.hqplayer.auth.username and password, or neither.";
      }
    ];

    environment = {
      etc = {
        "hqplayer/hqplayerd4-key.xml" = mkIf (cfg.licenseFile != null) { source = cfg.licenseFile; };
        "modules-load.d/taudio2.conf".source = "${pkg}/etc/modules-load.d/taudio2.conf";
      };
      systemPackages = [ pkg ];
    };

    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = [ 8088 ];
    };

    services.udev.packages = [ pkg ];

    systemd = {
      tmpfiles.rules = [
        "d ${configDir}      0755 ${cfg.user} ${cfg.group} - -"
        "d ${stateDir}       0755 ${cfg.user} ${cfg.group} - -"
        "d ${stateDir}/home  0755 ${cfg.user} ${cfg.group} - -"
      ];

      services.hqplayerd = {
        description = "HQPlayer daemon";
        wantedBy = [ "multi-user.target" ];
        requires = [ "network-online.target" "sound.target" "systemd-udev-settle.service" ];
        after = [ "network-online.target" "sound.target" "systemd-udev-settle.service" "local-fs.target" "remote-fs.target" "systemd-tmpfiles-setup.service" ];

        environment.HOME = "${stateDir}/home";

        unitConfig.ConditionPathExists = [ configDir stateDir ];

        preStart = ''
          cp -r "${pkg}/var/lib/hqplayer/web" "${stateDir}"
          chmod -R u+wX "${stateDir}/web"

          if [ ! -f "${configDir}/hqplayerd.xml" ]; then
            echo "creating initial config file"
            install -m 0644 "${pkg}/etc/hqplayer/hqplayerd.xml" "${configDir}/hqplayerd.xml"
          fi
        '' + optionalString (cfg.auth.username != null && cfg.auth.password != null) ''
          ${pkg}/bin/hqplayerd -s ${cfg.auth.username} ${cfg.auth.password}
        '';

        serviceConfig = {
          ExecStart = "${pkg}/bin/hqplayerd";

          User = cfg.user;
          Group = cfg.group;

          Restart = "on-failure";
          RestartSec = 5;

          Nice = -10;
          IOSchedulingClass = "realtime";
          LimitMEMLOCK = "1G";
          LimitNICE = -10;
          LimitRTPRIO = 98;
        };
      };
    };

    users.groups = mkIf (cfg.group == "hqplayer") {
      hqplayer.gid = config.ids.gids.hqplayer;
    };

    users.users = mkIf (cfg.user == "hqplayer") {
      hqplayer = {
        description = "hqplayer daemon user";
        extraGroups = [ "audio" ];
        group = cfg.group;
        uid = config.ids.uids.hqplayer;
      };
    };
  };
}<commit_msg>nixos/hqplayerd: remove configurable user/group<commit_after>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.hqplayerd;
  pkg = pkgs.hqplayerd;
  # XXX: This is hard-coded in the distributed binary, don't try to change it.
  stateDir = "/var/lib/hqplayer";
  configDir = "/etc/hqplayer";
in
{
  options = {
    services.hqplayerd = {
      enable = mkEnableOption "HQPlayer Embedded";

      licenseFile = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = ''
          Path to the HQPlayer license key file.

          Without this, the service will run in trial mode and restart every 30
          minutes.
        '';
      };

      auth = {
        username = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            Username used for HQPlayer's WebUI.

            Without this you will need to manually create the credentials after
            first start by going to http://your.ip/8088/auth
          '';
        };

        password = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            Password used for HQPlayer's WebUI.

            Without this you will need to manually create the credentials after
            first start by going to http://your.ip/8088/auth
          '';
        };
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Open TCP port 8088 in the firewall for the server.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = (cfg.auth.username != null -> cfg.auth.password != null)
                 && (cfg.auth.password != null -> cfg.auth.username != null);
        message = "You must set either both services.hqplayer.auth.username and password, or neither.";
      }
    ];

    environment = {
      etc = {
        "hqplayer/hqplayerd4-key.xml" = mkIf (cfg.licenseFile != null) { source = cfg.licenseFile; };
        "modules-load.d/taudio2.conf".source = "${pkg}/etc/modules-load.d/taudio2.conf";
      };
      systemPackages = [ pkg ];
    };

    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = [ 8088 ];
    };

    services.udev.packages = [ pkg ];

    systemd = {
      tmpfiles.rules = [
        "d ${configDir}      0755 hqplayer hqplayer - -"
        "d ${stateDir}       0755 hqplayer hqplayer - -"
        "d ${stateDir}/home  0755 hqplayer hqplayer - -"
      ];

      services.hqplayerd = {
        description = "HQPlayer daemon";
        wantedBy = [ "multi-user.target" ];
        requires = [ "network-online.target" "sound.target" "systemd-udev-settle.service" ];
        after = [ "network-online.target" "sound.target" "systemd-udev-settle.service" "local-fs.target" "remote-fs.target" "systemd-tmpfiles-setup.service" ];

        environment.HOME = "${stateDir}/home";

        unitConfig.ConditionPathExists = [ configDir stateDir ];

        preStart = ''
          cp -r "${pkg}/var/lib/hqplayer/web" "${stateDir}"
          chmod -R u+wX "${stateDir}/web"

          if [ ! -f "${configDir}/hqplayerd.xml" ]; then
            echo "creating initial config file"
            install -m 0644 "${pkg}/etc/hqplayer/hqplayerd.xml" "${configDir}/hqplayerd.xml"
          fi
        '' + optionalString (cfg.auth.username != null && cfg.auth.password != null) ''
          ${pkg}/bin/hqplayerd -s ${cfg.auth.username} ${cfg.auth.password}
        '';

        serviceConfig = {
          ExecStart = "${pkg}/bin/hqplayerd";

          User = "hqplayer";
          Group = "hqplayer";

          Restart = "on-failure";
          RestartSec = 5;

          Nice = -10;
          IOSchedulingClass = "realtime";
          LimitMEMLOCK = "1G";
          LimitNICE = -10;
          LimitRTPRIO = 98;
        };
      };
    };

    users.groups = {
      hqplayer.gid = config.ids.gids.hqplayer;
    };

    users.users = {
      hqplayer = {
        description = "hqplayer daemon user";
        extraGroups = [ "audio" ];
        group = "hqplayer";
        uid = config.ids.uids.hqplayer;
      };
    };
  };
}
{'fraction_non_alphanumeric': 0.12995245641838352, 'fraction_numerical': 0.010187910346388952, 'mean_word_length': 1.7950015817779184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1678844', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2651, 'n_words': 621}