about summary refs log tree commit diff
path: root/pkgs/applications/video/mpv/default.nix
blob: 965baf2abb9615cee6d5c8fc2c0d92239c232222 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
  lib,
  SDL2,
  addOpenGLRunpath,
  alsa-lib,
  bash,
  buildPackages,
  callPackage,
  config,
  darwin,
  docutils,
  fetchFromGitHub,
  ffmpeg,
  freefont_ttf,
  freetype,
  lcms2,
  libGL,
  libGLU,
  libX11,
  libXScrnSaver,
  libXext,
  libXinerama,
  libXpresent,
  libXrandr,
  libXv,
  libXxf86vm,
  libarchive,
  libass,
  libbluray,
  libbs2b,
  libcaca,
  libcdio,
  libcdio-paranoia,
  libdrm,
  libdvdnav,
  libiconv,
  libjack2,
  libplacebo,
  libpng,
  libpthreadstubs,
  libpulseaudio,
  libsixel,
  libtheora,
  libuchardet,
  libva,
  libvdpau,
  libxkbcommon,
  lua,
  mesa,
  meson,
  mujs,
  ninja,
  nixosTests,
  nv-codec-headers-11,
  openalSoft,
  pipewire,
  pkg-config,
  python3,
  rubberband,
  shaderc, # instead of spirv-cross
  speex,
  stdenv,
  swift,
  testers,
  vapoursynth,
  vulkan-headers,
  vulkan-loader,
  wayland,
  wayland-protocols,
  wayland-scanner,
  xcbuild,
  zimg,

  # Boolean
  alsaSupport ? stdenv.isLinux,
  archiveSupport ? true,
  bluraySupport ? true,
  bs2bSupport ? true,
  cacaSupport ? true,
  cddaSupport ? false,
  cmsSupport ? true,
  drmSupport ? stdenv.isLinux,
  dvbinSupport ? stdenv.isLinux,
  dvdnavSupport ? stdenv.isLinux,
  jackaudioSupport ? false,
  javascriptSupport ? true,
  libpngSupport ? true,
  openalSupport ? true,
  pipewireSupport ? stdenv.isLinux,
  pulseSupport ? config.pulseaudio or stdenv.isLinux,
  rubberbandSupport ? true,
  screenSaverSupport ? true,
  sdl2Support ? !stdenv.isDarwin,
  sixelSupport ? false,
  speexSupport ? true,
  swiftSupport ? stdenv.isDarwin,
  theoraSupport ? true,
  vaapiSupport ? x11Support || waylandSupport,
  vapoursynthSupport ? false,
  vdpauSupport ? true,
  vulkanSupport ? stdenv.isLinux,
  waylandSupport ? stdenv.isLinux,
  x11Support ? stdenv.isLinux,
  xineramaSupport ? stdenv.isLinux,
  xvSupport ? stdenv.isLinux,
  zimgSupport ? true,
}:

let
  inherit (darwin.apple_sdk_11_0.frameworks)
    AVFoundation
    Accelerate
    Cocoa
    CoreAudio
    CoreFoundation
    CoreMedia
    MediaPlayer
    VideoToolbox
    ;
  luaEnv = lua.withPackages (ps: with ps; [ luasocket ]);

  overrideSDK =
    platform: version:
    platform // lib.optionalAttrs (platform ? darwinMinVersion) { darwinMinVersion = version; };

  stdenv' =
    if swiftSupport && stdenv.isDarwin && stdenv.isx86_64 then
      stdenv.override (old: {
        buildPlatform = overrideSDK old.buildPlatform "11.0";
        hostPlatform = overrideSDK old.hostPlatform "11.0";
        targetPlatform = overrideSDK old.targetPlatform "11.0";
      })
    else
      stdenv;
