2020-11-30 08:33:03 +00:00
|
|
|
# Qt {#sec-language-qt}
|
|
|
|
|
2021-02-19 19:06:45 +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.
|
|
|
|
There are two problems that the Nixpkgs Qt infrastructure addresses,
|
|
|
|
which are not shared by other C++ software:
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
1. There are usually multiple supported versions of Qt in Nixpkgs.
|
|
|
|
All of a package's dependencies must be built with the same version of Qt.
|
|
|
|
This is similar to the version constraints imposed on interpreted languages like Python.
|
|
|
|
2. Qt makes extensive use of runtime dependency detection.
|
|
|
|
Runtime dependencies are made into build dependencies through wrappers.
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
## Nix expression for a Qt package (default.nix) {#qt-default-nix}
|
|
|
|
|
|
|
|
```{=docbook}
|
|
|
|
<programlisting>
|
2021-02-19 19:06:45 +00:00
|
|
|
{ stdenv, lib, qtbase, wrapQtAppsHook }: <co xml:id='qt-default-nix-co-1' />
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
stdenv.mkDerivation {
|
2020-11-30 08:33:03 +00:00
|
|
|
pname = "myapp";
|
|
|
|
version = "1.0";
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
buildInputs = [ qtbase ];
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ]; <co xml:id='qt-default-nix-co-2' />
|
2020-11-30 08:33:03 +00:00
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<calloutlist>
|
|
|
|
<callout arearefs='qt-default-nix-co-1'>
|
|
|
|
<para>
|
2021-02-19 19:06:45 +00: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 08:33:03 +00:00
|
|
|
</para>
|
|
|
|
</callout>
|
|
|
|
<callout arearefs='qt-default-nix-co-2'>
|
2021-02-19 19:06:45 +00:00
|
|
|
<para>
|
|
|
|
All Qt packages must include <literal>wrapQtAppsHook</literal> in
|
|
|
|
<literal>nativeBuildInputs</literal>, or you must explicitly set
|
|
|
|
<literal>dontWrapQtApps</literal>.
|
|
|
|
</para>
|
2020-11-30 08:33:03 +00:00
|
|
|
</callout>
|
|
|
|
</calloutlist>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Locating runtime dependencies {#qt-runtime-dependencies}
|
2021-02-19 19:06:45 +00:00
|
|
|
|
|
|
|
Qt applications must be wrapped to find runtime dependencies.
|
|
|
|
Include `wrapQtAppsHook` in `nativeBuildInputs`:
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
```nix
|
2021-02-19 19:06:45 +00:00
|
|
|
{ stdenv, wrapQtAppsHook }:
|
|
|
|
|
2020-11-30 08:33:03 +00:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
|
|
|
}
|
|
|
|
```
|
2021-02-19 19:06:45 +00:00
|
|
|
|
|
|
|
Add entries to `qtWrapperArgs` are to modify the wrappers created by
|
|
|
|
`wrapQtAppsHook`:
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
```nix
|
2021-02-19 19:06:45 +00:00
|
|
|
{ stdenv, wrapQtAppsHook }:
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
2020-11-30 08:33:03 +00:00
|
|
|
qtWrapperArgs = [ ''--prefix PATH : /path/to/bin'' ];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
The entries are passed as arguments to [wrapProgram](#fun-wrapProgram).
|
|
|
|
|
|
|
|
Set `dontWrapQtApps` to stop applications from being wrapped automatically.
|
|
|
|
Wrap programs manually with `wrapQtApp`, using the syntax of
|
|
|
|
[wrapProgram](#fun-wrapProgram):
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
```nix
|
2021-02-19 19:06:45 +00:00
|
|
|
{ stdenv, lib, wrapQtAppsHook }:
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
|
|
|
nativeBuildInputs = [ wrapQtAppsHook ];
|
2020-11-30 08:33:03 +00:00
|
|
|
dontWrapQtApps = true;
|
|
|
|
preFixup = ''
|
|
|
|
wrapQtApp "$out/bin/myapp" --prefix PATH : /path/to/bin
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
::: {.note}
|
2021-02-19 19:06:45 +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.
|
|
|
|
:::
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
## Adding a library to Nixpkgs {#adding-a-library-to-nixpkgs}
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
Add Qt libraries to `qt5-packages.nix` to make them available for every
|
|
|
|
supported Qt version.
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
### Example adding a Qt library {#qt-library-all-packages-nix}
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
The following represents the contents of `qt5-packages.nix`.
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2021-04-05 15:23:46 +00:00
|
|
|
```nix
|
2020-11-30 08:33:03 +00:00
|
|
|
{
|
|
|
|
# ...
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
mylib = callPackage ../path/to/mylib {};
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
2021-02-19 19:06:45 +00:00
|
|
|
|
|
|
|
Libraries are built with every available version of Qt.
|
|
|
|
Use the `meta.broken` attribute to disable the package for unsupported Qt versions:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
{ stdenv, lib, qtbase }:
|
|
|
|
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
# ...
|
2021-03-09 03:18:52 +00:00
|
|
|
# Disable this library with Qt < 5.9.0
|
2021-02-19 19:06:45 +00:00
|
|
|
meta.broken = lib.versionOlder qtbase.version "5.9.0";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
## Adding an application to Nixpkgs {#adding-an-application-to-nixpkgs}
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
Add Qt applications to `qt5-packages.nix`. Add an alias to `all-packages.nix`
|
|
|
|
to select the Qt 5 version used for the application.
|
2020-11-30 08:33:03 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
### Example adding a Qt application {#qt-application-all-packages-nix}
|
|
|
|
|
|
|
|
The following represents the contents of `qt5-packages.nix`.
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2021-04-05 15:23:46 +00:00
|
|
|
```nix
|
2021-02-05 17:12:51 +00:00
|
|
|
{
|
|
|
|
# ...
|
|
|
|
|
|
|
|
myapp = callPackage ../path/to/myapp {};
|
|
|
|
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following represents the contents of `all-packages.nix`.
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2021-04-05 15:23:46 +00:00
|
|
|
```nix
|
2020-11-30 08:33:03 +00:00
|
|
|
{
|
|
|
|
# ...
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
myapp = libsForQt5.myapp;
|
2020-11-30 08:33:03 +00:00
|
|
|
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|