1 { lib, stdenv, runCommand, fetchzip, fetchurl, fetchFromGitHub
2 , cmake, pkg-config, zlib, libpng, makeWrapper
3 , enableGSL ? true, gsl
4 , enableGhostScript ? true, ghostscript
5 , enableMuPDF ? true, mupdf_1_17
6 , enableDJVU ? true, djvulibre
7 , enableGOCR ? false, gocr # Disabled by default due to crashes
8 , enableTesseract ? true, leptonica, tesseract4
11 # k2pdfopt is a pain to package. It requires modified versions of mupdf,
12 # leptonica, and tesseract. Instead of shipping patches for these upstream
13 # packages, k2pdfopt includes just the modified source files for these
14 # packages. The individual files from the {mupdf,leptonica,tesseract}_mod/
15 # directories are intended to replace the corresponding source files in the
16 # upstream packages, for a particular version of that upstream package.
18 # There are a few ways we could approach packaging these modified versions of
19 # mupdf, leptonica, and mupdf:
20 # 1) Override the upstream source with a new derivation that involves copying
21 # the modified source files from k2pdfopt and replacing the corresponding
22 # source files in the upstream packages. Since the files are intended for a
23 # particular version of the upstream package, this would not allow us to easily
24 # use updates to those packages in nixpkgs.
25 # 2) Manually produce patches which can be applied against the upstream
26 # project, and have the same effect as replacing those files. This is what I
27 # believe k2pdfopt should do this for us anyway. The benefit of creating and
28 # applying patches in this way is that minor updates (esp. security fixes) to
29 # upstream packages might still allow these patches to apply successfully.
30 # 3) Automatically produce these patches inside a nix derivation. This is the
31 # approach taken here, using the "mkPatch" provided below. This has the
32 # benefit of easier review and should hopefully be simpler to update in the
36 # Create a patch against src based on changes applied in patchCommands
37 mkPatch = { name, src, patchCommands }: runCommand "${name}-k2pdfopt.patch" { inherit src; } ''
42 new=$sourceRoot-modded
49 diff -Naur $orig $new > $out || true
54 k2pdfopt_src = fetchzip {
55 url = "http://www.willus.com/${pname}/src/${pname}_v${version}_src.zip";
56 sha256 = "1fna8bg3pascjfc3hmc6xn0xi2yh7f1qp0d344mw9hqanbnykyy8";
58 in stdenv.mkDerivation rec {
59 inherit pname version;
63 ./0001-Fix-CMakeLists.patch
67 substituteInPlace willuslib/bmpdjvu.c \
68 --replace "<djvu.h>" "<libdjvu/ddjvuapi.h>"
71 nativeBuildInputs = [ cmake pkg-config makeWrapper ];
75 # We use specific versions of these sources below to match the versions
76 # used in the k2pdfopt source. Note that this does _not_ need to match the
77 # version used elsewhere in nixpkgs, since it is only used to create the
78 # patch that can then be applied to the version in nixpkgs.
79 mupdf_patch = mkPatch {
82 url = "https://mupdf.com/downloads/archive/mupdf-1.17.0-source.tar.gz";
83 sha256 = "13nl9nrcx2awz9l83mlv2psi1lmn3hdnfwxvwgwiwbxlkjl3zqq0";
86 cp ${k2pdfopt_src}/mupdf_mod/{filter-basic,font,stext-device,string}.c ./source/fitz/
87 cp ${k2pdfopt_src}/mupdf_mod/pdf-* ./source/pdf/
90 mupdf_modded = mupdf_1_17.overrideAttrs ({ patches ? [], ... }: {
91 patches = patches ++ [ mupdf_patch ];
92 # This function is missing in font.c, see font-win32.c
94 echo "void pdf_install_load_system_font_funcs(fz_context *ctx) {}" >> source/fitz/font.c
98 leptonica_patch = mkPatch {
101 url = "http://www.leptonica.org/source/leptonica-1.79.0.tar.gz";
102 sha256 = "1n004gv1dj3pq1fcnfdclvvx5nang80336aa67nvs3nnqp4ncn84";
104 patchCommands = "cp -r ${k2pdfopt_src}/leptonica_mod/. ./src/";
106 leptonica_modded = leptonica.overrideAttrs ({ patches ? [], ... }: {
107 patches = patches ++ [ leptonica_patch ];
110 tesseract_patch = mkPatch {
112 src = fetchFromGitHub {
113 owner = "tesseract-ocr";
116 sha256 = "1ca27zbjpx35nxh9fha410z3jskwyj06i5hqiqdc08s2d7kdivwn";
119 cp ${k2pdfopt_src}/tesseract_mod/{baseapi,tesscapi,tesseract}.* src/api/
120 cp ${k2pdfopt_src}/tesseract_mod/{tesscapi,tessedit,tesseract}.* src/ccmain/
121 cp ${k2pdfopt_src}/tesseract_mod/dotproduct{avx,fma,sse}.* src/arch/
122 cp ${k2pdfopt_src}/tesseract_mod/{intsimdmatrixsse,simddetect}.* src/arch/
123 cp ${k2pdfopt_src}/tesseract_mod/{errcode,genericvector,mainblk,params,serialis,tessdatamanager,tess_version,tprintf,unicharset}.* src/ccutil/
124 cp ${k2pdfopt_src}/tesseract_mod/{input,lstmrecognizer}.* src/lstm/
125 cp ${k2pdfopt_src}/tesseract_mod/openclwrapper.* src/opencl/
128 tesseract_modded = tesseract4.override {
129 tesseractBase = tesseract4.tesseractBase.overrideAttrs ({ patches ? [], ... }: {
130 patches = patches ++ [ tesseract_patch ];
131 # Additional compilation fixes
133 echo libtesseract_api_la_SOURCES += tesscapi.cpp >> src/api/Makefile.am
134 substituteInPlace src/api/tesseract.h \
135 --replace "#include <leptonica.h>" "//#include <leptonica.h>"
141 lib.optional enableGSL gsl ++
142 lib.optional enableGhostScript ghostscript ++
143 lib.optional enableMuPDF mupdf_modded ++
144 lib.optional enableDJVU djvulibre ++
145 lib.optional enableGOCR gocr ++
146 lib.optionals enableTesseract [ leptonica_modded tesseract_modded ];
148 dontUseCmakeBuildDir = true;
150 cmakeFlags = [ "-DCMAKE_C_FLAGS=-I${src}/include_mod" ];
152 NIX_LDFLAGS = "-lpthread";
155 install -D -m 755 k2pdfopt $out/bin/k2pdfopt
158 preFixup = lib.optionalString enableTesseract ''
159 wrapProgram $out/bin/k2pdfopt --set-default TESSDATA_PREFIX ${tesseract4}/share/tessdata
163 description = "Optimizes PDF/DJVU files for mobile e-readers (e.g. the Kindle) and smartphones";
164 homepage = "http://www.willus.com/k2pdfopt";
165 license = licenses.gpl3;
166 platforms = platforms.linux;
167 maintainers = with maintainers; [ bosu danielfullmer ];