1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-16 08:09:45 +01:00
home-manager/modules/programs/mpv.nix

191 lines
5.3 KiB
Nix
Raw Normal View History

2019-04-30 03:25:53 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
inherit (builtins) typeOf stringLength;
cfg = config.programs.mpv;
mpvOption = with types; either str (either int (either bool float));
mpvOptionDup = with types; either mpvOption (listOf mpvOption);
mpvOptions = with types; attrsOf mpvOptionDup;
2019-04-30 03:25:53 +02:00
mpvProfiles = with types; attrsOf mpvOptions;
mpvBindings = with types; attrsOf str;
2021-01-11 22:53:35 +01:00
mpvDefaultProfiles = with types; listOf str;
2019-04-30 03:25:53 +02:00
renderOption = option:
rec {
int = toString option;
float = int;
bool = lib.hm.booleans.yesNo option;
2019-04-30 03:25:53 +02:00
string = option;
}.${typeOf option};
renderOptionValue = value:
let
rendered = renderOption value;
length = toString (stringLength rendered);
in "%${length}%${rendered}";
renderOptions = generators.toKeyValue {
mkKeyValue =
generators.mkKeyValueDefault { mkValueString = renderOptionValue; } "=";
listsAsDuplicateKeys = true;
};
renderProfiles = generators.toINI {
mkKeyValue =
generators.mkKeyValueDefault { mkValueString = renderOptionValue; } "=";
listsAsDuplicateKeys = true;
};
2019-04-30 03:25:53 +02:00
renderBindings = bindings:
concatStringsSep "\n"
2020-02-02 00:39:17 +01:00
(mapAttrsToList (name: value: "${name} ${value}") bindings);
2019-04-30 03:25:53 +02:00
2021-01-11 22:53:35 +01:00
renderDefaultProfiles = profiles:
renderOptions { profile = concatStringsSep "," profiles; };
mpvPackage = if cfg.scripts == [ ] then
cfg.package
else
pkgs.wrapMpv pkgs.mpv-unwrapped { scripts = cfg.scripts; };
2019-04-30 03:25:53 +02:00
in {
options = {
programs.mpv = {
enable = mkEnableOption "mpv";
package = mkOption {
type = types.package;
default = pkgs.mpv;
example = literalExpression
"pkgs.wrapMpv (pkgs.mpv-unwrapped.override { vapoursynthSupport = true; }) { youtubeSupport = true; }";
description = ''
Package providing mpv.
'';
};
finalPackage = mkOption {
type = types.package;
readOnly = true;
visible = false;
description = ''
Resulting mpv package.
'';
};
2019-09-02 17:52:02 +02:00
scripts = mkOption {
type = with types; listOf package;
2020-02-02 00:39:17 +01:00
default = [ ];
example = literalExpression "[ pkgs.mpvScripts.mpris ]";
2019-09-02 17:52:02 +02:00
description = ''
List of scripts to use with mpv.
'';
};
2019-04-30 03:25:53 +02:00
config = mkOption {
description = ''
Configuration written to
<filename>$XDG_CONFIG_HOME/mpv/mpv.conf</filename>. See
2019-04-30 03:25:53 +02:00
<citerefentry>
<refentrytitle>mpv</refentrytitle>
<manvolnum>1</manvolnum>
</citerefentry>
for the full list of options.
'';
type = mpvOptions;
2020-02-02 00:39:17 +01:00
default = { };
example = literalExpression ''
2019-04-30 03:25:53 +02:00
{
profile = "gpu-hq";
force-window = true;
2019-04-30 03:25:53 +02:00
ytdl-format = "bestvideo+bestaudio";
cache-default = 4000000;
}
'';
};
profiles = mkOption {
description = ''
Sub-configuration options for specific profiles written to
<filename>$XDG_CONFIG_HOME/mpv/mpv.conf</filename>. See
2019-04-30 03:25:53 +02:00
<option>programs.mpv.config</option> for more information.
'';
type = mpvProfiles;
2020-02-02 00:39:17 +01:00
default = { };
example = literalExpression ''
2019-04-30 03:25:53 +02:00
{
fast = {
vo = "vdpau";
};
"protocol.dvd" = {
profile-desc = "profile for dvd:// streams";
alang = "en";
};
}
'';
};
2021-01-11 22:53:35 +01:00
defaultProfiles = mkOption {
description = ''
Profiles to be applied by default. Options set by them are overridden
by options set in <xref linkend="opt-programs.mpv.config"/>.
'';
type = mpvDefaultProfiles;
default = [ ];
example = [ "gpu-hq" ];
};
2019-04-30 03:25:53 +02:00
bindings = mkOption {
description = ''
Input configuration written to
<filename>$XDG_CONFIG_HOME/mpv/input.conf</filename>. See
2019-04-30 03:25:53 +02:00
<citerefentry>
<refentrytitle>mpv</refentrytitle>
<manvolnum>1</manvolnum>
</citerefentry>
for the full list of options.
'';
type = mpvBindings;
2020-02-02 00:39:17 +01:00
default = { };
example = literalExpression ''
2019-04-30 03:25:53 +02:00
{
WHEEL_UP = "seek 10";
WHEEL_DOWN = "seek -10";
"Alt+0" = "set window-scale 0.5";
}
'';
};
};
};
config = mkIf cfg.enable (mkMerge [
{
assertions = [{
assertion = (cfg.scripts == [ ]) || (cfg.package == pkgs.mpv);
message = ''
The programs.mpv "package" option is mutually exclusive with "scripts" option.'';
}];
}
2019-04-30 03:25:53 +02:00
{
home.packages = [ mpvPackage ];
programs.mpv.finalPackage = mpvPackage;
2019-04-30 03:25:53 +02:00
}
2020-02-02 00:39:17 +01:00
(mkIf (cfg.config != { } || cfg.profiles != { }) {
2019-04-30 03:25:53 +02:00
xdg.configFile."mpv/mpv.conf".text = ''
2021-01-11 22:53:35 +01:00
${optionalString (cfg.defaultProfiles != [ ])
(renderDefaultProfiles cfg.defaultProfiles)}
2020-02-02 00:39:17 +01:00
${optionalString (cfg.config != { }) (renderOptions cfg.config)}
${optionalString (cfg.profiles != { }) (renderProfiles cfg.profiles)}
2019-04-30 03:25:53 +02:00
'';
})
2020-02-02 00:39:17 +01:00
(mkIf (cfg.bindings != { }) {
2019-04-30 03:25:53 +02:00
xdg.configFile."mpv/input.conf".text = renderBindings cfg.bindings;
})
]);
meta.maintainers = with maintainers; [ tadeokondrak thiagokokada ];
2019-04-30 03:25:53 +02:00
}