latest clean up for fwbook sleep
This commit is contained in:
1 parent
2514d9935e
commit
c94b2923e8
2 files changed
+118
-14
No files matched your search
+5
-2
@@ -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;
|
||||
|
||||
@@ -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
|
||||
# '';
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user