fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
3.7 KiB132 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.ncmpcpp;
8
9  renderSettings = settings:
10    concatStringsSep "\n" (mapAttrsToList renderSetting settings);
11
12  renderSetting = name: value: "${name}=${renderValue value}";
13
14  renderValue = option:
15    {
16      int = toString option;
17      bool = lib.hm.booleans.yesNo option;
18      string = option;
19    }.${builtins.typeOf option};
20
21  renderBindings = bindings: concatStringsSep "\n" (map renderBinding bindings);
22
23  renderBinding = { key, command }:
24    concatStringsSep "\n  " ([ ''def_key "${key}"'' ] ++ maybeWrapList command);
25
26  maybeWrapList = xs: if isList xs then xs else [ xs ];
27
28  valueType = with types; oneOf [ bool int str ];
29
30  bindingType = types.submodule ({ name, config, ... }: {
31    options = {
32      key = mkOption {
33        type = types.str;
34        description = "Key to bind.";
35        example = "j";
36      };
37
38      command = mkOption {
39        type = with types; either str (listOf str);
40        description = "Command or sequence of commands to be executed.";
41        example = "scroll_down";
42      };
43    };
44  });
45
46in {
47  meta.maintainers = [ hm.maintainers.olmokramer ];
48
49  options.programs.ncmpcpp = {
50    enable =
51      mkEnableOption "ncmpcpp - an ncurses Music Player Daemon (MPD) client";
52
53    package = mkOption {
54      type = types.package;
55      default = pkgs.ncmpcpp;
56      defaultText = literalExpression "pkgs.ncmpcpp";
57      description = ''
58        Package providing the `ncmpcpp` command.
59      '';
60      example =
61        literalExpression "pkgs.ncmpcpp.override { visualizerSupport = true; }";
62    };
63
64    mpdMusicDir = mkOption {
65      type = with types; nullOr (coercedTo path toString str);
66      default = let mpdCfg = config.services.mpd;
67      in if pkgs.stdenv.hostPlatform.isLinux && mpdCfg.enable then
68        mpdCfg.musicDirectory
69      else
70        null;
71      defaultText = literalExpression ''
72        if pkgs.stdenv.hostPlatform.isLinux && config.services.mpd.enable then
73          config.services.mpd.musicDirectory
74        else
75          null
76      '';
77      description = ''
78        Value of the `mpd_music_dir` setting. On Linux platforms the
79        value of {var}`services.mpd.musicDirectory` is used as the
80        default if {var}`services.mpd.enable` is
81        `true`.
82      '';
83      example = "~/music";
84    };
85
86    settings = mkOption {
87      type = types.attrsOf valueType;
88      default = { };
89      description = ''
90        Attribute set from name of a setting to its value. For available options
91        see
92        {manpage}`ncmpcpp(1)`.
93      '';
94      example = { ncmpcpp_directory = "~/.local/share/ncmpcpp"; };
95    };
96
97    bindings = mkOption {
98      type = types.listOf bindingType;
99      default = [ ];
100      description = "List of keybindings.";
101      example = literalExpression ''
102        [
103          { key = "j"; command = "scroll_down"; }
104          { key = "k"; command = "scroll_up"; }
105          { key = "J"; command = [ "select_item" "scroll_down" ]; }
106          { key = "K"; command = [ "select_item" "scroll_up" ]; }
107        ]
108      '';
109    };
110  };
111
112  config = mkIf cfg.enable {
113    warnings = mkIf (cfg.settings ? mpd_music_dir && cfg.mpdMusicDir != null) [
114      ("programs.ncmpcpp.settings.mpd_music_dir will be overridden by"
115        + " programs.ncmpcpp.mpdMusicDir.")
116    ];
117
118    home.packages = [ cfg.package ];
119
120    xdg.configFile = {
121      "ncmpcpp/config" = let
122        settings = cfg.settings // optionalAttrs (cfg.mpdMusicDir != null) {
123          mpd_music_dir = cfg.mpdMusicDir;
124        };
125      in mkIf (settings != { }) { text = renderSettings settings + "\n"; };
126
127      "ncmpcpp/bindings" = mkIf (cfg.bindings != [ ]) {
128        text = renderBindings cfg.bindings + "\n";
129      };
130    };
131  };
132}