Power-targets.nix

Hi all,
I made a very simple module that creates systemd targets for AC and battery power states, allowing you to bind services and sockets to run only when plugged in.

Here is an example that will turn off borg, syncthing and ipfs when your laptop is unplugged.


{
  inputs.power-targets = {
    url = "git+https://seed.radicle.garden/z3nj6UEy3kcz2dVNNSe1FH6Je9PKH.git";
  };

  outputs = { inputs, ... }: {
    nixosConfigurations.your-machine = nixpkgs.lib.nixosSystem {
      modules = [
        inputs.power-targets
        {
          powerTargets = {
            acName = "AC";  # name in /sys/class/power_supply/
            services = {
              syncthing.enable = true;
              borgbackup-job-borgbase.enable = true;
            };
            sockets = {
              ipfs-api.enable = true;
            };
          };
        }
      ];
    };
  };
}
5 Likes

That’s neat. Do you think it should be upstreamed?

I don’t know, I guess I could open a PR. If anyone thinks it should let me know.

I think the threshold for entry is reduced for modular services. Perhaps this could be one. It’s a more recent module system pattern.