Rust program broken on Nixos but not other distro

I’m trying to package Av1an a rust project for Nix.
The same commit builds and runs without issue on Archlinux, but on Nixos, with buildRustPackage, the build completes but the resulting binary crashes with a SegFault immediately when run. Normally I’d just report this upstream, but since it works on Archlinux, that leads me to believe this is a build/linker environment issue with Nix.

First, since I’m a Nix newbie, please check the following derivation (I’m probably missing something, but I’ve checked the nixpkgs docs 5 times over and can’t find it).

{ lib
, rustPlatform
, fetchFromGitHub
, ffmpeg
, ffms
, libaom
, libvmaf
, llvmPackages
, l-smash
, nasm
, pkg-config
, svt-av1
, rav1e
, vapoursynth
}:

rustPlatform.buildRustPackage rec {
  pname = "av1an";
  version = "git";

  src = fetchFromGitHub {
    owner = "master-of-zen";
    repo = pname;
    rev = "9da59ad245e7f111365a2a14b92854b2a5b11c64";
    hash = "sha256-vhDLbJvT4F73ky8YPg7HSDE0lmBeP24Tat86uNj1A5c=";
  };

  cargoHash = "sha256-TKu3TnTsw3QFrScy8RC7ShKRtBJBDF4HpaIQz/H4cJU=";

  nativeBuildInputs = [
    nasm
    pkg-config
    rustPlatform.bindgenHook
    llvmPackages.clang
  ];

  buildInputs = [
    ffmpeg
    ffms
    libaom
    libvmaf
    l-smash
    svt-av1
    rav1e
    vapoursynth
  ];

  buildType = "debug";
  dontStrip = true;
  doCheck = false;

  meta = with lib; {
    description = "Cross-platform command-line AV1 / VP9 / HEVC / H264 encoding framework with per scene quality encoding";
    homepage = "https://github.com/master-of-zen/av1an";
    license = licenses.gpl3Plus;
  };
}

Running the program without arguments normally should just print the help message and exit.
Running gdb on it shows:

(gdb) run
Starting program: /nix/store/dnwap73wx1c88acah9did14mkmn55vka-av1an-dev/bin/av1an 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()

I’d recommend creating a draft PR on GitHub - NixOS/nixpkgs: Nix Packages collection & NixOS .
You get:

  • automated testing from PR bots
  • increased visibility for other people looking to use or package that application, so better collaboration possibilities.
  • easier to collaborate due to github UI
  • easier to collaborate because anyone can just point their nix-shell to that PR to check it locally.

BTW if you set doCheck = false; add a comment explaining why you had to do that.

Did you ever figure out a solution to this? I just spent several hours on this exact same problem lol. Running valgrind on it, it’s clear that it’s a plugin linking issue:

==1011250== Process terminating with default action of signal 11 (SIGSEGV)
==1011250==  Bad permissions for mapped region at address 0x0
==1011250==    at 0x0: ???
==1011250==    by 0x49E2CA2: vsscript_createScript (in /nix/store/hfaaz0gkvk4bs439qkphqrq3n0n3wcy7-vapoursynth-63-with-plugins/lib/libvapoursynth-script.so.0.0.0)
==1011250==    by 0x3B8742: vapoursynth::vsscript::environment::Environment::new (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x1AC99E: core::ops::function::FnOnce::call_once (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x211D69: once_cell::imp::OnceCell<T>::initialize::{{closure}} (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x32282E: once_cell::imp::initialize_or_wait (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x141FDB: once_cell::imp::OnceCell<T>::initialize (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x1AD7AE: core::ops::function::FnOnce::call_once (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x211EF7: once_cell::imp::OnceCell<T>::initialize::{{closure}} (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x32282E: once_cell::imp::initialize_or_wait (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x141DB1: once_cell::imp::OnceCell<T>::initialize (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)
==1011250==    by 0x173E07: once_cell::imp::OnceCell<T>::initialize::{{closure}} (in /nix/store/m1h65hs7gs73hsxd583zi0pybfcw257n-Av1an-latest/bin/av1an)

I tried using vapoursynth.withPlugins to include plugins, but they don’t appear to be linked when I check ldd :frowning:

I’m having same problem too. After looking up av1an’s issues, I found out that it requires python vapoursynth to be installed at runtime. Check this issue: Immediate crash in vapoursynth for all inputs on Linux · Issue #563 · master-of-zen/Av1an (github.com)

When packaging on nixos, you need to use makeWrapper to put the PYTHONPATH environment variable in the runtime environment.

Here is my derivation:

{
  lib,
  fetchFromGitHub,
  rustPlatform,
  vapoursynth,
  ffmpeg,
  x264,
  libaom,
  rav1e,
  nasm,
  pkg-config,
  python3,
  python3Packages,
  makeWrapper,
}:
let
  pname = "av1an";
  version = "0.4.1";

  sha256 = "0sqq0wwmxyrs6dy5i16qzf3fhsgwnbyq4kl81hvsjr2h6r8811ix";

  cargoHash = "sha256-mfI6pMQaWkXCjcjtgUhm6K0UeGSAZ5QJXsrmmFEhoFM=";
in
rustPlatform.buildRustPackage {
  inherit pname version cargoHash;

  src = fetchFromGitHub {
    owner = "master-of-zen";
    repo = pname;
    rev = version;
    inherit sha256;
  };

  nativeBuildInputs = [
    pkg-config
    nasm
    makeWrapper
    rustPlatform.bindgenHook
  ];

  nativeCheckInputs = [
    libaom
    rav1e
  ];

  buildInputs = [
    ffmpeg
    x264
    vapoursynth
    python3Packages.vapoursynth
  ];

  postInstall = ''
    wrapProgram $out/bin/av1an \
      --prefix PYTHONPATH : ${python3.pkgs.makePythonPath [ python3Packages.vapoursynth ]}
  '';

  meta = with lib; {
    description = "Cross-platform command-line AV1 / VP9 / HEVC / H264 encoding framework with per scene quality encoding";
    homepage = "https://github.com/master-of-zen/av1an";
    license = licenses.gpl3;
    maintainers = [];
  };
}
2 Likes

Thanks!

PYTHONPATH in wrapProgram is what I was missing.