1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.hardware.sane.brscan4;
9
10 netDeviceList = lib.attrValues cfg.netDevices;
11
12 etcFiles = pkgs.callPackage ./brscan4_etc_files.nix { netDevices = netDeviceList; };
13
14 netDeviceOpts =
15 { name, ... }:
16 {
17
18 options = {
19
20 name = lib.mkOption {
21 type = lib.types.str;
22 description = ''
23 The friendly name you give to the network device. If undefined,
24 the name of attribute will be used.
25 '';
26
27 example = "office1";
28 };
29
30 model = lib.mkOption {
31 type = lib.types.str;
32 description = ''
33 The model of the network device.
34 '';
35
36 example = "MFC-7860DW";
37 };
38
39 ip = lib.mkOption {
40 type = with lib.types; nullOr str;
41 default = null;
42 description = ''
43 The ip address of the device. If undefined, you will have to
44 provide a nodename.
45 '';
46
47 example = "192.168.1.2";
48 };
49
50 nodename = lib.mkOption {
51 type = with lib.types; nullOr str;
52 default = null;
53 description = ''
54 The node name of the device. If undefined, you will have to
55 provide an ip.
56 '';
57
58 example = "BRW0080927AFBCE";
59 };
60
61 };
62
63 config = {
64 name = lib.mkDefault name;
65 };
66 };
67
68in
69
70{
71 options = {
72
73 hardware.sane.brscan4.enable = lib.mkEnableOption "Brother's brscan4 scan backend" // {
74 description = ''
75 When enabled, will automatically register the "brscan4" sane
76 backend and bring configuration files to their expected location.
77 '';
78 };
79
80 hardware.sane.brscan4.netDevices = lib.mkOption {
81 default = { };
82 example = {
83 office1 = {
84 model = "MFC-7860DW";
85 ip = "192.168.1.2";
86 };
87 office2 = {
88 model = "MFC-7860DW";
89 nodename = "BRW0080927AFBCE";
90 };
91 };
92 type = with lib.types; attrsOf (submodule netDeviceOpts);
93 description = ''
94 The list of network devices that will be registered against the brscan4
95 sane backend.
96 '';
97 };
98 };
99
100 config = lib.mkIf (config.hardware.sane.enable && cfg.enable) {
101
102 hardware.sane.extraBackends = [
103 pkgs.brscan4
104 ];
105
106 environment.etc."opt/brother/scanner/brscan4" = {
107 source = "${etcFiles}/etc/opt/brother/scanner/brscan4";
108 };
109
110 assertions = [
111 {
112 assertion = lib.all (x: !(null != x.ip && null != x.nodename)) netDeviceList;
113 message = ''
114 When describing a network device as part of the attribute list
115 `hardware.sane.brscan4.netDevices`, only one of its `ip` or `nodename`
116 attribute should be specified, not both!
117 '';
118 }
119 ];
120
121 };
122}