blob: ff95aae6ec2a5ba35bfc3f7ab875509369626189 [file] [log] [blame]
Samuel Shuert1abb2ff2024-09-04 17:03:38 -04001{
2 lib
3, config
4, pkgs
5, ...
6}: let
7 cfg = config.chimera.cooling.OpenLinkHub;
8in {
9 options.chimera.cooling.OpenLinkHub = {
10 enable = lib.mkEnableOption "Enable OpenLinkHub service for Corsair iCUE Link";
11 package = lib.mkOption {
12 type = lib.types.package;
13 description = "OpenLinkHub package to use for the module";
14 default = pkgs.chimera.OpenLinkHub;
15 };
16 config = lib.mkOption {
17 type = lib.types.path;
18 default = "${cfg.package}/var/lib/OpenLinkHub/config.json";
19 };
20 };
21
22 config = lib.mkIf cfg.enable {
23 users.groups.OpenLinkHub = {};
24
25 users.users.OpenLinkHub = {
26 isSystemUser = true;
27 group = config.users.groups.OpenLinkHub.name;
28 extraGroups = [ config.users.groups.input.name ];
29 };
30
31 systemd.services.OpenLinkHub = let
32 path = "/var/lib/OpenLinkHub";
33 in {
34 enable = true;
35 description = "Open source interface for iCUE LINK System Hub, Corsair AIOs and Hubs";
36
37 preStart = ''
38 mkdir -p ${path}/database
39 [ -f ${path}/database/rgb.json ] || cp ${cfg.package}/var/lib/OpenLinkHub/rgb.json ${path}/database/rgb.json
40 mkdir -p ${path}/database/temperatures
41 mkdir -p ${path}/database/profiles
42 mkdir -p /run/udev/rules.d
43
Samuel Shuert76633542024-11-17 14:51:07 -050044 mkdir -p ${path}/database/keyboard
45 cp -r -n ${cfg.package}/var/lib/OpenLinkHub/database/keyboard ${path}/database/keyboard
46
Samuel Shuert1abb2ff2024-09-04 17:03:38 -040047 cp ${cfg.config} ${path}/config.json
48
49 [ -L ${path}/static ] || ln -s ${cfg.package}/var/lib/OpenLinkHub/static ${path}/static
50 [ -L ${path}/web ] || ln -s ${cfg.package}/var/lib/OpenLinkHub/web ${path}/web
51
52 ${pkgs.usbutils}/bin/lsusb -d 1b1c: | while read -r line; do
53 ids=$(echo "$line" | ${pkgs.gawk}/bin/awk '{print $6}')
54 vendor_id=$(${pkgs.coreutils}/bin/echo "$ids" | ${pkgs.coreutils}/bin/cut -d':' -f1)
55 device_id=$(${pkgs.coreutils}/bin/echo "$ids" | ${pkgs.coreutils}/bin/cut -d':' -f2)
56 ${pkgs.coreutils}/bin/cat > /run/udev/rules.d/99-corsair-openlinkhub-"$device_id".rules <<- EOM
57 KERNEL=="hidraw*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="$vendor_id", ATTRS{idProduct}=="$device_id", MODE="0666"
58 EOM
59 done
60
61 ${pkgs.coreutils}/bin/chmod -R 744 ${path}
62 ${pkgs.coreutils}/bin/chown -R OpenLinkHub:OpenLinkHub ${path}
63
64 ${pkgs.systemd}/bin/udevadm control --reload
65 ${pkgs.systemd}/bin/udevadm trigger
66 '';
67
68 postStop = ''
Samuel Shuert76633542024-11-17 14:51:07 -050069 ${pkgs.coreutils}/bin/rm /var/lib/OpenLinkHub/web
70 ${pkgs.coreutils}/bin/rm /var/lib/OpenLinkHub/static
Samuel Shuert1abb2ff2024-09-04 17:03:38 -040071
Samuel Shuert76633542024-11-17 14:51:07 -050072 ${pkgs.coreutils}/bin/rm /run/udev/rules.d/99-corsair-openlinkhub-*.rules
Samuel Shuert1abb2ff2024-09-04 17:03:38 -040073 ${pkgs.systemd}/bin/udevadm control --reload
74 ${pkgs.systemd}/bin/udevadm trigger
75 '';
76
77 path = [ pkgs.pciutils ];
78
79 serviceConfig = {
80 DynamicUser = true;
81 ExecStart = "${cfg.package}/bin/OpenLinkHub";
82 ExecReload = "${pkgs.coreutils}/bin/kill -s HUP \$MAINPID";
83 RestartSec = 5;
84 PermissionsStartOnly = true;
85 StateDirectory = "OpenLinkHub";
86 WorkingDirectory = "/var/lib/OpenLinkHub";
87 };
88
89 wantedBy = [ "multi-user.target" ];
90 };
91 };
92}