about summary refs log tree commit diff
path: root/nixpkgs/pkgs/applications/graphics/sane/backends/default.nix
blob: 000e8c17f9c977d5ad2e3f4bbca4f17fec6b5c49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{ stdenv, lib, fetchurl, fetchpatch, runtimeShell, buildPackages
, gettext, pkg-config, python3
, avahi, libgphoto2, libieee1284, libjpeg, libpng, libtiff, libusb1, libv4l, net-snmp
, curl, systemd, libxml2, poppler, gawk
, sane-drivers
, nixosTests

# List of { src name backend } attibute sets - see installFirmware below:
, extraFirmware ? []

# For backwards compatibility with older setups; use extraFirmware instead:
, gt68xxFirmware ? null, snapscanFirmware ? null

# Not included by default, scan snap drivers require fetching of unfree binaries.
, scanSnapDriversUnfree ? false, scanSnapDriversPackage ? sane-drivers.epjitsu
}:

stdenv.mkDerivation {
  pname = "sane-backends";
  version = "1.2.1";

  src = fetchurl {
    # raw checkouts of the repo do not work because, the configure script is
    # only functional in manually uploaded release tarballs.
    # https://gitlab.com/sane-project/backends/-/issues/440
    # unfortunately this make the url unpredictable on update, to find the link
    # go to https://gitlab.com/sane-project/backends/-/releases and choose
    # the link under the heading "Other".
    url = "https://gitlab.com/sane-project/backends/uploads/110fc43336d0fb5e514f1fdc7360dd87/sane-backends-1.2.1.tar.gz";
    sha256 = "f832395efcb90bb5ea8acd367a820c393dda7e0dd578b16f48928b8f5bdd0524";
  };

  patches = [
    # sane-desc will be used in postInstall so compile it for build
    # https://github.com/void-linux/void-packages/blob/master/srcpkgs/sane/patches/sane-desc-cross.patch
    (fetchpatch {
      name = "compile-sane-desc-for-build.patch";
      url = "https://raw.githubusercontent.com/void-linux/void-packages/4b97cd2fb4ec38712544438c2491b6d7d5ab334a/srcpkgs/sane/patches/sane-desc-cross.patch";
      sha256 = "sha256-y6BOXnOJBSTqvRp6LwAucqaqv+OLLyhCS/tXfLpnAPI=";
    })
    # generate hwdb entries for scanners handled by other backends like epkowa
    # https://gitlab.com/sane-project/backends/-/issues/619
    ./sane-desc-generate-entries-unsupported-scanners.patch
  ];

  postPatch = ''
    # related to the compile-sane-desc-for-build
    substituteInPlace tools/Makefile.in \
      --replace 'cc -I' '$(CC_FOR_BUILD) -I'
  '';

  outputs = [ "out" "doc" "man" ];

  depsBuildBuild = [ buildPackages.stdenv.cc ];

  nativeBuildInputs = [
    gettext
    pkg-config
    python3
  ];

  buildInputs = [
    avahi
    libgphoto2
    libjpeg
    libpng
    libtiff
    libusb1
    curl
    libxml2
    poppler
    gawk
  ] ++ lib.optionals stdenv.isLinux [
    libieee1284
    libv4l
    net-snmp
    systemd
  ];

  enableParallelBuilding = true;

  configureFlags =
    lib.optional (avahi != null)   "--with-avahi"
    ++ lib.optional (libusb1 != null) "--with-usb"
  ;

  # autoconf check for HAVE_MMAP is never set on cross compilation.
  # The pieusb backend fails compilation if HAVE_MMAP is not set.
  buildFlags = lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ "CFLAGS=-DHAVE_MMAP=${if stdenv.hostPlatform.isLinux then "1" else "0"}" ];

  postInstall = let

    compatFirmware = extraFirmware
      ++ lib.optional (gt68xxFirmware != null) {
        src = gt68xxFirmware.fw;
        inherit (gt68xxFirmware) name;
        backend = "gt68xx";
      }
      ++ lib.optional (snapscanFirmware != null) {
        src = snapscanFirmware;
        name = "your-firmwarefile.bin";
        backend = "snapscan";
      };

    installFirmware = f: ''
      mkdir -p $out/share/sane/${f.backend}
      ln -sv ${f.src} $out/share/sane/${f.backend}/${f.name}
    '';

  in ''
    mkdir -p $out/etc/udev/rules.d/ $out/etc/udev/hwdb.d
    ./tools/sane-desc -m udev+hwdb -s doc/descriptions:doc/descriptions-external > $out/etc/udev/rules.d/49-libsane.rules
    ./tools/sane-desc -m udev+hwdb -s doc/descriptions -m hwdb > $out/etc/udev/hwdb.d/20-sane.hwdb
    # the created 49-libsane references /bin/sh
    substituteInPlace $out/etc/udev/rules.d/49-libsane.rules \
      --replace "RUN+=\"/bin/sh" "RUN+=\"${runtimeShell}"

    substituteInPlace $out/lib/libsane.la \
      --replace "-ljpeg" "-L${lib.getLib libjpeg}/lib -ljpeg"

    # net.conf conflicts with the file generated by the nixos module
    rm $out/etc/sane.d/net.conf

  ''
  + lib.optionalString scanSnapDriversUnfree ''
    # the ScanSnap drivers live under the epjitsu subdirectory, which was already created by the build but is empty.
    rmdir $out/share/sane/epjitsu
    ln -svT ${scanSnapDriversPackage} $out/share/sane/epjitsu
  ''
  + lib.concatStrings (builtins.map installFirmware compatFirmware);

  # parallel install creates a bad symlink at $out/lib/sane/libsane.so.1 which prevents finding plugins
  # https://github.com/NixOS/nixpkgs/issues/224569
  enableParallelInstalling = false;

  passthru.tests = {
    inherit (nixosTests) sane;
  };

  meta = with lib; {
    description = "SANE (Scanner Access Now Easy) backends";
    longDescription = ''
      Collection of open-source SANE backends (device drivers).
      SANE is a universal scanner interface providing standardized access to
      any raster image scanner hardware: flatbed scanners, hand-held scanners,
      video- and still-cameras, frame-grabbers, etc. For a list of supported
      scanners, see http://www.sane-project.org/sane-backends.html.
    '';
    homepage = "http://www.sane-project.org/";
    license = licenses.gpl2Plus;
    platforms = platforms.linux ++ platforms.darwin;
    maintainers = [ maintainers.symphorien ];
  };
}