1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 uid = config.ids.uids.mopidy;
9 gid = config.ids.gids.mopidy;
10 cfg = config.services.mopidy;
11
12 mopidyConf = pkgs.writeText "mopidy.conf" cfg.configuration;
13
14 mopidyEnv = pkgs.buildEnv {
15 name = "mopidy-with-extensions-${pkgs.mopidy.version}";
16 ignoreCollisions = true;
17 paths = lib.closePropagation cfg.extensionPackages;
18 pathsToLink = [ "/${pkgs.mopidyPackages.python.sitePackages}" ];
19 nativeBuildInputs = [ pkgs.makeWrapper ];
20 postBuild = ''
21 makeWrapper ${lib.getExe pkgs.mopidy} $out/bin/mopidy \
22 --prefix PYTHONPATH : $out/${pkgs.mopidyPackages.python.sitePackages}
23 '';
24 };
25in
26{
27
28 options = {
29
30 services.mopidy = {
31
32 enable = lib.mkEnableOption "Mopidy, a music player daemon";
33
34 dataDir = lib.mkOption {
35 default = "/var/lib/mopidy";
36 type = lib.types.str;
37 description = ''
38 The directory where Mopidy stores its state.
39 '';
40 };
41
42 extensionPackages = lib.mkOption {
43 default = [ ];
44 type = lib.types.listOf lib.types.package;
45 example = lib.literalExpression "[ pkgs.mopidy-spotify ]";
46 description = ''
47 Mopidy extensions that should be loaded by the service.
48 '';
49 };
50
51 configuration = lib.mkOption {
52 default = "";
53 type = lib.types.lines;
54 description = ''
55 The configuration that Mopidy should use.
56 '';
57 };
58
59 extraConfigFiles = lib.mkOption {
60 default = [ ];
61 type = lib.types.listOf lib.types.str;
62 description = ''
63 Extra config file read by Mopidy when the service starts.
64 Later files in the list overrides earlier configuration.
65 '';
66 };
67 };
68 };
69
70 ###### implementation
71
72 config = lib.mkIf cfg.enable {
73
74 systemd.tmpfiles.settings."10-mopidy".${cfg.dataDir}.d = {
75 user = "mopidy";
76 group = "mopidy";
77 };
78
79 systemd.services.mopidy = {
80 wantedBy = [ "multi-user.target" ];
81 after = [
82 "network-online.target"
83 "sound.target"
84 ];
85 wants = [ "network-online.target" ];
86 description = "mopidy music player daemon";
87 serviceConfig = {
88 ExecStart = "${mopidyEnv}/bin/mopidy --config ${
89 lib.concatStringsSep ":" ([ mopidyConf ] ++ cfg.extraConfigFiles)
90 }";
91 Restart = "on-failure";
92 User = "mopidy";
93 };
94 };
95
96 systemd.services.mopidy-scan = {
97 description = "mopidy local files scanner";
98 serviceConfig = {
99 ExecStart = "${mopidyEnv}/bin/mopidy --config ${
100 lib.concatStringsSep ":" ([ mopidyConf ] ++ cfg.extraConfigFiles)
101 } local scan";
102 User = "mopidy";
103 Type = "oneshot";
104 };
105 };
106
107 users.users.mopidy = {
108 inherit uid;
109 group = "mopidy";
110 extraGroups = [ "audio" ];
111 description = "Mopidy daemon user";
112 home = cfg.dataDir;
113 };
114
115 users.groups.mopidy.gid = gid;
116 };
117}