1if [[ -z "${__nix_wrapQtAppsHook-}" ]]; then
2 __nix_wrapQtAppsHook=1 # Don't run this hook more than once.
3
4 # Inherit arguments given in mkDerivation
5 qtWrapperArgs=(${qtWrapperArgs-})
6
7 qtHostPathSeen=()
8
9 qtUnseenHostPath() {
10 for pkg in "${qtHostPathSeen[@]}"; do
11 if [ "${pkg:?}" == "$1" ]; then
12 return 1
13 fi
14 done
15
16 qtHostPathSeen+=("$1")
17 return 0
18 }
19
20 qtHostPathHook() {
21 qtUnseenHostPath "$1" || return 0
22
23 if ! [ -v qtPluginPrefix ]; then
24 echo "wrapQtAppsHook qtHostPathHook: qtPluginPrefix is unset. hint: add qt6.qtbase to buildInputs"
25 fi
26
27 local pluginDir="$1/${qtPluginPrefix:?}"
28 if [ -d "$pluginDir" ]; then
29 qtWrapperArgs+=(--prefix QT_PLUGIN_PATH : "$pluginDir")
30 fi
31
32 local qmlDir="$1/${qtQmlPrefix:?}"
33 if [ -d "$qmlDir" ]; then
34 qtWrapperArgs+=(--prefix NIXPKGS_QT6_QML_IMPORT_PATH : "$qmlDir")
35 fi
36 }
37 addEnvHooks "$targetOffset" qtHostPathHook
38
39 makeQtWrapper() {
40 local original="$1"
41 local wrapper="$2"
42 shift 2
43 makeWrapper "$original" "$wrapper" "${qtWrapperArgs[@]}" "$@"
44 }
45
46 wrapQtApp() {
47 local program="$1"
48 shift 1
49 wrapProgram "$program" "${qtWrapperArgs[@]}" "$@"
50 }
51
52 qtOwnPathsHook() {
53 local xdgDataDir="${!outputBin}/share"
54 if [ -d "$xdgDataDir" ]; then
55 qtWrapperArgs+=(--prefix XDG_DATA_DIRS : "$xdgDataDir")
56 fi
57
58 local xdgConfigDir="${!outputBin}/etc/xdg"
59 if [ -d "$xdgConfigDir" ]; then
60 qtWrapperArgs+=(--prefix XDG_CONFIG_DIRS : "$xdgConfigDir")
61 fi
62
63 qtHostPathHook "${!outputBin}"
64 }
65
66 appendToVar preFixupPhases qtOwnPathsHook
67
68 # Note: $qtWrapperArgs still gets defined even if ${dontWrapQtApps-} is set.
69 wrapQtAppsHook() {
70 # skip this hook when requested
71 [ -z "${dontWrapQtApps-}" ] || return 0
72
73 # guard against running multiple times (e.g. due to propagation)
74 [ -z "$wrapQtAppsHookHasRun" ] || return 0
75 wrapQtAppsHookHasRun=1
76
77 local targetDirs=("$prefix/bin" "$prefix/sbin" "$prefix/libexec" "$prefix/Applications" "$prefix/"*.app)
78 echo "wrapping Qt applications in ${targetDirs[@]}"
79
80 for targetDir in "${targetDirs[@]}"; do
81 [ -d "$targetDir" ] || continue
82
83 find "$targetDir" ! -type d -executable -print0 | while IFS= read -r -d '' file; do
84 # Skip the file if it is not a binary (ELF or Mach-O)
85 isELF "$file" || isMachO "$file" || continue
86
87 if [ -h "$file" ]; then
88 target="$(readlink -e "$file")"
89 echo "wrapping $file -> $target"
90 rm "$file"
91 makeQtWrapper "$target" "$file"
92 elif [ -f "$file" ]; then
93 echo "wrapping $file"
94 wrapQtApp "$file"
95 fi
96 done
97 done
98 }
99
100 fixupOutputHooks+=(wrapQtAppsHook)
101
102fi