From 0a30138c694ab3b048ac300794c2eb599dc40266 Mon Sep 17 00:00:00 2001 From: novenary Date: Tue, 2 Jul 2024 15:04:26 +0300 Subject: [PATCH] mpd: specify dependency of service on socket This allows `systemctl --user restart mpd.socket` to work properly. --- modules/services/mpd.nix | 15 +++++++--- tests/modules/services/mpd/default.nix | 1 + .../services/mpd/start-when-needed.nix | 29 +++++++++++++++++++ .../services/mpd/start-when-needed.service | 12 ++++++++ .../services/mpd/start-when-needed.socket | 8 +++++ 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/modules/services/mpd/start-when-needed.nix create mode 100644 tests/modules/services/mpd/start-when-needed.service create mode 100644 tests/modules/services/mpd/start-when-needed.socket diff --git a/modules/services/mpd.nix b/modules/services/mpd.nix index a0011b021..3d8d03be7 100644 --- a/modules/services/mpd.nix +++ b/modules/services/mpd.nix @@ -171,10 +171,17 @@ in { ]; systemd.user.services.mpd = { - Unit = { - After = [ "network.target" "sound.target" ]; - Description = "Music Player Daemon"; - }; + Unit = mkMerge [ + { + Description = "Music Player Daemon"; + After = [ "network.target" "sound.target" ]; + } + + (mkIf cfg.network.startWhenNeeded { + Requires = [ "mpd.socket" ]; + After = [ "mpd.socket" ]; + }) + ]; Install = mkIf (!cfg.network.startWhenNeeded) { WantedBy = [ "default.target" ]; diff --git a/tests/modules/services/mpd/default.nix b/tests/modules/services/mpd/default.nix index d2fd2a292..f7919b227 100644 --- a/tests/modules/services/mpd/default.nix +++ b/tests/modules/services/mpd/default.nix @@ -1,5 +1,6 @@ { mpd-basic-configuration = ./basic-configuration.nix; mpd-before-state-version-22_11 = ./before-state-version-22_11.nix; + mpd-start-when-needed = ./start-when-needed.nix; mpd-xdg-music-dir = ./xdg-music-dir.nix; } diff --git a/tests/modules/services/mpd/start-when-needed.nix b/tests/modules/services/mpd/start-when-needed.nix new file mode 100644 index 000000000..31017c9e3 --- /dev/null +++ b/tests/modules/services/mpd/start-when-needed.nix @@ -0,0 +1,29 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + services.mpd = { + enable = true; + musicDirectory = "/my/music/dir"; + extraArgs = [ "--verbose" ]; + network.startWhenNeeded = true; + }; + + home.stateVersion = "22.11"; + + test.stubs.mpd = { }; + + nmt.script = '' + serviceFile=$(normalizeStorePaths home-files/.config/systemd/user/mpd.service) + assertFileContent "$serviceFile" ${./start-when-needed.service} + + socketFile=home-files/.config/systemd/user/mpd.socket + assertFileContent "$socketFile" ${./start-when-needed.socket} + + confFile=$(grep -o \ + '/nix/store/.*-mpd.conf' \ + $TESTED/home-files/.config/systemd/user/mpd.service) + assertFileContent "$confFile" ${./basic-configuration.conf} + ''; +} diff --git a/tests/modules/services/mpd/start-when-needed.service b/tests/modules/services/mpd/start-when-needed.service new file mode 100644 index 000000000..87eb4b79a --- /dev/null +++ b/tests/modules/services/mpd/start-when-needed.service @@ -0,0 +1,12 @@ +[Service] +Environment=PATH=/home/hm-user/.nix-profile/bin +ExecStart=@mpd@/bin/mpd --no-daemon /nix/store/00000000000000000000000000000000-mpd.conf '--verbose' +ExecStartPre=/nix/store/00000000000000000000000000000000-bash/bin/bash -c "/nix/store/00000000000000000000000000000000-coreutils/bin/mkdir -p '/home/hm-user/.local/share/mpd' '/home/hm-user/.local/share/mpd/playlists'" +Type=notify + +[Unit] +After=network.target +After=sound.target +After=mpd.socket +Description=Music Player Daemon +Requires=mpd.socket diff --git a/tests/modules/services/mpd/start-when-needed.socket b/tests/modules/services/mpd/start-when-needed.socket new file mode 100644 index 000000000..b5687b117 --- /dev/null +++ b/tests/modules/services/mpd/start-when-needed.socket @@ -0,0 +1,8 @@ +[Install] +WantedBy=sockets.target + +[Socket] +Backlog=5 +KeepAlive=true +ListenStream=127.0.0.1:6600 +ListenStream=%t/mpd/socket