fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
4.2 KiB152 linesraw
1{ config, options, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.mopidy;
8
9  # The configuration format of Mopidy. It seems to use configparser with
10  # some quirky handling of its types. You can see how they're handled in
11  # `mopidy/config/types.py` from the source code.
12  toMopidyConf = generators.toINI {
13    mkKeyValue = generators.mkKeyValueDefault {
14      mkValueString = v:
15        if isList v then
16          "\n  " + concatStringsSep "\n  " v
17        else
18          generators.mkValueStringDefault { } v;
19    } " = ";
20  };
21
22  mopidyEnv = pkgs.buildEnv {
23    name = "mopidy-with-extensions-${pkgs.mopidy.version}";
24    paths = closePropagation cfg.extensionPackages;
25    pathsToLink = [ "/${pkgs.mopidyPackages.python.sitePackages}" ];
26    buildInputs = [ pkgs.makeWrapper ];
27    postBuild = ''
28      makeWrapper ${pkgs.mopidy}/bin/mopidy $out/bin/mopidy \
29        --prefix PYTHONPATH : $out/${pkgs.mopidyPackages.python.sitePackages}
30    '';
31  };
32
33  # Nix-representable format for Mopidy config.
34  mopidyConfFormat = { }: {
35    type = with types;
36      let
37        valueType = nullOr (oneOf [ bool float int str (listOf valueType) ])
38          // {
39            description = "Mopidy config value";
40          };
41      in attrsOf (attrsOf valueType);
42
43    generate = name: value: pkgs.writeText name (toMopidyConf value);
44  };
45
46  settingsFormat = mopidyConfFormat { };
47
48  configFilePaths = concatStringsSep ":"
49    ([ "${config.xdg.configHome}/mopidy/mopidy.conf" ] ++ cfg.extraConfigFiles);
50
51in {
52  meta.maintainers = [ hm.maintainers.foo-dogsquared ];
53
54  options.services.mopidy = {
55    enable = mkEnableOption "Mopidy music player daemon";
56
57    extensionPackages = mkOption {
58      type = with types; listOf package;
59      default = [ ];
60      example = literalExpression
61        "with pkgs; [ mopidy-spotify mopidy-mpd mopidy-mpris ]";
62      description = ''
63        Mopidy extensions that should be loaded by the service.
64      '';
65    };
66
67    settings = mkOption {
68      type = settingsFormat.type;
69      default = { };
70      example = literalExpression ''
71        {
72          file = {
73            media_dirs = [
74              "$XDG_MUSIC_DIR|Music"
75              "~/library|Library"
76            ];
77            follow_symlinks = true;
78            excluded_file_extensions = [
79              ".html"
80              ".zip"
81              ".jpg"
82              ".jpeg"
83              ".png"
84            ];
85          };
86
87          # Please don't put your mopidy-spotify configuration in the public. :)
88          # Think of your Spotify Premium subscription!
89          spotify = {
90            client_id = "CLIENT_ID";
91            client_secret = "CLIENT_SECRET";
92          };
93        }
94      '';
95      description = ''
96        Configuration written to
97        {file}`$XDG_CONFIG_HOME/mopidy/mopidy.conf`.
98
99        See <https://docs.mopidy.com/en/latest/config/> for
100        more details.
101      '';
102    };
103
104    extraConfigFiles = mkOption {
105      default = [ ];
106      type = types.listOf types.path;
107      description = ''
108        Extra configuration files read by Mopidy when the service starts.
109        Later files in the list override earlier configuration files and
110        structured settings.
111      '';
112    };
113  };
114
115  config = mkIf cfg.enable {
116    assertions =
117      [ (hm.assertions.assertPlatform "services.mopidy" pkgs platforms.linux) ];
118
119    xdg.configFile."mopidy/mopidy.conf".source =
120      settingsFormat.generate "mopidy-${config.home.username}" cfg.settings;
121
122    systemd.user.services.mopidy = {
123      Unit = {
124        Description = "mopidy music player daemon";
125        Documentation = [ "https://mopidy.com/" ];
126        After = [ "network.target" "sound.target" ];
127      };
128
129      Service = {
130        ExecStart = "${mopidyEnv}/bin/mopidy --config ${configFilePaths}";
131      };
132
133      Install.WantedBy = [ "default.target" ];
134    };
135
136    systemd.user.services.mopidy-scan = {
137      Unit = {
138        Description = "mopidy local files scanner";
139        Documentation = [ "https://mopidy.com/" ];
140        After = [ "network.target" "sound.target" ];
141      };
142
143      Service = {
144        ExecStart =
145          "${mopidyEnv}/bin/mopidy --config ${configFilePaths} local scan";
146        Type = "oneshot";
147      };
148
149      Install.WantedBy = [ "default.target" ];
150    };
151  };
152}