···11+{ config, lib, pkgs, ... }:
22+33+with lib;
44+55+let
66+ cfg = config.hardware.sane.brscan5;
77+88+ netDeviceList = attrValues cfg.netDevices;
99+1010+ etcFiles = pkgs.callPackage ./brscan5_etc_files.nix { netDevices = netDeviceList; };
1111+1212+ netDeviceOpts = { name, ... }: {
1313+1414+ options = {
1515+1616+ name = mkOption {
1717+ type = types.str;
1818+ description = ''
1919+ The friendly name you give to the network device. If undefined,
2020+ the name of attribute will be used.
2121+ '';
2222+2323+ example = literalExample "office1";
2424+ };
2525+2626+ model = mkOption {
2727+ type = types.str;
2828+ description = ''
2929+ The model of the network device.
3030+ '';
3131+3232+ example = literalExample "MFC-7860DW";
3333+ };
3434+3535+ ip = mkOption {
3636+ type = with types; nullOr str;
3737+ default = null;
3838+ description = ''
3939+ The ip address of the device. If undefined, you will have to
4040+ provide a nodename.
4141+ '';
4242+4343+ example = literalExample "192.168.1.2";
4444+ };
4545+4646+ nodename = mkOption {
4747+ type = with types; nullOr str;
4848+ default = null;
4949+ description = ''
5050+ The node name of the device. If undefined, you will have to
5151+ provide an ip.
5252+ '';
5353+5454+ example = literalExample "BRW0080927AFBCE";
5555+ };
5656+5757+ };
5858+5959+6060+ config =
6161+ { name = mkDefault name;
6262+ };
6363+ };
6464+6565+in
6666+6767+{
6868+ options = {
6969+7070+ hardware.sane.brscan5.enable =
7171+ mkEnableOption "Brother's brscan5 scan backend" // {
7272+ description = ''
7373+ When enabled, will automatically register the "brscan5" sane
7474+ backend and bring configuration files to their expected location.
7575+ '';
7676+ };
7777+7878+ hardware.sane.brscan5.netDevices = mkOption {
7979+ default = {};
8080+ example =
8181+ { office1 = { model = "MFC-7860DW"; ip = "192.168.1.2"; };
8282+ office2 = { model = "MFC-7860DW"; nodename = "BRW0080927AFBCE"; };
8383+ };
8484+ type = with types; attrsOf (submodule netDeviceOpts);
8585+ description = ''
8686+ The list of network devices that will be registered against the brscan5
8787+ sane backend.
8888+ '';
8989+ };
9090+ };
9191+9292+ config = mkIf (config.hardware.sane.enable && cfg.enable) {
9393+9494+ hardware.sane.extraBackends = [
9595+ pkgs.brscan5
9696+ ];
9797+9898+ environment.etc."opt/brother/scanner/brscan5" =
9999+ { source = "${etcFiles}/etc/opt/brother/scanner/brscan5"; };
100100+ environment.etc."opt/brother/scanner/models" =
101101+ { source = "${etcFiles}/etc/opt/brother/scanner/brscan5/models"; };
102102+ environment.etc."sane.d/dll.d/brother5.conf".source = "${pkgs.brscan5}/etc/sane.d/dll.d/brother.conf";
103103+104104+ assertions = [
105105+ { assertion = all (x: !(null != x.ip && null != x.nodename)) netDeviceList;
106106+ message = ''
107107+ When describing a network device as part of the attribute list
108108+ `hardware.sane.brscan5.netDevices`, only one of its `ip` or `nodename`
109109+ attribute should be specified, not both!
110110+ '';
111111+ }
112112+ ];
113113+114114+ };
115115+}