1# Module for MiniDLNA, a simple DLNA server.
2{ config, lib, pkgs, ... }:
3with lib;
4
5let
6 cfg = config.services.minidlna;
7 settingsFormat = pkgs.formats.keyValue { listsAsDuplicateKeys = true; };
8 settingsFile = settingsFormat.generate "minidlna.conf" cfg.settings;
9in
10
11{
12 ###### interface
13 options.services.minidlna.enable = mkOption {
14 type = types.bool;
15 default = false;
16 description = lib.mdDoc ''
17 Whether to enable MiniDLNA, a simple DLNA server.
18 It serves media files such as video and music to DLNA client devices
19 such as televisions and media players. If you use the firewall consider
20 adding the following: `services.minidlna.openFirewall = true;`
21 '';
22 };
23
24 options.services.minidlna.openFirewall = mkOption {
25 type = types.bool;
26 default = false;
27 description = lib.mdDoc ''
28 Whether to open both HTTP (TCP) and SSDP (UDP) ports in the firewall.
29 '';
30 };
31
32 options.services.minidlna.settings = mkOption {
33 default = {};
34 description = lib.mdDoc ''
35 The contents of MiniDLNA's configuration file.
36 When the service is activated, a basic template is generated from the current options opened here.
37 '';
38 type = types.submodule {
39 freeformType = settingsFormat.type;
40
41 options.media_dir = mkOption {
42 type = types.listOf types.str;
43 default = [];
44 example = [ "/data/media" "V,/home/alice/video" ];
45 description = lib.mdDoc ''
46 Directories to be scanned for media files.
47 The `A,` `V,` `P,` prefixes restrict a directory to audio, video or image files.
48 The directories must be accessible to the `minidlna` user account.
49 '';
50 };
51 options.notify_interval = mkOption {
52 type = types.int;
53 default = 90000;
54 description = lib.mdDoc ''
55 The interval between announces (in seconds).
56 Instead of waiting for announces, you should set `openFirewall` option to use SSDP discovery.
57 Furthermore, this option has been set to 90000 in order to prevent disconnects with certain
58 clients and relies solely on the discovery.
59
60 Lower values (e.g. 30 seconds) should be used if you can't use the discovery.
61 Some relevant information can be found here:
62 https://sourceforge.net/p/minidlna/discussion/879957/thread/1389d197/
63 '';
64 };
65 options.port = mkOption {
66 type = types.port;
67 default = 8200;
68 description = lib.mdDoc "Port number for HTTP traffic (descriptions, SOAP, media transfer).";
69 };
70 options.db_dir = mkOption {
71 type = types.path;
72 default = "/var/cache/minidlna";
73 example = "/tmp/minidlna";
74 description = lib.mdDoc "Specify the directory where you want MiniDLNA to store its database and album art cache.";
75 };
76 options.friendly_name = mkOption {
77 type = types.str;
78 default = config.networking.hostName;
79 defaultText = literalExpression "config.networking.hostName";
80 example = "rpi3";
81 description = lib.mdDoc "Name that the DLNA server presents to clients.";
82 };
83 options.root_container = mkOption {
84 type = types.str;
85 default = ".";
86 example = "B";
87 description = lib.mdDoc "Use a different container as the root of the directory tree presented to clients.";
88 };
89 options.log_level = mkOption {
90 type = types.str;
91 default = "warn";
92 example = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn";
93 description = lib.mdDoc "Defines the type of messages that should be logged and down to which level of importance.";
94 };
95 options.inotify = mkOption {
96 type = types.enum [ "yes" "no" ];
97 default = "no";
98 description = lib.mdDoc "Whether to enable inotify monitoring to automatically discover new files.";
99 };
100 options.enable_tivo = mkOption {
101 type = types.enum [ "yes" "no" ];
102 default = "no";
103 description = lib.mdDoc "Support for streaming .jpg and .mp3 files to a TiVo supporting HMO.";
104 };
105 options.wide_links = mkOption {
106 type = types.enum [ "yes" "no" ];
107 default = "no";
108 description = lib.mdDoc "Set this to yes to allow symlinks that point outside user-defined `media_dir`.";
109 };
110 };
111 };
112
113 imports = [
114 (mkRemovedOptionModule [ "services" "minidlna" "config" ] "")
115 (mkRemovedOptionModule [ "services" "minidlna" "extraConfig" ] "")
116 (mkRenamedOptionModule [ "services" "minidlna" "loglevel"] [ "services" "minidlna" "settings" "log_level" ])
117 (mkRenamedOptionModule [ "services" "minidlna" "rootContainer"] [ "services" "minidlna" "settings" "root_container" ])
118 (mkRenamedOptionModule [ "services" "minidlna" "mediaDirs"] [ "services" "minidlna" "settings" "media_dir" ])
119 (mkRenamedOptionModule [ "services" "minidlna" "friendlyName"] [ "services" "minidlna" "settings" "friendly_name" ])
120 (mkRenamedOptionModule [ "services" "minidlna" "announceInterval"] [ "services" "minidlna" "settings" "notify_interval" ])
121 ];
122
123 ###### implementation
124 config = mkIf cfg.enable {
125 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.settings.port ];
126 networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ 1900 ];
127
128 users.users.minidlna = {
129 description = "MiniDLNA daemon user";
130 group = "minidlna";
131 uid = config.ids.uids.minidlna;
132 };
133
134 users.groups.minidlna.gid = config.ids.gids.minidlna;
135
136 systemd.services.minidlna =
137 { description = "MiniDLNA Server";
138
139 wantedBy = [ "multi-user.target" ];
140 after = [ "network.target" ];
141
142 serviceConfig =
143 { User = "minidlna";
144 Group = "minidlna";
145 CacheDirectory = "minidlna";
146 RuntimeDirectory = "minidlna";
147 PIDFile = "/run/minidlna/pid";
148 ExecStart =
149 "${pkgs.minidlna}/sbin/minidlnad -S -P /run/minidlna/pid" +
150 " -f ${settingsFile}";
151 };
152 };
153 };
154}