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```nix 14{ stdenv, lib, qtbase, wrapQtAppsHook }: 15 16stdenv.mkDerivation { 17 pname = "myapp"; 18 version = "1.0"; 19 20 buildInputs = [ qtbase ]; 21 nativeBuildInputs = [ wrapQtAppsHook ]; 22} 23``` 24 25It is important to import Qt modules directly, that is: `qtbase`, `qtdeclarative`, etc. *Do not* import Qt package sets such as `qt5` because the Qt versions of dependencies may not be coherent, causing build and runtime failures. 26 27Additionally all Qt packages must include `wrapQtAppsHook` in `nativeBuildInputs`, or you must explicitly set `dontWrapQtApps`. 28 29## Locating runtime dependencies {#qt-runtime-dependencies} 30 31Qt applications must be wrapped to find runtime dependencies. 32Include `wrapQtAppsHook` in `nativeBuildInputs`: 33 34```nix 35{ stdenv, wrapQtAppsHook }: 36 37stdenv.mkDerivation { 38 # ... 39 nativeBuildInputs = [ wrapQtAppsHook ]; 40} 41``` 42 43Add entries to `qtWrapperArgs` are to modify the wrappers created by 44`wrapQtAppsHook`: 45 46```nix 47{ stdenv, wrapQtAppsHook }: 48 49stdenv.mkDerivation { 50 # ... 51 nativeBuildInputs = [ wrapQtAppsHook ]; 52 qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ]; 53} 54``` 55 56The entries are passed as arguments to [wrapProgram](#fun-wrapProgram). 57 58Set `dontWrapQtApps` to stop applications from being wrapped automatically. 59Wrap programs manually with `wrapQtApp`, using the syntax of 60[wrapProgram](#fun-wrapProgram): 61 62```nix 63{ stdenv, lib, wrapQtAppsHook }: 64 65stdenv.mkDerivation { 66 # ... 67 nativeBuildInputs = [ wrapQtAppsHook ]; 68 dontWrapQtApps = true; 69 preFixup = '' 70 wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin 71 ''; 72} 73``` 74 75::: {.note} 76`wrapQtAppsHook` ignores files that are non-ELF executables. 77This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned. 78An example of when you'd always need to do this is with Python applications that use PyQt. 79:::