1# Qt {#sec-language-qt}
2
3Writing Nix expressions for Qt libraries and applications is largely similar as for other C++ software.
4This section assumes some knowledge of the latter.
5There are two problems that the Nixpkgs Qt infrastructure addresses,
6which are not shared by other C++ software:
7
81. There are usually multiple supported versions of Qt in Nixpkgs.
9 All of a package's dependencies must be built with the same version of Qt.
10 This is similar to the version constraints imposed on interpreted languages like Python.
112. Qt makes extensive use of runtime dependency detection.
12 Runtime dependencies are made into build dependencies through wrappers.
13
14## Nix expression for a Qt package (default.nix) {#qt-default-nix}
15
16```{=docbook}
17<programlisting>
18{ stdenv, lib, qtbase, wrapQtAppsHook }: <co xml:id='qt-default-nix-co-1' />
19
20stdenv.mkDerivation {
21 pname = "myapp";
22 version = "1.0";
23
24 buildInputs = [ qtbase ];
25 nativeBuildInputs = [ wrapQtAppsHook ]; <co xml:id='qt-default-nix-co-2' />
26}
27</programlisting>
28
29 <calloutlist>
30 <callout arearefs='qt-default-nix-co-1'>
31 <para>
32 Import Qt modules directly, that is: <literal>qtbase</literal>, <literal>qtdeclarative</literal>, etc.
33 <emphasis>Do not</emphasis> import Qt package sets such as <literal>qt5</literal>
34 because the Qt versions of dependencies may not be coherent, causing build and runtime failures.
35 </para>
36 </callout>
37 <callout arearefs='qt-default-nix-co-2'>
38 <para>
39 All Qt packages must include <literal>wrapQtAppsHook</literal> in
40 <literal>nativeBuildInputs</literal>, or you must explicitly set
41 <literal>dontWrapQtApps</literal>.
42 </para>
43 </callout>
44 </calloutlist>
45```
46
47## Locating runtime dependencies {#qt-runtime-dependencies}
48
49Qt applications must be wrapped to find runtime dependencies.
50Include `wrapQtAppsHook` in `nativeBuildInputs`:
51
52```nix
53{ stdenv, wrapQtAppsHook }:
54
55stdenv.mkDerivation {
56 # ...
57 nativeBuildInputs = [ wrapQtAppsHook ];
58}
59```
60
61Add entries to `qtWrapperArgs` are to modify the wrappers created by
62`wrapQtAppsHook`:
63
64```nix
65{ stdenv, wrapQtAppsHook }:
66
67stdenv.mkDerivation {
68 # ...
69 nativeBuildInputs = [ wrapQtAppsHook ];
70 qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
71}
72```
73
74The entries are passed as arguments to [wrapProgram](#fun-wrapProgram).
75
76Set `dontWrapQtApps` to stop applications from being wrapped automatically.
77Wrap programs manually with `wrapQtApp`, using the syntax of
78[wrapProgram](#fun-wrapProgram):
79
80```nix
81{ stdenv, lib, wrapQtAppsHook }:
82
83stdenv.mkDerivation {
84 # ...
85 nativeBuildInputs = [ wrapQtAppsHook ];
86 dontWrapQtApps = true;
87 preFixup = ''
88 wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
89 '';
90}
91```
92
93::: note
94`wrapQtAppsHook` ignores files that are non-ELF executables.
95This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned.
96An example of when you'd always need to do this is with Python applications that use PyQt.
97:::
98
99## Adding a library to Nixpkgs
100Add Qt libraries to `qt5-packages.nix` to make them available for every
101supported Qt version.
102
103### Example adding a Qt library {#qt-library-all-packages-nix}
104
105The following represents the contents of `qt5-packages.nix`.
106```nix
107{
108 # ...
109
110 mylib = callPackage ../path/to/mylib {};
111
112 # ...
113}
114```
115
116Libraries are built with every available version of Qt.
117Use the `meta.broken` attribute to disable the package for unsupported Qt versions:
118
119```nix
120{ stdenv, lib, qtbase }:
121
122stdenv.mkDerivation {
123 # ...
124 # Disable this library with Qt < 5.9.0
125 meta.broken = lib.versionOlder qtbase.version "5.9.0";
126}
127```
128
129## Adding an application to Nixpkgs
130Add Qt applications to `qt5-packages.nix`. Add an alias to `all-packages.nix`
131to select the Qt 5 version used for the application.
132
133### Example adding a Qt application {#qt-application-all-packages-nix}
134
135The following represents the contents of `qt5-packages.nix`.
136```nix
137{
138 # ...
139
140 myapp = callPackage ../path/to/myapp {};
141
142 # ...
143}
144```
145
146The following represents the contents of `all-packages.nix`.
147```nix
148{
149 # ...
150
151 myapp = libsForQt5.myapp;
152
153 # ...
154}
155```