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. 7In Nixpkgs, we wrap Qt applications to inject environment variables telling Qt where to discover the required plugins and QML modules. 8 9This effectively makes the runtime dependencies pure and explicit at build-time, at the cost of introducing 10an extra indirection. 11 12## Nix expression for a Qt package (default.nix) {#qt-default-nix} 13 14```nix 15{ stdenv, qt6 }: 16 17stdenv.mkDerivation { 18 pname = "myapp"; 19 version = "1.0"; 20 21 buildInputs = [ qt6.qtbase ]; 22 nativeBuildInputs = [ qt6.wrapQtAppsHook ]; 23} 24``` 25 26The same goes for Qt 5 where libraries and tools are under `libsForQt5`. 27 28Any Qt package should include `wrapQtAppsHook` in `nativeBuildInputs`, or explicitly set `dontWrapQtApps` to bypass generating the wrappers. 29 30::: {.note} 31Qt 6 graphical applications should also include `qtwayland` in `buildInputs` on Linux (but not on platforms e.g. Darwin, where `qtwayland` is not available), to ensure the Wayland platform plugin is available. 32 33This may become default in the future, see [NixOS/nixpkgs#269674](https://github.com/NixOS/nixpkgs/pull/269674). 34::: 35 36## Packages supporting multiple Qt versions {#qt-versions} 37 38If your package is a library that can be built with multiple Qt versions, you may want to take Qt modules as separate arguments (`qtbase`, `qtdeclarative` etc.), and invoke the package from `pkgs/top-level/qt5-packages.nix` or `pkgs/top-level/qt6-packages.nix` using the respective `callPackage` functions. 39 40Applications should generally be built with upstream's preferred Qt version. 41 42## Locating additional runtime dependencies {#qt-runtime-dependencies} 43 44Add entries to `qtWrapperArgs` are to modify the wrappers created by 45`wrapQtAppsHook`: 46 47```nix 48{ stdenv, qt6 }: 49 50stdenv.mkDerivation { 51 # ... 52 nativeBuildInputs = [ qt6.wrapQtAppsHook ]; 53 qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ]; 54} 55``` 56 57The entries are passed as arguments to [wrapProgram](#fun-wrapProgram). 58 59If you need more control over the wrapping process, set `dontWrapQtApps` to disable automatic wrapper generation, 60and then create wrappers manually in `fixupPhase`, using `wrapQtApp`, which itself is a small wrapper over [wrapProgram](#fun-wrapProgram): 61 62The `makeWrapper` arguments required for Qt are also exposed in the environment as `$qtWrapperArgs`. 63 64```nix 65{ stdenv, lib, wrapQtAppsHook }: 66 67stdenv.mkDerivation { 68 # ... 69 nativeBuildInputs = [ wrapQtAppsHook ]; 70 dontWrapQtApps = true; 71 preFixup = '' 72 wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin 73 ''; 74} 75``` 76 77::: {.note} 78`wrapQtAppsHook` ignores files that are non-ELF executables. 79This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned. 80An example of when you'd always need to do this is with Python applications that use PyQt. 81:::