aboutsummaryrefslogtreecommitdiff
path: root/modules/services/mpdris2.nix
blob: cb8cefba6bd77b00acda9f8c444caec8054c9fe2 (plain)
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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.mpdris2;

  toIni = generators.toINI {
    mkKeyValue = key: value:
      let
        value' = if isBool value then
          (if value then "True" else "False")
        else
          toString value;
      in "${key} = ${value'}";
  };

  mpdris2Conf = {
    Connection = {
      host = cfg.mpd.host;
      port = cfg.mpd.port;
      music_dir = cfg.mpd.musicDirectory;
    };

    Bling = {
      notify = cfg.notifications;
      mmkeys = cfg.multimediaKeys;
    };
  };

in {
  meta.maintainers = [ maintainers.pjones ];

  options.services.mpdris2 = {
    enable = mkEnableOption "mpDris2 the MPD to MPRIS2 bridge";
    notifications = mkEnableOption "song change notifications";
    multimediaKeys = mkEnableOption "multimedia key support";

    package = mkOption {
      type = types.package;
      default = pkgs.mpdris2;
      defaultText = literalExample "pkgs.mpdris2";
      description = "The mpDris2 package to use.";
    };

    mpd = {
      host = mkOption {
        type = types.str;
        default = config.services.mpd.network.listenAddress;
        defaultText = "config.services.mpd.network.listenAddress";
        example = "192.168.1.1";
        description = "The address where MPD is listening for connections.";
      };

      port = mkOption {
        type = types.port;
        default = config.services.mpd.network.port;
        defaultText = "config.services.mpd.network.port";
        description = ''
          The port number where MPD is listening for connections.
        '';
      };

      musicDirectory = mkOption {
        type = types.nullOr types.path;
        default = config.services.mpd.musicDirectory;
        defaultText = "config.services.mpd.musicDirectory";
        description = ''
          If set, mpDris2 will use this directory to access music artwork.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [{
      assertion = config.services.mpd.enable;
      message = "The mpdris2 module requires 'services.mpd.enable = true'.";
    }];

    xdg.configFile."mpDris2/mpDris2.conf".text = toIni mpdris2Conf;

    systemd.user.services.mpdris2 = {
      Install = { WantedBy = [ "default.target" ]; };

      Unit = {
        Description = "MPRIS 2 support for MPD";
        After = [ "mpd.service" ];
      };

      Service = {
        Type = "simple";
        Restart = "on-failure";
        RestartSec = "5s";
        ExecStart = "${cfg.package}/bin/mpDris2";
        BusName = "org.mpris.MediaPlayer2.mpd";
      };
    };
  };
}