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