main
 1{
 2  den.aspects.desktop.app.script-tools.qrscanner = {
 3    hjem = {pkgs, ...}: let
 4      ix-qrscanner-script = pkgs.writeShellApplication {
 5        name = "ix-qrscanner";
 6        runtimeInputs = [
 7          pkgs.coreutils
 8          pkgs.grim
 9          pkgs.libnotify
10          pkgs.slurp
11          pkgs.zbar
12          pkgs.wl-clipboard-rs
13        ];
14        text = ''
15          geom="$(slurp -d)" || exit 0
16
17          img="$(mktemp --suffix=.png)"
18          trap 'rm -f "$img"' EXIT
19
20          grim -g "$geom" "$img" || {
21            notify-send -u critical "QR Scanner" "Screenshot failed (grim)"
22            exit 1
23          }
24
25          result="$(zbarimg -q --raw -1 "$img" 2>/dev/null)" || {
26            notify-send -u critical "QR Scanner" "QR Code recognition failed (zbar)"
27            exit 1
28          }
29
30          if [ -z "$(printf '%s' "$result" | tr -d '[:space:]')" ]; then
31            notify-send "QR Scanner" "No QR code recognized in the selection"
32            exit 0
33          fi
34
35          printf '%s' "$result" | wl-copy
36          printf '%s\n' "$result"
37        '';
38
39        meta.mainProgram = "ix-qrscanner";
40      };
41
42      ix-qrscanner-desktop-entry = pkgs.makeDesktopItem {
43        name = "ix-qrscanner";
44        exec = "ix-qrscanner";
45        icon = "scanner";
46        desktopName = "IX Tools - QR Scanner";
47        comment = "Select a screen region containing a QR code and scan it";
48        categories = ["Utility" "Graphics"];
49        keywords = ["qr" "barcode" "scan"];
50        terminal = false;
51        startupNotify = false;
52      };
53
54      ix-qrscanner = pkgs.stdenv.mkDerivation rec {
55        name = "ix-qrscanner";
56        buildCommand = ''
57          mkdir -p $out/bin
58          install -Dm755 ${ix-qrscanner-script}/bin/${name} $out/bin/${name}
59          mkdir -p $out/share/applications
60          install -Dm644 ${ix-qrscanner-desktop-entry}/share/applications/${name}.desktop \
61            $out/share/applications/${name}.desktop
62        '';
63        dontBuild = true;
64      };
65    in {
66      packages = [ix-qrscanner];
67    };
68  };
69}