repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
fan-throttle-guard.nixNix127 lines5.8 KiBraw download
# modules/hosts/laptop/fan-throttle-guard.nix
#
# Dead-GPU-fan workaround for the PCSpecialist Valeon II 17 (TongFang GM7RGxM,
# Ryzen 9 6900HX + RTX 3070 Ti Laptop). The embedded controller sees the dead
# "Secondary" fan (fan2: 0 rpm while commanded 100 %), raises its fan-abnormal
# flag and, once Tctl reaches ~79 °C, asserts PROCHOT and pins all 16 threads at
# 399 MHz until ~47 °C. That policy is firmware; Linux cannot switch it off.
# The fix is to keep the CPU from ever reaching the trip point:
#
#   1. a static scaling_max_freq floor of 3.2 GHz applied by tmpfiles at boot
#      (3.2 GHz ≈ base clock → ~67-70 °C under all-core load, no trips),
#   2. a staged guard (3200 → 2400 → 1800 MHz) driven by k10temp + the uniwill
#      board sensor, which also covers the "latched" low-temperature clamp,
#   3. the surviving CPU fan held at 60 % duty through the EC's own ACPI
#      methods (acpi_call → \_SB.INOU.ECRR/ECRW, TUXEDO register recipe),
#   4. power-profiles-daemon kept on, profile "performance", and the global
#      cpufreq boost flag left at 1 — never use boost=0, ppd 0.30 writes
#      per-policy boost on every switch and fails with EINVAL otherwise.
#
# Everything here was measured on the Arch install this was captured from
# (fanfix 2026-08-23, stability guard 2026-09-07). Files beside this module:
#   fanfix              the CLI/daemon (bash)         ← copied verbatim
#   stability_guard.py  the staged ceiling (python3)  ← copied verbatim
#   fan-ctl, fan-state  bar-widget helpers; need a polkit agent and a bar slot
#
# Verify on first boot:  fanfix status · fanfix fan status · fanfix test 30
#   expected: cap 3200 MHz, boost 1, profile performance, no THROTTLE event,
#   peak < 75 °C. If a trip still happens: lower the floor to 3000000 below.
{ ... }:
{
  flake.nixosModules.laptop-fan-throttle-guard =
  { config, pkgs, lib, ... }:

  let
    # fanfix is plain bash; wrap it so the shebang resolves and PATH is supplied
    # by the unit (fanDeps) rather than by whatever shell invoked it.
    fanfix = pkgs.writeShellScriptBin "fanfix" (builtins.readFile ./fanfix);

    fanDeps = with pkgs; [
      coreutils gnugrep gawk gnused procps util-linux
      kmod                   # modprobe acpi_call / uniwill-laptop
      systemd                # systemctl, systemd-tmpfiles
      power-profiles-daemon  # powerprofilesctl
    ];

    capKhz = 3200000;        # the floor. 3000000 is the documented fallback.
  in
  {
    ##### 1. EC access and fan/temperature readout ##############################
    # acpi_call is out-of-tree (nixpkgs: linuxPackages.acpi_call). uniwill-laptop
    # is in-tree; `force=1` is required because the DMI match list does not carry
    # this GM7RGxM. Verify `modinfo uniwill-laptop` exists on the chosen kernel.
    boot.extraModulePackages = [ config.boot.kernelPackages.acpi_call ];
    boot.kernelModules = [ "acpi_call" "uniwill-laptop" ];
    boot.extraModprobeConfig = ''
      options uniwill-laptop force=1
    '';

    ##### 2. Static floor, applied before any user load exists ###################
    systemd.tmpfiles.rules = [
      "w /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq - - - - ${toString capKhz}"
    ];

    ##### 3. power-profiles-daemon stays on ######################################
    services.power-profiles-daemon.enable = true;

    ##### 4. Staged thermal ceiling (replaces /etc/systemd/system/motherboard-stability.service)
    systemd.services.motherboard-stability = {
      description = "CPU stability limits for the GM7RGxM fan/power fault workaround";
      after = [ "systemd-tmpfiles-setup.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "simple";
        ExecStart = "${pkgs.python3}/bin/python3 -I ${./stability_guard.py}";
        Restart = "on-failure";
        RestartSec = 3;
        RuntimeDirectory = "motherboard-stability";
        RuntimeDirectoryMode = "0755";
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        ReadWritePaths = [ "/sys/devices/system/cpu" "/run/motherboard-stability" ];
        PrivateTmp = true;
        PrivateDevices = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;
        RestrictAddressFamilies = "AF_UNIX";
        LockPersonality = true;
        RestrictSUIDSGID = true;
        CapabilityBoundingSet = "";
        UMask = "0022";
      };
    };

    ##### 5. Surviving CPU fan at a fixed 60 % duty ##############################
    # CURVE is "temp:pct …" pairs; a flat 0:60 100:60 is what has been running.
    # fanfix refuses anything below 30 %. Edit here, not in /etc, then rebuild.
    #
    # NOT started at boot (wantedBy = []). Measured 2026-10-07 on NixOS: while the
    # daemon holds the EC in manual/custom-table fan mode, the EC asserts PROCHOT
    # (all cores 399 MHz) the moment any load starts, even at 37 °C. Stopping the
    # unit and `fanfix fan auto` cleared it instantly; 10 s all-core test then ran
    # at 3112 MHz, peak 53 °C, 0 trips. The 3.2 GHz floor + stability guard are
    # enough on their own. Start by hand to experiment: systemctl start fanfix-fan
    environment.etc."fanfix.conf".text = ''
      CURVE="0:60 100:60"
    '';

    systemd.services.fanfix-fan = {
      description = "fanfix: temperature → fan-duty curve for the surviving CPU fan (dead GPU fan workaround)";
      after = [ "multi-user.target" ];
      wantedBy = [ ];   # see note above; manual start only
      path = fanDeps;
      serviceConfig = {
        Type = "simple";
        ExecStart = "${fanfix}/bin/fanfix fan-daemon";
        ExecStopPost = "${fanfix}/bin/fanfix fan-release";
        Restart = "on-failure";
        RestartSec = 5;
      };
    };

    ##### 6. Tools on PATH ######################################################
    environment.systemPackages = [ fanfix pkgs.lm_sensors ] ++ fanDeps;
  }
  ;
}