Skip to content

Commit

Permalink
nixos/minidlna: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bb2020 committed Oct 5, 2024
1 parent 3cf9d5f commit ce70753
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions nixos/modules/services/networking/minidlna.nix
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
# Module for MiniDLNA, a simple DLNA server.
{ config, lib, pkgs, ... }:

let
cfg = config.services.minidlna;
settingsFormat = pkgs.formats.keyValue { listsAsDuplicateKeys = true; };
settingsFile = settingsFormat.generate "minidlna.conf" cfg.settings;
in
format = pkgs.formats.keyValue { listsAsDuplicateKeys = true; };
cfgfile = format.generate "minidlna.conf" cfg.settings;

{
###### interface
options.services.minidlna.enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to enable MiniDLNA, a simple DLNA server.
It serves media files such as video and music to DLNA client devices
such as televisions and media players. If you use the firewall, consider
adding the following: `services.minidlna.openFirewall = true;`
'';
};

options.services.minidlna.openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to open both HTTP (TCP) and SSDP (UDP) ports in the firewall.
'';
};
in {
options.services.minidlna.enable = lib.mkEnableOption "MiniDLNA, a simple DLNA server. Consider adding `openFirewall = true` into your config";
options.services.minidlna.openFirewall = lib.mkEnableOption "opening HTTP (TCP) and SSDP (UDP) ports in the firewall";

options.services.minidlna.settings = lib.mkOption {
default = {};
description = ''
The contents of MiniDLNA's configuration file.
When the service is activated, a basic template is generated from the current options opened here.
'';
description = "Configuration for `minidlna.conf(5)`.";
type = lib.types.submodule {
freeformType = settingsFormat.type;
freeformType = format.type;

options.media_dir = lib.mkOption {
type = lib.types.listOf lib.types.str;
Expand All @@ -53,9 +31,8 @@ in
description = ''
The interval between announces (in seconds).
Instead of waiting for announces, you should set `openFirewall` option to use SSDP discovery.
Lower values (e.g. 30 seconds) should be used if your network blocks the discovery unicast.
Some relevant information can be found here:
https://sourceforge.net/p/minidlna/discussion/879957/thread/1389d197/
Lower values (e.g. 30 seconds) should be used if your network is blocking the SSDP multicast.
Some relevant information can be found [here](https://sourceforge.net/p/minidlna/discussion/879957/thread/1389d197/).
'';
};
options.port = lib.mkOption {
Expand All @@ -67,7 +44,7 @@ in
type = lib.types.path;
default = "/var/cache/minidlna";
example = "/tmp/minidlna";
description = "Specify the directory where you want MiniDLNA to store its database and album art cache.";
description = "Specify the directory to store the database and album art cache.";
};
options.friendly_name = lib.mkOption {
type = lib.types.str;
Expand Down Expand Up @@ -111,29 +88,17 @@ in
};
};

imports = [
(lib.mkRemovedOptionModule [ "services" "minidlna" "config" ] "")
(lib.mkRemovedOptionModule [ "services" "minidlna" "extraConfig" ] "")
(lib.mkRenamedOptionModule [ "services" "minidlna" "loglevel"] [ "services" "minidlna" "settings" "log_level" ])
(lib.mkRenamedOptionModule [ "services" "minidlna" "rootContainer"] [ "services" "minidlna" "settings" "root_container" ])
(lib.mkRenamedOptionModule [ "services" "minidlna" "mediaDirs"] [ "services" "minidlna" "settings" "media_dir" ])
(lib.mkRenamedOptionModule [ "services" "minidlna" "friendlyName"] [ "services" "minidlna" "settings" "friendly_name" ])
(lib.mkRenamedOptionModule [ "services" "minidlna" "announceInterval"] [ "services" "minidlna" "settings" "notify_interval" ])
];

###### implementation
config = lib.mkIf cfg.enable {
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.settings.port ];
networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [ 1900 ];

users.groups.minidlna.gid = config.ids.gids.minidlna;
users.users.minidlna = {
description = "MiniDLNA daemon user";
group = "minidlna";
uid = config.ids.uids.minidlna;
};

users.groups.minidlna.gid = config.ids.gids.minidlna;

systemd.services.minidlna = {
description = "MiniDLNA Server";
wantedBy = [ "multi-user.target" ];
Expand All @@ -145,7 +110,7 @@ in
CacheDirectory = "minidlna";
RuntimeDirectory = "minidlna";
PIDFile = "/run/minidlna/pid";
ExecStart = "${pkgs.minidlna}/sbin/minidlnad -S -P /run/minidlna/pid -f ${settingsFile}";
ExecStart = "${pkgs.minidlna}/sbin/minidlnad -S -P /run/minidlna/pid -f ${cfgfile}";
};
};
};
Expand Down

0 comments on commit ce70753

Please sign in to comment.