From 5e889b385c43a8a72ada5ebc4888bbebb129b438 Mon Sep 17 00:00:00 2001 From: Olmo Kramer Date: Tue, 27 Dec 2022 18:15:51 +0100 Subject: [PATCH] mpd-mpris: add module --- .github/CODEOWNERS | 3 + modules/misc/news.nix | 8 ++ modules/modules.nix | 1 + modules/services/mpd-mpris.nix | 110 ++++++++++++++++++ tests/default.nix | 1 + .../mpd-mpris/configuration-basic.nix | 12 ++ .../mpd-mpris/configuration-basic.service | 12 ++ .../configuration-with-local-mpd.nix | 15 +++ .../configuration-with-local-mpd.service | 13 +++ .../mpd-mpris/configuration-with-password.nix | 20 ++++ .../configuration-with-password.service | 12 ++ tests/modules/services/mpd-mpris/default.nix | 5 + 12 files changed, 212 insertions(+) create mode 100644 modules/services/mpd-mpris.nix create mode 100644 tests/modules/services/mpd-mpris/configuration-basic.nix create mode 100644 tests/modules/services/mpd-mpris/configuration-basic.service create mode 100644 tests/modules/services/mpd-mpris/configuration-with-local-mpd.nix create mode 100644 tests/modules/services/mpd-mpris/configuration-with-local-mpd.service create mode 100644 tests/modules/services/mpd-mpris/configuration-with-password.nix create mode 100644 tests/modules/services/mpd-mpris/configuration-with-password.service create mode 100644 tests/modules/services/mpd-mpris/default.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f5d434834..7b409c4fd 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -447,6 +447,9 @@ Makefile @thiagokokada /modules/services/mpdris2.nix @pjones +/modules/services/mpd-mpris.nix @olmokramer +/tests/modules/services/mpd-mpris @olmokramer + /modules/services/mpd-discord-rpc.nix @Kranzes /modules/services/mpris-proxy.nix @ThibautMarty diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 57c5574fd..4eecb2817 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -915,6 +915,14 @@ in A new module is available: 'services.autorandr'. ''; } + + { + time = "2023-02-20T22:31:23+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'services.mpd-mpris'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 391ad5bd7..2a25b8a0b 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -250,6 +250,7 @@ let ./services/mpd.nix ./services/mpdris2.nix ./services/mpd-discord-rpc.nix + ./services/mpd-mpris.nix ./services/mpris-proxy.nix ./services/muchsync.nix ./services/network-manager-applet.nix diff --git a/modules/services/mpd-mpris.nix b/modules/services/mpd-mpris.nix new file mode 100644 index 000000000..681137f74 --- /dev/null +++ b/modules/services/mpd-mpris.nix @@ -0,0 +1,110 @@ +{ config, lib, pkgs, ... }: +let + cfg = config.services.mpd-mpris; + + ignoreIfLocalMpd = value: if cfg.mpd.useLocal then null else value; + + renderArg = name: value: + if lib.isBool value && value then + "-${name}" + else if lib.isInt value then + "-${name} ${toString value}" + else if lib.isString value then + "-${name} ${lib.escapeShellArg value}" + else + ""; + + concatArgs = strings: + lib.concatStringsSep " " (lib.filter (s: s != "") strings); + + renderArgs = args: concatArgs (lib.mapAttrsToList renderArg args); + + renderCmd = pkg: args: "${pkg}/bin/mpd-mpris ${renderArgs args}"; +in { + meta.maintainers = [ lib.hm.maintainers.olmokramer ]; + + options.services.mpd-mpris = { + enable = lib.mkEnableOption + "mpd-mpris: An implementation of the MPRIS protocol for MPD"; + + package = lib.mkPackageOption pkgs "mpd-mpris" { }; + + mpd = { + useLocal = lib.mkOption { + type = lib.types.bool; + default = config.services.mpd.enable; + defaultText = lib.literalExpression "config.services.mpd.enable"; + description = '' + Whether to configure for the local MPD daemon. If + true the network, + host, and port + settings are ignored. + ''; + }; + + network = lib.mkOption { + type = with lib.types; nullOr str; + default = null; + description = '' + The network used to dial to the MPD server. Check + + for available values (most common are "tcp" and "unix") + ''; + }; + + host = lib.mkOption { + type = with lib.types; nullOr str; + default = null; + example = "192.168.1.1"; + description = "The address where MPD is listening for connections."; + }; + + port = lib.mkOption { + type = with lib.types; nullOr port; + default = null; + description = '' + The port number where MPD is listening for connections. + ''; + }; + + password = lib.mkOption { + type = with lib.types; nullOr str; + default = null; + description = '' + The password to connect to MPD. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + (lib.hm.assertions.assertPlatform "services.mpd-mpris" pkgs + lib.platforms.linux) + ]; + + systemd.user.services.mpd-mpris = { + Install = { WantedBy = [ "default.target" ]; }; + + Unit = { + Description = + "mpd-mpris: An implementation of the MPRIS protocol for MPD"; + After = [ "mpd.service" ]; + Requires = lib.mkIf cfg.mpd.useLocal [ "mpd.service" ]; + }; + + Service = { + Type = "simple"; + Restart = "on-failure"; + RestartSec = "5s"; + ExecStart = renderCmd cfg.package { + no-instance = true; + network = ignoreIfLocalMpd cfg.mpd.network; + host = ignoreIfLocalMpd cfg.mpd.host; + port = ignoreIfLocalMpd cfg.mpd.port; + pwd = cfg.mpd.password; + }; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 98697c675..495fd5ae5 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -188,6 +188,7 @@ import nmt { ./modules/services/mopidy ./modules/services/mpd ./modules/services/mpdris2 + ./modules/services/mpd-mpris ./modules/services/pantalaimon ./modules/services/parcellite ./modules/services/pass-secret-service diff --git a/tests/modules/services/mpd-mpris/configuration-basic.nix b/tests/modules/services/mpd-mpris/configuration-basic.nix new file mode 100644 index 000000000..de7c594f3 --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-basic.nix @@ -0,0 +1,12 @@ +{ ... }: + +{ + services.mpd-mpris = { enable = true; }; + + test.stubs.mpd-mpris = { }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/mpd-mpris.service + assertFileContent "$serviceFile" ${./configuration-basic.service} + ''; +} diff --git a/tests/modules/services/mpd-mpris/configuration-basic.service b/tests/modules/services/mpd-mpris/configuration-basic.service new file mode 100644 index 000000000..32584eb3f --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-basic.service @@ -0,0 +1,12 @@ +[Install] +WantedBy=default.target + +[Service] +ExecStart=@mpd-mpris@/bin/mpd-mpris -no-instance +Restart=on-failure +RestartSec=5s +Type=simple + +[Unit] +After=mpd.service +Description=mpd-mpris: An implementation of the MPRIS protocol for MPD diff --git a/tests/modules/services/mpd-mpris/configuration-with-local-mpd.nix b/tests/modules/services/mpd-mpris/configuration-with-local-mpd.nix new file mode 100644 index 000000000..a3a6b38d9 --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-with-local-mpd.nix @@ -0,0 +1,15 @@ +{ ... }: + +{ + services.mpd-mpris = { + enable = true; + mpd.useLocal = true; + }; + + test.stubs.mpd-mpris = { }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/mpd-mpris.service + assertFileContent "$serviceFile" ${./configuration-with-local-mpd.service} + ''; +} diff --git a/tests/modules/services/mpd-mpris/configuration-with-local-mpd.service b/tests/modules/services/mpd-mpris/configuration-with-local-mpd.service new file mode 100644 index 000000000..a4a01fce1 --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-with-local-mpd.service @@ -0,0 +1,13 @@ +[Install] +WantedBy=default.target + +[Service] +ExecStart=@mpd-mpris@/bin/mpd-mpris -no-instance +Restart=on-failure +RestartSec=5s +Type=simple + +[Unit] +After=mpd.service +Description=mpd-mpris: An implementation of the MPRIS protocol for MPD +Requires=mpd.service diff --git a/tests/modules/services/mpd-mpris/configuration-with-password.nix b/tests/modules/services/mpd-mpris/configuration-with-password.nix new file mode 100644 index 000000000..b10958253 --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-with-password.nix @@ -0,0 +1,20 @@ +{ ... }: + +{ + services.mpd-mpris = { + enable = true; + mpd = { + network = "tcp"; + host = "example.com"; + port = 1234; + password = "my_password"; + }; + }; + + test.stubs.mpd-mpris = { }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/mpd-mpris.service + assertFileContent "$serviceFile" ${./configuration-with-password.service} + ''; +} diff --git a/tests/modules/services/mpd-mpris/configuration-with-password.service b/tests/modules/services/mpd-mpris/configuration-with-password.service new file mode 100644 index 000000000..868d55539 --- /dev/null +++ b/tests/modules/services/mpd-mpris/configuration-with-password.service @@ -0,0 +1,12 @@ +[Install] +WantedBy=default.target + +[Service] +ExecStart=@mpd-mpris@/bin/mpd-mpris -host 'example.com' -network 'tcp' -no-instance -port 1234 -pwd 'my_password' +Restart=on-failure +RestartSec=5s +Type=simple + +[Unit] +After=mpd.service +Description=mpd-mpris: An implementation of the MPRIS protocol for MPD diff --git a/tests/modules/services/mpd-mpris/default.nix b/tests/modules/services/mpd-mpris/default.nix new file mode 100644 index 000000000..ffcd04b17 --- /dev/null +++ b/tests/modules/services/mpd-mpris/default.nix @@ -0,0 +1,5 @@ +{ + mpd-mpris-configuration-basic = ./configuration-basic.nix; + mpd-mpris-configuration-with-local-mpd = ./configuration-with-local-mpd.nix; + mpd-mpris-configuration-with-password = ./configuration-with-password.nix; +}