Skip to content

Commit

Permalink
Merge pull request NixOS#303388 from SuperSandro2000/paperless-only-e…
Browse files Browse the repository at this point in the history
…nabled-languages

nixos/paperless: override enabled tesseract languages with the in paperless configured ones
  • Loading branch information
leona-ya authored Apr 14, 2024
2 parents b230780 + b579dac commit 025d3a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
39 changes: 24 additions & 15 deletions nixos/modules/services/misc/paperless.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
with lib;
let
cfg = config.services.paperless;
pkg = cfg.package;

defaultUser = "paperless";
defaultFont = "${pkgs.liberation_ttf}/share/fonts/truetype/LiberationSerif-Regular.ttf";
Expand All @@ -25,7 +24,7 @@ let
} // optionalAttrs (cfg.settings.PAPERLESS_ENABLE_NLTK or true) {
PAPERLESS_NLTK_DIR = pkgs.symlinkJoin {
name = "paperless_ngx_nltk_data";
paths = pkg.nltkData;
paths = cfg.package.nltkData;
};
} // optionalAttrs (cfg.openMPThreadingWorkaround) {
OMP_NUM_THREADS = "1";
Expand All @@ -38,7 +37,7 @@ let
manage = pkgs.writeShellScript "manage" ''
set -o allexport # Export the following env vars
${lib.toShellVars env}
exec ${pkg}/bin/paperless-ngx "$@"
exec ${cfg.package}/bin/paperless-ngx "$@"
'';

# Secure the services
Expand Down Expand Up @@ -200,7 +199,17 @@ in
description = "User under which Paperless runs.";
};

package = mkPackageOption pkgs "paperless-ngx" { };
package = mkPackageOption pkgs "paperless-ngx" { } // {
apply = pkg: pkg.override {
tesseract5 = pkg.tesseract5.override {
# always enable detection modules
enableLanguages = if cfg.settings ? PAPERLESS_OCR_LANGUAGE then
[ "equ" "osd" ]
++ lib.splitString "+" cfg.settings.PAPERLESS_OCR_LANGUAGE
else null;
};
};
};

openMPThreadingWorkaround = mkEnableOption ''
a workaround for document classifier timeouts.
Expand Down Expand Up @@ -237,7 +246,7 @@ in
wants = [ "paperless-consumer.service" "paperless-web.service" "paperless-task-queue.service" ];
serviceConfig = defaultServiceConfig // {
User = cfg.user;
ExecStart = "${pkg}/bin/celery --app paperless beat --loglevel INFO";
ExecStart = "${cfg.package}/bin/celery --app paperless beat --loglevel INFO";
Restart = "on-failure";
LoadCredential = lib.optionalString (cfg.passwordFile != null) "PAPERLESS_ADMIN_PASSWORD:${cfg.passwordFile}";
};
Expand All @@ -250,8 +259,8 @@ in
versionFile="${cfg.dataDir}/src-version"
version=$(cat "$versionFile" 2>/dev/null || echo 0)
if [[ $version != ${pkg.version} ]]; then
${pkg}/bin/paperless-ngx migrate
if [[ $version != ${cfg.package.version} ]]; then
${cfg.package}/bin/paperless-ngx migrate
# Parse old version string format for backwards compatibility
version=$(echo "$version" | grep -ohP '[^-]+$')
Expand All @@ -264,10 +273,10 @@ in
if versionLessThan 1.12.0; then
# Reindex documents as mentioned in https://github.com/paperless-ngx/paperless-ngx/releases/tag/v1.12.1
echo "Reindexing documents, to allow searching old comments. Required after the 1.12.x upgrade."
${pkg}/bin/paperless-ngx document_index reindex
${cfg.package}/bin/paperless-ngx document_index reindex
fi
echo ${pkg.version} > "$versionFile"
echo ${cfg.package.version} > "$versionFile"
fi
''
+ optionalString (cfg.passwordFile != null) ''
Expand All @@ -277,7 +286,7 @@ in
superuserStateFile="${cfg.dataDir}/superuser-state"
if [[ $(cat "$superuserStateFile" 2>/dev/null) != $superuserState ]]; then
${pkg}/bin/paperless-ngx manage_superuser
${cfg.package}/bin/paperless-ngx manage_superuser
echo "$superuserState" > "$superuserStateFile"
fi
'';
Expand All @@ -290,7 +299,7 @@ in
after = [ "paperless-scheduler.service" ];
serviceConfig = defaultServiceConfig // {
User = cfg.user;
ExecStart = "${pkg}/bin/celery --app paperless worker --loglevel INFO";
ExecStart = "${cfg.package}/bin/celery --app paperless worker --loglevel INFO";
Restart = "on-failure";
# The `mbind` syscall is needed for running the classifier.
SystemCallFilter = defaultServiceConfig.SystemCallFilter ++ [ "mbind" ];
Expand All @@ -308,7 +317,7 @@ in
after = [ "paperless-scheduler.service" ];
serviceConfig = defaultServiceConfig // {
User = cfg.user;
ExecStart = "${pkg}/bin/paperless-ngx document_consumer";
ExecStart = "${cfg.package}/bin/paperless-ngx document_consumer";
Restart = "on-failure";
};
environment = env;
Expand Down Expand Up @@ -340,8 +349,8 @@ in
echo "PAPERLESS_SECRET_KEY is empty, refusing to start."
exit 1
fi
exec ${pkg.python.pkgs.gunicorn}/bin/gunicorn \
-c ${pkg}/lib/paperless-ngx/gunicorn.conf.py paperless.asgi:application
exec ${cfg.package.python.pkgs.gunicorn}/bin/gunicorn \
-c ${cfg.package}/lib/paperless-ngx/gunicorn.conf.py paperless.asgi:application
'';
serviceConfig = defaultServiceConfig // {
User = cfg.user;
Expand All @@ -357,7 +366,7 @@ in
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
};
environment = env // {
PYTHONPATH = "${pkg.python.pkgs.makePythonPath pkg.propagatedBuildInputs}:${pkg}/lib/paperless-ngx/src";
PYTHONPATH = "${cfg.package.python.pkgs.makePythonPath cfg.package.propagatedBuildInputs}:${cfg.package}/lib/paperless-ngx/src";
};
# Allow the web interface to access the private /tmp directory of the server.
# This is required to support uploading files via the web interface.
Expand Down
9 changes: 6 additions & 3 deletions pkgs/applications/office/paperless-ngx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ let
# https://github.com/NixOS/nixpkgs/issues/298719
# https://github.com/paperless-ngx/paperless-ngx/issues/5494
python = python3.override {
packageOverrides = self: super: {
uvicorn = super.uvicorn.overridePythonAttrs (oldAttrs: {
packageOverrides = final: prev: {
# tesseract5 may be overwritten in the paperless module and we need to propagate that to make the closure reduction effective
ocrmypdf = prev.ocrmypdf.override { tesseract = tesseract5; };

uvicorn = prev.uvicorn.overridePythonAttrs (_: {
version = "0.25.0";
src = fetchFromGitHub {
owner = "encode";
Expand Down Expand Up @@ -245,7 +248,7 @@ python.pkgs.buildPythonApplication rec {
doCheck = !stdenv.isDarwin;

passthru = {
inherit python path frontend;
inherit python path frontend tesseract5;
nltkData = with nltk-data; [ punkt snowball_data stopwords ];
tests = { inherit (nixosTests) paperless; };
};
Expand Down

0 comments on commit 025d3a2

Please sign in to comment.