fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7name = "mpd" ; 8 9cfg = config . services . mpd ; 10 11mpdConf = pkgs . writeText "mpd.conf" '' 12music_directory " ${ cfg . musicDirectory } " 13playlist_directory " ${ cfg . playlistDirectory } " 14${ lib . optionalString ( cfg . dbFile != null ) '' 15db_file " ${ cfg . dbFile } " 16'' } 17state_file " ${ cfg . dataDir } /state" 18sticker_file " ${ cfg . dataDir } /sticker.sql" 19 20${ optionalString ( cfg . network . listenAddress != "any" ) 21''bind_to_address " ${ cfg . network . listenAddress } "'' } 22${ optionalString ( cfg . network . port != 6600 ) 23''port " ${ toString cfg . network . port } "'' } 24 25${ cfg . extraConfig } 26'' ; 27 28in { 29 30###### interface 31 32options = { 33 34services . mpd = { 35 36enable = mkOption { 37type = types . bool ; 38default = false ; 39description = '' 40Whether to enable MPD, the music player daemon. 41'' ; 42}; 43 44package = mkOption { 45type = types . package ; 46default = pkgs . mpd ; 47defaultText = "pkgs.mpd" ; 48description = '' 49The MPD package to run. 50'' ; 51}; 52 53musicDirectory = mkOption { 54type = with types ; either path str ; 55defaultText = literalExpression '' 56''${home.homeDirectory}/music if state version < 22.11 57''${xdg.userDirs.music} if xdg.userDirs.enable == true 58undefined otherwise 59'' ; 60apply = toString ; # Prevent copies to Nix store. 61description = '' 62The directory where mpd reads music from. 63 64If [](#opt-xdg.userDirs.enable) is 65`true` then the defined XDG music directory is used. 66Otherwise, you must explicitly specify a value. 67'' ; 68}; 69 70playlistDirectory = mkOption { 71type = types . path ; 72default = " ${ cfg . dataDir } /playlists" ; 73defaultText = "\${dataDir}/playlists" ; 74apply = toString ; # Prevent copies to Nix store. 75description = '' 76The directory where mpd stores playlists. 77'' ; 78}; 79 80extraConfig = mkOption { 81type = types . lines ; 82default = "" ; 83description = '' 84Extra directives added to to the end of MPD's configuration 85file, {file}`mpd.conf`. Basic configuration 86like file location and uid/gid is added automatically to the 87beginning of the file. For available options see 88{manpage}`mpd.conf(5)`. 89'' ; 90}; 91 92extraArgs = mkOption { 93type = types . listOf types . str ; 94default = [ ]; 95example = [ "--verbose" ]; 96description = '' 97Extra command-line arguments to pass to MPD. 98'' ; 99}; 100 101dataDir = mkOption { 102type = types . path ; 103default = " ${ config . xdg . dataHome } / ${ name } " ; 104defaultText = "$XDG_DATA_HOME/mpd" ; 105apply = toString ; # Prevent copies to Nix store. 106description = '' 107The directory where MPD stores its state, tag cache, 108playlists etc. 109'' ; 110}; 111 112network = { 113startWhenNeeded = mkOption { 114type = types . bool ; 115default = false ; 116description = '' 117Enable systemd socket activation. 118'' ; 119}; 120 121listenAddress = mkOption { 122type = types . str ; 123default = "127.0.0.1" ; 124example = "any" ; 125description = '' 126The address for the daemon to listen on. 127Use `any` to listen on all addresses. 128'' ; 129}; 130 131port = mkOption { 132type = types . port ; 133default = 6600 ; 134description = '' 135The TCP port on which the the daemon will listen. 136'' ; 137}; 138 139}; 140 141dbFile = mkOption { 142type = types . nullOr types . str ; 143default = " ${ cfg . dataDir } /tag_cache" ; 144defaultText = "\${dataDir}/tag_cache" ; 145description = '' 146The path to MPD's database. If set to 147`null` the parameter is omitted from the 148configuration. 149'' ; 150}; 151}; 152 153}; 154 155###### implementation 156 157config = mkIf cfg . enable { 158assertions = [ 159( lib . hm . assertions . assertPlatform "services.mpd" pkgs lib . platforms . linux ) 160]; 161 162services . mpd = mkMerge [ 163( mkIf ( versionAtLeast config . home . stateVersion "22.11" 164&& config . xdg . userDirs . enable ) { 165musicDirectory = mkOptionDefault config . xdg . userDirs . music ; 166}) 167 168( mkIf ( versionOlder config . home . stateVersion "22.11" ) { 169musicDirectory = mkOptionDefault " ${ config . home . homeDirectory } /music" ; 170}) 171]; 172 173systemd . user . services . mpd = { 174Unit = { 175After = [ "network.target" "sound.target" ]; 176Description = "Music Player Daemon" ; 177}; 178 179Install = mkIf ( ! cfg . network . startWhenNeeded ) { 180WantedBy = [ "default.target" ]; 181}; 182 183Service = { 184Environment = "PATH= ${ config . home . profileDirectory } /bin" ; 185ExecStart = " ${ cfg . package } /bin/mpd --no-daemon ${ mpdConf } ${ 186escapeShellArgs cfg . extraArgs 187} " ; 188Type = "notify" ; 189ExecStartPre = '' 190${ pkgs . bash } /bin/bash -c " ${ pkgs . coreutils } /bin/mkdir -p ' ${ cfg . dataDir } ' ' ${ cfg . playlistDirectory } '"'' ; 191}; 192}; 193 194systemd . user . sockets . mpd = mkIf cfg . network . startWhenNeeded { 195Socket = { 196ListenStream = let 197listen = if cfg . network . listenAddress == "any" then 198toString cfg . network . port 199else 200" ${ cfg . network . listenAddress } : ${ toString cfg . network . port } " ; 201in [ listen "%t/mpd/socket" ]; 202 203Backlog = 5 ; 204KeepAlive = true ; 205}; 206 207Install = { WantedBy = [ "sockets.target" ]; }; 208}; 209 210home . packages = [ cfg . package ]; 211}; 212 213}