fork/home-manager

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

Robert HelgessonMerge PR #421575790441

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