fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
5.8 KiB214 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  inherit (builtins) typeOf stringLength;
7
8  cfg = config.programs.mpv;
9
10  mpvOption = with types; either str (either int (either bool float));
11  mpvOptionDup = with types; either mpvOption (listOf mpvOption);
12  mpvOptions = with types; attrsOf mpvOptionDup;
13  mpvProfiles = with types; attrsOf mpvOptions;
14  mpvBindings = with types; attrsOf str;
15  mpvDefaultProfiles = with types; listOf str;
16
17  renderOption = option:
18    rec {
19      int = toString option;
20      float = int;
21      bool = lib.hm.booleans.yesNo option;
22      string = option;
23    }.${typeOf option};
24
25  renderOptionValue = value:
26    let
27      rendered = renderOption value;
28      length = toString (stringLength rendered);
29    in "%${length}%${rendered}";
30
31  renderOptions = generators.toKeyValue {
32    mkKeyValue =
33      generators.mkKeyValueDefault { mkValueString = renderOptionValue; } "=";
34    listsAsDuplicateKeys = true;
35  };
36
37  renderScriptOptions = generators.toKeyValue {
38    mkKeyValue =
39      generators.mkKeyValueDefault { mkValueString = renderOption; } "=";
40    listsAsDuplicateKeys = true;
41  };
42
43  renderProfiles = generators.toINI {
44    mkKeyValue =
45      generators.mkKeyValueDefault { mkValueString = renderOptionValue; } "=";
46    listsAsDuplicateKeys = true;
47  };
48
49  renderBindings = bindings:
50    concatStringsSep "\n"
51    (mapAttrsToList (name: value: "${name} ${value}") bindings);
52
53  renderDefaultProfiles = profiles:
54    renderOptions { profile = concatStringsSep "," profiles; };
55
56  mpvPackage = if cfg.scripts == [ ] then
57    cfg.package
58  else
59    pkgs.wrapMpv pkgs.mpv-unwrapped { scripts = cfg.scripts; };
60
61in {
62  options = {
63    programs.mpv = {
64      enable = mkEnableOption "mpv";
65
66      package = mkOption {
67        type = types.package;
68        default = pkgs.mpv;
69        example = literalExpression
70          "pkgs.wrapMpv (pkgs.mpv-unwrapped.override { vapoursynthSupport = true; }) { youtubeSupport = true; }";
71        description = ''
72          Package providing mpv.
73        '';
74      };
75
76      finalPackage = mkOption {
77        type = types.package;
78        readOnly = true;
79        visible = false;
80        description = ''
81          Resulting mpv package.
82        '';
83      };
84
85      scripts = mkOption {
86        type = with types; listOf package;
87        default = [ ];
88        example = literalExpression "[ pkgs.mpvScripts.mpris ]";
89        description = ''
90          List of scripts to use with mpv.
91        '';
92      };
93
94      scriptOpts = mkOption {
95        description = ''
96          Script options added to
97          {file}`$XDG_CONFIG_HOME/mpv/script-opts/`. See
98          {manpage}`mpv(1)`
99          for the full list of options of builtin scripts.
100        '';
101        type = types.attrsOf mpvOptions;
102        default = { };
103        example = {
104          osc = {
105            scalewindowed = 2.0;
106            vidscale = false;
107            visibility = "always";
108          };
109        };
110      };
111
112      config = mkOption {
113        description = ''
114          Configuration written to
115          {file}`$XDG_CONFIG_HOME/mpv/mpv.conf`. See
116          {manpage}`mpv(1)`
117          for the full list of options.
118        '';
119        type = mpvOptions;
120        default = { };
121        example = literalExpression ''
122          {
123            profile = "gpu-hq";
124            force-window = true;
125            ytdl-format = "bestvideo+bestaudio";
126            cache-default = 4000000;
127          }
128        '';
129      };
130
131      profiles = mkOption {
132        description = ''
133          Sub-configuration options for specific profiles written to
134          {file}`$XDG_CONFIG_HOME/mpv/mpv.conf`. See
135          {option}`programs.mpv.config` for more information.
136        '';
137        type = mpvProfiles;
138        default = { };
139        example = literalExpression ''
140          {
141            fast = {
142              vo = "vdpau";
143            };
144            "protocol.dvd" = {
145              profile-desc = "profile for dvd:// streams";
146              alang = "en";
147            };
148          }
149        '';
150      };
151
152      defaultProfiles = mkOption {
153        description = ''
154          Profiles to be applied by default. Options set by them are overridden
155          by options set in [](#opt-programs.mpv.config).
156        '';
157        type = mpvDefaultProfiles;
158        default = [ ];
159        example = [ "gpu-hq" ];
160      };
161
162      bindings = mkOption {
163        description = ''
164          Input configuration written to
165          {file}`$XDG_CONFIG_HOME/mpv/input.conf`. See
166          {manpage}`mpv(1)`
167          for the full list of options.
168        '';
169        type = mpvBindings;
170        default = { };
171        example = literalExpression ''
172          {
173            WHEEL_UP = "seek 10";
174            WHEEL_DOWN = "seek -10";
175            "Alt+0" = "set window-scale 0.5";
176          }
177        '';
178      };
179    };
180  };
181
182  config = mkIf cfg.enable (mkMerge [
183    {
184      assertions = [{
185        assertion = (cfg.scripts == [ ]) || (cfg.package == pkgs.mpv);
186        message = ''
187          The programs.mpv "package" option is mutually exclusive with "scripts" option.'';
188      }];
189    }
190    {
191      home.packages = [ mpvPackage ];
192      programs.mpv.finalPackage = mpvPackage;
193    }
194    (mkIf (cfg.config != { } || cfg.profiles != { }) {
195      xdg.configFile."mpv/mpv.conf".text = ''
196        ${optionalString (cfg.defaultProfiles != [ ])
197        (renderDefaultProfiles cfg.defaultProfiles)}
198        ${optionalString (cfg.config != { }) (renderOptions cfg.config)}
199        ${optionalString (cfg.profiles != { }) (renderProfiles cfg.profiles)}
200      '';
201    })
202    (mkIf (cfg.bindings != { }) {
203      xdg.configFile."mpv/input.conf".text = renderBindings cfg.bindings;
204    })
205    {
206      xdg.configFile = mapAttrs' (name: value:
207        nameValuePair "mpv/script-opts/${name}.conf" {
208          text = renderScriptOptions value;
209        }) cfg.scriptOpts;
210    }
211  ]);
212
213  meta.maintainers = with maintainers; [ tadeokondrak thiagokokada chuangzhu ];
214}