Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / misc / drivers / hplip / default.nix
blob66b26753095e421701b0bab5e979fb4ef63188ed
1 { lib, stdenv, fetchurl, substituteAll
2 , pkg-config, autoreconfHook
3 , cups, zlib, libjpeg, libusb1, python3Packages, sane-backends
4 , dbus, file, ghostscript, usbutils
5 , net-snmp, openssl, perl, nettools, avahi
6 , bash, util-linux
7 # To remove references to gcc-unwrapped
8 , removeReferencesTo, qt5
9 , withQt5 ? true
10 , withPlugin ? false
11 , withStaticPPDInstall ? false
14 let
16   pname = "hplip";
17   version = "3.23.3";
19   src = fetchurl {
20     url = "mirror://sourceforge/hplip/${pname}-${version}.tar.gz";
21     sha256 = "sha256-5CYKmKKx2I0h6CVi3kGaohyVvJ4qzjWDNGqA/SF+B7Y=";
22   };
24   plugin = fetchurl {
25     url = "https://developers.hp.com/sites/default/files/${pname}-${version}-plugin.run";
26     sha256 = "sha256-AyZBiF1B42dGnJeoJLFSCGNK83c86ZAM2uFciuv2H4A=";
27   };
29   hplipState = substituteAll {
30     inherit version;
31     src = ./hplip.state;
32   };
34   hplipPlatforms = {
35     i686-linux   = "x86_32";
36     x86_64-linux = "x86_64";
37     armv6l-linux = "arm32";
38     armv7l-linux = "arm32";
39     aarch64-linux = "arm64";
40   };
42   hplipArch = hplipPlatforms.${stdenv.hostPlatform.system}
43     or (throw "HPLIP not supported on ${stdenv.hostPlatform.system}");
45   pluginArches = [ "x86_32" "x86_64" "arm32" "arm64" ];
49 assert withPlugin -> builtins.elem hplipArch pluginArches
50   || throw "HPLIP plugin not supported on ${stdenv.hostPlatform.system}";
52 python3Packages.buildPythonApplication {
53   inherit pname version src;
54   format = "other";
56   buildInputs = [
57     libjpeg
58     cups
59     libusb1
60     sane-backends
61     dbus
62     file
63     ghostscript
64     net-snmp
65     openssl
66     perl
67     zlib
68     avahi
69   ];
71   nativeBuildInputs = [
72     pkg-config
73     removeReferencesTo
74     autoreconfHook
75   ] ++ lib.optional withQt5 qt5.wrapQtAppsHook;
77   pythonPath = with python3Packages; [
78     dbus
79     pillow
80     pygobject3
81     reportlab
82     usbutils
83     sip_4
84     dbus-python
85     distro
86   ] ++ lib.optionals withQt5 [
87     pyqt5
88     pyqt5_sip
89     enum-compat
90   ];
92   makeWrapperArgs = [ "--prefix" "PATH" ":" "${nettools}/bin" ];
94   patches = [
95     # HPLIP's getSystemPPDs() function relies on searching for PPDs below common FHS
96     # paths, and hp-setup crashes if none of these paths actually exist (which they
97     # don't on NixOS).  Add the equivalent NixOS path, /var/lib/cups/path/share.
98     # See: https://github.com/NixOS/nixpkgs/issues/21796
99     ./hplip-3.20.11-nixos-cups-ppd-search-path.patch
101     # Remove all ImageProcessor functionality since that is closed source
102     (fetchurl {
103       url = "https://web.archive.org/web/20230226174550/https://sources.debian.org/data/main/h/hplip/3.22.10+dfsg0-1/debian/patches/0028-Remove-ImageProcessor-binary-installs.patch";
104       sha256 = "sha256:18njrq5wrf3fi4lnpd1jqmaqr7ph5d7jxm7f15b1wwrbxir1rmml";
105     })
107     # Revert changes that break compilation under -Werror=format-security
108     ./revert-snprintf-change.patch
109   ];
111   postPatch = ''
112     # https://github.com/NixOS/nixpkgs/issues/44230
113     substituteInPlace createPPD.sh \
114       --replace ppdc "${cups}/bin/ppdc" \
115       --replace "gzip -c" "gzip -cn"
117     # HPLIP hardcodes absolute paths everywhere. Nuke from orbit.
118     find . -type f -exec sed -i \
119       -e s,/etc/hp,$out/etc/hp,g \
120       -e s,/etc/sane.d,$out/etc/sane.d,g \
121       -e s,/usr/include/libusb-1.0,${libusb1.dev}/include/libusb-1.0,g \
122       -e s,/usr/share/hal/fdi/preprobe/10osvendor,$out/share/hal/fdi/preprobe/10osvendor,g \
123       -e s,/usr/lib/systemd/system,$out/lib/systemd/system,g \
124       -e s,/var/lib/hp,$out/var/lib/hp,g \
125       -e s,/usr/bin/perl,${perl}/bin/perl,g \
126       -e s,/usr/bin/file,${file}/bin/file,g \
127       -e s,/usr/bin/gs,${ghostscript}/bin/gs,g \
128       -e s,/usr/share/cups/fonts,${ghostscript}/share/ghostscript/fonts,g \
129       -e "s,ExecStart=/usr/bin/python /usr/bin/hp-config_usb_printer,ExecStart=$out/bin/hp-config_usb_printer,g" \
130       -e s,Exec=/usr/bin/hp-uiscan,Exec=hp-uiscan,g \
131       -e s,Icon=/usr/share/icons/Humanity/devices/48/printer.svg,Icon=printer,g \
132       -e s,Icon=@abs_datadir@/hplip/data/images/128x128/hp_logo.png,Icon=hp_logo,g \
133       {} +
135     echo 'AUTOMAKE_OPTIONS = foreign' >> Makefile.am
136   '';
138   configureFlags = let out = placeholder "out"; in
139     [
140       "--with-hpppddir=${out}/share/cups/model/HP"
141       "--with-cupsfilterdir=${out}/lib/cups/filter"
142       "--with-cupsbackenddir=${out}/lib/cups/backend"
143       "--with-icondir=${out}/share/applications"
144       "--with-systraydir=${out}/xdg/autostart"
145       "--with-mimedir=${out}/etc/cups"
146       "--enable-policykit"
147       "--disable-qt4"
149       # remove ImageProcessor usage, it causes segfaults, see
150       # https://bugs.launchpad.net/hplip/+bug/1788706
151       # https://bugs.launchpad.net/hplip/+bug/1787289
152       "--disable-imageProcessor-build"
153     ]
154     ++ lib.optional withStaticPPDInstall "--enable-cups-ppd-install"
155     ++ lib.optional withQt5 "--enable-qt5"
156   ;
158   # Prevent 'ppdc: Unable to find include file "<font.defs>"' which prevent
159   # generation of '*.ppd' files.
160   # This seems to be a 'ppdc' issue when the tool is run in a hermetic sandbox.
161   # Could not find how to fix the problem in 'ppdc' so this is a workaround.
162   CUPS_DATADIR = "${cups}/share/cups";
164   makeFlags = let out = placeholder "out"; in [
165     "halpredir=${out}/share/hal/fdi/preprobe/10osvendor"
166     "rulesdir=${out}/etc/udev/rules.d"
167     "policykit_dir=${out}/share/polkit-1/actions"
168     "policykit_dbus_etcdir=${out}/etc/dbus-1/system.d"
169     "policykit_dbus_sharedir=${out}/share/dbus-1/system-services"
170     "hplip_confdir=${out}/etc/hp"
171     "hplip_statedir=${out}/var/lib/hp"
172   ];
174   postConfigure = ''
175     # don't save timestamp, in order to improve reproducibility
176     substituteInPlace Makefile \
177       --replace "GZIP_ENV = --best" "GZIP_ENV = --best -n"
178   '';
180   enableParallelBuilding = true;
181   enableParallelInstalling = false;
183   #
184   # Running `hp-diagnose_plugin -g` can be used to diagnose
185   # issues with plugins.
186   #
187   postInstall = ''
188     for resolution in 16x16 32x32 64x64 128x128 256x256; do
189       mkdir -p $out/share/icons/hicolor/$resolution/apps
190       ln -s $out/share/hplip/data/images/$resolution/hp_logo.png \
191         $out/share/icons/hicolor/$resolution/apps/hp_logo.png
192     done
193   '' + lib.optionalString withPlugin ''
194     sh ${plugin} --noexec --keep
195     cd plugin_tmp
197     cp plugin.spec $out/share/hplip/
199     mkdir -p $out/share/hplip/data/firmware
200     cp *.fw.gz $out/share/hplip/data/firmware
202     mkdir -p $out/share/hplip/data/plugins
203     cp license.txt $out/share/hplip/data/plugins
205     mkdir -p $out/share/hplip/prnt/plugins
206     for plugin in lj hbpl1; do
207       cp $plugin-${hplipArch}.so $out/share/hplip/prnt/plugins
208       chmod 0755 $out/share/hplip/prnt/plugins/$plugin-${hplipArch}.so
209       ln -s $out/share/hplip/prnt/plugins/$plugin-${hplipArch}.so \
210         $out/share/hplip/prnt/plugins/$plugin.so
211     done
213     mkdir -p $out/share/hplip/scan/plugins
214     for plugin in bb_soap bb_marvell bb_soapht bb_escl; do
215       cp $plugin-${hplipArch}.so $out/share/hplip/scan/plugins
216       chmod 0755 $out/share/hplip/scan/plugins/$plugin-${hplipArch}.so
217       ln -s $out/share/hplip/scan/plugins/$plugin-${hplipArch}.so \
218         $out/share/hplip/scan/plugins/$plugin.so
219     done
221     mkdir -p $out/share/hplip/fax/plugins
222     for plugin in fax_marvell; do
223       cp $plugin-${hplipArch}.so $out/share/hplip/fax/plugins
224       chmod 0755 $out/share/hplip/fax/plugins/$plugin-${hplipArch}.so
225       ln -s $out/share/hplip/fax/plugins/$plugin-${hplipArch}.so \
226         $out/share/hplip/fax/plugins/$plugin.so
227     done
229     mkdir -p $out/var/lib/hp
230     cp ${hplipState} $out/var/lib/hp/hplip.state
231   '';
233   # The installed executables are just symlinks into $out/share/hplip,
234   # but wrapPythonPrograms ignores symlinks. We cannot replace the Python
235   # modules in $out/share/hplip with wrapper scripts because they import
236   # each other as libraries. Instead, we emulate wrapPythonPrograms by
237   # 1. Calling patchPythonProgram on the original script in $out/share/hplip
238   # 2. Making our own wrapper pointing directly to the original script.
239   dontWrapPythonPrograms = true;
240   preFixup = ''
241     buildPythonPath "$out $pythonPath"
243     for bin in $out/bin/*; do
244       py=$(readlink -m $bin)
245       rm $bin
246       echo "patching \`$py'..."
247       patchPythonScript "$py"
248       echo "wrapping \`$bin'..."
249       makeWrapper "$py" "$bin" \
250           --prefix PATH ':' "$program_PATH" \
251           --set PYTHONNOUSERSITE "true" \
252           $makeWrapperArgs
253     done
254   '';
256   postFixup = ''
257     substituteInPlace $out/etc/hp/hplip.conf --replace /usr $out
258     # Patch udev rules:
259     # with plugin, they upload firmware to printers,
260     # without plugin, they complain about the missing plugin.
261     substituteInPlace $out/etc/udev/rules.d/56-hpmud.rules \
262       --replace {,${bash}}/bin/sh \
263       --replace /usr/bin/nohup "" \
264       --replace {,${util-linux}/bin/}logger \
265       --replace {/usr,$out}/bin
266     remove-references-to -t ${stdenv.cc.cc} $(readlink -f $out/lib/*.so)
267   '' + lib.optionalString withQt5 ''
268     for f in $out/bin/hp-*;do
269       wrapQtApp $f
270     done
271   '';
273   # There are some binaries there, which reference gcc-unwrapped otherwise.
274   stripDebugList = [
275     "share/hplip" "lib/cups/backend" "lib/cups/filter" python3Packages.python.sitePackages "lib/sane"
276   ];
278   meta = with lib; {
279     description = "Print, scan and fax HP drivers for Linux";
280     homepage = "https://developers.hp.com/hp-linux-imaging-and-printing";
281     downloadPage = "https://sourceforge.net/projects/hplip/files/hplip/";
282     license = if withPlugin
283       then licenses.unfree
284       else with licenses; [ mit bsd2 gpl2Plus ];
285     platforms = [ "i686-linux" "x86_64-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux" ];
286     maintainers = with maintainers; [ ttuegel ];
287   };