Samuel Shuert | 1abb2ff | 2024-09-04 17:03:38 -0400 | [diff] [blame^] | 1 | { |
| 2 | lib |
| 3 | , config |
| 4 | , pkgs |
| 5 | , ... |
| 6 | }: let |
| 7 | cfg = config.chimera.cooling.OpenLinkHub; |
| 8 | in { |
| 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 | |
| 44 | cp ${cfg.config} ${path}/config.json |
| 45 | |
| 46 | [ -L ${path}/static ] || ln -s ${cfg.package}/var/lib/OpenLinkHub/static ${path}/static |
| 47 | [ -L ${path}/web ] || ln -s ${cfg.package}/var/lib/OpenLinkHub/web ${path}/web |
| 48 | |
| 49 | ${pkgs.usbutils}/bin/lsusb -d 1b1c: | while read -r line; do |
| 50 | ids=$(echo "$line" | ${pkgs.gawk}/bin/awk '{print $6}') |
| 51 | vendor_id=$(${pkgs.coreutils}/bin/echo "$ids" | ${pkgs.coreutils}/bin/cut -d':' -f1) |
| 52 | device_id=$(${pkgs.coreutils}/bin/echo "$ids" | ${pkgs.coreutils}/bin/cut -d':' -f2) |
| 53 | ${pkgs.coreutils}/bin/cat > /run/udev/rules.d/99-corsair-openlinkhub-"$device_id".rules <<- EOM |
| 54 | KERNEL=="hidraw*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="$vendor_id", ATTRS{idProduct}=="$device_id", MODE="0666" |
| 55 | EOM |
| 56 | done |
| 57 | |
| 58 | ${pkgs.coreutils}/bin/chmod -R 744 ${path} |
| 59 | ${pkgs.coreutils}/bin/chown -R OpenLinkHub:OpenLinkHub ${path} |
| 60 | |
| 61 | ${pkgs.systemd}/bin/udevadm control --reload |
| 62 | ${pkgs.systemd}/bin/udevadm trigger |
| 63 | ''; |
| 64 | |
| 65 | postStop = '' |
| 66 | ${pkgs.coreutils}/bin/rm /run/udev/rules.d/99-corsair-openlinkhub-*.rules |
| 67 | |
| 68 | ${pkgs.systemd}/bin/udevadm control --reload |
| 69 | ${pkgs.systemd}/bin/udevadm trigger |
| 70 | ''; |
| 71 | |
| 72 | path = [ pkgs.pciutils ]; |
| 73 | |
| 74 | serviceConfig = { |
| 75 | DynamicUser = true; |
| 76 | ExecStart = "${cfg.package}/bin/OpenLinkHub"; |
| 77 | ExecReload = "${pkgs.coreutils}/bin/kill -s HUP \$MAINPID"; |
| 78 | RestartSec = 5; |
| 79 | PermissionsStartOnly = true; |
| 80 | StateDirectory = "OpenLinkHub"; |
| 81 | WorkingDirectory = "/var/lib/OpenLinkHub"; |
| 82 | }; |
| 83 | |
| 84 | wantedBy = [ "multi-user.target" ]; |
| 85 | }; |
| 86 | }; |
| 87 | } |