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 mopidyEnv = python.buildEnv.override {
15 extraLibs = [ mopidy ] ++ cfg.extensionPackages;
16 };
17
18in {
19
20 options = {
21
22 services.mopidy = {
23
24 enable = mkEnableOption "Mopidy, a music player daemon";
25
26 dataDir = mkOption {
27 default = "/var/lib/mopidy";
28 type = types.str;
29 description = ''
30 The directory where Mopidy stores its state.
31 '';
32 };
33
34 extensionPackages = mkOption {
35 default = [];
36 type = types.listOf types.package;
37 example = literalExample "[ pkgs.mopidy-spotify ]";
38 description = ''
39 Mopidy extensions that should be loaded by the service.
40 '';
41 };
42
43 configuration = mkOption {
44 default = "";
45 type = types.lines;
46 description = ''
47 The configuration that Mopidy should use.
48 '';
49 };
50
51 extraConfigFiles = mkOption {
52 default = [];
53 type = types.listOf types.str;
54 description = ''
55 Extra config file read by Mopidy when the service starts.
56 Later files in the list overrides earlier configuration.
57 '';
58 };
59
60 };
61
62 };
63
64
65 ###### implementation
66
67 config = mkIf cfg.enable {
68
69 systemd.services.mopidy = {
70 wantedBy = [ "multi-user.target" ];
71 after = [ "network.target" "sound.target" ];
72 description = "mopidy music player daemon";
73 preStart = "mkdir -p ${cfg.dataDir} && chown -R mopidy:mopidy ${cfg.dataDir}";
74 serviceConfig = {
75 ExecStart = "${mopidyEnv}/bin/mopidy --config ${concatStringsSep ":" ([mopidyConf] ++ cfg.extraConfigFiles)}";
76 User = "mopidy";
77 PermissionsStartOnly = true;
78 };
79 };
80
81 systemd.services.mopidy-scan = {
82 description = "mopidy local files scanner";
83 preStart = "mkdir -p ${cfg.dataDir} && chown -R mopidy:mopidy ${cfg.dataDir}";
84 serviceConfig = {
85 ExecStart = "${mopidyEnv}/bin/mopidy --config ${concatStringsSep ":" ([mopidyConf] ++ cfg.extraConfigFiles)} local scan";
86 User = "mopidy";
87 PermissionsStartOnly = true;
88 Type = "oneshot";
89 };
90 };
91
92 users.extraUsers.mopidy = {
93 inherit uid;
94 group = "mopidy";
95 extraGroups = [ "audio" ];
96 description = "Mopidy daemon user";
97 home = "${cfg.dataDir}";
98 };
99
100 users.extraGroups.mopidy.gid = gid;
101
102 };
103
104}