fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
3.0 KiB110 linesraw
1{ config, lib, pkgs, ... }:
2let
3  cfg = config.services.mpd-mpris;
4
5  ignoreIfLocalMpd = value: if cfg.mpd.useLocal then null else value;
6
7  renderArg = name: value:
8    if lib.isBool value && value then
9      "-${name}"
10    else if lib.isInt value then
11      "-${name} ${toString value}"
12    else if lib.isString value then
13      "-${name} ${lib.escapeShellArg value}"
14    else
15      "";
16
17  concatArgs = strings:
18    lib.concatStringsSep " " (lib.filter (s: s != "") strings);
19
20  renderArgs = args: concatArgs (lib.mapAttrsToList renderArg args);
21
22  renderCmd = pkg: args: "${pkg}/bin/mpd-mpris ${renderArgs args}";
23in {
24  meta.maintainers = [ lib.hm.maintainers.olmokramer ];
25
26  options.services.mpd-mpris = {
27    enable = lib.mkEnableOption
28      "mpd-mpris: An implementation of the MPRIS protocol for MPD";
29
30    package = lib.mkPackageOption pkgs "mpd-mpris" { };
31
32    mpd = {
33      useLocal = lib.mkOption {
34        type = lib.types.bool;
35        default = config.services.mpd.enable;
36        defaultText = lib.literalExpression "config.services.mpd.enable";
37        description = ''
38          Whether to configure for the local MPD daemon. If
39          `true` the `network`,
40          `host`, and `port`
41          settings are ignored.
42        '';
43      };
44
45      network = lib.mkOption {
46        type = with lib.types; nullOr str;
47        default = null;
48        description = ''
49          The network used to dial to the MPD server. Check
50          <https://golang.org/pkg/net/#Dial>
51          for available values (most common are "tcp" and "unix")
52        '';
53      };
54
55      host = lib.mkOption {
56        type = with lib.types; nullOr str;
57        default = null;
58        example = "192.168.1.1";
59        description = "The address where MPD is listening for connections.";
60      };
61
62      port = lib.mkOption {
63        type = with lib.types; nullOr port;
64        default = null;
65        description = ''
66          The port number where MPD is listening for connections.
67        '';
68      };
69
70      password = lib.mkOption {
71        type = with lib.types; nullOr str;
72        default = null;
73        description = ''
74          The password to connect to MPD.
75        '';
76      };
77    };
78  };
79
80  config = lib.mkIf cfg.enable {
81    assertions = [
82      (lib.hm.assertions.assertPlatform "services.mpd-mpris" pkgs
83        lib.platforms.linux)
84    ];
85
86    systemd.user.services.mpd-mpris = {
87      Install = { WantedBy = [ "default.target" ]; };
88
89      Unit = {
90        Description =
91          "mpd-mpris: An implementation of the MPRIS protocol for MPD";
92        After = [ "mpd.service" ];
93        Requires = lib.mkIf cfg.mpd.useLocal [ "mpd.service" ];
94      };
95
96      Service = {
97        Type = "simple";
98        Restart = "on-failure";
99        RestartSec = "5s";
100        ExecStart = renderCmd cfg.package {
101          no-instance = true;
102          network = ignoreIfLocalMpd cfg.mpd.network;
103          host = ignoreIfLocalMpd cfg.mpd.host;
104          port = ignoreIfLocalMpd cfg.mpd.port;
105          pwd = cfg.mpd.password;
106        };
107      };
108    };
109  };
110}