about summary refs log tree commit diff
path: root/modules/services/upower-minimal.nix
blob: c1067467ed317603b66cb04a74416f46ea6f0b7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# this is the
# Upower daemon.
# module from nixpkgs, but with the option to suspend and less configuration

{ config, lib, pkgs, ... }:

let

  cfg = config.vuizvui.services.upower;

  ini = pkgs.formats.ini {};

  pkg = pkgs.upower.overrideAttrs (old: {
    patches = [
      # Adds a "Suspend" action to what to do when the battery is critical
      (pkgs.fetchpatch {
        url = "https://gitlab.freedesktop.org/upower/upower/-/merge_requests/11.patch";
        sha256 = "sha256-y8ysD+fJIi5SZkWp2n061VBA5cs1EMftOof/h2tvDGo=";
      })
    ];
  });

  configFile = ini.generate "UPower.conf" cfg.settings;

in

{

  ###### interface

  options = {

    vuizvui.services.upower = {

      enable = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Whether to enable Upower, a DBus service that provides power
          management support to applications.
        '';
      };

      settings = lib.mkOption {
        type = lib.types.nullOr ini.type;
        default = {};
        description = ''
          The upower configuration.

          So far it looks like there is always only one Section called <literal>UPower</literal>
        '';
        example = {
          UPower = {
            TimeCritical = 300;
            CriticalPowerAction = "Hibernate";
          };
        };
      };

    };

  };


  ###### implementation

  config = lib.mkIf cfg.enable {

    # this is … questionable … l o w  e f f o r t

    environment.systemPackages = [ pkg ];

    services.dbus.packages = [ pkg ];

    services.udev.packages = [ pkg ];

    systemd.packages = [ pkg ];

    # this implicitly assumes the package has a `upower.service` unit file
    systemd.services."upower".restartTriggers = [ configFile ];

    environment.etc."UPower/UPower.conf".source = configFile ;
  };

}