os/modules/paperless/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
5.4 KiB
Nix
Raw Normal View History

2023-01-28 21:27:52 +00:00
{
lib,
config,
pkgs,
...
}:
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 {
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";
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
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";
2023-08-29 12:55:25 +00:00
default = "/home/${psCfg.user.name}/Nextcloud/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-09-12 20:07:05 +00:00
scan2paperless
2023-02-21 11:37:18 +00:00
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
};
};
systemd = let
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}/
cp ${cfg.dataDir}/db.sqlite3 ${cfg.sync.directory}/db.sqlite3
2023-08-29 12:55:25 +00:00
cp ${cfg.dataDir}/celerybeat-schedule.db ${cfg.sync.directory}/celerybeat-schedule.db
cp ${cfg.dataDir}/classification_model.pickle ${cfg.sync.directory}/classification_model.pickle
cp ${cfg.dataDir}/src-version ${cfg.sync.directory}/src-version
chown -R ${psCfg.user.name}:users ${cfg.sync.directory}
2023-08-29 12:55:25 +00:00
${pkgs.systemd}/bin/systemctl start paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
'';
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}/
2023-08-29 12:55:25 +00:00
cp ${cfg.sync.directory}/db.sqlite3 ${cfg.dataDir}/db.sqlite3
cp ${cfg.sync.directory}/celerybeat-schedule.db ${cfg.dataDir}/celerybeat-schedule.db
cp ${cfg.sync.directory}/classification_model.pickle ${cfg.dataDir}/classification_model.pickle
cp ${cfg.sync.directory}/src-version ${cfg.dataDir}/src-version
${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
};
}