# modules/hosts/laptop/fan-cli.nix — root side of the `fan` command (modules/home/fan.nix). # # `fan-ec` talks to the embedded controller the way fanfix does (same # acpi_call recipe, same registers; see ~/.local/bin/fanfix and the TUXEDO # driver), but it is a fixed script in the Nix store, so the wheel group may # run it through sudo without a password. That is what lets the watchdog in # fan.nix put the fans back to EC-automatic from a background unit, where # sudo could not ask for one. fanfix itself lives in the user-writable # ~/.local/bin and must never get such a rule. # # fan-ec status mode, duty %, EC flags fan-ec max 100 % (manual) # fan-ec auto hand control back to the EC fan-ec <30-100> fixed % (manual) # fan-ec mode one word: auto | manual | curve-daemon # # Manual mode on this laptop makes the EC assert PROCHOT (all cores 399 MHz) # under load (fan-throttle-guard.nix, line ~97). fan-ec does not try to # prevent that; the user-side watchdog detects the clamp and calls `fan-ec auto`. { ... }: { flake.nixosModules.laptop-fan-cli = { pkgs, lib, ... }: let fan-ec = pkgs.writeShellScriptBin "fan-ec" '' set -uo pipefail # PATH first and with nothing appended: sudo keeps the caller's PATH (no # secure_path), so a bare `id` or an inherited `:$PATH` would let any # process running as the user put its own `id` first and get root. PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.kmod pkgs.systemd ]} [ "$EUID" = 0 ] || { echo "fan-ec: run as root (sudo fan-ec …)" >&2; exit 1; } ACPI_CALL=/proc/acpi/call EC_DEV='\_SB.INOU' FAN_UNIT=fanfix-fan.service FAN_MIN_PCT=30 ec_ready() { [ -w "$ACPI_CALL" ] || modprobe acpi_call 2>/dev/null; [ -w "$ACPI_CALL" ]; } ec_raw() { printf '%s' "$1" > "$ACPI_CALL" || return 1; local o; o=$(tr -d '\0\n\r ' < "$ACPI_CALL"); printf '%s' "$o"; } ec_read() { local out; out=$(ec_raw "$(printf '%s.ECRR 0x%04x' "$EC_DEV" "$1")") || return 1 [[ "$out" =~ ^0x[0-9a-fA-F]+$ ]] || { echo "EC read $(printf '0x%04x' "$1") failed: $out" >&2; return 1; } echo $(( out )); } ec_write() { local out; out=$(ec_raw "$(printf '%s.ECRW 0x%04x 0x%02x' "$EC_DEV" "$1" "$2")") || return 1 case "$out" in Error*|"not called"|"") echo "EC write $(printf '0x%04x' "$1") failed: ''${out:-no result}" >&2; return 1;; esac sleep 0.005; } ec_set_bits() { local v; v=$(ec_read "$1") || return 1; ec_write "$1" $(( v | $2 )); } ec_clear_bits() { local v; v=$(ec_read "$1") || return 1; ec_write "$1" $(( v & ~$2 & 0xff )); } ec_bit() { local v; v=$(ec_read "$1") || return 1; echo $(( (v >> $2) & 1 )); } R_AP_OEM=0x0741; R_FAN_MODE=0x0751; R_PWM1=0x075B; R_PWM2=0x075C R_FAN_CTRL=0x078E; R_TBL_SPLIT=0x07C5; R_TBL_ENABLE=0x07C6 R_CPU_TBL_END=0x0F00; R_CPU_TBL_START=0x0F10; R_CPU_TBL_SPEED=0x0F20 R_GPU_TBL_END=0x0F30; R_GPU_TBL_START=0x0F40; R_GPU_TBL_SPEED=0x0F50 R_PWM1_W=0x1804; R_PWM2_W=0x1809 universal_ctrl() { ec_bit $R_FAN_CTRL 6; } tables_enabled() { ec_bit $R_TBL_ENABLE 2; } pct_to_duty() { echo $(( $1 * 200 / 100 )); } duty_to_pct() { echo $(( $1 * 100 / 200 )); } fan_init_tables() { local i ec_clear_bits $R_FAN_MODE 0x40 [ "$(ec_bit $R_TBL_SPLIT 7)" = 1 ] || ec_set_bits $R_TBL_SPLIT 0x80 ec_write $R_CPU_TBL_END 115; ec_write $R_CPU_TBL_START 0; ec_write $R_CPU_TBL_SPEED 1 ec_write $R_GPU_TBL_END 120; ec_write $R_GPU_TBL_START 0; ec_write $R_GPU_TBL_SPEED 1 for i in $(seq 1 15); do ec_write $(( R_CPU_TBL_END + i )) $(( 115 + i + 1 )); ec_write $(( R_CPU_TBL_START + i )) $(( 115 + i )); ec_write $(( R_CPU_TBL_SPEED + i )) 200 ec_write $(( R_GPU_TBL_END + i )) $(( 115 + i + 1 )); ec_write $(( R_GPU_TBL_START + i )) $(( 115 + i )); ec_write $(( R_GPU_TBL_SPEED + i )) 200 done [ "$(tables_enabled)" = 1 ] || ec_set_bits $R_TBL_ENABLE 0x04 } fan_apply_duty() { local d=$1 if [ "$(universal_ctrl)" = 1 ]; then [ "$(tables_enabled)" = 1 ] && [ "$(ec_bit $R_FAN_MODE 6)" = 0 ] || fan_init_tables ec_write $R_CPU_TBL_SPEED "$d"; ec_write $R_GPU_TBL_SPEED "$d" ec_write $R_PWM1_W "$d"; ec_write $R_PWM2_W "$d" else local i; [ "$(ec_bit $R_FAN_MODE 6)" = 1 ] || ec_set_bits $R_FAN_MODE 0x40 for i in 1 2 3 4 5 6 7 8 9 10; do ec_write $R_PWM1_W "$d"; ec_write $R_PWM2_W "$d"; sleep 0.01; done fi } fan_set_auto() { if [ "$(universal_ctrl)" = 1 ]; then [ "$(tables_enabled)" = 1 ] && ec_clear_bits $R_TBL_ENABLE 0x04 [ "$(ec_bit $R_TBL_SPLIT 7)" = 1 ] && ec_clear_bits $R_TBL_SPLIT 0x80 fi [ "$(ec_bit $R_FAN_MODE 6)" = 1 ] && ec_clear_bits $R_FAN_MODE 0x40 return 0 } mode_word() { if systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null; then echo curve-daemon; return; fi if [ "$(ec_bit $R_FAN_MODE 6)" = 1 ] || [ "$(tables_enabled)" = 1 ]; then echo manual; else echo auto; fi } stop_daemon() { systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null && systemctl stop "$FAN_UNIT"; return 0; } guard() { ec_ready || { echo "fan-ec: acpi_call not available" >&2; exit 1; } ec_read $R_AP_OEM >/dev/null || { echo "fan-ec: EC not reachable via $EC_DEV.ECRR" >&2; exit 1; }; } case "''${1:-status}" in mode) guard; mode_word ;; status) guard printf 'mode=%s duty=%s%% target=%s%% fan-abnormal=%s full-fan-bit=%s tables=%s\n' \ "$(mode_word)" "$(duty_to_pct "$(ec_read $R_PWM1)")" \ "$([ "$(tables_enabled)" = 1 ] && duty_to_pct "$(ec_read $R_CPU_TBL_SPEED)" || echo -)" \ "$(ec_bit $R_AP_OEM 5)" "$(ec_bit $R_FAN_MODE 6)" "$(tables_enabled)" ;; auto) guard; stop_daemon; fan_set_auto && echo "fans: auto (EC curve)" ;; max) guard; stop_daemon; fan_apply_duty 200 && echo "fans: 100 % (manual)" ;; [0-9]*) p=''${1%\%}; [[ "$p" =~ ^[0-9]{1,3}$ ]] && p=$((10#$p)) && [ "$p" -le 100 ] && [ "$p" -ge "$FAN_MIN_PCT" ] \ || { echo "fan-ec: percent must be $FAN_MIN_PCT-100 (below that the EC trips at 79 °C)" >&2; exit 2; } guard; stop_daemon; fan_apply_duty "$(pct_to_duty "$p")" && echo "fans: $p % (manual)" ;; *) echo "usage: fan-ec status|mode|auto|max|<30-100>" >&2; exit 2 ;; esac ''; in { environment.systemPackages = [ fan-ec ]; # wheel may run fan-ec without a password: it is immutable store content # (via the system profile symlink, which is root-owned), does one thing, # and the watchdog has no terminal to type into. security.sudo.extraRules = [ { groups = [ "wheel" ]; commands = [ { command = "/run/current-system/sw/bin/fan-ec"; options = [ "NOPASSWD" ]; } ]; } ]; } ; }