snac2: 2.68 -> 2.70 (#379043)
[NixPkgs.git] / pkgs / applications / video / mpv / wrapper.nix
blob54fa65890b03a42b0ec20a01171a8a33bf19a196
1 # Arguments that this derivation gets when it is created with `callPackage`
3   stdenv,
4   buildEnv,
5   lib,
6   makeWrapper,
7   mpvScripts,
8   symlinkJoin,
9   writeTextDir,
10   yt-dlp,
11 # the unwrapped mpv derivation
14 let
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}";
102       # TODO: don't link all mpv outputs and convert package to mpv-unwrapped?
103       paths = [ mpv.all ];
105       nativeBuildInputs = [ makeWrapper ];
107       passthru.unwrapped = mpv;
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       };
125       postBuild =
126         ''
127           # wrapProgram can't operate on symlinks
128           rm "$out/bin/mpv"
129           makeWrapper "${mpv}/bin/mpv" "$out/bin/mpv" ${mostMakeWrapperArgs}
130           rm "$out/bin/umpv"
131           makeWrapper "${mpv}/bin/umpv" "$out/bin/umpv" ${umpvWrapperArgs}
132         ''
133         + lib.optionalString stdenv.hostPlatform.isDarwin ''
134           # wrapProgram can't operate on symlinks
135           rm "$out/Applications/mpv.app/Contents/MacOS/mpv"
136           makeWrapper "${mpv}/Applications/mpv.app/Contents/MacOS/mpv" "$out/Applications/mpv.app/Contents/MacOS/mpv" ${mostMakeWrapperArgs}
137         '';
139       meta = {
140         inherit (mpv.meta)
141           homepage
142           description
143           longDescription
144           maintainers
145           ;
146         mainProgram = "mpv";
147       };
148     };
150 lib.makeOverridable wrapper