summary refs log tree commit diff
path: root/nixos/modules/services/video/mirakurun.nix
blob: 35303b2332c6772fae33e8b63b4850ef73126696 (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
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.mirakurun;
  mirakurun = pkgs.mirakurun;
  username = config.users.users.mirakurun.name;
  groupname = config.users.users.mirakurun.group;
  settingsFmt = pkgs.formats.yaml {};

  polkitRule = pkgs.writeTextDir "share/polkit-1/rules.d/10-mirakurun.rules" ''
    polkit.addRule(function (action, subject) {
      if (
        (action.id == "org.debian.pcsc-lite.access_pcsc" ||
          action.id == "org.debian.pcsc-lite.access_card") &&
        subject.user == "${username}"
      ) {
        return polkit.Result.YES;
      }
    });
  '';
in
  {
    options = {
      services.mirakurun = {
        enable = mkEnableOption "the Mirakurun DVR Tuner Server";

        port = mkOption {
          type = with types; nullOr port;
          default = 40772;
          description = ''
            Port to listen on. If <literal>null</literal>, it won't listen on
            any port.
          '';
        };

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

            <warning>
              <para>
                Exposing Mirakurun 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>
          '';
        };

        unixSocket = mkOption {
          type = with types; nullOr path;
          default = "/var/run/mirakurun/mirakurun.sock";
          description = ''
            Path to unix socket to listen on. If <literal>null</literal>, it
            won't listen on any unix sockets.
          '';
        };

        allowSmartCardAccess = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Install polkit rules to allow Mirakurun to access smart card readers
            which is commonly used along with tuner devices.
          '';
        };

        serverSettings = mkOption {
          type = settingsFmt.type;
          default = {};
          example = literalExpression ''
            {
              highWaterMark = 25165824;
              overflowTimeLimit = 30000;
            };
          '';
          description = ''
            Options for server.yml.

            Documentation:
            <link xlink:href="https://github.com/Chinachu/Mirakurun/blob/master/doc/Configuration.md"/>
          '';
        };

        tunerSettings = mkOption {
          type = with types; nullOr settingsFmt.type;
          default = null;
          example = literalExpression ''
            [
              {
                name = "tuner-name";
                types = [ "GR" "BS" "CS" "SKY" ];
                dvbDevicePath = "/dev/dvb/adapterX/dvrX";
              }
            ];
          '';
          description = ''
            Options which are added to tuners.yml. If none is specified, it will
            automatically be generated at runtime.

            Documentation:
            <link xlink:href="https://github.com/Chinachu/Mirakurun/blob/master/doc/Configuration.md"/>
          '';
        };

        channelSettings = mkOption {
          type = with types; nullOr settingsFmt.type;
          default = null;
          example = literalExpression ''
            [
              {
                name = "channel";
                types = "GR";
                channel = "0";
              }
            ];
          '';
          description = ''
            Options which are added to channels.yml. If none is specified, it
            will automatically be generated at runtime.

            Documentation:
            <link xlink:href="https://github.com/Chinachu/Mirakurun/blob/master/doc/Configuration.md"/>
          '';
        };
      };
    };

    config = mkIf cfg.enable {
      environment.systemPackages = [ mirakurun ] ++ optional cfg.allowSmartCardAccess polkitRule;
      environment.etc = {
        "mirakurun/server.yml".source = settingsFmt.generate "server.yml" cfg.serverSettings;
        "mirakurun/tuners.yml" = mkIf (cfg.tunerSettings != null) {
          source = settingsFmt.generate "tuners.yml" cfg.tunerSettings;
          mode = "0644";
          user = username;
          group = groupname;
        };
        "mirakurun/channels.yml" = mkIf (cfg.channelSettings != null) {
          source = settingsFmt.generate "channels.yml" cfg.channelSettings;
          mode = "0644";
          user = username;
          group = groupname;
        };
      };

      networking.firewall = mkIf cfg.openFirewall {
        allowedTCPPorts = mkIf (cfg.port != null) [ cfg.port ];
      };

      users.users.mirakurun = {
        description = "Mirakurun user";
        group = "video";
        isSystemUser = true;
      };

      services.mirakurun.serverSettings = {
        logLevel = mkDefault 2;
        path = mkIf (cfg.unixSocket != null) cfg.unixSocket;
        port = mkIf (cfg.port != null) cfg.port;
      };

      systemd.tmpfiles.rules = [
        "d '/etc/mirakurun' - ${username} ${groupname} - -"
      ];

      systemd.services.mirakurun = {
        description = mirakurun.meta.description;
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];
        serviceConfig = {
          ExecStart = "${mirakurun}/bin/mirakurun-start";
          User = username;
          Group = groupname;
          RuntimeDirectory="mirakurun";
          StateDirectory="mirakurun";
          Nice = -10;
          IOSchedulingClass = "realtime";
          IOSchedulingPriority = 7;
        };

        environment = {
          SERVER_CONFIG_PATH = "/etc/mirakurun/server.yml";
          TUNERS_CONFIG_PATH = "/etc/mirakurun/tuners.yml";
          CHANNELS_CONFIG_PATH = "/etc/mirakurun/channels.yml";
          SERVICES_DB_PATH = "/var/lib/mirakurun/services.json";
          PROGRAMS_DB_PATH = "/var/lib/mirakurun/programs.json";
          NODE_ENV = "production";
        };

        restartTriggers = let
          getconf = target: config.environment.etc."mirakurun/${target}.yml".source;
          targets = [
            "server"
          ] ++ optional (cfg.tunerSettings != null) "tuners"
            ++ optional (cfg.channelSettings != null) "channels";
        in (map getconf targets);
      };
    };
  }