How to install auto-cpufreq in NixOS?

NixOS

How to install auto-cpufreq in NixOS?

Install auto-cpufreq in NixOS with easy steps.

Published in
4 min readMar 19, 2024

--

The biggest problems in working with Linux distro are CPU and power consumption.

Opening multiple tabs in the browser and watching one YouTube video is enough for the Linus distro to increase the temperature and CPU, increasing power consumption.

After a Full battery recharge, my laptop works for 3 to 4 hours.

I installed the NixOS distro with GNOME, and Now NixOS has been my primary laptop for the last one to two years.

You can use Auto cpufreq to improve battery and CPU performance. Other alternatives may be available, but I do not know.

Installing Auto cpufreq in NixOS is very easy. You need to copy and paste the code and rebuild your NixOS.

There are two ways to install Auto cpufreq in your NixOS.

  1. Flakes
  2. Nixpkgs

Flakes

If you use flakes in your NixOS to manage Nix packages, you need to follow the two steps I mentioned here.

  1. Step 1
  2. Step 2

Step 1

First, copy the following code and paste it into a flake.nix file. First, add the flake URL in the inputs section.

# flake.nix

# ---Auto CPU---
auto-cpufreq = {
url = "github:AdnanHodzic/auto-cpufreq";
inputs.nixpkgs.follows = "nixpkgs";
};
# ---Auto CPU---

The next step in the outputs section is to add auto-cpufreq in the function as an argument and then use it in modules.

# flake.nix
# --- add auto-cpufreq as argument ---
outputs = { self, nixpkgs, auto-cpufreq, ... }@inputs:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{

nixosConfigurations.default = nixpkgs.lib.nixosSystem {
specialArgs = {inherit inputs;};
modules = [

./configuration.nix
inputs.home-manager.nixosModules.default

auto-cpufreq.nixosModules.default # --- use it here ---

];
};

};
}

The final result looks like this.

# flake.nix

{
description = "Nixos config flake";

inputs = {

# --- Nixpkgs URL ---
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

# ---Auto CPU---
auto-cpufreq = {
url = "github:AdnanHodzic/auto-cpufreq";
inputs.nixpkgs.follows = "nixpkgs";
};
# ---Auto CPU---

# ---Home Manage ---
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
# ---Home Manage ---

};

outputs = { self, nixpkgs, auto-cpufreq , ... }@inputs: {

nixosConfigurations.default = nixpkgs.lib.nixosSystem {
specialArgs = {inherit inputs;};
modules = [
./configuration.nix
inputs.home-manager.nixosModules.default
auto-cpufreq.nixosModules.default # --- use it here ---
];
};

};
}

Step 2

The next and last step is to enable the Auto cpufreq program in your configuration.nix file:

# configuration.nix

programs.auto-cpufreq.enable = true;
# optionally, you can configure your auto-cpufreq settings, if you have any
programs.auto-cpufreq.settings = {
charger = {
governor = "performance";
turbo = "auto";
};

battery = {
governor = "powersave";
turbo = "auto";
};
};

Finally, rebuild your NixOS using the nixos-rebuild command.

During Auto cpufreq installation with Flake, you may encounter an error. The error looks like this.

➜  nixos git:(main) ✗ sudo systemctl status auto-cpufreq.service          
[sudo] password for officialrajdeepsingh:
Sorry, try again.
[sudo] password for officialrajdeepsingh:
× auto-cpufreq.service - auto-cpufreq - Automatic CPU speed & power optimizer for Linux
Loaded: loaded (/etc/systemd/system/auto-cpufreq.service; enabled; preset: enabled)
Drop-In: /nix/store/3zs7gj59yv15af2g0h1d3mijnh7yv2pp-system-units/auto-cpufreq.service.d
└─overrides.conf
Active: failed (Result: exit-code) since Sun 2024-03-17 14:21:46 IST; 11min ago
Duration: 118ms
Process: 26801 ExecStart=/nix/store/dlrwsyzm0hv07dn8k30mgxy3dpa7a7zf-python3.10-auto-cpufreq-2.2.0/bin/auto-cpufreq --daemon --config /nix/store/qxipyyb8blbxfb925sr96593as582wjs-auto-cp>
Main PID: 26801 (code=exited, status=1/FAILURE)
IP: 0B in, 0B out
CPU: 116ms

Mar 17 14:21:46 nixos systemd[1]: auto-cpufreq.service: Scheduled restart job, restart counter is at 5.
Mar 17 14:21:46 nixos systemd[1]: auto-cpufreq.service: Start request repeated too quickly.
Mar 17 14:21:46 nixos systemd[1]: auto-cpufreq.service: Failed with result 'exit-code'.

There are two solutions: the first is to use the nixpkgs method. The second is to stop and restart your auto-cpufreq service.

# First stop the auto-cpufreq service
sudo systemctl stop auto-cpufreq.service # if possible work

# Start the auto-cpufreq service
sudo systemctl start auto-cpufreq.service

# or use restart auto-cpufreq.service
sudo systemctl restart auto-cpufreq.service

# Check your auto-cpufreq.service status
sudo systemctl status auto-cpufreq.service

I hope this solution works for you, and it works for me.

Nixpkgs

If you choose Nixpkgs to install Auto cpufreq, paste the following code.

# configuration.nix

# ---Snip---
environment.systemPackages = with pkgs; [
auto-cpufreq
];

services.auto-cpufreq.enable = true;
# ---Snip---

# Add custom settings
services.auto-cpufreq.settings= {}

Finally, rebuild your NixOS using the nixos-rebuild command.

To verify whether your Auto cpufreq is working or not in your nixos, run the following command.

sudo systemctl status auto-cpufreq.service

I hope auto-cpufreq is successfully installed in your nixos. If there is any problem, ask me in the comment section.

To learn more about NixOS and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.

--

--

JavaScript | TypeScript | Reactjs | Nextjs | Rust | Biotechnology | Bioinformatic | Frontend Developer | Author | https://linktr.ee/officialrajdeepsingh