1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

mpd: refactor implementation (#6537)

Remove with lib, reorganize, cleanup conditionals, hide options that are
linux only from darwin.
This commit is contained in:
Austin Horstman 2025-02-27 11:56:56 -05:00 committed by GitHub
parent 11e6d20803
commit cf3bf4f1b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 73 additions and 92 deletions

View file

@ -1,41 +1,11 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
inherit (lib) mkIf mkOption types;
name = "mpd";
cfg = config.services.mpd; cfg = config.services.mpd;
mpdConf = pkgs.writeText "mpd.conf" ''
music_directory "${cfg.musicDirectory}"
playlist_directory "${cfg.playlistDirectory}"
${lib.optionalString (cfg.dbFile != null) ''
db_file "${cfg.dbFile}"
''}
${lib.optionalString (pkgs.stdenv.hostPlatform.isDarwin) ''
log_file "${config.home.homeDirectory}/Library/Logs/mpd/log.txt"
''}
state_file "${cfg.dataDir}/state"
sticker_file "${cfg.dataDir}/sticker.sql"
${optionalString (cfg.network.listenAddress != "any")
''bind_to_address "${cfg.network.listenAddress}"''}
${optionalString (cfg.network.port != 6600)
''port "${toString cfg.network.port}"''}
${cfg.extraConfig}
'';
in { in {
###### interface
options = { options = {
services.mpd = { services.mpd = {
enable = mkOption { enable = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
@ -55,7 +25,7 @@ in {
musicDirectory = mkOption { musicDirectory = mkOption {
type = with types; either path str; type = with types; either path str;
defaultText = literalExpression '' defaultText = lib.literalExpression ''
''${home.homeDirectory}/music if state version < 22.11 ''${home.homeDirectory}/music if state version < 22.11
''${xdg.userDirs.music} if xdg.userDirs.enable == true ''${xdg.userDirs.music} if xdg.userDirs.enable == true
undefined otherwise undefined otherwise
@ -103,7 +73,7 @@ in {
dataDir = mkOption { dataDir = mkOption {
type = types.path; type = types.path;
default = "${config.xdg.dataHome}/${name}"; default = "${config.xdg.dataHome}/mpd";
defaultText = "$XDG_DATA_HOME/mpd"; defaultText = "$XDG_DATA_HOME/mpd";
apply = toString; # Prevent copies to Nix store. apply = toString; # Prevent copies to Nix store.
description = '' description = ''
@ -116,6 +86,8 @@ in {
startWhenNeeded = mkOption { startWhenNeeded = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
visible = pkgs.stdenv.hostPlatform.isLinux;
readOnly = pkgs.stdenv.hostPlatform.isDarwin;
description = '' description = ''
Enable systemd socket activation. This is only supported on Linux. Enable systemd socket activation. This is only supported on Linux.
''; '';
@ -152,67 +124,88 @@ in {
''; '';
}; };
}; };
}; };
###### implementation config = let
mpdConf = pkgs.writeText "mpd.conf" (''
music_directory "${cfg.musicDirectory}"
playlist_directory "${cfg.playlistDirectory}"
'' + lib.optionalString (cfg.dbFile != null) ''
db_file "${cfg.dbFile}"
'' + lib.optionalString (pkgs.stdenv.hostPlatform.isDarwin) ''
log_file "${config.home.homeDirectory}/Library/Logs/mpd/log.txt"
'' + ''
state_file "${cfg.dataDir}/state"
sticker_file "${cfg.dataDir}/sticker.sql"
config = mkIf cfg.enable { '' + lib.optionalString (cfg.network.listenAddress != "any") ''
services.mpd = mkMerge [ bind_to_address "${cfg.network.listenAddress}"
(mkIf (versionAtLeast config.home.stateVersion "22.11" '' + lib.optionalString (cfg.network.port != 6600) ''
port "${toString cfg.network.port}"
'' + lib.optionalString (cfg.extraConfig != "") ''
${cfg.extraConfig}
'');
in mkIf cfg.enable {
home.packages = [ cfg.package ];
services.mpd = lib.mkMerge [
(mkIf (lib.versionAtLeast config.home.stateVersion "22.11"
&& config.xdg.userDirs.enable) { && config.xdg.userDirs.enable) {
musicDirectory = mkOptionDefault config.xdg.userDirs.music; musicDirectory = lib.mkOptionDefault config.xdg.userDirs.music;
}) })
(mkIf (versionOlder config.home.stateVersion "22.11") { (mkIf (lib.versionOlder config.home.stateVersion "22.11") {
musicDirectory = mkOptionDefault "${config.home.homeDirectory}/music"; musicDirectory =
lib.mkOptionDefault "${config.home.homeDirectory}/music";
}) })
]; ];
systemd.user.services.mpd = lib.mkIf pkgs.stdenv.hostPlatform.isLinux { systemd.user = lib.mkIf pkgs.stdenv.hostPlatform.isLinux {
Unit = mkMerge [ services.mpd = {
{ Unit = lib.mkMerge [
Description = "Music Player Daemon"; {
After = [ "network.target" "sound.target" ]; Description = "Music Player Daemon";
} After = [ "network.target" "sound.target" ];
}
(mkIf cfg.network.startWhenNeeded { (mkIf cfg.network.startWhenNeeded {
Requires = [ "mpd.socket" ]; Requires = [ "mpd.socket" ];
After = [ "mpd.socket" ]; After = [ "mpd.socket" ];
}) })
]; ];
Install = mkIf (!cfg.network.startWhenNeeded) { Install = mkIf (!cfg.network.startWhenNeeded) {
WantedBy = [ "default.target" ]; WantedBy = [ "default.target" ];
};
Service = {
Environment = [ "PATH=${config.home.profileDirectory}/bin" ];
ExecStart = "${cfg.package}/bin/mpd --no-daemon ${mpdConf} ${
lib.escapeShellArgs cfg.extraArgs
}";
Type = "notify";
ExecStartPre = ''
${pkgs.bash}/bin/bash -c "${pkgs.coreutils}/bin/mkdir -p '${cfg.dataDir}' '${cfg.playlistDirectory}'"'';
};
}; };
Service = { sockets.mpd = mkIf cfg.network.startWhenNeeded {
Environment = [ "PATH=${config.home.profileDirectory}/bin" ]; Socket = {
ExecStart = "${cfg.package}/bin/mpd --no-daemon ${mpdConf} ${ ListenStream = let
escapeShellArgs cfg.extraArgs listen = if cfg.network.listenAddress == "any" then
}"; toString cfg.network.port
Type = "notify"; else
ExecStartPre = '' "${cfg.network.listenAddress}:${toString cfg.network.port}";
${pkgs.bash}/bin/bash -c "${pkgs.coreutils}/bin/mkdir -p '${cfg.dataDir}' '${cfg.playlistDirectory}'"''; in [ listen "%t/mpd/socket" ];
Backlog = 5;
KeepAlive = true;
};
Install = { WantedBy = [ "sockets.target" ]; };
}; };
}; };
systemd.user.sockets.mpd = mkIf cfg.network.startWhenNeeded {
Socket = {
ListenStream = let
listen = if cfg.network.listenAddress == "any" then
toString cfg.network.port
else
"${cfg.network.listenAddress}:${toString cfg.network.port}";
in [ listen "%t/mpd/socket" ];
Backlog = 5;
KeepAlive = true;
};
Install = { WantedBy = [ "sockets.target" ]; };
};
launchd.agents.mpd = lib.mkIf pkgs.stdenv.hostPlatform.isDarwin { launchd.agents.mpd = lib.mkIf pkgs.stdenv.hostPlatform.isDarwin {
enable = true; enable = true;
config = { config = {
@ -223,7 +216,5 @@ in {
ProcessType = "Interactive"; ProcessType = "Interactive";
}; };
}; };
home.packages = [ cfg.package ];
}; };
} }

View file

@ -1,12 +1,7 @@
music_directory "/my/music/dir" music_directory "/my/music/dir"
playlist_directory "/home/hm-user/.local/share/mpd/playlists" playlist_directory "/home/hm-user/.local/share/mpd/playlists"
db_file "/home/hm-user/.local/share/mpd/tag_cache" db_file "/home/hm-user/.local/share/mpd/tag_cache"
state_file "/home/hm-user/.local/share/mpd/state" state_file "/home/hm-user/.local/share/mpd/state"
sticker_file "/home/hm-user/.local/share/mpd/sticker.sql" sticker_file "/home/hm-user/.local/share/mpd/sticker.sql"
bind_to_address "127.0.0.1" bind_to_address "127.0.0.1"

View file

@ -1,12 +1,7 @@
music_directory "/home/hm-user/Music" music_directory "/home/hm-user/Music"
playlist_directory "/home/hm-user/.local/share/mpd/playlists" playlist_directory "/home/hm-user/.local/share/mpd/playlists"
db_file "/home/hm-user/.local/share/mpd/tag_cache" db_file "/home/hm-user/.local/share/mpd/tag_cache"
state_file "/home/hm-user/.local/share/mpd/state" state_file "/home/hm-user/.local/share/mpd/state"
sticker_file "/home/hm-user/.local/share/mpd/sticker.sql" sticker_file "/home/hm-user/.local/share/mpd/sticker.sql"
bind_to_address "127.0.0.1" bind_to_address "127.0.0.1"