2021-05-13 14:42:22 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2022-04-12 15:48:53 +00:00
|
|
|
cfg = config.services.paperless;
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
defaultUser = "paperless";
|
2023-08-05 18:45:07 +00:00
|
|
|
defaultFont = "${pkgs.liberation_ttf}/share/fonts/truetype/LiberationSerif-Regular.ttf";
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2022-04-12 15:46:57 +00:00
|
|
|
# Don't start a redis instance if the user sets a custom redis connection
|
2023-12-08 01:54:04 +00:00
|
|
|
enableRedis = !(cfg.settings ? PAPERLESS_REDIS);
|
2022-04-12 15:48:53 +00:00
|
|
|
redisServer = config.services.redis.servers.paperless;
|
2021-12-22 19:24:43 +00:00
|
|
|
|
2021-05-13 14:42:22 +00:00
|
|
|
env = {
|
|
|
|
PAPERLESS_DATA_DIR = cfg.dataDir;
|
|
|
|
PAPERLESS_MEDIA_ROOT = cfg.mediaDir;
|
|
|
|
PAPERLESS_CONSUMPTION_DIR = cfg.consumptionDir;
|
2023-08-05 18:45:07 +00:00
|
|
|
PAPERLESS_THUMBNAIL_FONT_NAME = defaultFont;
|
2021-05-13 14:42:22 +00:00
|
|
|
GUNICORN_CMD_ARGS = "--bind=${cfg.address}:${toString cfg.port}";
|
2022-09-10 20:10:06 +00:00
|
|
|
} // optionalAttrs (config.time.timeZone != null) {
|
|
|
|
PAPERLESS_TIME_ZONE = config.time.timeZone;
|
2022-09-04 11:50:17 +00:00
|
|
|
} // optionalAttrs enableRedis {
|
2022-04-12 15:46:57 +00:00
|
|
|
PAPERLESS_REDIS = "unix://${redisServer.unixSocket}";
|
2024-02-01 15:53:15 +00:00
|
|
|
} // optionalAttrs (cfg.settings.PAPERLESS_ENABLE_NLTK or true) {
|
|
|
|
PAPERLESS_NLTK_DIR = pkgs.symlinkJoin {
|
|
|
|
name = "paperless_ngx_nltk_data";
|
2023-12-08 01:53:17 +00:00
|
|
|
paths = cfg.package.nltkData;
|
2024-02-01 15:53:15 +00:00
|
|
|
};
|
2024-03-25 18:15:14 +00:00
|
|
|
} // optionalAttrs (cfg.openMPThreadingWorkaround) {
|
|
|
|
OMP_NUM_THREADS = "1";
|
2023-12-08 01:54:04 +00:00
|
|
|
} // (lib.mapAttrs (_: s:
|
|
|
|
if (lib.isAttrs s || lib.isList s) then builtins.toJSON s
|
|
|
|
else if lib.isBool s then lib.boolToString s
|
|
|
|
else toString s
|
|
|
|
) cfg.settings);
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2023-07-07 15:48:48 +00:00
|
|
|
manage = pkgs.writeShellScript "manage" ''
|
|
|
|
set -o allexport # Export the following env vars
|
|
|
|
${lib.toShellVars env}
|
2023-12-08 01:53:17 +00:00
|
|
|
exec ${cfg.package}/bin/paperless-ngx "$@"
|
2023-07-07 15:48:48 +00:00
|
|
|
'';
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
defaultServiceConfig = {
|
2024-09-12 00:22:22 +00:00
|
|
|
Slice = "system-paperless.slice";
|
|
|
|
# Secure the services
|
2023-10-04 20:03:18 +00:00
|
|
|
ReadWritePaths = [
|
2021-05-13 14:42:22 +00:00
|
|
|
cfg.consumptionDir
|
|
|
|
cfg.dataDir
|
|
|
|
cfg.mediaDir
|
|
|
|
];
|
2023-02-09 21:59:50 +00:00
|
|
|
CacheDirectory = "paperless";
|
2021-05-13 14:42:22 +00:00
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# ProtectClock adds DeviceAllow=char-rtc r
|
|
|
|
DeviceAllow = "";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
2021-12-22 19:24:43 +00:00
|
|
|
PrivateNetwork = true;
|
2021-05-13 14:42:22 +00:00
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
# Breaks if the home dir of the user is in /home
|
|
|
|
# ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
2023-10-04 20:03:18 +00:00
|
|
|
ProtectSystem = "strict";
|
2021-05-13 14:42:22 +00:00
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2022-04-11 15:06:03 +00:00
|
|
|
# Don't restrict ProcSubset because django-q requires read access to /proc/stat
|
|
|
|
# to query CPU and memory information.
|
|
|
|
# Note that /proc only contains processes of user `paperless`, so this is safe.
|
|
|
|
# ProcSubset = "pid";
|
2021-08-23 20:30:54 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
2021-05-13 14:42:22 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2022-04-12 15:46:57 +00:00
|
|
|
SupplementaryGroups = optional enableRedis redisServer.user;
|
2021-05-13 14:42:22 +00:00
|
|
|
SystemCallArchitectures = "native";
|
2022-09-22 06:14:21 +00:00
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ];
|
2023-06-28 14:48:25 +00:00
|
|
|
UMask = "0066";
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2024-08-23 13:44:14 +00:00
|
|
|
meta.maintainers = with maintainers; [ leona SuperSandro2000 erikarvstedt ];
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2021-06-03 13:59:17 +00:00
|
|
|
imports = [
|
2022-04-12 15:48:53 +00:00
|
|
|
(mkRenamedOptionModule [ "services" "paperless-ng" ] [ "services" "paperless" ])
|
2023-12-08 01:54:04 +00:00
|
|
|
(mkRenamedOptionModule [ "services" "paperless" "extraConfig" ] [ "services" "paperless" "settings" ])
|
2021-06-03 13:59:17 +00:00
|
|
|
];
|
|
|
|
|
2022-04-12 15:48:53 +00:00
|
|
|
options.services.paperless = {
|
2021-05-13 14:42:22 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2022-04-12 15:48:53 +00:00
|
|
|
Enable Paperless.
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
When started, the Paperless database is automatically created if it doesn't
|
|
|
|
exist and updated if the Paperless package has changed.
|
|
|
|
Both tasks are achieved by running a Django migration.
|
|
|
|
|
|
|
|
A script to manage the Paperless instance (by wrapping Django's manage.py) is linked to
|
2022-04-12 15:48:53 +00:00
|
|
|
`''${dataDir}/paperless-manage`.
|
2021-05-13 14:42:22 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/paperless";
|
|
|
|
description = "Directory to store the Paperless data.";
|
|
|
|
};
|
|
|
|
|
|
|
|
mediaDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.dataDir}/media";
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression ''"''${dataDir}/media"'';
|
2021-05-13 14:42:22 +00:00
|
|
|
description = "Directory to store the Paperless documents.";
|
|
|
|
};
|
|
|
|
|
|
|
|
consumptionDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.dataDir}/consume";
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression ''"''${dataDir}/consume"'';
|
2021-05-13 14:42:22 +00:00
|
|
|
description = "Directory from which new documents are imported.";
|
|
|
|
};
|
|
|
|
|
|
|
|
consumptionDirIsPublic = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether all users can write to the consumption dir.";
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-04-12 15:48:53 +00:00
|
|
|
example = "/run/keys/paperless-password";
|
2021-05-13 14:42:22 +00:00
|
|
|
description = ''
|
|
|
|
A file containing the superuser password.
|
|
|
|
|
|
|
|
A superuser is required to access the web interface.
|
|
|
|
If unset, you can create a superuser manually by running
|
2022-04-12 15:48:53 +00:00
|
|
|
`''${dataDir}/paperless-manage createsuperuser`.
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
The default superuser name is `admin`. To change it, set
|
2024-01-30 10:22:52 +00:00
|
|
|
option {option}`settings.PAPERLESS_ADMIN_USER`.
|
2021-05-13 14:42:22 +00:00
|
|
|
WARNING: When changing the superuser name after the initial setup, the old superuser
|
|
|
|
will continue to exist.
|
|
|
|
|
|
|
|
To disable login for the web interface, set the following:
|
2024-01-30 10:22:52 +00:00
|
|
|
`settings.PAPERLESS_AUTO_LOGIN_USERNAME = "admin";`.
|
2021-05-13 14:42:22 +00:00
|
|
|
WARNING: Only use this on a trusted system without internet access to Paperless.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = "Web interface address.";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 28981;
|
|
|
|
description = "Web interface port.";
|
|
|
|
};
|
|
|
|
|
2023-12-08 01:54:04 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = lib.types.submodule {
|
|
|
|
freeformType = with lib.types; attrsOf (let
|
|
|
|
typeList = [ bool float int str path package ];
|
|
|
|
in oneOf (typeList ++ [ (listOf (oneOf typeList)) (attrsOf (oneOf typeList)) ]));
|
|
|
|
};
|
2023-02-09 21:36:39 +00:00
|
|
|
default = { };
|
2021-05-13 14:42:22 +00:00
|
|
|
description = ''
|
2022-04-12 15:48:53 +00:00
|
|
|
Extra paperless config options.
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2023-12-08 01:54:04 +00:00
|
|
|
See [the documentation](https://docs.paperless-ngx.com/configuration/) for available options.
|
2023-07-09 17:47:58 +00:00
|
|
|
|
2023-12-08 01:54:04 +00:00
|
|
|
Note that some settings such as `PAPERLESS_CONSUMER_IGNORE_PATTERN` expect JSON values.
|
|
|
|
Settings declared as lists or attrsets will automatically be serialised into JSON strings for your convenience.
|
2023-07-09 17:47:58 +00:00
|
|
|
'';
|
2023-12-08 01:54:04 +00:00
|
|
|
example = {
|
|
|
|
PAPERLESS_OCR_LANGUAGE = "deu+eng";
|
|
|
|
PAPERLESS_DBHOST = "/run/postgresql";
|
|
|
|
PAPERLESS_CONSUMER_IGNORE_PATTERN = [ ".DS_STORE/*" "desktop.ini" ];
|
|
|
|
PAPERLESS_OCR_USER_ARGS = {
|
|
|
|
optimize = 1;
|
|
|
|
pdfa_image_compression = "lossless";
|
2023-07-09 17:47:58 +00:00
|
|
|
};
|
2023-12-08 01:54:04 +00:00
|
|
|
};
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = defaultUser;
|
|
|
|
description = "User under which Paperless runs.";
|
|
|
|
};
|
|
|
|
|
2023-12-08 01:53:17 +00:00
|
|
|
package = mkPackageOption pkgs "paperless-ngx" { } // {
|
|
|
|
apply = pkg: pkg.override {
|
|
|
|
tesseract5 = pkg.tesseract5.override {
|
|
|
|
# always enable detection modules
|
2024-04-19 08:45:08 +00:00
|
|
|
# tesseract fails to build when eng is not present
|
2023-12-08 01:53:17 +00:00
|
|
|
enableLanguages = if cfg.settings ? PAPERLESS_OCR_LANGUAGE then
|
2024-04-19 08:45:08 +00:00
|
|
|
lists.unique (
|
|
|
|
[ "equ" "osd" "eng" ]
|
2023-12-08 01:53:17 +00:00
|
|
|
++ lib.splitString "+" cfg.settings.PAPERLESS_OCR_LANGUAGE
|
2024-04-19 08:45:08 +00:00
|
|
|
)
|
2023-12-08 01:53:17 +00:00
|
|
|
else null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-03-25 18:15:14 +00:00
|
|
|
|
|
|
|
openMPThreadingWorkaround = mkEnableOption ''
|
|
|
|
a workaround for document classifier timeouts.
|
|
|
|
|
|
|
|
Paperless uses OpenBLAS via scikit-learn for document classification.
|
|
|
|
|
|
|
|
The default is to use threading for OpenMP but this would cause the
|
|
|
|
document classifier to spin on one core seemingly indefinitely if there
|
|
|
|
are large amounts of classes per classification; causing it to
|
|
|
|
effectively never complete due to running into timeouts.
|
|
|
|
|
|
|
|
This sets `OMP_NUM_THREADS` to `1` in order to mitigate the issue. See
|
2024-06-03 16:59:45 +00:00
|
|
|
https://github.com/NixOS/nixpkgs/issues/240591 for more information
|
2024-03-25 18:15:14 +00:00
|
|
|
'' // mkOption { default = true; };
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-04-12 15:48:53 +00:00
|
|
|
services.redis.servers.paperless.enable = mkIf enableRedis true;
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2024-09-12 00:22:22 +00:00
|
|
|
systemd.slices.system-paperless = {
|
2024-10-02 18:24:13 +00:00
|
|
|
description = "Paperless Document Management System Slice";
|
2024-09-14 12:11:31 +00:00
|
|
|
documentation = [ "https://docs.paperless-ngx.com" ];
|
2024-09-12 00:22:22 +00:00
|
|
|
};
|
|
|
|
|
2024-04-10 15:30:55 +00:00
|
|
|
systemd.tmpfiles.settings."10-paperless" = let
|
|
|
|
defaultRule = {
|
|
|
|
inherit (cfg) user;
|
|
|
|
inherit (config.users.users.${cfg.user}) group;
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
"${cfg.dataDir}".d = defaultRule;
|
|
|
|
"${cfg.mediaDir}".d = defaultRule;
|
|
|
|
"${cfg.consumptionDir}".d = if cfg.consumptionDirIsPublic then { mode = "777"; } else defaultRule;
|
|
|
|
};
|
2021-05-13 14:42:22 +00:00
|
|
|
|
2022-04-12 15:48:53 +00:00
|
|
|
systemd.services.paperless-scheduler = {
|
2022-12-25 23:07:17 +00:00
|
|
|
description = "Paperless Celery Beat";
|
2023-01-02 14:22:37 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "paperless-consumer.service" "paperless-web.service" "paperless-task-queue.service" ];
|
2021-05-13 14:42:22 +00:00
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
2023-12-08 01:53:17 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/celery --app paperless beat --loglevel INFO";
|
2021-05-13 14:42:22 +00:00
|
|
|
Restart = "on-failure";
|
2024-04-05 07:38:19 +00:00
|
|
|
LoadCredential = lib.optionalString (cfg.passwordFile != null) "PAPERLESS_ADMIN_PASSWORD:${cfg.passwordFile}";
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
environment = env;
|
|
|
|
|
|
|
|
preStart = ''
|
2022-04-12 15:48:53 +00:00
|
|
|
ln -sf ${manage} ${cfg.dataDir}/paperless-manage
|
2021-05-13 14:42:22 +00:00
|
|
|
|
|
|
|
# Auto-migrate on first run or if the package has changed
|
|
|
|
versionFile="${cfg.dataDir}/src-version"
|
2023-01-30 19:20:16 +00:00
|
|
|
version=$(cat "$versionFile" 2>/dev/null || echo 0)
|
|
|
|
|
2023-12-08 01:53:17 +00:00
|
|
|
if [[ $version != ${cfg.package.version} ]]; then
|
|
|
|
${cfg.package}/bin/paperless-ngx migrate
|
2023-01-30 19:20:16 +00:00
|
|
|
|
|
|
|
# Parse old version string format for backwards compatibility
|
|
|
|
version=$(echo "$version" | grep -ohP '[^-]+$')
|
|
|
|
|
|
|
|
versionLessThan() {
|
|
|
|
target=$1
|
|
|
|
[[ $({ echo "$version"; echo "$target"; } | sort -V | head -1) != "$target" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
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."
|
2023-12-08 01:53:17 +00:00
|
|
|
${cfg.package}/bin/paperless-ngx document_index reindex
|
2023-01-30 19:20:16 +00:00
|
|
|
fi
|
|
|
|
|
2023-12-08 01:53:17 +00:00
|
|
|
echo ${cfg.package.version} > "$versionFile"
|
2021-05-13 14:42:22 +00:00
|
|
|
fi
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.passwordFile != null) ''
|
|
|
|
export PAPERLESS_ADMIN_USER="''${PAPERLESS_ADMIN_USER:-admin}"
|
2024-10-14 16:16:09 +00:00
|
|
|
PAPERLESS_ADMIN_PASSWORD=$(cat "$CREDENTIALS_DIRECTORY/PAPERLESS_ADMIN_PASSWORD")
|
|
|
|
export PAPERLESS_ADMIN_PASSWORD
|
2021-05-13 14:42:22 +00:00
|
|
|
superuserState="$PAPERLESS_ADMIN_USER:$PAPERLESS_ADMIN_PASSWORD"
|
|
|
|
superuserStateFile="${cfg.dataDir}/superuser-state"
|
|
|
|
|
2024-10-14 16:16:09 +00:00
|
|
|
if [[ $(cat "$superuserStateFile" 2>/dev/null) != "$superuserState" ]]; then
|
2023-12-08 01:53:17 +00:00
|
|
|
${cfg.package}/bin/paperless-ngx manage_superuser
|
2021-05-13 14:42:22 +00:00
|
|
|
echo "$superuserState" > "$superuserStateFile"
|
|
|
|
fi
|
|
|
|
'';
|
2022-04-12 15:46:57 +00:00
|
|
|
} // optionalAttrs enableRedis {
|
2022-04-12 15:48:53 +00:00
|
|
|
after = [ "redis-paperless.service" ];
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
|
2022-12-25 23:07:17 +00:00
|
|
|
systemd.services.paperless-task-queue = {
|
|
|
|
description = "Paperless Celery Workers";
|
2023-01-02 14:22:37 +00:00
|
|
|
after = [ "paperless-scheduler.service" ];
|
2022-12-25 23:07:17 +00:00
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
2023-12-08 01:53:17 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/celery --app paperless worker --loglevel INFO";
|
2022-12-25 23:07:17 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
# The `mbind` syscall is needed for running the classifier.
|
|
|
|
SystemCallFilter = defaultServiceConfig.SystemCallFilter ++ [ "mbind" ];
|
|
|
|
# Needs to talk to mail server for automated import rules
|
|
|
|
PrivateNetwork = false;
|
|
|
|
};
|
|
|
|
environment = env;
|
|
|
|
};
|
|
|
|
|
2022-04-12 15:48:53 +00:00
|
|
|
systemd.services.paperless-consumer = {
|
2021-05-13 14:42:22 +00:00
|
|
|
description = "Paperless document consumer";
|
2023-01-02 14:22:37 +00:00
|
|
|
# Bind to `paperless-scheduler` so that the consumer never runs
|
|
|
|
# during migrations
|
|
|
|
bindsTo = [ "paperless-scheduler.service" ];
|
|
|
|
after = [ "paperless-scheduler.service" ];
|
2021-05-13 14:42:22 +00:00
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
2023-12-08 01:53:17 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/paperless-ngx document_consumer";
|
2021-05-13 14:42:22 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
|
|
|
environment = env;
|
2024-02-28 08:12:48 +00:00
|
|
|
# Allow the consumer to access the private /tmp directory of the server.
|
|
|
|
# This is required to support consuming files via a local folder.
|
|
|
|
unitConfig.JoinsNamespaceOf = "paperless-task-queue.service";
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
|
2022-04-12 15:48:53 +00:00
|
|
|
systemd.services.paperless-web = {
|
2021-05-13 14:42:22 +00:00
|
|
|
description = "Paperless web server";
|
2023-01-02 14:22:37 +00:00
|
|
|
# Bind to `paperless-scheduler` so that the web server never runs
|
|
|
|
# during migrations
|
|
|
|
bindsTo = [ "paperless-scheduler.service" ];
|
|
|
|
after = [ "paperless-scheduler.service" ];
|
2023-10-18 16:01:34 +00:00
|
|
|
# Setup PAPERLESS_SECRET_KEY.
|
|
|
|
# If this environment variable is left unset, paperless-ngx defaults
|
|
|
|
# to a well-known value, which is insecure.
|
|
|
|
script = let
|
|
|
|
secretKeyFile = "${cfg.dataDir}/nixos-paperless-secret-key";
|
|
|
|
in ''
|
|
|
|
if [[ ! -f '${secretKeyFile}' ]]; then
|
|
|
|
(
|
|
|
|
umask 0377
|
|
|
|
tr -dc A-Za-z0-9 < /dev/urandom | head -c64 | ${pkgs.moreutils}/bin/sponge '${secretKeyFile}'
|
|
|
|
)
|
|
|
|
fi
|
2024-10-14 16:16:09 +00:00
|
|
|
PAPERLESS_SECRET_KEY="$(cat '${secretKeyFile}')"
|
|
|
|
export PAPERLESS_SECRET_KEY
|
2023-10-18 16:01:34 +00:00
|
|
|
if [[ ! $PAPERLESS_SECRET_KEY ]]; then
|
|
|
|
echo "PAPERLESS_SECRET_KEY is empty, refusing to start."
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-12-08 01:53:17 +00:00
|
|
|
exec ${cfg.package.python.pkgs.gunicorn}/bin/gunicorn \
|
|
|
|
-c ${cfg.package}/lib/paperless-ngx/gunicorn.conf.py paperless.asgi:application
|
2023-10-18 16:01:34 +00:00
|
|
|
'';
|
2021-05-13 14:42:22 +00:00
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
2024-03-02 16:50:42 +00:00
|
|
|
LimitNOFILE = 65536;
|
2022-09-04 11:46:35 +00:00
|
|
|
# gunicorn needs setuid, liblapack needs mbind
|
|
|
|
SystemCallFilter = defaultServiceConfig.SystemCallFilter ++ [ "@setuid mbind" ];
|
2021-12-22 19:24:43 +00:00
|
|
|
# Needs to serve web page
|
|
|
|
PrivateNetwork = false;
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
environment = env // {
|
2023-12-08 01:53:17 +00:00
|
|
|
PYTHONPATH = "${cfg.package.python.pkgs.makePythonPath cfg.package.propagatedBuildInputs}:${cfg.package}/lib/paperless-ngx/src";
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
2021-08-23 06:58:16 +00:00
|
|
|
# Allow the web interface to access the private /tmp directory of the server.
|
|
|
|
# This is required to support uploading files via the web interface.
|
2022-12-25 23:07:17 +00:00
|
|
|
unitConfig.JoinsNamespaceOf = "paperless-task-queue.service";
|
2021-05-13 14:42:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
users = optionalAttrs (cfg.user == defaultUser) {
|
|
|
|
users.${defaultUser} = {
|
|
|
|
group = defaultUser;
|
|
|
|
uid = config.ids.uids.paperless;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
};
|
|
|
|
|
|
|
|
groups.${defaultUser} = {
|
|
|
|
gid = config.ids.gids.paperless;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|