2020-11-30 05:30:29 +01:00
|
|
|
# Qt {#sec-language-qt}
|
|
|
|
|
2021-01-10 18:34:34 +01: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 05:30:29 +01:00
|
|
|
|
2023-01-03 13:39:40 +01: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 05:30:29 +01:00
|
|
|
|
|
|
|
## Nix expression for a Qt package (default.nix) {#qt-default-nix}
|
|
|
|
|
|
|
|
```{=docbook}
|
|
|
|
<programlisting>
|
2021-01-12 12:50:23 +01:00
|
|
|
{ stdenv, lib, qtbase, wrapQtAppsHook }: <co xml:id='qt-default-nix-co-1' />
|
2020-11-30 05:30:29 +01:00
|
|
|
|
2021-01-10 18:34:34 +01:00
|
|
|
stdenv.mkDerivation {
|
2020-11-30 05:30:29 +01:00
|
|
|
pname = "myapp";
|
|
|
|
version = "1.0";
|
|
|
|
|
2021-01-10 18:34:34 +01:00
|
|
|
buildInputs = [ qtbase ];
|
2021-02-09 02:05:41 +01:00
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ]; <co xml:id='qt-default-nix-co-2' />
|
2020-11-30 05:30:29 +01:00
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<calloutlist>
|
|
|
|
<callout arearefs='qt-default-nix-co-1'>
|
|
|
|
<para>
|
2021-01-10 18:34:34 +01:00
|
|
|
Import Qt modules directly, that is: <literal>qtbase</literal>, <literal>qtdeclarative</literal>, etc.
|
|
|
|
<emphasis>Do not</emphasis> import Qt package sets such as <literal>qt5</literal>
|
|
|
|
because the Qt versions of dependencies may not be coherent, causing build and runtime failures.
|
2020-11-30 05:30:29 +01:00
|
|
|
</para>
|
|
|
|
</callout>
|
2021-02-11 14:49:52 +01:00
|
|
|
<callout arearefs='qt-default-nix-co-2'>
|
2021-01-12 12:50:23 +01:00
|
|
|
<para>
|
|
|
|
All Qt packages must include <literal>wrapQtAppsHook</literal> in
|
|
|
|
<literal>nativeBuildInputs</literal>, or you must explicitly set
|
|
|
|
<literal>dontWrapQtApps</literal>.
|
|
|
|
</para>
|
|
|
|
</callout>
|
2020-11-30 05:30:29 +01:00
|
|
|
</calloutlist>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Locating runtime dependencies {#qt-runtime-dependencies}
|
2021-01-10 18:34:34 +01:00
|
|
|
|
|
|
|
Qt applications must be wrapped to find runtime dependencies.
|
|
|
|
Include `wrapQtAppsHook` in `nativeBuildInputs`:
|
2020-11-30 05:30:29 +01:00
|
|
|
|
|
|
|
```nix
|
2021-01-10 18:34:34 +01:00
|
|
|
{ stdenv, wrapQtAppsHook }:
|
|
|
|
|
2020-11-30 05:30:29 +01:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
|
|
|
}
|
|
|
|
```
|
2021-01-10 18:34:34 +01:00
|
|
|
|
2021-01-12 12:50:23 +01:00
|
|
|
Add entries to `qtWrapperArgs` are to modify the wrappers created by
|
|
|
|
`wrapQtAppsHook`:
|
2020-11-30 05:30:29 +01:00
|
|
|
|
|
|
|
```nix
|
2021-01-10 18:34:34 +01:00
|
|
|
{ stdenv, wrapQtAppsHook }:
|
2020-11-30 05:30:29 +01:00
|
|
|
|
2021-01-10 18:34:34 +01:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
2020-11-30 05:30:29 +01:00
|
|
|
qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-10 18:34:34 +01:00
|
|
|
The entries are passed as arguments to [wrapProgram](#fun-wrapProgram).
|
|
|
|
|
|
|
|
Set `dontWrapQtApps` to stop applications from being wrapped automatically.
|
2021-01-12 12:50:23 +01:00
|
|
|
Wrap programs manually with `wrapQtApp`, using the syntax of
|
|
|
|
[wrapProgram](#fun-wrapProgram):
|
2020-11-30 05:30:29 +01:00
|
|
|
|
|
|
|
```nix
|
2021-01-10 18:34:34 +01:00
|
|
|
{ stdenv, lib, wrapQtAppsHook }:
|
2020-11-30 05:30:29 +01:00
|
|
|
|
2021-01-10 18:34:34 +01:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
2020-11-30 05:30:29 +01:00
|
|
|
dontWrapQtApps = true;
|
|
|
|
preFixup = ''
|
|
|
|
wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-06-05 21:22:45 +02:00
|
|
|
::: {.note}
|
2021-01-11 19:09:48 +01: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.
|
|
|
|
:::
|