Compare commits

...
Author SHA1 Message Date
müde
167f213fd0 add cpucap to muede home config 2026-08-02 17:15:48 +02:00
müde
30adb68c04 nix flake update, remove missed override 2026-08-02 14:17:23 +02:00
5 changed files with 218 additions and 36 deletions

64
flake.lock generated
View file

@ -373,11 +373,11 @@
]
},
"locked": {
"lastModified": 1785542226,
"narHash": "sha256-QUCQBPwY2fEAnNiXC7p7vilJXOExbsrHs4MhTxp5ubY=",
"lastModified": 1785592426,
"narHash": "sha256-LvmGttFv/nJNMEWkiDoWczZ3S/4809pBfHzvlq5/XDo=",
"ref": "refs/heads/main",
"rev": "38aa5f77f4901286c384011dd3b51dc272f47985",
"revCount": 3078,
"rev": "0aac20d863276316a01c0da21e8d5e286caa2900",
"revCount": 3090,
"type": "git",
"url": "https://forge.darkest.space/hyperhive/hyperhive.git"
},
@ -450,11 +450,11 @@
"xwayland-satellite-unstable": "xwayland-satellite-unstable"
},
"locked": {
"lastModified": 1785494453,
"narHash": "sha256-nK6DCmFpPgaHweU2Y8BMxutfTihwXJ2XkxaoMZPBpMQ=",
"lastModified": 1785610189,
"narHash": "sha256-6m4cY11Fzv/GXrWBwC9RTrK7Kj5g2rmxrAy+cBIHnSY=",
"owner": "sodiboo",
"repo": "niri-flake",
"rev": "68414146fbe13d8fce7748cf2f4e9e46e4834e2e",
"rev": "db5a178309551a16c0ca895ec55a2592c6daf963",
"type": "github"
},
"original": {
@ -503,11 +503,11 @@
]
},
"locked": {
"lastModified": 1785468833,
"narHash": "sha256-e5ACzQu7wKCje0ziNMm+xSyIaZWfMB2W573p6dgStHA=",
"lastModified": 1785555171,
"narHash": "sha256-MLBtKBAbLXB81qm5UZq7w0IFnP/fh6QWhfU55dA8uFo=",
"owner": "nix-community",
"repo": "nix-vscode-extensions",
"rev": "b9452374ccfd2b13c946198f932814e79f28e2bc",
"rev": "730fe1d38d63878df9cf6b46b36308d197a9334e",
"type": "github"
},
"original": {
@ -586,11 +586,11 @@
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1783459375,
"narHash": "sha256-S2pb+Mtv0qTeRr+6J5ESgelNjG2YKl4WqKjdPrJx5rM=",
"lastModified": 1785596452,
"narHash": "sha256-KT/OleUMpSKsWgi0eTuqS/0GD4ucPQcvLgmvlw8ZuCM=",
"owner": "nvmd",
"repo": "nixos-raspberrypi",
"rev": "c845922f260b306d4529a4535bbb6e7f8cfe2887",
"rev": "7e39508bcf9c1da82cf11c1e22f74f9d9fd0fe10",
"type": "github"
},
"original": {
@ -602,27 +602,27 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1782847189,
"narHash": "sha256-twXPFqFsrrY5r28Zh7Homgcp2gUMBgQ6WDS98Q/3xFI=",
"lastModified": 1784856561,
"narHash": "sha256-J+Bx1Z6Oeoj2FgnBhRMKyUhhtDoOpTgXYaVLZpDjW4A=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b6018f87da91d19d0ab4cf979885689b469cdd41",
"rev": "597283ad8aa0b331c788e97c4c262d58877074ef",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-25.11",
"ref": "nixos-26.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1785301185,
"narHash": "sha256-eoS3KQTO0aPWXZvIaRbRAzSSHW3l5wdMFXtT1ISfoKA=",
"lastModified": 1785542685,
"narHash": "sha256-sjfvXMbG5pCFgpvw2OZAFcZiTvrppCWvnB6cuGrSY08=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9bc02893134c733dd85de46ee4fb2fac696b5529",
"rev": "f8e81fc7eb063db454f563cdd596fb96a5ad1497",
"type": "github"
},
"original": {
@ -634,11 +634,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1785386831,
"narHash": "sha256-sPS3CaXH8RAT3FZRuy4VcV47iuYIWMMfa0GbyJKC3o4=",
"lastModified": 1785491804,
"narHash": "sha256-p00vplVOyfKGlhju0+eGGPAxyYYKaY0lpyuoHLFIx2Y=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "21ea275a7c46aef9d4d6ddc962e6d562e9d94183",
"rev": "5b4f72e1705a63aace42900610c4db82cb4af0ec",
"type": "github"
},
"original": {
@ -679,11 +679,11 @@
]
},
"locked": {
"lastModified": 1785541122,
"narHash": "sha256-+977ybTIFOFYHK9xMBsgXHQmVR8EgjQSzkloBdgMJsU=",
"lastModified": 1785618461,
"narHash": "sha256-jADZGwlUb2LJHujw6lHhLJTZS4MKQPid8Bw3xkIxnNc=",
"owner": "nix-community",
"repo": "NUR",
"rev": "64e74ace88f3101a283d364ca73ee16ae0aa992a",
"rev": "45f0f529b705b537be04a41a4d4a35c0d2bef71e",
"type": "github"
},
"original": {
@ -871,11 +871,11 @@
"tinted-zed": "tinted-zed"
},
"locked": {
"lastModified": 1785537671,
"narHash": "sha256-i8AdoRLhoh05jpmrGifLfIEGorlxp2hdGjMsojkKBuE=",
"lastModified": 1785618782,
"narHash": "sha256-7meaDuR+VNhPBjhywii1Sz+rNs4BQuM+NouyJ2Pghw4=",
"owner": "nix-community",
"repo": "stylix",
"rev": "06c8a9425f1c0248dc9b84c1e2c9001fa41d7d0a",
"rev": "3e9ffce379abce9816dad066de09deae8565504c",
"type": "github"
},
"original": {
@ -1019,11 +1019,11 @@
]
},
"locked": {
"lastModified": 1785484808,
"narHash": "sha256-PCxudeJmGJ5Gjft5/iHyKxtJyK1EZXgBRjIoDwc0fv8=",
"lastModified": 1785598680,
"narHash": "sha256-/S31ZiXco1m0HCtRjr55Wt81kt33zOxfjs2EP2h7a2o=",
"owner": "vibec0re",
"repo": "trollshell",
"rev": "13f189cf2cb2e0d57b0f6c43550366c0a09ea970",
"rev": "4e9d0a2bcb3f352eb9e301e0aaee97387db0bf5a",
"type": "github"
},
"original": {

View file

@ -63,10 +63,7 @@
};
servicepoint-simulator = {
url = "git+https://git.berlin.ccc.de/servicepoint/servicepoint-simulator.git";
inputs = {
nixpkgs.follows = "nixpkgs";
naersk.follows = "naersk";
};
inputs.nixpkgs.follows = "nixpkgs";
};
servicepoint-tanks = {
url = "git+https://forge.darkest.space/servicepoint/servicepoint-tanks.git";
@ -175,5 +172,12 @@
formatting = treefmt-eval.config.build.check self;
}
);
devShells = forAllSystems (
{ pkgs, ... }:
{
default = pkgs.mkShellNoCC { packages = [ pkgs.shellcheck ]; };
}
);
};
}

