about summary refs log tree commit diff
path: root/nixos/modules/services/audio/hqplayerd.nix
blob: 3a703c97c0f77d4e6674f2c0f0279539f5fadc36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{ 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} - -"
      ];

      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" ];

        unitConfig.ConditionPathExists = [ configDir stateDir ];

        preStart =
          let
            blankCfg = pkgs.writeText "hqplayerd.xml" ''
              <?xml version="1.0" encoding="utf-8"?>
              <xml>
              </xml>
            '';
          in
          ''
            cp -r "${pkg}/var/lib/hqplayer/web" "${stateDir}"
            chmod -R u+wX "${stateDir}/web"

            if [ ! -f "${configDir}/hqplayerd.xml" ]; then
              echo "creating blank config file"
              install -m 0644 "${blankCfg}" "${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;
      };
    };
  };
}