summary refs log tree commit diff
path: root/nixos/modules/services/networking/minidlna.nix
blob: 73fcb1eeea8e4a936fcc0aa5bc7547f56ef898ec (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
# Module for MiniDLNA, a simple DLNA server.

{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.services.minidlna;

  port = 8200;

in

{

  ###### interface

  options = {

    services.minidlna.enable = mkOption {
      type = 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.
        '';
    };

    services.minidlna.mediaDirs = mkOption {
      type = types.listOf types.string;
      default = [];
      example = [ "/data/media" "V,/home/alice/video" ];
      description =
        ''
          Directories to be scanned for media files.  The prefixes
          <literal>A,</literal>, <literal>V,</literal> and
          <literal>P,</literal> restrict a directory to audio, video
          or image files.  The directories must be accessible to the
          <literal>minidlna</literal> user account.
        '';
    };

    services.minidlna.config = mkOption {
      type = types.lines;
      description = "The contents of MiniDLNA's configuration file.";
    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    services.minidlna.config =
      ''
        port=${toString port}
        friendly_name=NixOS Media Server
        db_dir=/var/cache/minidlna
        log_dir=/var/log/minidlna
        inotify=yes
        ${concatMapStrings (dir: ''
          media_dir=${dir}
        '') cfg.mediaDirs}
      '';

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

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

    systemd.services.minidlna =
      { description = "MiniDLNA Server";

        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];

        preStart =
          ''
            mkdir -p /var/cache/minidlna /var/log/minidlna /run/minidlna
            chown minidlna /var/cache/minidlna /var/log/minidlna /run/minidlna
          '';

        # FIXME: log through the journal rather than
        # /var/log/minidlna.  The -d flag does that, but also raises
        # the log level to debug...
        serviceConfig =
          { User = "minidlna";
            Group = "nogroup";
            PermissionsStartOnly = true;
            Type = "forking";
            PIDFile = "/run/minidlna/pid";
            ExecStart =
              "@${pkgs.minidlna}/sbin/minidlna minidlna -P /run/minidlna/pid" +
              " -f ${pkgs.writeText "minidlna.conf" cfg.config}";
          };
      };

  };

}