nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 stdenv,
3 lib,
4 fetchurl,
5 patchelf,
6 makeWrapper,
7 libusb1,
8 avahi-compat,
9 glib,
10 libredirect,
11 nixosTests,
12 udevCheckHook,
13}:
14let
15 myPatchElf = file: ''
16 patchelf --set-interpreter \
17 ${stdenv.cc.libc}/lib/ld-linux${lib.optionalString stdenv.hostPlatform.is64bit "-x86-64"}.so.2 \
18 ${file}
19 '';
20 system = stdenv.hostPlatform.system;
21
22in
23stdenv.mkDerivation rec {
24 pname = "brscan5";
25 version = "1.3.1-0";
26 src =
27 {
28 "i686-linux" = fetchurl {
29 url = "https://download.brother.com/welcome/dlf104034/${pname}-${version}.i386.deb";
30 hash = "sha256-BgS64vwsKESJBDz9H2MDwcGiresROSNFP1b+7+zlE5c=";
31 };
32 "x86_64-linux" = fetchurl {
33 url = "https://download.brother.com/welcome/dlf104033/${pname}-${version}.amd64.deb";
34 hash = "sha256-0UMbXMBlyiZI90WG5FWEP2mIZEBsxXd11dtgtyuSDnY=";
35 };
36 }
37 ."${system}" or (throw "Unsupported system: ${system}");
38
39 unpackPhase = ''
40 ar x $src
41 tar xfv data.tar.xz
42 '';
43
44 nativeBuildInputs = [
45 makeWrapper
46 patchelf
47 udevCheckHook
48 ];
49 buildInputs = [
50 libusb1
51 avahi-compat
52 stdenv.cc.cc
53 glib
54 ];
55 dontBuild = true;
56
57 postPatch =
58 let
59 # Download .deb for both amd64 and i386, then unpack like unpackPhase
60 # to get the offset, run:
61 # strings -n 10 --radix=d opt/brother/scanner/brscan5/libsane-brother5.so.1.0.7 | grep "/opt/brother/scanner/brscan5/models"
62 patchOffsetBytes =
63 if system == "x86_64-linux" then
64 86592
65 else if system == "i686-linux" then
66 79236
67 else
68 throw "Unsupported system: ${system}";
69 in
70 ''
71 ${myPatchElf "opt/brother/scanner/brscan5/brsaneconfig5"}
72 ${myPatchElf "opt/brother/scanner/brscan5/brscan_cnetconfig"}
73 ${myPatchElf "opt/brother/scanner/brscan5/brscan_gnetconfig"}
74
75 for file in opt/brother/scanner/brscan5/*.so.* opt/brother/scanner/brscan5/brscan_[cg]netconfig; do
76 if ! test -L $file; then
77 patchelf --set-rpath ${lib.makeLibraryPath buildInputs} $file
78 fi
79 done
80
81 # driver is hardcoded to look in /opt/brother/scanner/brscan5/models for model metadata.
82 # patch it to look in /etc/opt/brother/scanner/models instead, so nixos environment.etc can make it available
83 printf '/etc/opt/brother/scanner/models\x00' | dd of=opt/brother/scanner/brscan5/libsane-brother5.so.1.0.7 bs=1 seek=${toString patchOffsetBytes} conv=notrunc
84
85 # remove deprecated SYSFS udev rule
86 sed -i -e '/^SYSFS/d' opt/brother/scanner/brscan5/udev-rules/*.rules
87 '';
88
89 installPhase = ''
90 runHook preInstall
91 PATH_TO_BRSCAN5="opt/brother/scanner/brscan5"
92 mkdir -p $out/$PATH_TO_BRSCAN5
93 cp -rp $PATH_TO_BRSCAN5/* $out/$PATH_TO_BRSCAN5
94
95
96 pushd $out/$PATH_TO_BRSCAN5
97 ln -s libLxBsDeviceAccs.so.1.0.0 libLxBsDeviceAccs.so.1
98 ln -s libLxBsNetDevAccs.so.1.0.0 libLxBsNetDevAccs.so.1
99 ln -s libLxBsScanCoreApi.so.3.2.0 libLxBsScanCoreApi.so.3
100 ln -s libLxBsUsbDevAccs.so.1.0.0 libLxBsUsbDevAccs.so.1
101 ln -s libsane-brother5.so.1.0.7 libsane-brother5.so.1
102 popd
103
104 mkdir -p $out/lib/sane
105 for file in $out/$PATH_TO_BRSCAN5/*.so.* ; do
106 ln -s $file $out/lib/sane/
107 done
108
109 makeWrapper \
110 "$out/$PATH_TO_BRSCAN5/brsaneconfig5" \
111 "$out/bin/brsaneconfig5" \
112 --suffix-each NIX_REDIRECT ":" "/etc/opt/brother/scanner/brscan5=$out/opt/brother/scanner/brscan5 /opt/brother/scanner/brscan5=$out/opt/brother/scanner/brscan5" \
113 --set LD_PRELOAD ${libredirect}/lib/libredirect.so
114
115 mkdir -p $out/etc/sane.d/dll.d
116 echo "brother5" > $out/etc/sane.d/dll.d/brother5.conf
117
118 mkdir -p $out/etc/udev/rules.d
119 install -m 0444 $PATH_TO_BRSCAN5/udev-rules/NN-brother-mfp-brscan5-1.0.2-2.rules \
120 $out/etc/udev/rules.d/49-brother-mfp-brscan5-1.0.2-2.rules
121
122 ETCDIR=$out/etc/opt/brother/scanner/brscan5
123 mkdir -p $ETCDIR
124 cp -rp $PATH_TO_BRSCAN5/{models,brscan5.ini,brsanenetdevice.cfg} $ETCDIR/
125
126 runHook postInstall
127 '';
128
129 # We want to run the udevCheckHook
130 doInstallCheck = true;
131
132 dontPatchELF = true;
133
134 passthru.tests = { inherit (nixosTests) brscan5; };
135
136 meta = {
137 description = "Brother brscan5 sane backend driver";
138 homepage = "https://www.brother.com";
139 platforms = [
140 "i686-linux"
141 "x86_64-linux"
142 ];
143 sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
144 license = lib.licenses.unfree;
145 maintainers = with lib.maintainers; [ mattchrist ];
146 };
147}