1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.hardware.i2c;
7in
8
9{
10 options.hardware.i2c = {
11 enable = mkEnableOption ''
12 i2c devices support. By default access is granted to users in the "i2c"
13 group (will be created if non-existent) and any user with a seat, meaning
14 logged on the computer locally
15 '';
16
17 group = mkOption {
18 type = types.str;
19 default = "i2c";
20 description = ''
21 Grant access to i2c devices (/dev/i2c-*) to users in this group.
22 '';
23 };
24 };
25
26 config = mkIf cfg.enable {
27
28 boot.kernelModules = [ "i2c-dev" ];
29
30 users.groups = mkIf (cfg.group == "i2c") {
31 i2c = { };
32 };
33
34 services.udev.packages = lib.singleton (pkgs.writeTextFile
35 { name = "i2c-udev-rules";
36 text = ''
37 # allow group ${cfg.group} and users with a seat use of i2c devices
38 ACTION=="add", KERNEL=="i2c-[0-9]*", TAG+="uaccess", GROUP="${cfg.group}", MODE="660"
39 '';
40 destination = "/etc/udev/rules.d/70-i2c.rules";
41 });
42
43 };
44
45 meta.maintainers = [ maintainers.rnhmjoj ];
46
47}