aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/services/video/epgstation/default.nix
blob: 8d6d431fa55aaa5ee6c77ef5049af392775cc1a4 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.epgstation;

  username = config.users.users.epgstation.name;
  groupname = config.users.users.epgstation.group;

  settingsFmt = pkgs.formats.json {};
  settingsTemplate = settingsFmt.generate "config.json" cfg.settings;
  preStartScript = pkgs.writeScript "epgstation-prestart" ''
    #!${pkgs.runtimeShell}

    PASSWORD="$(head -n1 "${cfg.basicAuth.passwordFile}")"
    DB_PASSWORD="$(head -n1 "${cfg.database.passwordFile}")"

    # setup configuration
    touch /etc/epgstation/config.json
    chmod 640 /etc/epgstation/config.json
    sed \
      -e "s,@password@,$PASSWORD,g" \
      -e "s,@dbPassword@,$DB_PASSWORD,g" \
      ${settingsTemplate} > /etc/epgstation/config.json
    chown "${username}:${groupname}" /etc/epgstation/config.json

    # NOTE: Use password authentication, since mysqljs does not yet support auth_socket
    if [ ! -e /var/lib/epgstation/db-created ]; then
      ${pkgs.mysql}/bin/mysql -e \
        "GRANT ALL ON \`${cfg.database.name}\`.* TO '${username}'@'localhost' IDENTIFIED by '$DB_PASSWORD';"
      touch /var/lib/epgstation/db-created
    fi
  '';

  streamingConfig = builtins.fromJSON (builtins.readFile ./streaming.json);
  logConfig = {
    appenders.stdout.type = "stdout";
    categories = {
      default = { appenders = [ "stdout" ]; level = "info"; };
      system = { appenders = [ "stdout" ]; level = "info"; };
      access = { appenders = [ "stdout" ]; level = "info"; };
      stream = { appenders = [ "stdout" ]; level = "info"; };
    };
  };

  defaultPassword = "INSECURE_GO_CHECK_CONFIGURATION_NIX\n";
