From c94b2923e8f257b8c822f442c29bb31a372add72 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 28 Apr 2026 17:30:29 -0400 Subject: [PATCH] latest clean up for fwbook sleep --- hosts/fwbook.nix | 7 +- .../nixos/common/hardware/laptop-power.nix | 125 ++++++++++++++++-- 2 files changed, 118 insertions(+), 14 deletions(-) diff --git a/hosts/fwbook.nix b/hosts/fwbook.nix index 6ee3d10..75026e9 100644 --- a/hosts/fwbook.nix +++ b/hosts/fwbook.nix @@ -22,7 +22,11 @@ in common.hardware = { sound.enable = true; pipewire.enable = true; - laptop-power-mgr.enable = true; + laptop-power-mgr = { + enable = true; + enableLidMonitorMode = true; + disableLidSwitch = true; + }; amdgpu.enable = true; }; desktop = { @@ -72,7 +76,6 @@ in # Firmware updater hardware.framework.enableKmod = true; services.fprintd.enable = true; - hardware.framework.amd-7040.preventWakeOnAC = true; # Graphics hardware.graphics = { enable = true; diff --git a/modules/nixos/common/hardware/laptop-power.nix b/modules/nixos/common/hardware/laptop-power.nix index 6fa9700..3b4a8ef 100644 --- a/modules/nixos/common/hardware/laptop-power.nix +++ b/modules/nixos/common/hardware/laptop-power.nix @@ -1,13 +1,94 @@ { lib , config +, pkgs , ... }: let cfg = config.skyg.nixos.common.hardware.laptop-power-mgr; + + # Script to monitor lid events and send notifications + lid-monitor-script = pkgs.writeShellScript "lid-event-monitor" '' + ${pkgs.python3}/bin/python3 << 'EOFPYTHON' + import struct + import sys + import subprocess + import os + import time + + try: + print("Lid event monitor started - watching /dev/input/event2", file=sys.stderr, flush=True) + with open('/dev/input/event2', 'rb') as f: + while True: + event = f.read(24) + if not event: + break + + tv_sec, tv_usec, evt_type, code, value = struct.unpack('llHHI', event) + + # EV_SW = 5, SW_LID = 0 + if evt_type == 5 and code == 0: + status = "CLOSED" if value else "OPEN" + print(f"[{time.time()}] Lid event detected: {status}", file=sys.stderr, flush=True) + + # Try to send notification - look for active user sessions + for pid_dir in os.listdir('/proc'): + if not pid_dir.isdigit(): + continue + try: + environ_path = f'/proc/{pid_dir}/environ' + with open(environ_path, 'rb') as ef: + env_data = ef.read() + env_vars = {} + for var in env_data.split(b'\0'): + if b'=' in var: + key, val = var.split(b'=', 1) + env_vars[key.decode('utf-8', errors='ignore')] = val.decode('utf-8', errors='ignore') + + if 'DISPLAY' in env_vars and 'DBUS_SESSION_BUS_ADDRESS' in env_vars: + try: + subprocess.run([ + '${pkgs.libnotify}/bin/notify-send', + 'Lid Event', + f'Lid is now {status}' + ], env=env_vars, timeout=5, capture_output=True) + break + except Exception: + pass + except (IOError, OSError, ValueError): + pass + except IOError as e: + print(f"Could not open lid device: {e}", file=sys.stderr, flush=True) + sys.exit(1) + except KeyboardInterrupt: + print("Lid event monitor stopped", file=sys.stderr, flush=True) + EOFPYTHON + ''; in { options.skyg.nixos.common.hardware.laptop-power-mgr = with lib; { enable = mkEnableOption "Settings for laptop power management"; + + enableLidMonitorMode = mkOption { + type = types.bool; + default = false; + description = '' + Enable lid event monitoring mode. When enabled, lid close/open events + are monitored and sent as desktop notifications instead of triggering + system actions (hibernate/suspend). This is useful when the lid sensor + is working but you want visibility into lid events without automatic actions. + ''; + }; + + disableLidSwitch = mkOption { + type = types.bool; + default = false; + description = '' + Disable lid switch handling in logind. When enabled, systemd-logind + will ignore lid close/open events and not trigger any automatic actions. + This is useful when the lid sensor is not working properly and causes + issues (like infinite hibernation loops). + ''; + }; }; config = lib.mkIf cfg.enable { @@ -53,23 +134,43 @@ in turbo = "auto"; }; }; - systemd.sleep.extraConfig = "HibernateDelaySec=5m"; - # Logind + + # Logind configuration - optionally disable lid handling services.logind = { settings = { Login = { - HandleLidSwitchExternalPower = "hibernate"; - HandleLidSwitch = "hibernate"; HandlePowerKey = "lock"; - HandleRFKillKey = "ignore"; - }; + # Removed: HandleRFKillKey = "ignore"; (not recognized in systemd 258) + } // (if cfg.disableLidSwitch then { + HandleLidSwitch = "ignore"; + HandleLidSwitchExternalPower = "ignore"; + } else { + HandleLidSwitch = "suspend"; + HandleLidSwitchExternalPower = "suspend"; + }); + }; + }; + + # Add libnotify for notifications (needed for lid monitoring) + environment.systemPackages = with pkgs; [ + libnotify + ]; + + # System service to monitor lid events and send notifications + # Only enabled if enableLidMonitorMode is true + systemd.services.lid-event-monitor = lib.mkIf cfg.enableLidMonitorMode { + description = "Monitor lid switch events and send desktop notifications"; + wantedBy = [ "multi-user.target" ]; + after = [ "systemd-logind.service" ]; + + serviceConfig = { + Type = "simple"; + ExecStart = "${lid-monitor-script}"; + Restart = "on-failure"; + RestartSec = 5; + StandardOutput = "journal"; + StandardError = "journal"; }; - # extraConfig = '' - # LidSwitchIgnoreInhibited=yes - # HandlePowerKey=suspend-then-hibernate - # IdleAction=suspend-then-hibernate - # IdleActionSec=2m - # ''; }; }; }