1 { lib, makeWrapper, tesseractBase, languages
2 , runCommand, imagemagick
4 # A list of languages like [ "eng" "spa" … ] or `null` for all available languages
5 , enableLanguages ? null
7 # A list of files or a directory containing files
8 , tessdata ? (if enableLanguages == null then languages.all
9 else map (lang: languages.${lang}) enableLanguages)
11 # This argument is obsolete
12 , enableLanguagesHash ? null
16 tesseractWithData = tesseractBase.overrideAttrs (_: {
17 inherit tesseractBase tessdata;
19 nativeBuildInputs = [ makeWrapper ];
22 makeWrapper {$tesseractBase,$out}/bin/tesseract --set-default TESSDATA_PREFIX $out/share/tessdata
24 # Recursively link include, share
25 cp -rs --no-preserve=mode $tesseractBase/{include,share} $out
27 cp -r --no-preserve=mode $tesseractBase/lib $out
28 # Fixup the store paths in lib so that the tessdata from this derivation is used.
29 if (( ''${#tesseractBase} != ''${#out} )); then
30 echo "Can't replace store paths due to differing lengths"
33 find $out/lib -type f -exec sed -i "s|$tesseractBase|$out|g" {} \;
35 if [[ -d "$tessdata" ]]; then
36 ln -s $tessdata/* $out/share/tessdata
38 for lang in $tessdata; do
39 ln -s $lang $out/share/tessdata/''${lang#/nix/store*-}
43 if [[ ! -e $out/share/tessdata/eng.traineddata ]]; then
44 # This is a bug in Tesseract's internal tessdata discovery mechanism
45 echo "eng.traineddata must be present in tessdata for Tesseract to work"
52 passthru = { inherit tesseractBase languages tessdata; };
54 # Only run test when all languages are available
55 test = lib.optionalAttrs (enableLanguages == null) {
56 tests.default = runCommand "tesseract-test-ocr" {
64 convert -size 400x40 xc:white -font 'DejaVu-Sans' -pointsize 20 \
65 -fill black -annotate +5+20 "$text" /tmp/test-img.png 2>/dev/null
66 ocrResult=$(tesseract /tmp/test-img.png - | tr -d "\f")
68 if [[ $ocrResult != $text ]]; then
69 echo "OCR test failed"
70 echo "expected: '$text'"
71 echo "actual: '$ocrResult'"
78 tesseract = (if enableLanguages == [] then tesseractBase else tesseractWithData) // passthru // test;
80 if enableLanguagesHash == null then
83 lib.warn "Argument `enableLanguagesHash` is obsolete and can be removed."