View file

@ -0,0 +1,25 @@
{ lib, pkgs, ... }:
let
# ./cpucap.sh goes into the store verbatim rather than through readFile or a
# generated wrapper script, because `cpucap --help` sed's the comment header
# back out of "$0" - only an unaltered copy of the file keeps that working.
cpucap = pkgs.runCommandLocal "cpucap" { nativeBuildInputs = [ pkgs.makeWrapper ]; } ''
install -Dm755 ${./cpucap.sh} $out/bin/cpucap
# Only line 1 changes, so the comment header --help prints stays put.
patchShebangs $out/bin/cpucap
wrapProgram $out/bin/cpucap \
--prefix PATH : ${
lib.makeBinPath [
pkgs.gawk
pkgs.gnused
]
} \
--set CPUCAP_SELF $out/bin/cpucap
'';
in
{
# PATH is prefixed, not set: the script re-execs itself through
# /run/wrappers/bin/sudo for the writing subcommands, and sudo's own env
# handling plus the inherited system PATH have to survive that round trip.
home.packages = [ cpucap ];
}

View file

@ -0,0 +1,152 @@
#!/usr/bin/env bash
# cpucap - clamp scaling_max_freq on every cpufreq policy, per-policy hw-range safe.
#
# cpucap 1.2GHz cap every policy at 1.2 GHz (clamped into its own hw range)
# cpucap 900MHz
# cpucap 1200000 raw kHz also accepted
# cpucap --reset restore each policy to its cpuinfo_max_freq
# cpucap --show print current state (freq caps, package power, temp)
# cpucap --power 12 also set RAPL PL1+PL2 to 12 W (see note at bottom)
# cpucap --no-turbo pin to base clock (2400 P / 1800 E); --turbo to undo
#
# Anything that writes needs root; it re-runs itself under sudo on its own.
set -euo pipefail
POLICIES=(/sys/devices/system/cpu/cpufreq/policy*)
RAPL=/sys/class/powercap/intel-rapl:0
die() { printf 'cpucap: %s\n' "$*" >&2; exit 1; }
# Re-exec under sudo rather than dying with a hint: this lives on the user's
# PATH via home.packages, so plain `sudo cpucap` would not resolve. CPUCAP_SELF
# is set by the nix wrapper to its own $out/bin path; "$0" is the fallback for
# running the file straight out of a checkout. Hardcoded sudo path because only
# /run/wrappers/bin's copy is setuid - the one in the store is not.
need_root() {
if [[ $EUID -ne 0 ]]; then
exec /run/wrappers/bin/sudo "${CPUCAP_SELF:-$0}" "$@"
fi
}
# Accept 1.2GHz / 1200MHz / 1200000kHz / 1200000 -> kHz integer
to_khz() {
local v="${1,,}" n
case "$v" in
*ghz) n="${v%ghz}"; awk -v n="$n" 'BEGIN{printf "%d", n * 1000000}' ;;
*mhz) n="${v%mhz}"; awk -v n="$n" 'BEGIN{printf "%d", n * 1000}' ;;
*khz) n="${v%khz}"; awk -v n="$n" 'BEGIN{printf "%d", n}' ;;
*[!0-9]*) die "cannot parse frequency '$1' (use e.g. 1.2GHz, 900MHz, or raw kHz)" ;;
*) echo "$v" ;;
esac
}
fmt_mhz() { awk -v k="$1" 'BEGIN{printf "%.0f MHz", k / 1000}'; }
show() {
local p hw_min hw_max cur_min cur_max
printf '%-9s %-11s %-11s %s\n' POLICY CAP MIN 'HW RANGE'
for p in "${POLICIES[@]}"; do
hw_min=$(< "$p/cpuinfo_min_freq"); hw_max=$(< "$p/cpuinfo_max_freq")
cur_min=$(< "$p/scaling_min_freq"); cur_max=$(< "$p/scaling_max_freq")
printf '%-9s %-11s %-11s %s - %s\n' \
"${p##*/}" "$(fmt_mhz "$cur_max")" "$(fmt_mhz "$cur_min")" \
"$(fmt_mhz "$hw_min")" "$(fmt_mhz "$hw_max")"
done
if [[ -r /sys/devices/system/cpu/intel_pstate/no_turbo ]]; then
printf '\nintel_pstate: status=%s no_turbo=%s max_perf_pct=%s\n' \
"$(< /sys/devices/system/cpu/intel_pstate/status)" \
"$(< /sys/devices/system/cpu/intel_pstate/no_turbo)" \
"$(< /sys/devices/system/cpu/intel_pstate/max_perf_pct)"
fi
if [[ -r $RAPL/constraint_0_power_limit_uw ]]; then
printf 'RAPL package: PL1=%sW PL2=%sW\n' \
"$(( $(< "$RAPL/constraint_0_power_limit_uw") / 1000000 ))" \
"$(( $(< "$RAPL/constraint_1_power_limit_uw") / 1000000 ))"
fi
local t
for t in /sys/class/hwmon/hwmon*; do
[[ $(< "$t/name") == coretemp ]] || continue
[[ -r $t/temp1_input ]] && printf 'package temp: %s C\n' "$(( $(< "$t/temp1_input") / 1000 ))"
done
}
set_cap() {
local target=$1 p hw_min hw_max want cur_min got
printf '%-9s %-11s %s\n' POLICY REQUESTED APPLIED
for p in "${POLICIES[@]}"; do
hw_min=$(< "$p/cpuinfo_min_freq")
hw_max=$(< "$p/cpuinfo_max_freq")
# Clamp into this policy's own hw range. P-cores and E-cores differ.
want=$target
(( want < hw_min )) && want=$hw_min
(( want > hw_max )) && want=$hw_max
# scaling_min must not exceed the new max, or the write is rejected.
cur_min=$(< "$p/scaling_min_freq")
(( cur_min > want )) && echo "$want" > "$p/scaling_min_freq"
echo "$want" > "$p/scaling_max_freq"
# intel_pstate rounds to the nearest achievable P-state, so read back.
got=$(< "$p/scaling_max_freq")
printf '%-9s %-11s %s%s\n' "${p##*/}" "$(fmt_mhz "$want")" "$(fmt_mhz "$got")" \
"$( (( got != want )) && echo ' (rounded)' )"
done
}
reset_cap() {
local p
for p in "${POLICIES[@]}"; do
echo "$(< "$p/cpuinfo_min_freq")" > "$p/scaling_min_freq"
echo "$(< "$p/cpuinfo_max_freq")" > "$p/scaling_max_freq"
done
echo "restored all policies to their hardware maximum"
}
set_power() {
local watts=$1 uw max
if [[ -z $watts || $watts == *[!0-9]* ]]; then
die "--power takes whole watts, got '$watts'"
fi
uw=$((watts * 1000000))
[[ -w $RAPL/constraint_0_power_limit_uw ]] || die "RAPL not writable at $RAPL"
max=$(< "$RAPL/constraint_0_max_power_uw")
(( max > 0 && uw > max )) && die "requested ${watts}W exceeds RAPL max $((max / 1000000))W"
echo "$uw" > "$RAPL/constraint_0_power_limit_uw" # PL1, sustained
echo "$uw" > "$RAPL/constraint_1_power_limit_uw" # PL2, burst
printf 'RAPL package: PL1=PL2=%sW\n' \
"$(( $(< "$RAPL/constraint_0_power_limit_uw") / 1000000 ))"
}
set_turbo() {
local off=$1 f=/sys/devices/system/cpu/intel_pstate/no_turbo
[[ -w $f ]] || die "no_turbo not writable (intel_pstate not in active mode?)"
echo "$off" > "$f"
# Turbo-off pins each policy to base_frequency; an existing lower cap still wins.
if [[ $off == 1 ]]; then
echo "turbo disabled - ceiling is now base clock, or your cap if it is lower"
else
echo "turbo enabled"
fi
}
[[ ${#POLICIES[@]} -gt 0 && -d ${POLICIES[0]} ]] || die "no cpufreq policies found"
case "${1-}" in
''|--show|-s) show ;;
--reset|-r) need_root "$@"; reset_cap; echo; show ;;
--power|-p) need_root "$@"; [[ -n ${2-} ]] || die "--power needs a wattage"
set_power "$2" ;;
--no-turbo) need_root "$@"; set_turbo 1; echo; show ;;
--turbo) need_root "$@"; set_turbo 0; echo; show ;;
-h|--help) sed -n '2,12p' "$0" | sed 's/^# \?//' ;;
-*) die "unknown option '$1' (see --help)" ;;
*) need_root "$@"; set_cap "$(to_khz "$1")"
if [[ ${2-} == --power || ${2-} == -p ]]; then set_power "$3"; fi
echo; show ;;
esac

View file

@ -3,6 +3,7 @@
imports = [
# keep-sorted start
./claude.nix
./cpucap.nix
./editorconfig.nix
./element.nix
./fonts.nix