os/hosts/fae/paperless.nix

125 lines
3.3 KiB
Nix

{
flake,
lib,
config,
pkgs,
...
}:
with lib; let
psCfg = config.pub-solar;
xdg = config.home-manager.users."${psCfg.user.name}".xdg;
dataDir = "${xdg.dataHome}/Paperless";
backupDir = "${xdg.dataHome}/PaperlessBackup";
consumptionDir = "/home/${psCfg.user.name}/.local/share/scandir";
scan2paperless = with pkgs; writeShellScriptBin "scan2paperless" ''
DEVICE=$1
NUM_PAGES=$2
NAME=$3
if [ -z "''${DEVICE}" ] || [ -z "''${NUM_PAGES}" ] || [ -z "''${NAME}" ]; then
echo "Usage: scan2paperless <device> <num_pages> <name>"
exit 1
fi
tmpDir=$(${coreutils}/bin/mktemp -d)
files=()
for i in $(seq 1 $NUM_PAGES); do
fileName=$(${openssl}/bin/openssl rand -hex 12)
file="$tmpDir/$fileName.jpg"
echo "Start scanning page $i/$NUM_PAGES";
${sane-backends}/bin/scanimage -d $DEVICE --format=jpeg --resolution 300 --progress -o $file
echo "Finished scanning page $i";
files+=($file)
done
pdf="${consumptionDir}/$NAME.pdf"
${python3Packages.img2pdf}/bin/img2pdf --output $pdf ''${files[@]}
echo "PDF written to $pdf"
'';
in {
services.paperless = {
enable = true;
user = psCfg.user.name;
consumptionDir = consumptionDir;
dataDir = dataDir;
address = "127.0.0.1";
extraConfig = {
PAPERLESS_OCR_LANGUAGE = "nld+deu";
PAPERLESS_ADMIN_USER = psCfg.user.name;
PAPERLESS_AUTO_LOGIN_USERNAME = psCfg.user.name;
PAPERLESS_URL = "https://paperless.local";
};
};
#hardware.sane = {
# enable = true;
# extraBackends = [pkgs.hplipWithPlugin];
#};
users.users."${psCfg.user.name}".packages = with pkgs; [
scan2paperless
python3Packages.img2pdf
];
home-manager.users."${psCfg.user.name}" = {
home.sessionVariables = {
SCANNER_OUTPUT_DIR = consumptionDir;
};
systemd.user.sessionVariables = {
SCANNER_OUTPUT_DIR = consumptionDir;
};
};
#security.acme.certs = {
# "paperless.b12f.io" = {};
#};
services.caddy = {
enable = true;
globalConfig = ''
local_certs
'';
virtualHosts = {
"paperless.fritz.box" = {
extraConfig = ''
reverse_proxy :${builtins.toString config.services.paperless.port}
'';
};
};
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
systemd.tmpfiles.rules = [
"d /home/${psCfg.user.name}/.local 0700 ${psCfg.user.name} users - -"
"d /home/${psCfg.user.name}/.local/share 0700 ${psCfg.user.name} users - -"
"d '${backupDir}' 0700 ${psCfg.user.name} users - -"
];
#age.secrets."rclone-pie.conf" = {
# file = "${flake.self}/secrets/rclone-pie.conf.age";
# path = "/root/.config/rclone/rclone.conf";
# mode = "400";
#};
#age.secrets."restic-password" = {
# file = "${flake.self}/secrets/restic-password.age";
# mode = "400";
#};
#services.restic.backups = {
# paperless = {
# paths = [ backupDir ];
# initialize = true;
# passwordFile = config.age.secrets."restic-password".path;
# # See https://www.hosting.de/blog/verschluesselte-backups-mit-rclone-und-restic-in-nextcloud/
# repository = "rclone:cloud.pub.solar:/backups/Paperless";
# backupPrepareCommand = "${dataDir}/paperless-manage document_exporter ${backupDir} -c -p";
# rcloneConfigFile = config.age.secrets."rclone-pie.conf".path;
# };
#};
}