aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2022-10-24 22:40:23 +0200
committerKarel Kočí <cynerd@email.cz>2022-10-24 22:48:59 +0200
commit2cc6c594f3c7a1c9c4d99fd8f10d3d385a1006bf (patch)
tree03238886bf90144e2d17222da1d39af6b7c3e1ae /nixos/modules
parentb95e8b11cfff1397b784781c83a3d736cde4a9ac (diff)
downloadnixturris-2cc6c594f3c7a1c9c4d99fd8f10d3d385a1006bf.tar.gz
nixturris-2cc6c594f3c7a1c9c4d99fd8f10d3d385a1006bf.tar.bz2
nixturris-2cc6c594f3c7a1c9c4d99fd8f10d3d385a1006bf.zip
nixos: add Omnia LEDs module
Diffstat (limited to 'nixos/modules')
-rw-r--r--nixos/modules/turris-omnialeds.nix152
1 files changed, 152 insertions, 0 deletions
diff --git a/nixos/modules/turris-omnialeds.nix b/nixos/modules/turris-omnialeds.nix
new file mode 100644
index 0000000..8063eb6
--- /dev/null
+++ b/nixos/modules/turris-omnialeds.nix
@@ -0,0 +1,152 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+
+ cfg = config.turris.omnialeds;
+
+ ledConfig = {name, trigger ? "none", netdevName ? null, color}: {
+ enabled = mkOption {
+ type = types.bool;
+ default = true;
+ description = "If LED should be enabled at all";
+ };
+ brightness = mkOption {
+ type = types.ints.u8;
+ default = 255;
+ description = "Set brightness intensity";
+ };
+ color = let
+ clr = c: mkOption {
+ type = types.ints.u8;
+ default = color.${c};
+ description = "Set intensity for the color ${c}";
+ };
+ in {
+ red = clr "red";
+ green = clr "green";
+ blue = clr "blue";
+ };
+ trigger = mkOption {
+ type = types.str;
+ default = trigger;
+ description = "Trigger for the LED";
+ };
+ # netdev trigger
+ netdevName = mkOption {
+ type = with types; nullOr str;
+ default = netdevName;
+ description = ''
+ Name of the network device to trigger when trigger is set to `netdev`
+ '';
+ };
+ };
+ lanConfig = i: ledConfig {
+ name = "lan${toString i}";
+ trigger = "netdev";
+ netdevName = "lan${toString i}";
+ color = {
+ red = 255; green = 255; blue = 0;
+ };
+ };
+ wlanConfig = i: ledConfig {
+ name = "wlan${toString i}";
+ color = {
+ red = 0; green = 255; blue = 255;
+ };
+ };
+ indicatorConfig = i: ledConfig {
+ name = "indicator${toString i}";
+ color = {
+ red = 0; green = 255; blue = 255;
+ };
+ };
+
+ ledSetup = sysname: lcfg: ''
+ echo ${toString lcfg.brightness} > /sys/class/leds/rgb:${sysname}/brightness
+ echo ${toString lcfg.color.red} ${toString lcfg.color.green} ${toString lcfg.color.blue} > /sys/class/leds/rgb:${sysname}/multi_intensity
+ echo ${lcfg.trigger} > /sys/class/leds/rgb:${sysname}/trigger
+ ${optionalString (lcfg.trigger == "netdev") "echo '${lcfg.netdevName}' > /sys/class/leds/rgb:${sysname}/device_name"}
+ '';
+
+in {
+
+ options = {
+ turris.omnialeds = {
+ enabled = mkOption {
+ type = types.bool;
+ default = true;
+ description = "If Omnia LEDs setup should be enabled or not.";
+ };
+ brightness = mkOption {
+ type = with types; nullOr (ints.between 0 100);
+ default = null;
+ description = "Global brightness (overrides brightness set by the front button)";
+ };
+ power = ledConfig {
+ name = "power";
+ trigger = "heartbeat";
+ color = {
+ red = 0; green = 255; blue = 0;
+ };
+ };
+ wan = ledConfig {
+ name = "wan";
+ trigger = "netdev";
+ netdevName = "eth2";
+ color = {
+ red = 0; green = 255; blue = 0;
+ };
+ };
+ lan0 = lanConfig 0;
+ lan1 = lanConfig 1;
+ lan2 = lanConfig 2;
+ lan3 = lanConfig 3;
+ lan4 = lanConfig 4;
+ wlan1 = wlanConfig 1;
+ wlan2 = wlanConfig 2;
+ wlan3 = wlanConfig 3;
+ indicator1 = indicatorConfig 1;
+ indicator2 = indicatorConfig 2;
+ extraCommands = mkOption {
+ type = types.lines;
+ default = "";
+ description = "Extra commands executed to setup LEDs";
+ };
+ };
+ };
+
+ config = mkIf (config.turris.board == "omnia" && cfg.enabled) {
+
+ # TODO modprobe triggers only if required
+ boot.kernelModules = [
+ "ledtrig_tty" "ledtrig_activity" "ledtrig_pattern" "ledtrig_netdev"
+ "ledtrig_usbport"
+ ];
+
+ systemd.services."omnia-leds" = {
+ script = ''
+ ${optionalString (cfg.brightness != null)
+ "echo ${toString cfg.brightness} > /sys/devices/platform/soc/soc:internal-regs/f1011000.i2c/i2c-0/i2c-1/1-002b/brightness"}
+ ${ledSetup "power" cfg.power}
+ ${ledSetup "wan" cfg.wan}
+ ${ledSetup "lan-0" cfg.lan0}
+ ${ledSetup "lan-1" cfg.lan1}
+ ${ledSetup "lan-2" cfg.lan2}
+ ${ledSetup "lan-3" cfg.lan3}
+ ${ledSetup "lan-4" cfg.lan4}
+ ${ledSetup "wlan-1" cfg.wlan1}
+ ${ledSetup "wlan-2" cfg.wlan2}
+ ${ledSetup "wlan-3" cfg.wlan3}
+ ${ledSetup "indicator-1" cfg.indicator1}
+ ${ledSetup "indicator-2" cfg.indicator2}
+
+ # Extra commands
+ ${cfg.extraCommands}
+ '';
+ wantedBy = [ "multi-user.target" ];
+ };
+
+ };
+}