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 Lower values (e.g. 30 seconds) should be used if your network blocks the discovery unicast.
58 Some relevant information can be found here:
59 https://sourceforge.net/p/minidlna/discussion/879957/thread/1389d197/
60 '';
61 };
62 options.port = mkOption {
63 type = types.port;
64 default = 8200;
65 description = lib.mdDoc "Port number for HTTP traffic (descriptions, SOAP, media transfer).";
66 };
67 options.db_dir = mkOption {
68 type = types.path;
69 default = "/var/cache/minidlna";
70 example = "/tmp/minidlna";
71 description = lib.mdDoc "Specify the directory where you want MiniDLNA to store its database and album art cache.";
72 };
73 options.friendly_name = mkOption {
74 type = types.str;
75 default = config.networking.hostName;
76 defaultText = literalExpression "config.networking.hostName";
77 example = "rpi3";
78 description = lib.mdDoc "Name that the DLNA server presents to clients.";
79 };
80 options.root_container = mkOption {
81 type = types.str;
82 default = "B";
83 example = ".";
84 description = lib.mdDoc "Use a different container as the root of the directory tree presented to clients.";
85 };
86 options.log_level = mkOption {
87 type = types.str;
88 default = "warn";
89 example = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn";
90 description = lib.mdDoc "Defines the type of messages that should be logged and down to which level of importance.";
91 };
92 options.inotify = mkOption {
93 type = types.enum [ "yes" "no" ];
94 default = "no";
95 description = lib.mdDoc "Whether to enable inotify monitoring to automatically discover new files.";
96 };
97 options.enable_tivo = mkOption {
98 type = types.enum [ "yes" "no" ];
99 default = "no";
100 description = lib.mdDoc "Support for streaming .jpg and .mp3 files to a TiVo supporting HMO.";
101 };
102 options.wide_links = mkOption {
103 type = types.enum [ "yes" "no" ];
104 default = "no";
105 description = lib.mdDoc "Set this to yes to allow symlinks that point outside user-defined `media_dir`.";
106 };
107 };
108 };
109
110 imports = [
111 (mkRemovedOptionModule [ "services" "minidlna" "config" ] "")
112 (mkRemovedOptionModule [ "services" "minidlna" "extraConfig" ] "")
113 (mkRenamedOptionModule [ "services" "minidlna" "loglevel"] [ "services" "minidlna" "settings" "log_level" ])
114 (mkRenamedOptionModule [ "services" "minidlna" "rootContainer"] [ "services" "minidlna" "settings" "root_container" ])
115 (mkRenamedOptionModule [ "services" "minidlna" "mediaDirs"] [ "services" "minidlna" "settings" "media_dir" ])
116 (mkRenamedOptionModule [ "services" "minidlna" "friendlyName"] [ "services" "minidlna" "settings" "friendly_name" ])
117 (mkRenamedOptionModule [ "services" "minidlna" "announceInterval"] [ "services" "minidlna" "settings" "notify_interval" ])
118 ];
119
120 ###### implementation
121 config = mkIf cfg.enable {
122 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.settings.port ];
123 networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ 1900 ];
124
125 users.users.minidlna = {
126 description = "MiniDLNA daemon user";
127 group = "minidlna";
128 uid = config.ids.uids.minidlna;
129 };
130
131 users.groups.minidlna.gid = config.ids.gids.minidlna;
132
133 systemd.services.minidlna = {
134 description = "MiniDLNA Server";
135 wantedBy = [ "multi-user.target" ];
136 after = [ "network.target" ];
137
138 serviceConfig = {
139 User = "minidlna";
140 Group = "minidlna";
141 CacheDirectory = "minidlna";
142 RuntimeDirectory = "minidlna";
143 PIDFile = "/run/minidlna/pid";
144 ExecStart = "${pkgs.minidlna}/sbin/minidlnad -S -P /run/minidlna/pid -f ${settingsFile}";
145 };
146 };
147 };
148}