summaryrefslogtreecommitdiffstats
path: root/pkgs/applications/office/libreoffice/wrapper.nix
blob: ad9f219dc14bec6d78307df1a402db0115fd141d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
  lib,
  stdenv,
  # The unwrapped libreoffice derivation
  unwrapped,
  makeWrapper,
  lndir,
  runCommand,
  # For Emulating wrapGAppsHook3
  gsettings-desktop-schemas,
  hicolor-icon-theme,
  dconf,
  librsvg,
  gdk-pixbuf,
  # some scripts need these when used in conjunction with firejail
  coreutils,
  gnugrep,
  # Configuration options for the wrapper
  extraMakeWrapperArgs ? [ ],
  dbusVerify ? stdenv.hostPlatform.isLinux,
  dbus,
  unixodbc,
}:

let
  inherit (unwrapped) version;
  major = lib.versions.major version;
  minor = lib.versions.minor version;

  makeWrapperArgs = builtins.concatStringsSep " " (
    [
      "--prefix"
      "LD_LIBRARY_PATH"
      ":"
      "${lib.getLib unixodbc}/lib"
      "--set"
      "GDK_PIXBUF_MODULE_FILE"
      "${librsvg}/${gdk-pixbuf.moduleDir}.cache"
      "--prefix"
      "GIO_EXTRA_MODULES"
      ":"
      "${lib.getLib dconf}/lib/gio/modules"
      "--prefix"
      "XDG_DATA_DIRS"
      ":"
      "${unwrapped.gtk3}/share/gsettings-schemas/${unwrapped.gtk3.name}"
      "--prefix"
      "XDG_DATA_DIRS"
      ":"
      "$out/share"
      "--prefix"
      "XDG_DATA_DIRS"
      ":"
      "${gsettings-desktop-schemas}/share/gsettings-schemas/${gsettings-desktop-schemas.name}"
      "--prefix"
      "XDG_DATA_DIRS"
      ":"
      "${hicolor-icon-theme}/share"
      "--prefix"
      "GST_PLUGIN_SYSTEM_PATH_1_0"
      ":"
      "${lib.makeSearchPath "lib/gstreamer-1.0" unwrapped.gst_packages}"
      "--suffix"
      "PATH"
      ":"
      "${lib.makeBinPath [
        coreutils
        gnugrep
      ]}"
    ]
    ++ lib.optionals unwrapped.kdeIntegration [
      "--prefix"
      "QT_PLUGIN_PATH"
      ":"
      "${lib.makeSearchPath unwrapped.qtbase.qtPluginPrefix (map lib.getBin unwrapped.qtPackages)}"
      "--prefix"
      "QML2_IMPORT_PATH"
      ":"
      "${lib.makeSearchPath unwrapped.qtbase.qtQmlPrefix (map lib.getBin unwrapped.qmlPackages)}"
    ]
    ++ [
      # Add dictionaries from all NIX_PROFILES
      "--run"
      (lib.escapeShellArg ''
        for PROFILE in $NIX_PROFILES; do
            HU_DIR="$PROFILE/share/hunspell"
            HY_DIR="$PROFILE/share/hyphen"
            if [ -d "$HU_DIR" ]; then
                export DICPATH=$DICPATH''${DICPATH:+:}$HU_DIR
            fi
            if [ -d "$HY_DIR" ]; then
                export DICPATH=$DICPATH''${DICPATH:+:}$HY_DIR
            fi
        done
      '')
    ]
    ++ lib.optionals dbusVerify [
      # If no dbus is running, start a dedicated dbus daemon
      "--run"
      (lib.escapeShellArg ''
        if ! ( test -n "$DBUS_SESSION_BUS_ADDRESS" ); then
            dbus_tmp_dir="/run/user/$(id -u)/libreoffice-dbus"
            if ! test -d "$dbus_tmp_dir" && test -d "/run"; then
                    mkdir -p "$dbus_tmp_dir"
            fi
            if ! test -d "$dbus_tmp_dir"; then
                    dbus_tmp_dir="/tmp/libreoffice-$(id -u)/libreoffice-dbus"
                    mkdir -p "$dbus_tmp_dir"
            fi
            dbus_socket_dir="$(mktemp -d -p "$dbus_tmp_dir")"
            "${dbus}"/bin/dbus-daemon \
              --nopidfile \
              --nofork \
              --config-file "${dbus}"/share/dbus-1/session.conf \
              --address "unix:path=$dbus_socket_dir/session"  &> /dev/null &
            dbus_pid=$!
            export DBUS_SESSION_BUS_ADDRESS="unix:path=$dbus_socket_dir/session"
        fi
      '')
    ]
    ++ lib.optionals unwrapped.withJava [
      "--set"
      "JAVA_HOME"
      "${unwrapped.jdk.home}"
    ]
    ++ [
      "--inherit-argv0"
    ]
    ++ extraMakeWrapperArgs
  );
in
runCommand "${unwrapped.name}-wrapped"
  {

    # Not sure if meaningful for runCommand, but nixpkgs-vet demands this anyway
    __structuredAttrs = true;
    strictDeps = true;

    inherit (unwrapped) meta pname version;
    paths = [ unwrapped ];
    nativeBuildInputs = [
      makeWrapper
      lndir
    ];
    passthru = {
      inherit unwrapped;
      # For backwards compatibility:
      libreoffice = lib.warn "libreoffice: Use the unwrapped attributed, using libreoffice.libreoffice is deprecated." unwrapped;
      inherit (unwrapped) kdeIntegration;
    };
  }
  (
    ''
      mkdir -p $out/share
      for dir in ${unwrapped}/share/*; do
        dirname="''${dir##*/}"
        if [[ $dirname == "applications" ]]; then
          cp -r $dir/ $out/share/
        else
          ln -s $dir $out/share/
        fi
      done
      for f in $out/share/applications/*.desktop; do
        substituteInPlace "$f" \
          --replace "Exec=libreoffice${major}.${minor}" "Exec=soffice"
      done

      mkdir -p $out/bin
      mkdir -p $out/lib/libreoffice/program
      lndir -silent ${unwrapped}/lib/libreoffice/program $out/lib/libreoffice/program
      for i in sbase scalc sdraw smath swriter simpress soffice unopkg; do
        # Delete the symlink created by lndir, and replace it by our wrapper
        rm $out/lib/libreoffice/program/$i
        makeWrapper \
          ${unwrapped}/lib/libreoffice/program/$i \
          $out/lib/libreoffice/program/$i \
          ${makeWrapperArgs}
    ''
    + lib.optionalString dbusVerify ''
      # Delete the dbus socket directory after libreoffice quits
      sed -i 's/^exec -a "$0" //g' $out/lib/libreoffice/program/$i
      echo 'code="$?"' >> $out/lib/libreoffice/program/$i
      echo 'test -n "$dbus_socket_dir" && { rm -rf "$dbus_socket_dir"; kill $dbus_pid; }' >> $out/lib/libreoffice/program/$i
      echo 'exit "$code"' >> $out/lib/libreoffice/program/$i
    ''
    + ''
        ln -s $out/lib/libreoffice/program/$i $out/bin/$i
      done
      # A symlink many users rely upon
      ln -s $out/bin/soffice $out/bin/libreoffice
    ''
  )