in
{
  options.services.epgstation = {
    enable = mkEnableOption pkgs.epgstation.meta.description;

    usePreconfiguredStreaming = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Use preconfigured default streaming options.

        Upstream defaults:
        <link xlink:href="https://github.com/l3tnun/EPGStation/blob/master/config/config.sample.json"/>
      '';
    };

    port = mkOption {
      type = types.port;
      default = 20772;
      description = ''
        HTTP port for EPGStation to listen on.
      '';
    };

    socketioPort = mkOption {
      type = types.port;
      default = cfg.port + 1;
      description = ''
        Socket.io port for EPGStation to listen on.
      '';
    };

    clientSocketioPort = mkOption {
      type = types.port;
      default = cfg.socketioPort;
      description = ''
        Socket.io port that the web client is going to connect to. This may be
        different from <option>socketioPort</option> if EPGStation is hidden
        behind a reverse proxy.
      '';
    };

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Open ports in the firewall for the EPGStation web interface.

        <warning>
          <para>
            Exposing EPGStation to the open internet is generally advised
            against. Only use it inside a trusted local network, or consider
            putting it behind a VPN if you want remote access.
          </para>
        </warning>
      '';
    };

    basicAuth = {
      user = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "epgstation";
        description = ''
          Basic auth username for EPGStation. If <literal>null</literal>, basic
          auth will be disabled.

          <warning>
            <para>
              Basic authentication has known weaknesses, the most critical being
              that it sends passwords over the network in clear text. Use this
              feature to control access to EPGStation within your family and
              friends, but don't rely on it for security.
            </para>
          </warning>
        '';
      };

      passwordFile = mkOption {
        type = types.path;
        default = pkgs.writeText "epgstation-password" defaultPassword;
        example = "/run/keys/epgstation-password";
        description = ''
          A file containing the password for <option>basicAuth.user</option>.
        '';
      };
    };

    database =  {
      name = mkOption {
        type = types.str;
        default = "epgstation";
        description = ''
          Name of the MySQL database that holds EPGStation's data.
        '';
      };

      passwordFile = mkOption {
        type = types.path;
        default = pkgs.writeText "epgstation-db-password" defaultPassword;
        example = "/run/keys/epgstation-db-password";
        description = ''
          A file containing the password for the database named
          <option>database.name</option>.
        '';
      };
    };

    settings = mkOption {
      description = ''
        Options to add to config.json.

        Documentation:
        <link xlink:href="https://github.com/l3tnun/EPGStation/blob/master/doc/conf-manual.md"/>
      '';

      default = {};
      example = {
        recPriority = 20;
        conflictPriority = 10;
      };

      type = types.submodule {
        freeformType = settingsFmt.type;

        options.readOnlyOnce = mkOption {
          type = types.bool;
          default = false;
          description = "Don't reload configuration files at runtime.";
        };

        options.mirakurunPath = mkOption (let
          sockPath = config.services.mirakurun.unixSocket;
        in {
          type = types.str;
          default = "http+unix://${replaceStrings ["/"] ["%2F"] sockPath}";
          example = "http://localhost:40772";
          description = "URL to connect to Mirakurun.";
        });

        options.encode = mkOption {
          type = with types; listOf attrs;
          description = "Encoding presets for recorded videos.";
          default = [
            { name = "H264";
              cmd = "${pkgs.epgstation}/libexec/enc.sh main";
              suffix = ".mp4";
              default = true; }
            { name = "H264-sub";
              cmd = "${pkgs.epgstation}/libexec/enc.sh sub";
              suffix = "-sub.mp4"; }
          ];
        };
      };
    };
  };

  config = mkIf cfg.enable {
    environment.etc = {
      "epgstation/operatorLogConfig.json".text = builtins.toJSON logConfig;
      "epgstation/serviceLogConfig.json".text = builtins.toJSON logConfig;
    };

    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = with cfg; [ port socketioPort ];
    };

    users.users.epgstation = {
      description = "EPGStation user";
      group = config.users.groups.epgstation.name;
      isSystemUser = true;
    };

    users.groups.epgstation = {};

    services.mirakurun.enable = mkDefault true;

    services.mysql = {
      enable = mkDefault true;
      package = mkDefault pkgs.mysql;
      ensureDatabases = [ cfg.database.name ];
      # FIXME: enable once mysqljs supports auth_socket
      # ensureUsers = [ {
      #   name = username;
      #   ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
      # } ];
    };

    services.epgstation.settings = let
      defaultSettings = {
        serverPort = cfg.port;
        socketioPort = cfg.socketioPort;
        clientSocketioPort = cfg.clientSocketioPort;

        dbType = mkDefault "mysql";
        mysql = {
          user = username;
          database = cfg.database.name;
          socketPath = mkDefault "/run/mysqld/mysqld.sock";
          password = mkDefault "@dbPassword@";
          connectTimeout = mkDefault 1000;
          connectionLimit = mkDefault 10;
        };

        basicAuth = mkIf (cfg.basicAuth.user != null) {
          user = mkDefault cfg.basicAuth.user;
          password = mkDefault "@password@";
        };

        ffmpeg = mkDefault "${pkgs.ffmpeg-full}/bin/ffmpeg";
        ffprobe = mkDefault "${pkgs.ffmpeg-full}/bin/ffprobe";

        fileExtension = mkDefault ".m2ts";
        maxEncode = mkDefault 2;
        maxStreaming = mkDefault 2;
      };
    in
    mkMerge [
      defaultSettings
      (mkIf cfg.usePreconfiguredStreaming streamingConfig)
    ];

    systemd.tmpfiles.rules = [
      "d '/var/lib/epgstation/streamfiles' - ${username} ${groupname} - -"
      "d '/var/lib/epgstation/recorded' - ${username} ${groupname} - -"
      "d '/var/lib/epgstation/thumbnail' - ${username} ${groupname} - -"
    ];

    systemd.services.epgstation = {
      description = pkgs.epgstation.meta.description;
      wantedBy = [ "multi-user.target" ];
      after = [
        "network.target"
      ] ++ optional config.services.mirakurun.enable "mirakurun.service"
        ++ optional config.services.mysql.enable "mysql.service";

      serviceConfig = {
        ExecStart = "${pkgs.epgstation}/bin/epgstation start";
        ExecStartPre = "+${preStartScript}";
        User = username;
        Group = groupname;
        StateDirectory = "epgstation";
        LogsDirectory = "epgstation";
        ConfigurationDirectory = "epgstation";
      };
    };
  };
}