Recreating MacBook Charge Control on NixOS


After replacing the battery in my 2015 MacBook Pro, I wanted NixOS to stop holding it at 100 percent charge all day.

macOS has proprietary battery-health and optimized-charging behavior. I did not try to reproduce Apple’s prediction model.

I built a deterministic alternative: 80 percent normally, 100 percent after unplugging for travel, then back to 80.

The behavior missing from Linux

Apple’s battery management learns the daily charging routine and may delay charging above 80 percent until it predicts the full capacity will be needed. It also offers “Charge to Full Now”, and it occasionally charges to 100 percent anyway to keep its estimates accurate.

NixOS on this Intel MacBook did not have that integration.

The machine would remain connected to mains power at full charge unless I managed it manually. In March 2026 I installed a replacement battery and reset the SMC and NVRAM, and I wanted a policy I could understand and reproduce.

Exposing the SMC threshold

The applesmc-next project patches Linux’s support for the Apple SMC on Intel Macs, so that battery charge thresholds can be set. It exposes the charge-stop limit through the standard power-supply sysfs interface.

Writing 80 to charge_control_end_threshold stops charging at around 80 percent. The value persists in the SMC across power cycles and operating systems until it is changed or the SMC is reset.

I packaged a pinned applesmc-next revision as a Nix kernel-module package. It builds against whichever kernel the NixOS configuration selects and installs the patched applesmc and sbs modules. The NixOS module makes sure sbs is loaded first. It also installs the project’s TLP integration and writes the normal end threshold at boot.

On the laptop’s side the whole thing is one option:

hardware.apple.battery = {
  enable = true;
  endThreshold = 80;
  enableSmartTravelMode = true;
};

Behind it are the source revision and hash, the kernel-specific build, the module ordering, the boot threshold, a service, a timer and a command-line tool.

Travel mode

A permanent 80 percent cap is inconvenient immediately before leaving home.

The module generates a battery-threshold command with a normal and a travel mode. Run without arguments, it applies a simple state machine:

unplugged                  -> threshold 100, mode travel
reconnected below 95%     -> keep threshold 100
reconnected at 95% or more -> restore threshold 80

An unplug event is treated as intent to travel. The laptop may then charge nearly full when power returns. Once it reaches 95 percent, the normal cap returns.

A udev rule reacts to changes of the power adapter and starts a systemd oneshot service which runs the command. A timer starts the same service every four minutes, which is how the system notices that travel charging has reached the point where it should revert.

The mode is a one-word file under /var/lib. The controller and the policy are declarative.

What it does not reproduce

This is not Apple’s Optimized Battery Charging.

It does not learn my schedule. It does not predict departure time. The driver does not implement a start threshold, and nothing actively discharges the battery. Returning the limit to 80 at 95 percent stops further charging; it does not pull the battery back down.

Any unplug event selects travel mode, including a brief accidental disconnect. The helper also tolerates sysfs write errors too quietly, and the udev rule matches the adapter name this MacBook exposes.

applesmc-next remains an out-of-tree kernel module with the maintenance risks that implies.

Those limitations are acceptable for my current use.

Apple now has the simple version too. Since macOS 26.4 there is a Charge Limit setting between 80 and 100 percent, for Macs with Apple silicon. Mine is from 2015.

Declarative policy over mutable hardware

The battery percentage is physical state. The SMC threshold is persistent mutable firmware state. The travel marker is mutable filesystem state.

NixOS does not turn any of those into immutable derivations. It defines how the machine should manage them.

It is still running on the MacBook. Most days it caps charge at 80 percent. When I unplug, it prepares for travel without requiring me to remember a command in advance.

This is less clever than Apple’s model and much easier to explain.