depot/third_party/nixpkgs/doc/languages-frameworks/qt.section.md
Default email 841d9c7fc1 Project import generated by Copybara.
GitOrigin-RevId: 3a8d7958a610cd3fec3a6f424480f91a1b259185
2021-06-29 00:13:55 +01:00

4.1 KiB

Qt

Writing Nix expressions for Qt libraries and applications is largely similar as for other C++ software. This section assumes some knowledge of the latter. There are two problems that the Nixpkgs Qt infrastructure addresses, which are not shared by other C++ software:

  1. There are usually multiple supported versions of Qt in Nixpkgs. All of a package's dependencies must be built with the same version of Qt. This is similar to the version constraints imposed on interpreted languages like Python.
  2. Qt makes extensive use of runtime dependency detection. Runtime dependencies are made into build dependencies through wrappers.

Nix expression for a Qt package (default.nix)

<programlisting>
{ stdenv, lib, qtbase, wrapQtAppsHook }: <co xml:id='qt-default-nix-co-1' />

stdenv.mkDerivation {
  pname = "myapp";
  version = "1.0";

  buildInputs = [ qtbase ];
  nativeBuildInputs = [ wrapQtAppsHook ]; <co xml:id='qt-default-nix-co-2' />
}
</programlisting>

 <calloutlist>
  <callout arearefs='qt-default-nix-co-1'>
   <para>
    Import Qt modules directly, that is: <literal>qtbase</literal>, <literal>qtdeclarative</literal>, etc.
    <emphasis>Do not</emphasis> import Qt package sets such as <literal>qt5</literal>
    because the Qt versions of dependencies may not be coherent, causing build and runtime failures.
   </para>
  </callout>
  <callout arearefs='qt-default-nix-co-2'>
    <para>
      All Qt packages must include <literal>wrapQtAppsHook</literal> in
      <literal>nativeBuildInputs</literal>, or you must explicitly set
      <literal>dontWrapQtApps</literal>.
    </para>
  </callout>
 </calloutlist>

Locating runtime dependencies

Qt applications must be wrapped to find runtime dependencies. Include wrapQtAppsHook in nativeBuildInputs:

{ stdenv, wrapQtAppsHook }:

stdenv.mkDerivation {
  # ...
  nativeBuildInputs = [ wrapQtAppsHook ];
}

Add entries to qtWrapperArgs are to modify the wrappers created by wrapQtAppsHook:

{ stdenv, wrapQtAppsHook }:

stdenv.mkDerivation {
  # ...
  nativeBuildInputs = [ wrapQtAppsHook ];
  qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
}

The entries are passed as arguments to wrapProgram.

Set dontWrapQtApps to stop applications from being wrapped automatically. Wrap programs manually with wrapQtApp, using the syntax of wrapProgram:

{ stdenv, lib, wrapQtAppsHook }:

stdenv.mkDerivation {
  # ...
  nativeBuildInputs = [ wrapQtAppsHook ];
  dontWrapQtApps = true;
  preFixup = ''
      wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
  '';
}

::: {.note} wrapQtAppsHook ignores files that are non-ELF executables. This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned. An example of when you'd always need to do this is with Python applications that use PyQt. :::

Adding a library to Nixpkgs

Add Qt libraries to qt5-packages.nix to make them available for every supported Qt version.

Example adding a Qt library

The following represents the contents of qt5-packages.nix.

{
  # ...

  mylib = callPackage ../path/to/mylib {};

  # ...
}

Libraries are built with every available version of Qt. Use the meta.broken attribute to disable the package for unsupported Qt versions:

{ stdenv, lib, qtbase }:

stdenv.mkDerivation {
  # ...
  # Disable this library with Qt < 5.9.0
  meta.broken = lib.versionOlder qtbase.version "5.9.0";
}

Adding an application to Nixpkgs

Add Qt applications to qt5-packages.nix. Add an alias to all-packages.nix to select the Qt 5 version used for the application.

Example adding a Qt application

The following represents the contents of qt5-packages.nix.

{
  # ...

  myapp = callPackage ../path/to/myapp {};

  # ...
}

The following represents the contents of all-packages.nix.

{
  # ...

  myapp = libsForQt5.myapp;

  # ...
}