1# Arguments that this derivation gets when it is created with `callPackage`
2{
3 stdenv,
4 buildEnv,
5 lib,
6 makeWrapper,
7 mpvScripts,
8 symlinkJoin,
9 writeTextDir,
10 yt-dlp,
11# the unwrapped mpv derivation
12}:
13
14let
15 # arguments to the function (exposed as `mpv-unwrapped.wrapper` in top-level)
16 wrapper =
17 {
18 mpv,
19 extraMakeWrapperArgs ? [ ],
20 youtubeSupport ? true,
21 # a set of derivations (probably from `mpvScripts`) where each is expected
22 # to have a `scriptName` passthru attribute that points to the name of the
23 # script that would reside in the script's derivation's
24 # `$out/share/mpv/scripts/`.
25 #
26 # A script can optionally also provide `passthru.extraWrapperArgs`
27 # attribute.
28 scripts ? [ ],
29 extraUmpvWrapperArgs ? [ ],
30 }:
31 let
32 binPath = lib.makeBinPath (
33 [
34 mpv.luaEnv
35 ]
36 ++ lib.optionals youtubeSupport [
37 yt-dlp
38 ]
39 ++ lib.optionals mpv.vapoursynthSupport [
40 mpv.vapoursynth.python3
41 ]
42 );
43 # All arguments besides the input and output binaries (${mpv}/bin/mpv and
44 # $out/bin/mpv). These are used by the darwin specific makeWrapper call
45 # used to wrap $out/Applications/mpv.app/Contents/MacOS/mpv as well.
46 mostMakeWrapperArgs = lib.strings.escapeShellArgs (
47 [
48 "--inherit-argv0"
49 # These are always needed (TODO: Explain why)
50 "--prefix"
51 "LUA_CPATH"
52 ";"
53 "${mpv.luaEnv}/lib/lua/${mpv.lua.luaversion}/?.so"
54 "--prefix"
55 "LUA_PATH"
56 ";"
57 "${mpv.luaEnv}/share/lua/${mpv.lua.luaversion}/?.lua"
58 ]
59 ++ lib.optionals mpv.vapoursynthSupport [
60 "--prefix"
61 "PYTHONPATH"
62 ":"
63 "${mpv.vapoursynth}/${mpv.vapoursynth.python3.sitePackages}"
64 ]
65 ++ lib.optionals (binPath != "") [
66 "--prefix"
67 "PATH"
68 ":"
69 binPath
70 ]
71 ++ (lib.lists.flatten (
72 map
73 # For every script in the `scripts` argument, add the necessary flags to the wrapper
74 (
75 script:
76 [
77 "--add-flags"
78 # Here we rely on the existence of the `scriptName` passthru
79 # attribute of the script derivation from the `scripts`
80 "--script=${script}/share/mpv/scripts/${script.scriptName}"
81 ]
82 # scripts can also set the `extraWrapperArgs` passthru
83 ++ (script.extraWrapperArgs or [ ])
84 )
85 scripts
86 ))
87 ++ extraMakeWrapperArgs
88 );
89 umpvWrapperArgs = lib.strings.escapeShellArgs (
90 [
91 "--inherit-argv0"
92 "--set"
93 "MPV"
94 "${placeholder "out"}/bin/mpv"
95 ]
96 ++ extraUmpvWrapperArgs
97 );
98 in
99 symlinkJoin {
100 name = "mpv-with-scripts-${mpv.version}";
101
102 # TODO: don't link all mpv outputs and convert package to mpv-unwrapped?
103 paths = [ mpv.all ];
104
105 nativeBuildInputs = [ makeWrapper ];
106
107 passthru.unwrapped = mpv;
108
109 passthru.tests.mpv-scripts-should-not-collide = buildEnv {
110 name = "mpv-scripts-env";
111 paths = lib.pipe mpvScripts [
112 # filters "override" "overrideDerivation" "recurseForDerivations"
113 (lib.filterAttrs (key: script: lib.isDerivation script))
114 # replaces unfree and meta.broken scripts with decent placeholders
115 (lib.mapAttrsToList (
116 key: script:
117 if (builtins.tryEval script.outPath).success then
118 script
119 else
120 writeTextDir "share/mpv/scripts/${script.scriptName}" "placeholder of ${script.name}"
121 ))
122 ];
123 };
124
125 postBuild = ''
126 # wrapProgram can't operate on symlinks
127 rm "$out/bin/mpv"
128 makeWrapper "${mpv}/bin/mpv" "$out/bin/mpv" ${mostMakeWrapperArgs}
129 rm "$out/bin/umpv"
130 makeWrapper "${mpv}/bin/umpv" "$out/bin/umpv" ${umpvWrapperArgs}
131 ''
132 + lib.optionalString stdenv.hostPlatform.isDarwin ''
133 # wrapProgram can't operate on symlinks
134 rm "$out/Applications/mpv.app/Contents/MacOS/mpv"
135 makeWrapper "${mpv}/Applications/mpv.app/Contents/MacOS/mpv" "$out/Applications/mpv.app/Contents/MacOS/mpv" ${mostMakeWrapperArgs}
136 '';
137
138 meta = {
139 inherit (mpv.meta)
140 homepage
141 description
142 longDescription
143 maintainers
144 ;
145 mainProgram = "mpv";
146 };
147 };
148in
149lib.makeOverridable wrapper