Fixing mpv not playing my webcam on NixOS

Posted on Mar 17, 2023

[note: I am not an advanced NixOS user. If I’m making a mistake, let me know!]

I use this command line to open my webcam in mpv on Linux:

# note: $DEVICE is the webcam location. ex: /dev/video4
mpv --demuxer-lavf-format=video4linux2 --demuxer-lavf-o-set=input_format=mjpeg av://v4l2:"$DEVICE" --profile=low-latency --untimed --vf=hflip --no-keepaspect-window

This command line shows my webcam with very little delay.


On NixOS, this did not work. It had been working on Arch. I wasn’t sure what was different. I would see errors like unknown lavf format video4linux2 or unknown lavf format v4l2. This was frustrating.


The issue was that the version of ffmpeg used by mpv on NixOS did not support v4l2. When the binary was compiled, this codec (I think?) was not included. On NixOS, there are two different ffmpeg packages: ffmpeg_5 and ffmpeg_5-full. The ffmpeg_5-full package is the one we want. It supports v4l2.

But, installing ffmpeg_5-full isn’t going to do the job. We need to install mpv so that it is built with ffmpeg_5-full.


I needed to create a nix overlay for the mpv-unwrapped package.

Here is the overlay:

    (self: super: {
      mpv-unwrapped =
        super.mpv-unwrapped.override { ffmpeg_5 = super.ffmpeg_5-full; };

This overlay is taking the mpv-unwrapped package, and changing the ffmpeg package it is built with.

You can look here for how this is built: Frankly, I still find these hard to read.

Now, you need to make sure you are installing the mpv-unwrapped package, and not the mpv package. Don’t worry, the executable is called mpv either way.

I figured this out by stumbling upon this:


If there is a better way, or if this has helped you, please let me know. I’m on Twitter, or you can email me.

comments powered by Disqus