home-manager/services/mopidy: Simplify the service

Now, the configuration is made into a proper Nix configuration with the
output being converted to INI format.

For mapping the types, look for `mopidy/config/types.py`. The only
quirky mapping so far is the list type.
This commit is contained in:
Gabriel Arazas 2022-01-20 16:12:48 +08:00
parent b750b29031
commit 342a077f04

View File

@ -4,7 +4,14 @@
let let
cfg = config.services.mopidy; cfg = config.services.mopidy;
mopidyConf = pkgs.writeText "mopidy.conf" cfg.configuration; customToINI = with lib; generators.toINI {
mkKeyValue = generators.mkKeyValueDefault {
mkValueString = v:
if isList v then "\n " + concatStringsSep "\n " v
else generators.mkValueStringDefault {} v;
} " = ";
};
mopidyEnv = pkgs.buildEnv { mopidyEnv = pkgs.buildEnv {
name = "mopidy-with-extensions-${pkgs.mopidy.version}"; name = "mopidy-with-extensions-${pkgs.mopidy.version}";
# TODO: Improve this that doesn't use `lib.misc`. # TODO: Improve this that doesn't use `lib.misc`.
@ -19,14 +26,9 @@ in {
options.services.mopidy = { options.services.mopidy = {
enable = lib.mkEnableOption "Mopidy music player daemon"; enable = lib.mkEnableOption "Mopidy music player daemon";
dataDir = lib.mkOption {
default = "${config.xdg.dataHome}/mopidy";
type = with lib.types; either path str;
description = "The directory where Mopidy stores its state.";
};
extensionPackages = lib.mkOption { extensionPackages = lib.mkOption {
default = [ ]; default = [ ];
defaultText = lib.literalExpression "[ ]";
type = with lib.types; listOf package; type = with lib.types; listOf package;
example = lib.literalExpression example = lib.literalExpression
"with pkgs; [ mopidy-spotify mopidy-mpd mopidy-mpris ]"; "with pkgs; [ mopidy-spotify mopidy-mpd mopidy-mpris ]";
@ -35,19 +37,34 @@ in {
''; '';
}; };
# TODO: Revise the configuration to a proper Nix setting.
configuration = lib.mkOption { configuration = lib.mkOption {
default = ""; default = {};
type = lib.types.lines; type = lib.types.attrs;
description = "The configuration Mopidy uses in the service."; description = "The configuration Mopidy uses in the service.";
}; example = lib.literalExpression ''
{
file = {
media_dirs = [
"$XDG_MUSIC_DIR|Music"
"~/library|Library"
];
follow_symlinks = true;
excluded_file_extensions = [
".html"
".zip"
".jpg"
".jpeg"
".png"
];
};
extraConfigFiles = lib.mkOption { # Please don't put your mopidy-spotify configuration in the public. :)
default = [ ]; # Think of your Spotify Premium subscription!
type = with lib.types; listOf str; spotify = {
description = '' client_id = "CLIENT_ID";
Extra config files to be read to the service. client_secret = "CLIENT_SECRET";
Note that later files overrides earlier configuration. };
}
''; '';
}; };
}; };
@ -58,6 +75,8 @@ in {
lib.platforms.linux) lib.platforms.linux)
]; ];
xdg.configFile."mopidy/mopidy.conf".text = customToINI cfg.configuration;
systemd.user.services.mopidy = { systemd.user.services.mopidy = {
Unit = { Unit = {
Description = "mopidy music player daemon"; Description = "mopidy music player daemon";
@ -66,9 +85,7 @@ in {
}; };
Service = { Service = {
ExecStart = "${mopidyEnv}/bin/mopidy --config ${ ExecStart = "${mopidyEnv}/bin/mopidy";
lib.concatStringsSep ":" ([ mopidyConf ] ++ cfg.extraConfigFiles)
}";
}; };
Install.WantedBy = [ "default.target" ]; Install.WantedBy = [ "default.target" ];
@ -82,9 +99,7 @@ in {
}; };
Service = { Service = {
ExecStart = "${mopidyEnv}/bin/mopidy --config ${ ExecStart = "${mopidyEnv}/bin/mopidy local scan";
lib.concatStringsSep ":" ([ mopidyConf ] ++ cfg.extraConfigFiles)
} local scan";
Type = "oneshot"; Type = "oneshot";
}; };