Skip to content

Commit 85df462

Browse files
committed
programs/mpv: add module and tests
1 parent 7c931af commit 85df462

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
name = "mpv";
3+
nodes.machine = {
4+
hjem.users.bob.rum = {
5+
programs.mpv = {
6+
enable = true;
7+
config = {
8+
hwdec = true;
9+
};
10+
profiles = {
11+
fast = {
12+
cache = true;
13+
};
14+
};
15+
bindings = {
16+
WHEEL_UP = "seek 10";
17+
};
18+
scriptOpts = {
19+
osc = {
20+
vidscale = false;
21+
};
22+
};
23+
};
24+
};
25+
};
26+
27+
testScript =
28+
#python
29+
''
30+
# Waiting for our user to load.
31+
machine.succeed("loginctl enable-linger bob")
32+
machine.wait_for_unit("default.target")
33+
34+
# Assert that the mpv config is in place
35+
pattern = 'hwdec=yes'
36+
machine.succeed(f"grep -E '{pattern}' %s" % "/home/bob/.config/mpv/mpv.conf")
37+
38+
# Assert that the profiles are in place
39+
pattern = '[fast]'
40+
machine.succeed(f"grep -E '{pattern}' %s" % "/home/bob/.config/mpv/mpv.conf")
41+
42+
# Assert that the bindings are in place
43+
pattern = 'WHEEL_UP seek 10'
44+
machine.succeed(f"grep -E '{pattern}' %s" % "/home/bob/.config/mpv/bindings.conf")
45+
46+
# Assert that the script's options are in place
47+
pattern = 'vidscale=no'
48+
machine.succeed(f"grep -E '{pattern}' %s" % "/home/bob/.config/mpv/script-opts/osc.conf")
49+
'';
50+
}

0 commit comments

Comments
 (0)