14 , enableGSL ? true, gsl
15 , enableGhostScript ? true, ghostscript
16 , enableMuPDF ? true, mupdf
17 , enableDJVU ? true, djvulibre
18 , enableGOCR ? false, gocr # Disabled by default due to crashes
19 , enableTesseract ? true, leptonica, tesseract
22 # k2pdfopt is a pain to package. It requires modified versions of mupdf,
23 # leptonica, and tesseract. Instead of shipping patches for these upstream
24 # packages, k2pdfopt includes just the modified source files for these
25 # packages. The individual files from the {mupdf,leptonica,tesseract}_mod/
26 # directories are intended to replace the corresponding source files in the
27 # upstream packages, for a particular version of that upstream package.
29 # There are a few ways we could approach packaging these modified versions of
30 # mupdf, leptonica, and mupdf:
31 # 1) Override the upstream source with a new derivation that involves copying
32 # the modified source files from k2pdfopt and replacing the corresponding
33 # source files in the upstream packages. Since the files are intended for a
34 # particular version of the upstream package, this would not allow us to easily
35 # use updates to those packages in nixpkgs.
36 # 2) Manually produce patches which can be applied against the upstream
37 # project, and have the same effect as replacing those files. This is what I
38 # believe k2pdfopt should do this for us anyway. The benefit of creating and
39 # applying patches in this way is that minor updates (esp. security fixes) to
40 # upstream packages might still allow these patches to apply successfully.
41 # 3) Automatically produce these patches inside a nix derivation. This is the
42 # approach taken here, using the "mkPatch" provided below. This has the
43 # benefit of easier review and should hopefully be simpler to update in the
47 # Create a patch against src based on changes applied in patchCommands
48 mkPatch = { name, src, patchCommands }: runCommand "${name}-k2pdfopt.patch" { inherit src; } ''
53 new=$sourceRoot-modded
60 diff -Naur $orig $new > $out || true
65 k2pdfopt_src = fetchzip {
66 url = "http://www.willus.com/${pname}/src/${pname}_v${version}_src.zip";
67 hash = "sha256-orQNDXQkkcCtlA8wndss6SiJk4+ImiFCG8XRLEg963k=";
69 in stdenv.mkDerivation rec {
70 inherit pname version;
74 ./0001-Fix-CMakeLists.patch
78 substituteInPlace willuslib/bmpdjvu.c \
79 --replace "<djvu.h>" "<libdjvu/ddjvuapi.h>"
82 nativeBuildInputs = [ cmake pkg-config makeWrapper ];
86 # We use specific versions of these sources below to match the versions
87 # used in the k2pdfopt source. Note that this does _not_ need to match the
88 # version used elsewhere in nixpkgs, since it is only used to create the
89 # patch that can then be applied to the version in nixpkgs.
90 mupdf_patch = mkPatch {
93 url = "https://mupdf.com/downloads/archive/mupdf-1.23.7-source.tar.gz";
94 hash = "sha256-NaVJM/QA6JZnoImkJfHGXNadRiOU/tnAZ558Uu+6pWg=";
97 cp ${k2pdfopt_src}/mupdf_mod/{filter-basic,font,stext-device,string}.c ./source/fitz/
98 cp ${k2pdfopt_src}/mupdf_mod/pdf-* ./source/pdf/
101 mupdf_modded = mupdf.overrideAttrs ({ patches ? [], ... }: {
102 patches = patches ++ [ mupdf_patch ];
103 # This function is missing in font.c, see font-win32.c
105 echo "void pdf_install_load_system_font_funcs(fz_context *ctx) {}" >> source/fitz/font.c
109 leptonica_patch = mkPatch {
112 url = "http://www.leptonica.org/source/leptonica-1.83.0.tar.gz";
113 hash = "sha256-IGWR3VjPhO84CDba0TO1jJ0a+SSR9amCXDRqFiBEvP4=";
115 patchCommands = "cp -r ${k2pdfopt_src}/leptonica_mod/. ./src/";
117 leptonica_modded = leptonica.overrideAttrs ({ patches ? [], ... }: {
118 patches = patches ++ [ leptonica_patch ];
121 tesseract_patch = mkPatch {
123 src = fetchFromGitHub {
124 owner = "tesseract-ocr";
127 hash = "sha256-/aGzwm2+0y8fheOnRi/OJXZy3o0xjY1cCq+B3GTzfos=";
130 cp ${k2pdfopt_src}/tesseract_mod/tesseract.* include/tesseract/
131 cp ${k2pdfopt_src}/tesseract_mod/tesseract/baseapi.h include/tesseract/
132 cp ${k2pdfopt_src}/tesseract_mod/{baseapi,config_auto,tesscapi,tesseract}.* src/api/
133 cp ${k2pdfopt_src}/tesseract_mod/tesseract/baseapi.h src/api/
134 cp ${k2pdfopt_src}/tesseract_mod/{tesscapi,tessedit,tesseract}.* src/ccmain/
135 cp ${k2pdfopt_src}/tesseract_mod/tesseract/baseapi.h src/ccmain/
136 cp ${k2pdfopt_src}/tesseract_mod/dotproduct{avx,fma,sse}.* src/arch/
137 cp ${k2pdfopt_src}/tesseract_mod/{intsimdmatrixsse,simddetect}.* src/arch/
138 cp ${k2pdfopt_src}/tesseract_mod/{errcode,genericvector,mainblk,params,serialis,tessdatamanager,tess_version,tprintf,unicharset}.* src/ccutil/
139 cp ${k2pdfopt_src}/tesseract_mod/{input,lstmrecognizer}.* src/lstm/
140 cp ${k2pdfopt_src}/tesseract_mod/openclwrapper.* src/opencl/
143 tesseract_modded = tesseract.override {
144 tesseractBase = tesseract.tesseractBase.overrideAttrs ({ patches ? [], ... }: {
145 patches = patches ++ [ tesseract_patch ];
146 # Additional compilation fixes
148 echo libtesseract_la_SOURCES += src/api/tesscapi.cpp >> Makefile.am
149 substituteInPlace src/api/tesseract.h \
150 --replace "#include <leptonica.h>" "//#include <leptonica.h>"
151 substituteInPlace include/tesseract/tesseract.h \
152 --replace "#include <leptonica.h>" "//#include <leptonica.h>"
157 [ jbig2dec libjpeg_turbo libpng zlib ] ++
158 lib.optional enableGSL gsl ++
159 lib.optional enableGhostScript ghostscript ++
160 lib.optional enableMuPDF mupdf_modded ++
161 lib.optional enableDJVU djvulibre ++
162 lib.optional enableGOCR gocr ++
163 lib.optionals enableTesseract [ leptonica_modded tesseract_modded ];
165 dontUseCmakeBuildDir = true;
167 cmakeFlags = [ "-DCMAKE_C_FLAGS=-I${src}/include_mod" ];
169 NIX_LDFLAGS = "-lpthread";
172 install -D -m 755 k2pdfopt $out/bin/k2pdfopt
175 preFixup = lib.optionalString enableTesseract ''
176 wrapProgram $out/bin/k2pdfopt --set-default TESSDATA_PREFIX ${tesseract}/share/tessdata
180 description = "Optimizes PDF/DJVU files for mobile e-readers (e.g. the Kindle) and smartphones";
181 homepage = "http://www.willus.com/k2pdfopt";
182 changelog = "https://www.willus.com/k2pdfopt/k2pdfopt_version.txt";
183 license = licenses.gpl3;
184 platforms = platforms.linux;
185 maintainers = with maintainers; [ bosu danielfullmer ];