summary refs log tree commit diff
path: root/nixos/modules/services/hardware/sane_extra_backends/brscan4.nix
blob: 8f9998108406b7931a402ac5292a2e07243c41de (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.hardware.sane.brscan4;

  netDeviceList = attrValues cfg.netDevices;

  etcFiles = pkgs.callPackage ./brscan4_etc_files.nix { netDevices = netDeviceList; };

  netDeviceOpts = { name, ... }: {

    options = {

      name = mkOption {
        type = types.str;
        description = ''
          The friendly name you give to the network device. If undefined,
          the name of attribute will be used.
        '';

        example = "office1";
      };

      model = mkOption {
        type = types.str;
        description = ''
          The model of the network device.
        '';

        example = "MFC-7860DW";
      };

      ip = mkOption {
        type = with types; nullOr str;
        default = null;
        description = ''
          The ip address of the device. If undefined, you will have to
          provide a nodename.
        '';

        example = "192.168.1.2";
      };

      nodename = mkOption {
        type = with types; nullOr str;
        default = null;
        description = ''
          The node name of the device. If undefined, you will have to
          provide an ip.
        '';

        example = "BRW0080927AFBCE";
      };

    };


    config =
      { name = mkDefault name;
      };
  };

in

{
  options = {

    hardware.sane.brscan4.enable =
      mkEnableOption "Brother's brscan4 scan backend" // {
      description = ''
        When enabled, will automatically register the "brscan4" sane
        backend and bring configuration files to their expected location.
      '';
    };

    hardware.sane.brscan4.netDevices = mkOption {
      default = {};
      example =
        { office1 = { model = "MFC-7860DW"; ip = "192.168.1.2"; };
          office2 = { model = "MFC-7860DW"; nodename = "BRW0080927AFBCE"; };
        };
      type = with types; attrsOf (submodule netDeviceOpts);
      description = ''
        The list of network devices that will be registered against the brscan4
        sane backend.
      '';
    };
  };

  config = mkIf (config.hardware.sane.enable && cfg.enable) {

    hardware.sane.extraBackends = [
      pkgs.brscan4
    ];

    environment.etc."opt/brother/scanner/brscan4" =
      { source = "${etcFiles}/etc/opt/brother/scanner/brscan4"; };

    assertions = [
      { assertion = all (x: !(null != x.ip && null != x.nodename)) netDeviceList;
        message = ''
          When describing a network device as part of the attribute list
          `hardware.sane.brscan4.netDevices`, only one of its `ip` or `nodename`
          attribute should be specified, not both!
        '';
      }
    ];

  };
}