|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: let |
| 7 | + inherit (lib) generators literalExpression mkIf mkOption; |
| 8 | + inherit (lib.types) oneOf attrsOf listOf str int bool float package; |
| 9 | + inherit (builtins) typeOf; |
| 10 | + |
| 11 | + cfg = config.rum.programs.mpv; |
| 12 | + |
| 13 | + mpvOptions = let |
| 14 | + opt = |
| 15 | + oneOf [str int bool float (listOf opt)]; |
| 16 | + in |
| 17 | + attrsOf opt; |
| 18 | + |
| 19 | + renderOption = option: let |
| 20 | + optT = typeOf option; |
| 21 | + in |
| 22 | + if optT == "int" || optT == "float" |
| 23 | + then toString option |
| 24 | + else if optT == "bool" |
| 25 | + then |
| 26 | + if option |
| 27 | + then "yes" |
| 28 | + else "no" |
| 29 | + else option; |
| 30 | + |
| 31 | + renderOptions = generators.toKeyValue { |
| 32 | + mkKeyValue = generators.mkKeyValueDefault {mkValueString = renderOption;} "="; |
| 33 | + listsAsDuplicateKeys = true; |
| 34 | + }; |
| 35 | + |
| 36 | + renderProfiles = generators.toINI { |
| 37 | + mkKeyValue = generators.mkKeyValueDefault {mkValueString = renderOption;} "="; |
| 38 | + listsAsDuplicateKeys = true; |
| 39 | + }; |
| 40 | + |
| 41 | + renderBindings = bindings: lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: "${name} ${value}") bindings); |
| 42 | +in { |
| 43 | + options.rum.programs.mpv = { |
| 44 | + enable = lib.mkEnableOption "mpv"; |
| 45 | + |
| 46 | + package = lib.mkPackageOption pkgs "mpv" {nullable = true;}; |
| 47 | + |
| 48 | + config = mkOption { |
| 49 | + description = '' |
| 50 | + Configuration converted and written to |
| 51 | + {file}`$XDG_CONFIG_HOME/mpv/mpv.conf`. |
| 52 | +
|
| 53 | + See the manpage {manpage}`mpv(1)` |
| 54 | + for the full list of options. |
| 55 | + ''; |
| 56 | + type = mpvOptions; |
| 57 | + default = {}; |
| 58 | + example = { |
| 59 | + autofit-larger = "100%x100%"; |
| 60 | + hwdec = true; |
| 61 | + osd-playing-msg = "File: $\{filename}"; |
| 62 | + }; |
| 63 | + }; |
| 64 | + |
| 65 | + profiles = mkOption { |
| 66 | + description = '' |
| 67 | + Profiles converted and written to |
| 68 | + {file}`$XDG_CONFIG_HOME/mpv/mpv.conf`. |
| 69 | +
|
| 70 | + See the manpage {manpage}`mpv(1)` |
| 71 | + for more information. |
| 72 | + ''; |
| 73 | + type = attrsOf mpvOptions; |
| 74 | + default = {}; |
| 75 | + example = { |
| 76 | + big-cache = { |
| 77 | + cache = true; |
| 78 | + demuxer-max-bytes = "512Mib"; |
| 79 | + demuxer-readhead-secs = 20; |
| 80 | + }; |
| 81 | + reduce-judder = { |
| 82 | + video-sync = "display-resample"; |
| 83 | + interpolation = true; |
| 84 | + }; |
| 85 | + }; |
| 86 | + }; |
| 87 | + |
| 88 | + bindings = mkOption { |
| 89 | + description = '' |
| 90 | + Inputs converted and written to |
| 91 | + {file}`$XDG_CONFIG_HOME/mpv/input.conf`. |
| 92 | +
|
| 93 | + See the manpage {manpage}`mpv(1)` |
| 94 | + for more information. |
| 95 | + ''; |
| 96 | + type = attrsOf str; |
| 97 | + default = {}; |
| 98 | + example = { |
| 99 | + WHEEL_UP = "seek 10"; |
| 100 | + WHEEL_DOWN = "seek -10"; |
| 101 | + "Alt+0" = "set window-scale 0.5"; |
| 102 | + }; |
| 103 | + }; |
| 104 | + |
| 105 | + scripts = mkOption { |
| 106 | + type = listOf package; |
| 107 | + default = []; |
| 108 | + example = literalExpression "with pkgs.mpvScripts; [ sponsorblock mpris ]"; |
| 109 | + description = '' |
| 110 | + List of scripts to use with mpv. |
| 111 | + ''; |
| 112 | + }; |
| 113 | + |
| 114 | + scriptOpts = mkOption { |
| 115 | + description = '' |
| 116 | + Script options converted and written to |
| 117 | + {file}`$XDG_CONFIG_HOME/mpv/script-opts/<name>.conf`. |
| 118 | +
|
| 119 | + See the manpage {manpage}`mpv(1)` |
| 120 | + for the full list of options of builtin scripts. |
| 121 | + ''; |
| 122 | + type = attrsOf mpvOptions; |
| 123 | + default = {}; |
| 124 | + example = { |
| 125 | + osc = { |
| 126 | + scalewindowed = 2.0; |
| 127 | + vidscale = false; |
| 128 | + visibility = "always"; |
| 129 | + }; |
| 130 | + }; |
| 131 | + }; |
| 132 | + }; |
| 133 | + |
| 134 | + config = mkIf cfg.enable { |
| 135 | + packages = [ |
| 136 | + ( |
| 137 | + if cfg.scripts == [] |
| 138 | + then cfg.package |
| 139 | + else pkgs.mpv.override {inherit (cfg) scripts;} |
| 140 | + ) |
| 141 | + ]; |
| 142 | + |
| 143 | + xdg.config.files = |
| 144 | + { |
| 145 | + "mpv/mpv.conf".text = mkIf (cfg.config != {} || cfg.profiles != {}) '' |
| 146 | + ${lib.optionalString (cfg.config != {}) (renderOptions cfg.config)} |
| 147 | + ${lib.optionalString (cfg.profiles != {}) (renderProfiles cfg.profiles)} |
| 148 | + ''; |
| 149 | + "mpv/input.conf".text = |
| 150 | + mkIf (cfg.bindings != {}) (renderBindings cfg.bindings); |
| 151 | + } |
| 152 | + // (lib.mapAttrs' ( |
| 153 | + name: value: |
| 154 | + lib.nameValuePair "mpv/script-opts/${name}.conf" { |
| 155 | + text = renderOptions value; |
| 156 | + } |
| 157 | + ) |
| 158 | + cfg.scriptOpts); |
| 159 | + }; |
| 160 | +} |
0 commit comments