@@ -4,71 +4,173 @@
44 <title>Qt</title>
55
66 <para>
7− Qt is a comprehensive desktop and mobile application development toolkit for
8− C++. Legacy support is available for Qt 3 and Qt 4, but all current
9− development uses Qt 5. The Qt 5 packages in Nixpkgs are updated frequently to
10− take advantage of new features, but older versions are typically retained
11− until their support window ends. The most important consideration in
12− packaging Qt-based software is ensuring that each package and all its
13− dependencies use the same version of Qt 5; this consideration motivates most
14− of the tools described below.
7+ This section describes the differences between Nix expressions for Qt
8+ libraries and applications and Nix expressions for other C++ software. Some
9+ knowledge of the latter is assumed. There are primarily two problems which
10+ the Qt infrastructure is designed to address: ensuring consistent versioning
11+ of all dependencies and finding dependencies at runtime.
1512 </para>
1613
17− <section xml:id="ssec-qt-libraries">
18− <title>Packaging Libraries for Nixpkgs</title>
19−
20− <para>
21− Whenever possible, libraries that use Qt 5 should be built with each
22− available version. Packages providing libraries should be added to the
23− top-level function <varname>mkLibsForQt5</varname>, which is used to build a
24− set of libraries for every Qt 5 version. A special
25− <varname>callPackage</varname> function is used in this scope to ensure that
26− the entire dependency tree uses the same Qt 5 version. Import dependencies
27− unqualified, i.e., <literal>qtbase</literal> not
28− <literal>qt5.qtbase</literal>. <emphasis>Do not</emphasis> import a package
29− set such as <literal>qt5</literal> or <literal>libsForQt5</literal>.
30− </para>
31−
32− <para>
33− If a library does not support a particular version of Qt 5, it is best to
34− mark it as broken by setting its <literal>meta.broken</literal> attribute. A
35− package may be marked broken for certain versions by testing the
36− <literal>qtbase.version</literal> attribute, which will always give the
37− current Qt 5 version.
38− </para>
39− </section>
40−
41− <section xml:id="ssec-qt-applications">
42− <title>Packaging Applications for Nixpkgs</title>
43−
44− <para>
45− Call your application expression using
46− <literal>libsForQt5.callPackage</literal> instead of
47− <literal>callPackage</literal>. Import dependencies unqualified, i.e.,
48− <literal>qtbase</literal> not <literal>qt5.qtbase</literal>. <emphasis>Do
49− not</emphasis> import a package set such as <literal>qt5</literal> or
50− <literal>libsForQt5</literal>.
51− </para>
52−
53− <para>
54− Qt 5 maintains strict backward compatibility, so it is generally best to
55− build an application package against the latest version using the
56− <varname>libsForQt5</varname> library set. In case a package does not build
57− with the latest Qt version, it is possible to pick a set pinned to a
58− particular version, e.g. <varname>libsForQt55</varname> for Qt 5.5, if that
59− is the latest version the package supports. If a package must be pinned to
60− an older Qt version, be sure to file a bug upstream; because Qt is strictly
61− backwards-compatible, any incompatibility is by definition a bug in the
62− application.
63− </para>
64−
65− <para>
66− When testing applications in Nixpkgs, it is a common practice to build the
67− package with <literal>nix-build</literal> and run it using the created
68− symbolic link. This will not work with Qt applications, however, because
69− they have many hard runtime requirements that can only be guaranteed if the
70− package is actually installed. To test a Qt application, install it with
71− <literal>nix-env</literal> or run it inside <literal>nix-shell</literal>.
72− </para>
73− </section>
14+ <example xml:id='qt-default-nix'>
15+ <title>Nix expression for a Qt package (<filename>default.nix</filename>)</title>
16+ <programlisting>
17+{ mkDerivation, lib, qtbase }: <co xml:id='qt-default-nix-co-1' />
18+
19+mkDerivation { <co xml:id='qt-default-nix-co-2' />
20+ pname = "myapp";
21+ version = "1.0";
22+
23+ buildInputs = [ qtbase ]; <co xml:id='qt-default-nix-co-3' />
24+}
25+ </programlisting>
26+ </example>
27+
28+ <calloutlist>
29+ <callout arearefs='qt-default-nix-co-1'>
30+ <para>
31+ Import <literal>mkDerivation</literal> and Qt (such as
32+ <literal>qtbase</literal> modules directly. <emphasis>Do not</emphasis>
33+ import Qt package sets; the Qt versions of dependencies may not be
34+ coherent, causing build and runtime failures.
35+ </para>
36+ </callout>
37+ <callout arearefs='qt-default-nix-co-2'>
38+ <para>
39+ Use <literal>mkDerivation</literal> instead of
40+ <literal>stdenv.mkDerivation</literal>. <literal>mkDerivation</literal>
41+ is a wrapper around <literal>stdenv.mkDerivation</literal> which
42+ applies some Qt-specific settings.
43+ This deriver accepts the same arguments as
44+ <literal>stdenv.mkDerivation</literal>; refer to
45+ <xref linkend='chap-stdenv' /> for details.
46+ </para>
47+ <para>
48+ To use another deriver instead of
49+ <literal>stdenv.mkDerivation</literal>, use
50+ <literal>mkDerivationWith</literal>:
51+<programlisting>
52+mkDerivationWith myDeriver {
53+ # ...
54+}
55+</programlisting>
56+ If you cannot use <literal>mkDerivationWith</literal>, please refer to
57+ <xref linkend='qt-runtime-dependencies' />.
58+ </para>
59+ </callout>
60+ <callout arearefs='qt-default-nix-co-3'>
61+ <para>
62+ <literal>mkDerivation</literal> accepts the same arguments as
63+ <literal>stdenv.mkDerivation</literal>, such as
64+ <literal>buildInputs</literal>.
65+ </para>
66+ </callout>
67+ </calloutlist>
68+
69+ <formalpara xml:id='qt-runtime-dependencies'>
70+ <title>Locating runtime dependencies</title>
71+ <para>
72+ Qt applications need to be wrapped to find runtime dependencies. If you
73+ cannot use <literal>mkDerivation</literal> or
74+ <literal>mkDerivationWith</literal> above, include
75+ <literal>wrapQtAppsHook</literal> in <literal>nativeBuildInputs</literal>:
76+<programlisting>
77+stdenv.mkDerivation {
78+ # ...
79+
80+ nativeBuildInputs = [ wrapQtAppsHook ];
81+}
82+</programlisting>
83+ </para>
84+ </formalpara>
85+
86+ <para>
87+ Entries added to <literal>qtWrapperArgs</literal> are used to modify the
88+ wrappers created by <literal>wrapQtAppsHook</literal>. The entries are
89+ passed as arguments to <xref linkend='fun-wrapProgram' />.
90+<programlisting>
91+mkDerivation {
92+ # ...
93+
94+ qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
95+}
96+</programlisting>
97+ </para>
98+
99+ <para>
100+ Set <literal>dontWrapQtApps</literal> to stop applications from being
101+ wrapped automatically. It is required to wrap applications manually with
102+ <literal>wrapQtApp</literal>, using the syntax of
103+ <xref linkend='fun-wrapProgram' />:
104+<programlisting>
105+mkDerivation {
106+ # ...
107+
108+ dontWrapQtApps = true;
109+ preFixup = ''
110+ wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
111+ '';
112+}
113+</programlisting>
114+ </para>
115+
116+ <para>
117+ Libraries are built with every available version of Qt. Use the <literal>meta.broken</literal>
118+ attribute to disable the package for unsupported Qt versions:
119+<programlisting>
120+mkDerivation {
121+ # ...
122+
123+ # Disable this library with Qt < 5.9.0
124+ meta.broken = builtins.compareVersions qtbase.version "5.9.0" < 0;
125+}
126+</programlisting>
127+ </para>
128+
129+ <formalpara>
130+ <title>Adding a library to Nixpkgs</title>
131+ <para>
132+ Add a Qt library to <filename>all-packages.nix</filename> by adding it to the
133+ collection inside <literal>mkLibsForQt5</literal>. This ensures that the
134+ library is built with every available version of Qt as needed.
135+ <example xml:id='qt-library-all-packages-nix'>
136+ <title>Adding a Qt library to <filename>all-packages.nix</filename></title>
137+<programlisting>
138+{
139+ # ...
140+
141+ mkLibsForQt5 = self: with self; {
142+ # ...
143+
144+ mylib = callPackage ../path/to/mylib {};
145+ };
146+
147+ # ...
148+}
149+</programlisting>
150+ </example>
151+ </para>
152+ </formalpara>
153+
154+ <formalpara>
155+ <title>Adding an application to Nixpkgs</title>
156+ <para>
157+ Add a Qt application to <filename>all-packages.nix</filename> using
158+ <literal>libsForQt5.callPackage</literal> instead of the usual
159+ <literal>callPackage</literal>. The former ensures that all dependencies
160+ are built with the same version of Qt.
161+ <example xml:id='qt-application-all-packages-nix'>
162+ <title>Adding a Qt application to <filename>all-packages.nix</filename></title>
163+<programlisting>
164+{
165+ # ...
166+
167+ myapp = libsForQt5.callPackage ../path/to/myapp/ {};
168+
169+ # ...
170+}
171+</programlisting>
172+ </example>
173+ </para>
174+ </formalpara>
175+
74176</section>