mirror of
https://github.com/tomasriveral/nixos.git
synced 2026-08-12 10:30:44 +02:00
Modules: separated hosts-specific modules in new dir
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
pkgs-unstable,
|
||||
...
|
||||
}:
|
||||
|
||||
{
|
||||
wayland.windowManager.hyprland.settings = {
|
||||
exec-once = [
|
||||
"qtbatticon" # custom battery tray
|
||||
];
|
||||
monitor = [
|
||||
"eDP-1, highres@highrr, 0x0, 1"
|
||||
];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: {
|
||||
programs.zsh = {
|
||||
shellAliases = {
|
||||
snrt = "git add -A && sudo nixos-rebuild test --flake ~/nixos/#laptop";
|
||||
snrs = "git add -A && sudo nixos-rebuild switch --flake ~/nixos/#laptop";
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
# https://discourse.nixos.org/t/what-is-the-best-option-for-power-management/63406/2
|
||||
{...}:
|
||||
# we just activate tlp unconditonally
|
||||
#let
|
||||
# cfg = config.custom;
|
||||
#hasBattery =
|
||||
# lib.any (x: lib.strings.hasPrefix "BAT" x)
|
||||
# (builtins.attrNames (builtins.readDir "/sys/class/power_supply"));
|
||||
#in
|
||||
{
|
||||
# options.custom = {
|
||||
# battery.enable = lib.mkOption {
|
||||
# default = hasBattery;
|
||||
# description = "Enable better battery support";
|
||||
# type = lib.types.bool;
|
||||
# };
|
||||
# };
|
||||
|
||||
# config = lib.mkIf cfg.battery.enable {
|
||||
powerManagement.powertop.enable = true; # enable powertop auto tuning on startup.
|
||||
services.system76-scheduler.settings.cfsProfiles.enable = true; # Better scheduling for CPU cycles - thanks System76!!!
|
||||
services.thermald.enable = true; # Enable thermald, the temperature management daemon. (only necessary if on Intel CPUs)
|
||||
services.power-profiles-daemon.enable = false; # Disable GNOMEs power management
|
||||
services.tlp = {
|
||||
enable = true; # Enable TLP (better than gnomes internal power manager)
|
||||
settings = {
|
||||
CPU_BOOST_ON_AC = 1;
|
||||
CPU_BOOST_ON_BAT = 1;
|
||||
CPU_HWP_DYN_BOOST_ON_AC = 1;
|
||||
CPU_HWP_DYN_BOOST_ON_BAT = 1;
|
||||
CPU_SCALING_GOVERNOR_ON_AC = "performance";
|
||||
CPU_SCALING_GOVERNOR_ON_BAT = "powersave";
|
||||
CPU_ENERGY_PERF_POLICY_ON_AC = "performance";
|
||||
CPU_ENERGY_PERF_POLICY_ON_BAT = "balance_power";
|
||||
PLATFORM_PROFILE_ON_AC = "performance";
|
||||
PLATFORM_PROFILE_ON_BAT = "balanced";
|
||||
START_CHARGE_THRESH_BAT1 = 75;
|
||||
STOP_CHARGE_THRESH_BAT1 = 85;
|
||||
};
|
||||
};
|
||||
# };
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}: {
|
||||
# udev with only the necessary rules. See udev.nix to get all the rules
|
||||
services.udev.extraRules = ''
|
||||
# adapted from here https://wiki.nixos.org/wiki/Keychron_M6
|
||||
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="32ac", MODE="0660", GROUP="users", TAG+="uaccess"
|
||||
|
||||
|
||||
# taken from here
|
||||
SUBSYSTEMS=="usb", ATTRS{idVendor}=="32ac", ATTRS{idProduct}=="0013", TAG+="uaccess"
|
||||
SUBSYSTEMS=="usb", ATTRS{idVendor}=="32ac", ATTRS{idProduct}=="0018", TAG+="uaccess"
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
# because cbatticon, batticonplus and other things didn't work.
|
||||
# chatGpt did this.
|
||||
# https://chatgpt.com/c/69da11b3-ba24-8331-b7b0-a58a72e69d42
|
||||
# at your risk and perils
|
||||
# click on the icon to get a notification with state, percentage and wattage
|
||||
{
|
||||
lib,
|
||||
stdenv,
|
||||
qt6,
|
||||
pkg-config,
|
||||
pkgs,
|
||||
}:
|
||||
stdenv.mkDerivation {
|
||||
pname = "qtbatticon";
|
||||
version = "1.0";
|
||||
|
||||
src = pkgs.writeTextFile {
|
||||
name = "qtbatticon.cpp";
|
||||
text = ''
|
||||
#include <QApplication>
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QTimer>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QIcon>
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
#include <cstdlib>
|
||||
|
||||
QString readFile(const QString &path) {
|
||||
QFile f(path);
|
||||
if (!f.open(QIODevice::ReadOnly)) return "";
|
||||
QTextStream in(&f);
|
||||
return in.readAll().trimmed();
|
||||
}
|
||||
|
||||
int readInt(const QString &path) {
|
||||
return readFile(path).toInt();
|
||||
}
|
||||
|
||||
double readDouble(const QString &path) {
|
||||
QFile f(path);
|
||||
if (!f.open(QIODevice::ReadOnly)) return 0;
|
||||
QTextStream in(&f);
|
||||
return in.readAll().trimmed().toDouble();
|
||||
}
|
||||
|
||||
int toStep(int capacity) {
|
||||
return (capacity / 10) * 10;
|
||||
}
|
||||
|
||||
QString pad3(int n) {
|
||||
return QString("%1").arg(n, 3, 10, QChar('0'));
|
||||
}
|
||||
|
||||
double getPowerW(const QString &batPath) {
|
||||
double current_uA = readDouble(batPath + "/current_now");
|
||||
double voltage_uV = readDouble(batPath + "/voltage_now");
|
||||
|
||||
if (current_uA > 0 && voltage_uV > 0) {
|
||||
return (current_uA * voltage_uV) / 1e12;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QIcon batteryIcon(int capacity, bool charging) {
|
||||
int step = toStep(capacity);
|
||||
QString num = pad3(step);
|
||||
|
||||
QString base = "/run/current-system/sw/share/icons/Papirus/24x24/panel/";
|
||||
|
||||
QString file;
|
||||
if (charging) {
|
||||
file = base + "battery-" + num + "-charging.svg";
|
||||
} else {
|
||||
file = base + "battery-" + num + ".svg";
|
||||
}
|
||||
|
||||
QIcon icon(file);
|
||||
|
||||
if (icon.isNull()) {
|
||||
qDebug() << "FAILED icon:" << file;
|
||||
icon = QIcon(base + "battery-100.svg");
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
void sendNotification(const QString &title, const QString &body) {
|
||||
QString cmd = "notify-send \"" + title + "\" \"" + body + "\"";
|
||||
system(cmd.toUtf8().constData());
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QString batPath = "/sys/class/power_supply/BAT1";
|
||||
QSystemTrayIcon tray;
|
||||
|
||||
auto updateIcon = [&]() {
|
||||
int capacity = readInt(batPath + "/capacity");
|
||||
QString status = readFile(batPath + "/status");
|
||||
bool charging = status.contains("Charging");
|
||||
|
||||
tray.setIcon(batteryIcon(capacity, charging));
|
||||
};
|
||||
|
||||
auto notify = [&]() {
|
||||
int capacity = readInt(batPath + "/capacity");
|
||||
QString status = readFile(batPath + "/status");
|
||||
|
||||
bool charging = status.contains("Charging");
|
||||
bool discharging = status.contains("Discharging");
|
||||
|
||||
double power = getPowerW(batPath);
|
||||
|
||||
QString state;
|
||||
if (charging) state = "Charging";
|
||||
else if (discharging) state = "Discharging";
|
||||
else state = status;
|
||||
|
||||
QString powerStr = (power > 0.01)
|
||||
? QString::number(power, 'f', 2) + " W"
|
||||
: "N/A";
|
||||
|
||||
QString msg =
|
||||
QString("%1\n%2%\n%3")
|
||||
.arg(state)
|
||||
.arg(capacity)
|
||||
.arg(powerStr);
|
||||
|
||||
sendNotification("Battery status", msg);
|
||||
};
|
||||
|
||||
QObject::connect(&tray, &QSystemTrayIcon::activated,
|
||||
[&](QSystemTrayIcon::ActivationReason r) {
|
||||
if (r == QSystemTrayIcon::Trigger) {
|
||||
notify();
|
||||
}
|
||||
});
|
||||
|
||||
updateIcon();
|
||||
tray.setVisible(true);
|
||||
|
||||
QTimer timer;
|
||||
QObject::connect(&timer, &QTimer::timeout, updateIcon);
|
||||
timer.start(5000);
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
'';
|
||||
};
|
||||
|
||||
nativeBuildInputs = [
|
||||
pkg-config
|
||||
qt6.wrapQtAppsHook
|
||||
];
|
||||
|
||||
buildInputs = [
|
||||
qt6.qtbase
|
||||
qt6.qtsvg
|
||||
pkgs.papirus-icon-theme
|
||||
];
|
||||
|
||||
dontUnpack = true;
|
||||
|
||||
buildPhase = ''
|
||||
g++ $src -o qtbatticon \
|
||||
$(pkg-config --cflags --libs Qt6Widgets)
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
install -m755 qtbatticon $out/bin/
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
# taken and adapted https://github.com/miniMinn24/Battery_Notify with MIT License
|
||||
{
|
||||
writeShellApplication,
|
||||
mplayer,
|
||||
brightnessctl,
|
||||
...
|
||||
}: let
|
||||
NotifySound = ../../../assets/battery_notify.mp3;
|
||||
in
|
||||
writeShellApplication {
|
||||
name = "custom-batterynotify";
|
||||
runtimeInputs = [
|
||||
mplayer
|
||||
brightnessctl
|
||||
];
|
||||
text = ''
|
||||
|
||||
# Delay for startup
|
||||
sleep 5
|
||||
|
||||
# Change your sound path here
|
||||
|
||||
playsound() {
|
||||
mplayer ${NotifySound}
|
||||
}
|
||||
|
||||
adjustBrightness() {
|
||||
local adjustment="$1"
|
||||
brightnessctl set "$adjustment"
|
||||
}
|
||||
|
||||
sendNotification() {
|
||||
local title="$1"
|
||||
local message="$2"
|
||||
notify-send -h string:x-canonical-private-synchronous:sys-notify -e -u low "$title" "$message"
|
||||
}
|
||||
|
||||
# Runs full time in background
|
||||
x=0
|
||||
while true; do
|
||||
Battery_Status="$(\cat /sys/class/power_supply/BAT*/status)"
|
||||
Battery_Capacity=$(\cat /sys/class/power_supply/BAT*/capacity)
|
||||
|
||||
case "$Battery_Status" in
|
||||
"Discharging")
|
||||
if [ $x -eq 1 ]; then
|
||||
sendNotification "Discharging" "Remains: <b>''${Battery_Capacity}%</b>"
|
||||
adjustBrightness "15%-"
|
||||
playsound
|
||||
x=0
|
||||
fi
|
||||
;;
|
||||
"Charging")
|
||||
if [ $x -eq 0 ]; then
|
||||
sendNotification "Charging" "Current: <b>''${Battery_Capacity}%</b>"
|
||||
adjustBrightness "15%+"
|
||||
playsound
|
||||
x=1
|
||||
fi
|
||||
;;
|
||||
"Full")
|
||||
if [ $x -eq 0 ]; then
|
||||
sendNotification "Fully Charged" "Noice: <b>''${Battery_Capacity}%</b>"
|
||||
playsound
|
||||
x=1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
sleep 1
|
||||
done
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
# taken and adapted https://github.com/miniMinn24/Battery_Notify with MIT License
|
||||
{
|
||||
writeShellApplication,
|
||||
mplayer,
|
||||
brightnessctl,
|
||||
...
|
||||
}: let
|
||||
NotifySound = ../../../assets/battery_notify.mp3;
|
||||
in
|
||||
writeShellApplication {
|
||||
name = "custom-batterywarning";
|
||||
runtimeInputs = [
|
||||
mplayer
|
||||
brightnessctl
|
||||
];
|
||||
text = ''
|
||||
|
||||
# Delay for startup
|
||||
sleep 5
|
||||
|
||||
|
||||
playsound() {
|
||||
mplayer ${NotifySound}
|
||||
}
|
||||
|
||||
|
||||
|
||||
while true; do
|
||||
Battery_Status="$(cat /sys/class/power_supply/BAT*/status)"
|
||||
Battery_Capacity="$(cat /sys/class/power_supply/BAT*/capacity)"
|
||||
|
||||
if [ "$Battery_Status" == "Discharging" ]; then
|
||||
if [ "$Battery_Capacity" -le 20 ] && [ "$Battery_Capacity" -ge 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "Power running out: <b>''${Battery_Capacity}%</b>"
|
||||
playsound
|
||||
sleep 180
|
||||
|
||||
elif [ "$Battery_Capacity" -lt 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "<b>Save your works</b> before immediate shutdown."
|
||||
playsound
|
||||
sleep 180
|
||||
fi
|
||||
else
|
||||
sleep 120
|
||||
fi
|
||||
done
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user