1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.hardware.logitech;
7
8 vendor = "046d";
9
10 daemon = "g15daemon";
11
12in
13{
14 imports = [
15 (mkRenamedOptionModule [ "hardware" "logitech" "enable" ] [ "hardware" "logitech" "wireless" "enable" ])
16 (mkRenamedOptionModule [ "hardware" "logitech" "enableGraphical" ] [ "hardware" "logitech" "wireless" "enableGraphical" ])
17 ];
18
19 options.hardware.logitech = {
20
21 lcd = {
22 enable = mkEnableOption "support for Logitech LCD Devices";
23
24 startWhenNeeded = mkOption {
25 type = types.bool;
26 default = true;
27 description = ''
28 Only run the service when an actual supported device is plugged.
29 '';
30 };
31
32 devices = mkOption {
33 type = types.listOf types.str;
34 default = [ "0a07" "c222" "c225" "c227" "c251" ];
35 description = ''
36 List of USB device ids supported by g15daemon.
37
38 You most likely do not need to change this.
39 '';
40 };
41 };
42
43 wireless = {
44 enable = mkEnableOption "support for Logitech Wireless Devices";
45
46 enableGraphical = mkOption {
47 type = types.bool;
48 default = false;
49 description = "Enable graphical support applications.";
50 };
51 };
52 };
53
54 config = lib.mkIf (cfg.wireless.enable || cfg.lcd.enable) {
55 environment.systemPackages = []
56 ++ lib.optional cfg.wireless.enable pkgs.ltunify
57 ++ lib.optional cfg.wireless.enableGraphical pkgs.solaar;
58
59 services.udev = {
60 # ltunifi and solaar both provide udev rules but the most up-to-date have been split
61 # out into a dedicated derivation
62
63 packages = []
64 ++ lib.optional cfg.wireless.enable pkgs.logitech-udev-rules
65 ++ lib.optional cfg.lcd.enable pkgs.g15daemon;
66
67 extraRules = ''
68 # nixos: hardware.logitech.lcd
69 '' + lib.concatMapStringsSep "\n" (
70 dev:
71 ''ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="${vendor}", ATTRS{idProduct}=="${dev}", TAG+="systemd", ENV{SYSTEMD_WANTS}+="${daemon}.service"''
72 ) cfg.lcd.devices;
73 };
74
75 systemd.services."${daemon}" = lib.mkIf cfg.lcd.enable {
76 description = "Logitech LCD Support Daemon";
77 documentation = [ "man:g15daemon(1)" ];
78 wantedBy = lib.mkIf (! cfg.lcd.startWhenNeeded) "multi-user.target";
79
80 serviceConfig = {
81 Type = "forking";
82 ExecStart = "${pkgs.g15daemon}/bin/g15daemon";
83 # we patch it to write to /run/g15daemon/g15daemon.pid instead of
84 # /run/g15daemon.pid so systemd will do the cleanup for us.
85 PIDFile = "/run/${daemon}/g15daemon.pid";
86 PrivateTmp = true;
87 PrivateNetwork = true;
88 ProtectHome = "tmpfs";
89 ProtectSystem = "full"; # strict doesn't work
90 RuntimeDirectory = daemon;
91 Restart = "on-failure";
92 };
93 };
94 };
95}