2023-01-28 21:27:52 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
pkgs,
|
2023-02-07 23:31:55 +00:00
|
|
|
masterModulesPath,
|
2023-02-21 11:37:18 +00:00
|
|
|
inputs,
|
2023-01-28 21:27:52 +00:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2022-02-14 09:49:49 +00:00
|
|
|
psCfg = config.pub-solar;
|
|
|
|
cfg = config.pub-solar.paperless;
|
|
|
|
xdg = config.home-manager.users."${psCfg.user.name}".xdg;
|
2023-01-28 21:27:52 +00:00
|
|
|
in {
|
2023-02-07 23:31:55 +00:00
|
|
|
imports = [
|
|
|
|
"${masterModulesPath}/services/misc/paperless.nix"
|
|
|
|
];
|
|
|
|
|
|
|
|
disabledModules = [
|
|
|
|
"services/misc/paperless.nix"
|
|
|
|
];
|
|
|
|
|
2022-02-14 09:49:49 +00:00
|
|
|
options.pub-solar.paperless = {
|
|
|
|
enable = mkEnableOption "All you need to go paperless";
|
|
|
|
ocrLanguage = mkOption {
|
|
|
|
description = "OCR language";
|
|
|
|
type = types.str;
|
|
|
|
example = "eng+deu";
|
|
|
|
default = "eng";
|
|
|
|
};
|
2023-02-07 23:31:55 +00:00
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
description = "Directory to save data in";
|
|
|
|
type = types.str;
|
|
|
|
example = "/home/pub_solar/Paperless";
|
2023-08-29 11:56:18 +00:00
|
|
|
default = "${xdg.dataHome}/Paperless";
|
2023-02-07 23:31:55 +00:00
|
|
|
};
|
|
|
|
|
2022-02-14 09:49:49 +00:00
|
|
|
consumptionDir = mkOption {
|
|
|
|
description = "Directory to be watched";
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/paperless/consume";
|
2023-02-07 23:31:55 +00:00
|
|
|
default = "/var/lib/paperless/consume";
|
2022-02-14 09:49:49 +00:00
|
|
|
};
|
2023-02-07 23:31:55 +00:00
|
|
|
|
2023-08-29 11:56:18 +00:00
|
|
|
sync = {
|
|
|
|
enable = mkEnableOption ''
|
|
|
|
You can use this option to sync several paperless instances, for example via nextcloud.
|
|
|
|
It will sync the media directory and database, automatically merging sqlite dbs via dump and import.
|
|
|
|
Logs, the classification model, and other files are left unsynced.
|
|
|
|
'';
|
|
|
|
|
|
|
|
masterNode = mkEnableOption "If this node is the master node, it will only export paperless data, otherwise it will only import";
|
|
|
|
|
|
|
|
directory = mkOption {
|
|
|
|
description = "Directory to sync with.";
|
|
|
|
type = types.str;
|
|
|
|
example = "/home/pub_solar/Nextcloud/Paperless";
|
|
|
|
default = "/home/${psCfg.user.name}/Paperless";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-07 14:56:19 +00:00
|
|
|
scannerDefaultDevice = mkOption {
|
2023-02-07 23:31:55 +00:00
|
|
|
description = ''
|
|
|
|
The scanner device. To find this, use `scanimage -L`.
|
2023-02-07 14:56:19 +00:00
|
|
|
|
2023-02-07 23:31:55 +00:00
|
|
|
For example, your output might be the following:
|
2023-02-07 14:56:19 +00:00
|
|
|
|
2023-02-07 23:31:55 +00:00
|
|
|
```
|
|
|
|
device `v4l:/dev/video3' is a Noname Logitech StreamCam virtual device
|
|
|
|
device `hp3900:libusb:005:002' is a Hewlett-Packard Scanjet G3010 flatbed scanner
|
|
|
|
```
|
2023-02-07 14:56:19 +00:00
|
|
|
|
2023-02-07 23:31:55 +00:00
|
|
|
Here, the scannerDevice is `hp3900:libusb:005:002`.
|
2023-02-07 14:56:19 +00:00
|
|
|
'';
|
|
|
|
type = types.str;
|
2023-02-24 17:33:01 +00:00
|
|
|
example = "hp3900:libusb:005:002";
|
|
|
|
default = "";
|
2023-02-07 14:56:19 +00:00
|
|
|
};
|
2022-02-14 09:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-11-28 10:39:28 +00:00
|
|
|
services.paperless = {
|
2022-02-14 09:49:49 +00:00
|
|
|
enable = true;
|
2022-11-28 10:39:28 +00:00
|
|
|
user = psCfg.user.name;
|
2022-02-14 09:49:49 +00:00
|
|
|
consumptionDir = cfg.consumptionDir;
|
2023-02-07 23:31:55 +00:00
|
|
|
dataDir = cfg.dataDir;
|
2023-02-07 14:56:19 +00:00
|
|
|
address = "paperless.local";
|
2022-02-14 09:49:49 +00:00
|
|
|
extraConfig = {
|
|
|
|
PAPERLESS_OCR_LANGUAGE = cfg.ocrLanguage;
|
2022-11-28 10:39:28 +00:00
|
|
|
PAPERLESS_ADMIN_USER = psCfg.user.name;
|
|
|
|
PAPERLESS_AUTO_LOGIN_USERNAME = psCfg.user.name;
|
2023-02-07 14:56:19 +00:00
|
|
|
PAPERLESS_URL = "http://paperless.local";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
home-manager = pkgs.lib.setAttrByPath ["users" psCfg.user.name] {
|
|
|
|
home.packages = with pkgs; [
|
2023-02-21 11:37:18 +00:00
|
|
|
inputs.scan2paperless.legacyPackages.x86_64-linux.scan2paperless
|
|
|
|
sane-backends
|
2023-06-11 20:54:11 +00:00
|
|
|
python310Packages.img2pdf
|
2023-02-07 14:56:19 +00:00
|
|
|
];
|
|
|
|
home.sessionVariables = {
|
|
|
|
SCANNER_DEFAULT_DEVICE = cfg.scannerDefaultDevice;
|
|
|
|
SCANNER_OUTPUT_DIR = cfg.consumptionDir;
|
|
|
|
};
|
|
|
|
systemd.user.sessionVariables = {
|
|
|
|
SCANNER_DEFAULT_DEVICE = cfg.scannerDefaultDevice;
|
|
|
|
SCANNER_OUTPUT_DIR = cfg.consumptionDir;
|
2022-02-14 09:49:49 +00:00
|
|
|
};
|
|
|
|
};
|
2023-08-29 11:56:18 +00:00
|
|
|
|
|
|
|
systemd = let
|
|
|
|
copy-in = pkgs.writeShellScriptBin "copy-in" ''
|
|
|
|
${pkgs.systemd}/bin/systemctl stop paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
|
|
|
|
cp -r ${cfg.sync.directory}/media ${cfg.dataDir}/media
|
|
|
|
cp ${cfg.sync.directory}/db.sqlite3 ${cfg.dataDir}/db.sqlite3
|
|
|
|
${pkgs.systemd}/bin/systemctl start paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
|
|
|
|
'';
|
|
|
|
copy-out = pkgs.writeShellScriptBin "copy-out" ''
|
|
|
|
${pkgs.systemd}/bin/systemctl stop paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
|
|
|
|
cp -r ${cfg.dataDir}/media ${cfg.sync.directory}/media
|
|
|
|
cp ${cfg.dataDir}/db.sqlite3 ${cfg.sync.directory}/db.sqlite3
|
|
|
|
${pkgs.systemd}/bin/systemctl start paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
|
|
|
|
'';
|
|
|
|
in mkIf cfg.sync.enable {
|
|
|
|
services.nextcloud-paperless-autosync = {
|
|
|
|
unitConfig = {
|
|
|
|
Description = "Auto sync paperless to or from Nextcloud";
|
|
|
|
After = "network-online.target";
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart= if cfg.sync.masterNode then "${copy-out}/bin/copy-out" else "${copy-in}/bin/copy-in";
|
|
|
|
TimeoutStopSec = "180";
|
|
|
|
KillMode = "process";
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
};
|
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
};
|
|
|
|
|
|
|
|
timers.nextcloud-paperless-autosync = {
|
|
|
|
unitConfig.Description = "Automatic sync files with Nextcloud when booted up after 5 minutes then rerun every 30 minutes";
|
|
|
|
timerConfig.OnUnitActiveSec = "30min";
|
|
|
|
wantedBy = ["multi-user.target" "timers.target"];
|
|
|
|
};
|
|
|
|
};
|
2022-02-14 09:49:49 +00:00
|
|
|
};
|
|
|
|
}
|