in
stdenv'.mkDerivation (finalAttrs: {
  pname = "mpv";
  version = "0.38.0";

  outputs = [
    "out"
    "dev"
    "doc"
    "man"
  ];

  src = fetchFromGitHub {
    owner = "mpv-player";
    repo = "mpv";
    rev = "v${finalAttrs.version}";
    hash = "sha256-dFajnCpGlNqUv33A8eFEn8kjtzIPkcBY5j0gNVlaiIY=";
  };

  patches = [
    # Fix build with Darwin SDK 11
    ./0001-fix-darwin-build.patch
  ];

  postPatch = lib.concatStringsSep "\n" [
    # Don't reference compile time dependencies or create a build outputs cycle
    # between out and dev
    ''
      substituteInPlace meson.build \
        --replace-fail "conf_data.set_quoted('CONFIGURATION', configuration)" \
                       "conf_data.set_quoted('CONFIGURATION', '<ommited>')"
    ''
    # A trick to patchShebang everything except mpv_identify.sh
    ''
      pushd TOOLS
      mv mpv_identify.sh mpv_identify
      patchShebangs *.py *.sh
      mv mpv_identify mpv_identify.sh
      popd
    ''
  ];

  # Ensure we reference 'lib' (not 'out') of Swift.
  preConfigure = lib.optionalString swiftSupport ''
    export SWIFT_LIB_DYNAMIC="${lib.getLib swift.swift}/lib/swift/macosx"
  '';

  mesonFlags =
    [
      (lib.mesonOption "default_library" "shared")
      (lib.mesonBool "libmpv" true)
      (lib.mesonEnable "libarchive" archiveSupport)
      (lib.mesonEnable "manpage-build" true)
      (lib.mesonEnable "cdda" cddaSupport)
      (lib.mesonEnable "dvbin" dvbinSupport)
      (lib.mesonEnable "dvdnav" dvdnavSupport)
      (lib.mesonEnable "openal" openalSupport)
      (lib.mesonEnable "sdl2" sdl2Support)
      # Disable whilst Swift isn't supported
      (lib.mesonEnable "swift-build" swiftSupport)
      (lib.mesonEnable "macos-cocoa-cb" swiftSupport)
    ]
    ++ lib.optionals stdenv.isDarwin [
      # Toggle explicitly because it fails on darwin
      (lib.mesonEnable "videotoolbox-pl" vulkanSupport)
    ];

  mesonAutoFeatures = "auto";

  nativeBuildInputs =
    [
      addOpenGLRunpath
      docutils # for rst2man
      meson
      ninja
      pkg-config
    ]
    ++ lib.optionals stdenv.isDarwin [
      buildPackages.darwin.sigtool
      xcbuild.xcrun
    ]
    ++ lib.optionals swiftSupport [ swift ]
    ++ lib.optionals waylandSupport [ wayland-scanner ];

  buildInputs =
    [
      bash
      ffmpeg
      freetype
      libass
      libplacebo
      libpthreadstubs
      libuchardet
      luaEnv
      python3
    ]
    ++ lib.optionals alsaSupport [ alsa-lib ]
    ++ lib.optionals archiveSupport [ libarchive ]
    ++ lib.optionals bluraySupport [ libbluray ]
    ++ lib.optionals bs2bSupport [ libbs2b ]
    ++ lib.optionals cacaSupport [ libcaca ]
    ++ lib.optionals cddaSupport [
      libcdio
      libcdio-paranoia
    ]
    ++ lib.optionals cmsSupport [ lcms2 ]
    ++ lib.optionals drmSupport [
      libdrm
      mesa
    ]
    ++ lib.optionals dvdnavSupport [
      libdvdnav
      libdvdnav.libdvdread
    ]
    ++ lib.optionals jackaudioSupport [ libjack2 ]
    ++ lib.optionals javascriptSupport [ mujs ]
    ++ lib.optionals libpngSupport [ libpng ]
    ++ lib.optionals openalSupport [ openalSoft ]
    ++ lib.optionals pipewireSupport [ pipewire ]
    ++ lib.optionals pulseSupport [ libpulseaudio ]
    ++ lib.optionals rubberbandSupport [ rubberband ]
    ++ lib.optionals screenSaverSupport [ libXScrnSaver ]
    ++ lib.optionals sdl2Support [ SDL2 ]
    ++ lib.optionals sixelSupport [ libsixel ]
    ++ lib.optionals speexSupport [ speex ]
    ++ lib.optionals theoraSupport [ libtheora ]
    ++ lib.optionals vaapiSupport [ libva ]
    ++ lib.optionals vapoursynthSupport [ vapoursynth ]
    ++ lib.optionals vdpauSupport [ libvdpau ]
    ++ lib.optionals vulkanSupport [
      shaderc
      vulkan-headers
      vulkan-loader
    ]
    ++ lib.optionals waylandSupport [
      wayland
      wayland-protocols
      libxkbcommon
    ]
    ++ lib.optionals x11Support [
      libX11
      libXext
      libGLU
      libGL
      libXxf86vm
      libXrandr
      libXpresent
    ]
    ++ lib.optionals xineramaSupport [ libXinerama ]
    ++ lib.optionals xvSupport [ libXv ]
    ++ lib.optionals zimgSupport [ zimg ]
    ++ lib.optionals stdenv.isLinux [ nv-codec-headers-11 ]
    ++ lib.optionals stdenv.isDarwin [ libiconv ]
    ++ lib.optionals stdenv.isDarwin [
      Accelerate
      CoreFoundation
      Cocoa
      CoreAudio
      MediaPlayer
      VideoToolbox
    ]
    ++ lib.optionals (stdenv.isDarwin && swiftSupport) [
      AVFoundation
      CoreMedia
    ];

  postBuild = lib.optionalString stdenv.isDarwin ''
    pushd .. # Must be run from the source dir because it uses relative paths
    python3 TOOLS/osxbundle.py -s build/mpv
    popd
  '';

  postInstall =
    ''
      # Use a standard font
      mkdir -p $out/share/mpv
      ln -s ${freefont_ttf}/share/fonts/truetype/FreeSans.ttf $out/share/mpv/subfont.ttf

      pushd ../TOOLS
      cp mpv_identify.sh umpv $out/bin/
      popd
      pushd $out/share/applications

      # patch out smb protocol reference, since our ffmpeg can't handle it
      substituteInPlace mpv.desktop --replace-fail "smb," ""

      sed -e '/Icon=/ ! s|mpv|umpv|g; s|^Exec=.*|Exec=umpv %U|' \
        mpv.desktop > umpv.desktop
      printf "NoDisplay=true\n" >> umpv.desktop
      popd
    ''
    + lib.optionalString stdenv.isDarwin ''
      mkdir -p $out/Applications
      cp -r mpv.app $out/Applications
    '';

  # Set RUNPATH so that libcuda in /run/opengl-driver(-32)/lib can be found.
  # See the explanation in addOpenGLRunpath.
  postFixup = lib.optionalString stdenv.isLinux ''
    addOpenGLRunpath $out/bin/mpv
    patchShebangs --update --host $out/bin/umpv $out/bin/mpv_identify.sh
  '';

  passthru = {
    inherit
      # The wrapper consults luaEnv and lua.version
      luaEnv
      lua
      # In the wrapper, we want to reference vapoursynth which has the `python3`
      # passthru attribute (which has the `sitePrefix` attribute). This way we'll
      # be sure that in the wrapper we'll use the same python3.sitePrefix used to
      # build vapoursynth.
      vapoursynthSupport
      vapoursynth
      ;

    wrapper = callPackage ./wrapper.nix { };
    scripts = callPackage ./scripts { };

    tests = {
      inherit (nixosTests) mpv;

      version = testers.testVersion { package = finalAttrs.finalPackage; };
      pkg-config = testers.hasPkgConfigModules {
        package = finalAttrs.finalPackage;
        moduleNames = [ "mpv" ];
      };
    };
  };

  meta = {
    homepage = "https://mpv.io";
    description = "General-purpose media player, fork of MPlayer and mplayer2";
    longDescription = ''
      mpv is a free and open-source general-purpose video player, based on the
      MPlayer and mplayer2 projects, with great improvements above both.
    '';
    changelog = "https://github.com/mpv-player/mpv/releases/tag/v${finalAttrs.version}";
    license = lib.licenses.gpl2Plus;
    mainProgram = "mpv";
    maintainers = with lib.maintainers; [
      AndersonTorres
      fpletz
      globin
      ma27
    ];
    platforms = lib.platforms.unix;
  };
})