1{
2 lib,
3 stdenv,
4 # The unwrapped libreoffice derivation
5 unwrapped,
6 makeWrapper,
7 xorg, # for lndir
8 runCommand,
9 # For Emulating wrapGAppsHook3
10 gsettings-desktop-schemas,
11 hicolor-icon-theme,
12 dconf,
13 librsvg,
14 gdk-pixbuf,
15 # some scripts need these when used in conjunction with firejail
16 coreutils,
17 gnugrep,
18 # Configuration options for the wrapper
19 extraMakeWrapperArgs ? [ ],
20 dbusVerify ? stdenv.hostPlatform.isLinux,
21 dbus,
22}:
23
24let
25 inherit (unwrapped) version;
26 major = lib.versions.major version;
27 minor = lib.versions.minor version;
28
29 makeWrapperArgs = builtins.concatStringsSep " " (
30 [
31 "--set"
32 "GDK_PIXBUF_MODULE_FILE"
33 "${librsvg}/${gdk-pixbuf.moduleDir}.cache"
34 "--prefix"
35 "GIO_EXTRA_MODULES"
36 ":"
37 "${lib.getLib dconf}/lib/gio/modules"
38 "--prefix"
39 "XDG_DATA_DIRS"
40 ":"
41 "${unwrapped.gtk3}/share/gsettings-schemas/${unwrapped.gtk3.name}"
42 "--prefix"
43 "XDG_DATA_DIRS"
44 ":"
45 "$out/share"
46 "--prefix"
47 "XDG_DATA_DIRS"
48 ":"
49 "${gsettings-desktop-schemas}/share/gsettings-schemas/${gsettings-desktop-schemas.name}"
50 "--prefix"
51 "XDG_DATA_DIRS"
52 ":"
53 "${hicolor-icon-theme}/share"
54 "--prefix"
55 "GST_PLUGIN_SYSTEM_PATH_1_0"
56 ":"
57 "${lib.makeSearchPath "lib/girepository-1.0" unwrapped.gst_packages}"
58 "--suffix"
59 "PATH"
60 ":"
61 "${lib.makeBinPath [
62 coreutils
63 gnugrep
64 ]}"
65 ]
66 ++ lib.optionals unwrapped.kdeIntegration [
67 "--prefix"
68 "QT_PLUGIN_PATH"
69 ":"
70 "${lib.makeSearchPath unwrapped.qtbase.qtPluginPrefix (
71 builtins.map lib.getBin unwrapped.qtPackages
72 )}"
73 "--prefix"
74 "QML2_IMPORT_PATH"
75 ":"
76 "${lib.makeSearchPath unwrapped.qtbase.qtQmlPrefix (builtins.map lib.getBin unwrapped.qmlPackages)}"
77 ]
78 ++ [
79 # Add dictionaries from all NIX_PROFILES
80 "--run"
81 (lib.escapeShellArg ''
82 for PROFILE in $NIX_PROFILES; do
83 HU_DIR="$PROFILE/share/hunspell"
84 HY_DIR="$PROFILE/share/hyphen"
85 if [ -d "$HU_DIR" ]; then
86 export DICPATH=$DICPATH''${DICPATH:+:}$HU_DIR
87 fi
88 if [ -d "$HY_DIR" ]; then
89 export DICPATH=$DICPATH''${DICPATH:+:}$HY_DIR
90 fi
91 done
92 '')
93 ]
94 ++ lib.optionals dbusVerify [
95 # If no dbus is running, start a dedicated dbus daemon
96 "--run"
97 (lib.escapeShellArg ''
98 if ! ( test -n "$DBUS_SESSION_BUS_ADDRESS" ); then
99 dbus_tmp_dir="/run/user/$(id -u)/libreoffice-dbus"
100 if ! test -d "$dbus_tmp_dir" && test -d "/run"; then
101 mkdir -p "$dbus_tmp_dir"
102 fi
103 if ! test -d "$dbus_tmp_dir"; then
104 dbus_tmp_dir="/tmp/libreoffice-$(id -u)/libreoffice-dbus"
105 mkdir -p "$dbus_tmp_dir"
106 fi
107 dbus_socket_dir="$(mktemp -d -p "$dbus_tmp_dir")"
108 "${dbus}"/bin/dbus-daemon \
109 --nopidfile \
110 --nofork \
111 --config-file "${dbus}"/share/dbus-1/session.conf \
112 --address "unix:path=$dbus_socket_dir/session" &> /dev/null &
113 dbus_pid=$!
114 export DBUS_SESSION_BUS_ADDRESS="unix:path=$dbus_socket_dir/session"
115 fi
116 '')
117 ]
118 ++ [
119 "--inherit-argv0"
120 ]
121 ++ extraMakeWrapperArgs
122 );
123in
124runCommand "${unwrapped.name}-wrapped"
125 {
126 inherit (unwrapped) meta;
127 paths = [ unwrapped ];
128 nativeBuildInputs = [
129 makeWrapper
130 xorg.lndir
131 ];
132 passthru = {
133 inherit unwrapped;
134 # For backwards compatibility:
135 libreoffice = lib.warn "libreoffice: Use the unwrapped attributed, using libreoffice.libreoffice is deprecated." unwrapped;
136 inherit (unwrapped) kdeIntegration;
137 };
138 }
139 (
140 ''
141 mkdir -p $out/share
142 for dir in ${unwrapped}/share/*; do
143 dirname="''${dir##*/}"
144 if [[ $dirname == "applications" ]]; then
145 cp -r $dir/ $out/share/
146 else
147 ln -s $dir $out/share/
148 fi
149 done
150 for f in $out/share/applications/*.desktop; do
151 substituteInPlace "$f" \
152 --replace "Exec=libreoffice${major}.${minor}" "Exec=soffice"
153 done
154
155 mkdir -p $out/bin
156 mkdir -p $out/lib/libreoffice/program
157 lndir -silent ${unwrapped}/lib/libreoffice/program $out/lib/libreoffice/program
158 for i in sbase scalc sdraw smath swriter simpress soffice unopkg; do
159 # Delete the symlink created by lndir, and replace it by our wrapper
160 rm $out/lib/libreoffice/program/$i
161 makeWrapper \
162 ${unwrapped}/lib/libreoffice/program/$i \
163 $out/lib/libreoffice/program/$i \
164 ${makeWrapperArgs}
165 ''
166 + lib.optionalString dbusVerify ''
167 # Delete the dbus socket directory after libreoffice quits
168 sed -i 's/^exec -a "$0" //g' $out/lib/libreoffice/program/$i
169 echo 'code="$?"' >> $out/lib/libreoffice/program/$i
170 echo 'test -n "$dbus_socket_dir" && { rm -rf "$dbus_socket_dir"; kill $dbus_pid; }' >> $out/lib/libreoffice/program/$i
171 echo 'exit "$code"' >> $out/lib/libreoffice/program/$i
172 ''
173 + ''
174 ln -s $out/lib/libreoffice/program/$i $out/bin/$i
175 done
176 # A symlink many users rely upon
177 ln -s $out/bin/soffice $out/bin/libreoffice
178 ''
179 )