2021-08-18 13:19:15 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2022-04-15 01:41:22 +00:00
|
|
|
cfg = config.services.paperless;
|
2022-09-09 14:08:57 +00:00
|
|
|
pkg = cfg.package;
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
defaultUser = "paperless";
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
# Don't start a redis instance if the user sets a custom redis connection
|
|
|
|
enableRedis = !hasAttr "PAPERLESS_REDIS" cfg.extraConfig;
|
|
|
|
redisServer = config.services.redis.servers.paperless;
|
2022-02-20 05:27:41 +00:00
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
env = {
|
|
|
|
PAPERLESS_DATA_DIR = cfg.dataDir;
|
|
|
|
PAPERLESS_MEDIA_ROOT = cfg.mediaDir;
|
|
|
|
PAPERLESS_CONSUMPTION_DIR = cfg.consumptionDir;
|
|
|
|
GUNICORN_CMD_ARGS = "--bind=${cfg.address}:${toString cfg.port}";
|
2022-09-10 19:16:41 +00:00
|
|
|
} // optionalAttrs (config.time.timeZone != null) {
|
|
|
|
PAPERLESS_TIME_ZONE = config.time.timeZone;
|
2022-09-14 18:05:37 +00:00
|
|
|
} // optionalAttrs enableRedis {
|
|
|
|
PAPERLESS_REDIS = "unix://${redisServer.unixSocket}";
|
2022-02-20 05:27:41 +00:00
|
|
|
} // (
|
|
|
|
lib.mapAttrs (_: toString) cfg.extraConfig
|
2022-09-14 18:05:37 +00:00
|
|
|
);
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
manage = let
|
|
|
|
setupEnv = lib.concatStringsSep "\n" (mapAttrsToList (name: val: "export ${name}=\"${val}\"") env);
|
|
|
|
in pkgs.writeShellScript "manage" ''
|
|
|
|
${setupEnv}
|
2022-09-09 14:08:57 +00:00
|
|
|
exec ${pkg}/bin/paperless-ngx "$@"
|
2021-08-18 13:19:15 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# Secure the services
|
|
|
|
defaultServiceConfig = {
|
|
|
|
TemporaryFileSystem = "/:ro";
|
|
|
|
BindReadOnlyPaths = [
|
|
|
|
"/nix/store"
|
|
|
|
"-/etc/resolv.conf"
|
|
|
|
"-/etc/nsswitch.conf"
|
|
|
|
"-/etc/hosts"
|
|
|
|
"-/etc/localtime"
|
2021-08-25 08:27:29 +00:00
|
|
|
"-/run/postgresql"
|
2022-04-15 01:41:22 +00:00
|
|
|
] ++ (optional enableRedis redisServer.unixSocket);
|
2021-08-18 13:19:15 +00:00
|
|
|
BindPaths = [
|
|
|
|
cfg.consumptionDir
|
|
|
|
cfg.dataDir
|
|
|
|
cfg.mediaDir
|
|
|
|
];
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# ProtectClock adds DeviceAllow=char-rtc r
|
|
|
|
DeviceAllow = "";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
2022-02-20 05:27:41 +00:00
|
|
|
PrivateNetwork = true;
|
2021-08-18 13:19:15 +00:00
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
# Breaks if the home dir of the user is in /home
|
|
|
|
# Also does not add much value in combination with the TemporaryFileSystem.
|
|
|
|
# ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
# Would re-mount paths ignored by temporary root
|
|
|
|
#ProtectSystem = "strict";
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2022-04-15 01:41:22 +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-25 08:27:29 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2022-04-15 01:41:22 +00:00
|
|
|
SupplementaryGroups = optional enableRedis redisServer.user;
|
2021-08-18 13:19:15 +00:00
|
|
|
SystemCallArchitectures = "native";
|
2022-09-30 11:47:45 +00:00
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
# Does not work well with the temporary root
|
|
|
|
#UMask = "0066";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2022-07-14 12:49:19 +00:00
|
|
|
meta.maintainers = with maintainers; [ erikarvstedt Flakebi ];
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
imports = [
|
2022-04-15 01:41:22 +00:00
|
|
|
(mkRenamedOptionModule [ "services" "paperless-ng" ] [ "services" "paperless" ])
|
2021-08-18 13:19:15 +00:00
|
|
|
];
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
options.services.paperless = {
|
2021-08-18 13:19:15 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-04-15 01:41:22 +00:00
|
|
|
Enable Paperless.
|
2021-08-18 13:19:15 +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-08-12 12:06:08 +00:00
|
|
|
`''${dataDir}/paperless-manage`.
|
2021-08-18 13:19:15 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/paperless";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Directory to store the Paperless data.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mediaDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.dataDir}/media";
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression ''"''${dataDir}/media"'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Directory to store the Paperless documents.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
consumptionDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.dataDir}/consume";
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression ''"''${dataDir}/consume"'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Directory from which new documents are imported.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
consumptionDirIsPublic = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether all users can write to the consumption dir.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-04-15 01:41:22 +00:00
|
|
|
example = "/run/keys/paperless-password";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-08-18 13:19:15 +00:00
|
|
|
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-08-12 12:06:08 +00:00
|
|
|
`''${dataDir}/paperless-manage createsuperuser`.
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
The default superuser name is `admin`. To change it, set
|
|
|
|
option {option}`extraConfig.PAPERLESS_ADMIN_USER`.
|
2021-08-18 13:19:15 +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:
|
2022-08-12 12:06:08 +00:00
|
|
|
`extraConfig.PAPERLESS_AUTO_LOGIN_USERNAME = "admin";`.
|
2021-08-18 13:19:15 +00:00
|
|
|
WARNING: Only use this on a trusted system without internet access to Paperless.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Web interface address.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 28981;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Web interface port.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-04-15 01:41:22 +00:00
|
|
|
Extra paperless config options.
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
See [the documentation](https://paperless-ngx.readthedocs.io/en/latest/configuration.html)
|
2021-08-18 13:19:15 +00:00
|
|
|
for available options.
|
|
|
|
'';
|
2022-09-09 14:08:57 +00:00
|
|
|
example = {
|
|
|
|
PAPERLESS_OCR_LANGUAGE = "deu+eng";
|
|
|
|
PAPERLESS_DBHOST = "/run/postgresql";
|
|
|
|
};
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = defaultUser;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "User under which Paperless runs.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2022-04-15 01:41:22 +00:00
|
|
|
default = pkgs.paperless-ngx;
|
|
|
|
defaultText = literalExpression "pkgs.paperless-ngx";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The Paperless package to use.";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-04-15 01:41:22 +00:00
|
|
|
services.redis.servers.paperless.enable = mkIf enableRedis true;
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' - ${cfg.user} ${config.users.users.${cfg.user}.group} - -"
|
|
|
|
"d '${cfg.mediaDir}' - ${cfg.user} ${config.users.users.${cfg.user}.group} - -"
|
|
|
|
(if cfg.consumptionDirIsPublic then
|
|
|
|
"d '${cfg.consumptionDir}' 777 - - - -"
|
|
|
|
else
|
|
|
|
"d '${cfg.consumptionDir}' - ${cfg.user} ${config.users.users.${cfg.user}.group} - -"
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
systemd.services.paperless-scheduler = {
|
2022-12-28 21:21:41 +00:00
|
|
|
description = "Paperless Celery Beat";
|
2021-08-18 13:19:15 +00:00
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
2022-12-28 21:21:41 +00:00
|
|
|
ExecStart = "${pkg}/bin/celery --app paperless beat --loglevel INFO";
|
2021-08-18 13:19:15 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
|
|
|
environment = env;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-12-28 21:21:41 +00:00
|
|
|
wants = [ "paperless-consumer.service" "paperless-web.service" "paperless-task-queue.service" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
preStart = ''
|
2022-04-15 01:41:22 +00:00
|
|
|
ln -sf ${manage} ${cfg.dataDir}/paperless-manage
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
# Auto-migrate on first run or if the package has changed
|
|
|
|
versionFile="${cfg.dataDir}/src-version"
|
2022-09-09 14:08:57 +00:00
|
|
|
if [[ $(cat "$versionFile" 2>/dev/null) != ${pkg} ]]; then
|
|
|
|
${pkg}/bin/paperless-ngx migrate
|
|
|
|
echo ${pkg} > "$versionFile"
|
2021-08-18 13:19:15 +00:00
|
|
|
fi
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.passwordFile != null) ''
|
|
|
|
export PAPERLESS_ADMIN_USER="''${PAPERLESS_ADMIN_USER:-admin}"
|
|
|
|
export PAPERLESS_ADMIN_PASSWORD=$(cat "${cfg.dataDir}/superuser-password")
|
|
|
|
superuserState="$PAPERLESS_ADMIN_USER:$PAPERLESS_ADMIN_PASSWORD"
|
|
|
|
superuserStateFile="${cfg.dataDir}/superuser-state"
|
|
|
|
|
|
|
|
if [[ $(cat "$superuserStateFile" 2>/dev/null) != $superuserState ]]; then
|
2022-09-09 14:08:57 +00:00
|
|
|
${pkg}/bin/paperless-ngx manage_superuser
|
2021-08-18 13:19:15 +00:00
|
|
|
echo "$superuserState" > "$superuserStateFile"
|
|
|
|
fi
|
|
|
|
'';
|
2022-04-15 01:41:22 +00:00
|
|
|
} // optionalAttrs enableRedis {
|
|
|
|
after = [ "redis-paperless.service" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
2022-12-28 21:21:41 +00:00
|
|
|
systemd.services.paperless-task-queue = {
|
|
|
|
description = "Paperless Celery Workers";
|
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
|
|
|
ExecStart = "${pkg}/bin/celery --app paperless worker --loglevel INFO";
|
|
|
|
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-15 01:41:22 +00:00
|
|
|
# Reading the user-provided password file requires root access
|
|
|
|
systemd.services.paperless-copy-password = mkIf (cfg.passwordFile != null) {
|
|
|
|
requiredBy = [ "paperless-scheduler.service" ];
|
|
|
|
before = [ "paperless-scheduler.service" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
|
|
|
${pkgs.coreutils}/bin/install --mode 600 --owner '${cfg.user}' --compare \
|
|
|
|
'${cfg.passwordFile}' '${cfg.dataDir}/superuser-password'
|
|
|
|
'';
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
systemd.services.paperless-consumer = {
|
2021-08-18 13:19:15 +00:00
|
|
|
description = "Paperless document consumer";
|
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
2022-09-09 14:08:57 +00:00
|
|
|
ExecStart = "${pkg}/bin/paperless-ngx document_consumer";
|
2021-08-18 13:19:15 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
};
|
|
|
|
environment = env;
|
2022-04-15 01:41:22 +00:00
|
|
|
# Bind to `paperless-scheduler` so that the consumer never runs
|
2021-08-18 13:19:15 +00:00
|
|
|
# during migrations
|
2022-04-15 01:41:22 +00:00
|
|
|
bindsTo = [ "paperless-scheduler.service" ];
|
|
|
|
after = [ "paperless-scheduler.service" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
systemd.services.paperless-web = {
|
2021-08-18 13:19:15 +00:00
|
|
|
description = "Paperless web server";
|
|
|
|
serviceConfig = defaultServiceConfig // {
|
|
|
|
User = cfg.user;
|
|
|
|
ExecStart = ''
|
2022-09-14 18:05:37 +00:00
|
|
|
${pkg.python.pkgs.gunicorn}/bin/gunicorn \
|
2022-09-09 14:08:57 +00:00
|
|
|
-c ${pkg}/lib/paperless-ngx/gunicorn.conf.py paperless.asgi:application
|
2021-08-18 13:19:15 +00:00
|
|
|
'';
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
# gunicorn needs setuid, liblapack needs mbind
|
|
|
|
SystemCallFilter = defaultServiceConfig.SystemCallFilter ++ [ "@setuid mbind" ];
|
2022-02-20 05:27:41 +00:00
|
|
|
# Needs to serve web page
|
|
|
|
PrivateNetwork = false;
|
2022-09-09 14:08:57 +00:00
|
|
|
} // lib.optionalAttrs (cfg.port < 1024) {
|
|
|
|
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
|
|
|
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
|
|
|
environment = env // {
|
2022-09-09 14:08:57 +00:00
|
|
|
PATH = mkForce pkg.path;
|
|
|
|
PYTHONPATH = "${pkg.python.pkgs.makePythonPath pkg.propagatedBuildInputs}:${pkg}/lib/paperless-ngx/src";
|
2021-08-18 13:19:15 +00:00
|
|
|
};
|
2021-09-18 10:52:07 +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-28 21:21:41 +00:00
|
|
|
unitConfig.JoinsNamespaceOf = "paperless-task-queue.service";
|
2022-04-15 01:41:22 +00:00
|
|
|
# Bind to `paperless-scheduler` so that the web server never runs
|
2021-08-18 13:19:15 +00:00
|
|
|
# during migrations
|
2022-04-15 01:41:22 +00:00
|
|
|
bindsTo = [ "paperless-scheduler.service" ];
|
|
|
|
after = [ "paperless-scheduler.service" ];
|
2021-08-18 13:19:15 +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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|