nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

commit b7c43b1f0748839946ab1c80f65ff205ff5e9ebd
parent 37734a537eacb06ff14517a2e7d6d610046c433a
Author: breadcat <breadcat@users.noreply.github.com>
Date:   Mon, 26 Jan 2026 16:00:25 +0000

Preliminary scanner support

Diffstat:
Acommon/scanning.nix | 6++++++
Mmachines/atlas.nix | 1+
Ascripts/scan-to-pdf.nix | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/common/scanning.nix b/common/scanning.nix @@ -0,0 +1,6 @@ +{ username, ... }: + +{ + hardware.sane.enable = true; + users.users."${username}".extraGroups = [ "scanner" ]; +} diff --git a/machines/atlas.nix b/machines/atlas.nix @@ -22,6 +22,7 @@ let machine = "atlas"; in { ../common/nur.nix ../common/packages-unfree.nix ../common/packages.nix + ../common/scanning.nix ../common/ssh.nix ../common/steam.nix ../common/syncthing.nix diff --git a/scripts/scan-to-pdf.nix b/scripts/scan-to-pdf.nix @@ -0,0 +1,72 @@ +{ pkgs, ... }: + +{ + environment.systemPackages = [ + (pkgs.writeShellScriptBin "scan-to-pdf" '' + set -e + + # Default values + MODE="Gray" + SOURCE="ADF Front" + + # Parse arguments in any order + for arg in "$@"; do + case "$arg" in + color) + MODE="Color" + ;; + duplex) + SOURCE="ADF Duplex" + ;; + *) + echo "Unknown argument: $arg" + echo "Usage: scan-to-pdf [color] [duplex]" + exit 1 + ;; + esac + done + + # Display scanning mode + if [ "$MODE" = "Color" ]; then + echo "Scanning in color mode" + else + echo "Scanning in greyscale, add 'color' argument to scan in full color" + fi + + if [ "$SOURCE" = "ADF Duplex" ]; then + echo "Scanning both sides (duplex mode)" + else + echo "Scanning single-sided, add 'duplex' argument to scan both sides" + fi + + OUTPUT="output-$(date +%Y%m%d-%H%M%S).pdf" + TMPDIR=$(mktemp -d) + + cleanup() { + rm -rf "$TMPDIR" + } + trap cleanup EXIT + + echo "Scanning pages..." + if ! ${pkgs.sane-backends}/bin/scanimage --format=tiff --resolution 300 --mode "$MODE" \ + --batch="$TMPDIR/page%d.tiff" --source "$SOURCE"; then + echo "Error: Scanning failed. Is there paper in the ADF?" + exit 1 + fi + + if ! ls "$TMPDIR"/*.tiff 1> /dev/null 2>&1; then + echo "Error: No pages were scanned." + exit 1 + fi + + echo "Converting to PDF..." + ${pkgs.imagemagick}/bin/magick "$TMPDIR"/*.tiff "$TMPDIR/combined.pdf" + + echo "Running OCR..." + ${pkgs.ocrmypdf}/bin/ocrmypdf --deskew --rotate-pages --rotate-pages-threshold 0.5 \ + --optimize 1 "$TMPDIR/combined.pdf" "$OUTPUT" + + echo "Done! Output saved to: $OUTPUT" + '') + ]; +}