1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 pkg = pkgs.sane-backends.override {
8 scanSnapDriversUnfree = config.hardware.sane.drivers.scanSnap.enable;
9 scanSnapDriversPackage = config.hardware.sane.drivers.scanSnap.package;
10 };
11
12 sanedConf = pkgs.writeTextFile {
13 name = "saned.conf";
14 destination = "/etc/sane.d/saned.conf";
15 text = ''
16 localhost
17 ${config.services.saned.extraConfig}
18 '';
19 };
20
21 netConf = pkgs.writeTextFile {
22 name = "net.conf";
23 destination = "/etc/sane.d/net.conf";
24 text = ''
25 ${lib.optionalString config.services.saned.enable "localhost"}
26 ${config.hardware.sane.netConf}
27 '';
28 };
29
30 env = {
31 SANE_CONFIG_DIR = "/etc/sane.d";
32 LD_LIBRARY_PATH = [ "/etc/sane-libs" ];
33 };
34
35 backends = [ pkg netConf ] ++ optional config.services.saned.enable sanedConf ++ config.hardware.sane.extraBackends;
36 saneConfig = pkgs.mkSaneConfig { paths = backends; inherit (config.hardware.sane) disabledDefaultBackends; };
37
38 enabled = config.hardware.sane.enable || config.services.saned.enable;
39
40in
41
42{
43
44 ###### interface
45
46 options = {
47
48 hardware.sane.enable = mkOption {
49 type = types.bool;
50 default = false;
51 description = lib.mdDoc ''
52 Enable support for SANE scanners.
53
54 ::: {.note}
55 Users in the "scanner" group will gain access to the scanner, or the "lp" group if it's also a printer.
56 :::
57 '';
58 };
59
60 hardware.sane.snapshot = mkOption {
61 type = types.bool;
62 default = false;
63 description = lib.mdDoc "Use a development snapshot of SANE scanner drivers.";
64 };
65
66 hardware.sane.extraBackends = mkOption {
67 type = types.listOf types.path;
68 default = [];
69 description = lib.mdDoc ''
70 Packages providing extra SANE backends to enable.
71
72 ::: {.note}
73 The example contains the package for HP scanners, and the package for
74 Apple AirScan and Microsoft WSD support (supports many
75 vendors/devices).
76 :::
77 '';
78 example = literalExpression "[ pkgs.hplipWithPlugin pkgs.sane-airscan ]";
79 };
80
81 hardware.sane.disabledDefaultBackends = mkOption {
82 type = types.listOf types.str;
83 default = [];
84 example = [ "v4l" ];
85 description = lib.mdDoc ''
86 Names of backends which are enabled by default but should be disabled.
87 See `$SANE_CONFIG_DIR/dll.conf` for the list of possible names.
88 '';
89 };
90
91 hardware.sane.configDir = mkOption {
92 type = types.str;
93 internal = true;
94 description = lib.mdDoc "The value of SANE_CONFIG_DIR.";
95 };
96
97 hardware.sane.netConf = mkOption {
98 type = types.lines;
99 default = "";
100 example = "192.168.0.16";
101 description = lib.mdDoc ''
102 Network hosts that should be probed for remote scanners.
103 '';
104 };
105
106 hardware.sane.drivers.scanSnap.enable = mkOption {
107 type = types.bool;
108 default = false;
109 example = true;
110 description = lib.mdDoc ''
111 Whether to enable drivers for the Fujitsu ScanSnap scanners.
112
113 The driver files are unfree and extracted from the Windows driver image.
114 '';
115 };
116
117 hardware.sane.drivers.scanSnap.package = mkOption {
118 type = types.package;
119 default = pkgs.sane-drivers.epjitsu;
120 defaultText = literalExpression "pkgs.sane-drivers.epjitsu";
121 description = lib.mdDoc ''
122 Epjitsu driver package to use. Useful if you want to extract the driver files yourself.
123
124 The process is described in the `/etc/sane.d/epjitsu.conf` file in
125 the `sane-backends` package.
126 '';
127 };
128
129 services.saned.enable = mkOption {
130 type = types.bool;
131 default = false;
132 description = lib.mdDoc ''
133 Enable saned network daemon for remote connection to scanners.
134
135 saned would be runned from `scanner` user; to allow
136 access to hardware that doesn't have `scanner` group
137 you should add needed groups to this user.
138 '';
139 };
140
141 services.saned.extraConfig = mkOption {
142 type = types.lines;
143 default = "";
144 example = "192.168.0.0/24";
145 description = lib.mdDoc ''
146 Extra saned configuration lines.
147 '';
148 };
149
150 };
151
152
153 ###### implementation
154
155 config = mkMerge [
156 (mkIf enabled {
157 hardware.sane.configDir = mkDefault "${saneConfig}/etc/sane.d";
158
159 environment.systemPackages = backends;
160 environment.sessionVariables = env;
161 environment.etc."sane.d".source = config.hardware.sane.configDir;
162 environment.etc."sane-libs".source = "${saneConfig}/lib/sane";
163 services.udev.packages = backends;
164
165 users.groups.scanner.gid = config.ids.gids.scanner;
166 })
167
168 (mkIf config.services.saned.enable {
169 networking.firewall.connectionTrackingModules = [ "sane" ];
170
171 systemd.services."saned@" = {
172 description = "Scanner Service";
173 environment = mapAttrs (name: val: toString val) env;
174 serviceConfig = {
175 User = "scanner";
176 Group = "scanner";
177 ExecStart = "${pkg}/bin/saned";
178 };
179 };
180
181 systemd.sockets.saned = {
182 description = "saned incoming socket";
183 wantedBy = [ "sockets.target" ];
184 listenStreams = [ "0.0.0.0:6566" "[::]:6566" ];
185 socketConfig = {
186 # saned needs to distinguish between IPv4 and IPv6 to open matching data sockets.
187 BindIPv6Only = "ipv6-only";
188 Accept = true;
189 MaxConnections = 64;
190 };
191 };
192
193 users.users.scanner = {
194 uid = config.ids.uids.scanner;
195 group = "scanner";
196 extraGroups = [ "lp" ] ++ optionals config.services.avahi.enable [ "avahi" ];
197 };
198 })
199 ];
200
201}