2020-11-30 04:30:29 +00:00
# Qt {#sec-language-qt}
2021-01-10 17:34:34 +00:00
Writing Nix expressions for Qt libraries and applications is largely similar as for other C++ software.
This section assumes some knowledge of the latter.
2020-11-30 04:30:29 +00:00
2023-01-03 12:39:40 +00:00
The major caveat with Qt applications is that Qt uses a plugin system to load additional modules at runtime,
from a list of well-known locations. In Nixpkgs, we patch QtCore to instead use an environment variable,
and wrap Qt applications to set it to the right paths. This effectively makes the runtime dependencies
pure and explicit at build-time, at the cost of introducing an extra indirection.
2020-11-30 04:30:29 +00:00
## Nix expression for a Qt package (default.nix) {#qt-default-nix}
2023-06-10 16:13:33 +00:00
```nix
{ stdenv, lib, qtbase, wrapQtAppsHook }:
2020-11-30 04:30:29 +00:00
2021-01-10 17:34:34 +00:00
stdenv.mkDerivation {
2020-11-30 04:30:29 +00:00
pname = "myapp";
version = "1.0";
2021-01-10 17:34:34 +00:00
buildInputs = [ qtbase ];
2023-06-10 16:13:33 +00:00
nativeBuildInputs = [ wrapQtAppsHook ];
2020-11-30 04:30:29 +00:00
}
```
2023-06-10 16:13:33 +00:00
It 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.
Additionally all Qt packages must include `wrapQtAppsHook` in `nativeBuildInputs` , or you must explicitly set `dontWrapQtApps` .
2020-11-30 04:30:29 +00:00
## Locating runtime dependencies {#qt-runtime-dependencies}
2021-01-10 17:34:34 +00:00
Qt applications must be wrapped to find runtime dependencies.
Include `wrapQtAppsHook` in `nativeBuildInputs` :
2020-11-30 04:30:29 +00:00
```nix
2021-01-10 17:34:34 +00:00
{ stdenv, wrapQtAppsHook }:
2020-11-30 04:30:29 +00:00
stdenv.mkDerivation {
# ...
nativeBuildInputs = [ wrapQtAppsHook ];
}
```
2021-01-10 17:34:34 +00:00
2021-01-12 11:50:23 +00:00
Add entries to `qtWrapperArgs` are to modify the wrappers created by
`wrapQtAppsHook` :
2020-11-30 04:30:29 +00:00
```nix
2021-01-10 17:34:34 +00:00
{ stdenv, wrapQtAppsHook }:
2020-11-30 04:30:29 +00:00
2021-01-10 17:34:34 +00:00
stdenv.mkDerivation {
# ...
nativeBuildInputs = [ wrapQtAppsHook ];
2020-11-30 04:30:29 +00:00
qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
}
```
2021-01-10 17:34:34 +00:00
The entries are passed as arguments to [wrapProgram ](#fun-wrapProgram ).
Set `dontWrapQtApps` to stop applications from being wrapped automatically.
2021-01-12 11:50:23 +00:00
Wrap programs manually with `wrapQtApp` , using the syntax of
[wrapProgram ](#fun-wrapProgram ):
2020-11-30 04:30:29 +00:00
```nix
2021-01-10 17:34:34 +00:00
{ stdenv, lib, wrapQtAppsHook }:
2020-11-30 04:30:29 +00:00
2021-01-10 17:34:34 +00:00
stdenv.mkDerivation {
# ...
nativeBuildInputs = [ wrapQtAppsHook ];
2020-11-30 04:30:29 +00:00
dontWrapQtApps = true;
preFixup = ''
wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
'';
}
```
2021-06-05 19:22:45 +00:00
::: {.note}
2021-01-11 18:09:48 +00:00
`wrapQtAppsHook` ignores files that are non-ELF executables.
This means that scripts won't be automatically wrapped so you'll need to manually wrap them as previously mentioned.
An example of when you'd always need to do this is with Python applications that use PyQt.
:::