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. 5 6The major caveat with Qt applications is that Qt uses a plugin system to load additional modules at runtime, 7from a list of well-known locations. In Nixpkgs, we patch QtCore to instead use an environment variable, 8and wrap Qt applications to set it to the right paths. This effectively makes the runtime dependencies 9pure and explicit at build-time, at the cost of introducing an extra indirection. 10 11## Nix expression for a Qt package (default.nix) {#qt-default-nix} 12 13```{=docbook} 14<programlisting> 15{ stdenv, lib, qtbase, wrapQtAppsHook }: <co xml:id='qt-default-nix-co-1' /> 16 17stdenv.mkDerivation { 18 pname = "myapp"; 19 version = "1.0"; 20 21 buildInputs = [ qtbase ]; 22 nativeBuildInputs = [ wrapQtAppsHook ]; <co xml:id='qt-default-nix-co-2' /> 23} 24</programlisting> 25 26 <calloutlist> 27 <callout arearefs='qt-default-nix-co-1'> 28 <para> 29 Import Qt modules directly, that is: <literal>qtbase</literal>, <literal>qtdeclarative</literal>, etc. 30 <emphasis>Do not</emphasis> import Qt package sets such as <literal>qt5</literal> 31 because the Qt versions of dependencies may not be coherent, causing build and runtime failures. 32 </para> 33 </callout> 34 <callout arearefs='qt-default-nix-co-2'> 35 <para> 36 All Qt packages must include <literal>wrapQtAppsHook</literal> in 37 <literal>nativeBuildInputs</literal>, or you must explicitly set 38 <literal>dontWrapQtApps</literal>. 39 </para> 40 </callout> 41 </calloutlist> 42``` 43 44## Locating runtime dependencies {#qt-runtime-dependencies} 45 46Qt applications must be wrapped to find runtime dependencies. 47Include `wrapQtAppsHook` in `nativeBuildInputs`: 48 49```nix 50{ stdenv, wrapQtAppsHook }: 51 52stdenv.mkDerivation { 53 # ... 54 nativeBuildInputs = [ wrapQtAppsHook ]; 55} 56``` 57 58Add entries to `qtWrapperArgs` are to modify the wrappers created by 59`wrapQtAppsHook`: 60 61```nix 62{ stdenv, wrapQtAppsHook }: 63 64stdenv.mkDerivation { 65 # ... 66 nativeBuildInputs = [ wrapQtAppsHook ]; 67 qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ]; 68} 69``` 70 71The entries are passed as arguments to [wrapProgram](#fun-wrapProgram). 72 73Set `dontWrapQtApps` to stop applications from being wrapped automatically. 74Wrap programs manually with `wrapQtApp`, using the syntax of 75[wrapProgram](#fun-wrapProgram): 76 77```nix 78{ stdenv, lib, wrapQtAppsHook }: 79 80stdenv.mkDerivation { 81 # ... 82 nativeBuildInputs = [ wrapQtAppsHook ]; 83 dontWrapQtApps = true; 84 preFixup = '' 85 wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin 86 ''; 87} 88``` 89 90::: {.note} 91`wrapQtAppsHook` ignores files that are non-ELF executables. 92This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned. 93An example of when you'd always need to do this is with Python applications that use PyQt. 94:::