1{ config, stdenv, lib, fetchurl, pkgconfig, zlib, expat, openssl, autoconf
2, libjpeg, libpng, libtiff, freetype, fontconfig, libpaper, jbig2dec
3, libiconv, ijs, lcms2, fetchpatch
4, cupsSupport ? config.ghostscript.cups or (!stdenv.isDarwin), cups ? null
5, x11Support ? cupsSupport, xlibsWrapper ? null # with CUPS, X11 only adds very little
6}:
7
8assert x11Support -> xlibsWrapper != null;
9assert cupsSupport -> cups != null;
10
11let
12 version = "9.${ver_min}";
13 ver_min = "50";
14 sha512 = "3p46kzn6kh7z4qqnqydmmvdlgzy5730z3yyvyxv6i4yb22mgihzrwqmhmvfn3b7lypwf6fdkkndarzv7ly3zndqpyvg89x436sms7iw";
15
16 fonts = stdenv.mkDerivation {
17 name = "ghostscript-fonts";
18
19 srcs = [
20 (fetchurl {
21 url = "mirror://sourceforge/gs-fonts/ghostscript-fonts-std-8.11.tar.gz";
22 sha256 = "00f4l10xd826kak51wsmaz69szzm2wp8a41jasr4jblz25bg7dhf";
23 })
24 (fetchurl {
25 url = "mirror://gnu/ghostscript/gnu-gs-fonts-other-6.0.tar.gz";
26 sha256 = "1cxaah3r52qq152bbkiyj2f7dx1rf38vsihlhjmrvzlr8v6cqil1";
27 })
28 # ... add other fonts here
29 ];
30
31 installPhase = ''
32 mkdir "$out"
33 mv -v * "$out/"
34 '';
35 };
36
37in
38stdenv.mkDerivation rec {
39 pname = "ghostscript";
40 inherit version;
41
42 src = fetchurl {
43 url = "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs9${ver_min}/${pname}-${version}.tar.xz";
44 inherit sha512;
45 };
46
47 patches = [
48 ./urw-font-files.patch
49 ./doc-no-ref.diff
50 (fetchpatch {
51 name = "CVE-2019-14869.patch";
52 url = "https://git.ghostscript.com/?p=ghostpdl.git;a=patch;h=485904772c5f0aa1140032746e5a0abfc40f4cef";
53 sha256 = "0z5gnvgpp0dlzgvpw9a1yan7qyycv3mf88l93fvb1kyay893rshp";
54 })
55 ];
56
57 outputs = [ "out" "man" "doc" ];
58
59 enableParallelBuilding = true;
60
61 nativeBuildInputs = [ pkgconfig autoconf ];
62 buildInputs =
63 [ zlib expat openssl
64 libjpeg libpng libtiff freetype fontconfig libpaper jbig2dec
65 libiconv ijs lcms2
66 ]
67 ++ lib.optional x11Support xlibsWrapper
68 ++ lib.optional cupsSupport cups
69 ;
70
71 preConfigure = ''
72 # requires in-tree (heavily patched) openjpeg
73 rm -rf jpeg libpng zlib jasper expat tiff lcms2mt jbig2dec freetype cups/libs ijs
74
75 sed "s@if ( test -f \$(INCLUDE)[^ ]* )@if ( true )@; s@INCLUDE=/usr/include@INCLUDE=/no-such-path@" -i base/unix-aux.mak
76 sed "s@^ZLIBDIR=.*@ZLIBDIR=${zlib.dev}/include@" -i configure.ac
77
78 autoconf
79 '';
80
81 configureFlags = [
82 "--with-system-libtiff"
83 "--enable-dynamic"
84 ]
85 ++ lib.optional x11Support "--with-x"
86 ++ lib.optionals cupsSupport [
87 "--enable-cups"
88 "--with-cups-serverbin=$(out)/lib/cups"
89 "--with-cups-serverroot=$(out)/etc/cups"
90 "--with-cups-datadir=$(out)/share/cups"
91 ];
92
93 doCheck = true;
94
95 # don't build/install statically linked bin/gs
96 buildFlags = [ "so" ];
97 installTargets = [ "soinstall" ];
98
99 postInstall = ''
100 ln -s gsc "$out"/bin/gs
101
102 cp -r Resource "$out/share/ghostscript/${version}"
103
104 mkdir -p "$doc/share/doc/ghostscript"
105 mv "$doc/share/doc/${version}" "$doc/share/doc/ghostscript/"
106
107 ln -s "${fonts}" "$out/share/ghostscript/fonts"
108 '' + stdenv.lib.optionalString stdenv.isDarwin ''
109 for file in $out/lib/*.dylib* ; do
110 install_name_tool -id "$file" $file
111 done
112 '';
113
114 preFixup = lib.optionalString stdenv.isDarwin ''
115 install_name_tool -change libgs.dylib.${version} $out/lib/libgs.dylib.${version} $out/bin/gs
116 '';
117
118 passthru = { inherit version; };
119
120 meta = {
121 homepage = https://www.ghostscript.com/;
122 description = "PostScript interpreter (mainline version)";
123
124 longDescription = ''
125 Ghostscript is the name of a set of tools that provides (i) an
126 interpreter for the PostScript language and the PDF file format,
127 (ii) a set of C procedures (the Ghostscript library) that
128 implement the graphics capabilities that appear as primitive
129 operations in the PostScript language, and (iii) a wide variety
130 of output drivers for various file formats and printers.
131 '';
132
133 license = stdenv.lib.licenses.agpl3;
134
135 platforms = stdenv.lib.platforms.all;
136 maintainers = [ stdenv.lib.maintainers.viric ];
137 };
138}