From 3a3657b107bc1aa32619c41d00085bfb3f36d7c5 Mon Sep 17 00:00:00 2001
From: Philipp Middendorf <middendorf@plapadoo.de>
Date: Sun, 29 Dec 2019 11:26:24 +0100
Subject: [PATCH] cbatticon: add module (#963)

---
 modules/misc/news.nix          |   8 ++
 modules/modules.nix            |   1 +
 modules/services/cbatticon.nix | 132 +++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
 create mode 100644 modules/services/cbatticon.nix

diff --git a/modules/misc/news.nix b/modules/misc/news.nix
index 0a3cac77a..3f56cb022 100644
--- a/modules/misc/news.nix
+++ b/modules/misc/news.nix
@@ -1287,6 +1287,14 @@ in
           A new module is available: 'programs.readline'.
         '';
       }
+
+      {
+        time = "2020-01-11T11:49:51+00:00";
+        condition = hostPlatform.isLinux;
+        message = ''
+          A new module is available: 'services.cbatticon'.
+        '';
+      }
     ];
   };
 }
diff --git a/modules/modules.nix b/modules/modules.nix
index ded3587eb..dbd1b7772 100644
--- a/modules/modules.nix
+++ b/modules/modules.nix
@@ -104,6 +104,7 @@ let
     (loadModule ./programs/zathura.nix { })
     (loadModule ./programs/zsh.nix { })
     (loadModule ./services/blueman-applet.nix { })
+    (loadModule ./services/cbatticon.nix { condition = hostPlatform.isLinux; })
     (loadModule ./services/compton.nix { })
     (loadModule ./services/dunst.nix { })
     (loadModule ./services/dwm-status.nix { condition = hostPlatform.isLinux; })
diff --git a/modules/services/cbatticon.nix b/modules/services/cbatticon.nix
new file mode 100644
index 000000000..7bf3b7055
--- /dev/null
+++ b/modules/services/cbatticon.nix
@@ -0,0 +1,132 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+
+  cfg = config.services.cbatticon;
+
+  package = pkgs.cbatticon;
+
+  makeCommand = commandName: commandArg:
+    optional (commandArg != null) (
+      let
+        cmd = pkgs.writeShellScript commandName commandArg;
+      in
+        "--${commandName} ${cmd}"
+    );
+
+  commandLine = concatStringsSep " " (
+    [ "${package}/bin/cbatticon" ]
+    ++ makeCommand "command-critical-level" cfg.commandCriticalLevel
+    ++ makeCommand "command-left-click" cfg.commandLeftClick
+    ++ optional
+      (cfg.iconType != null)
+      "--icon-type ${cfg.iconType}"
+    ++ optional
+      (cfg.lowLevelPercent != null)
+      "--low-level ${toString cfg.lowLevelPercent}"
+    ++ optional
+      (cfg.criticalLevelPercent != null)
+      "--critical-level ${toString cfg.criticalLevelPercent}"
+    ++ optional
+      (cfg.updateIntervalSeconds != null)
+      "--update-interval ${toString cfg.updateIntervalSeconds}"
+    ++ optional
+      (cfg.hideNotification != null && cfg.hideNotification)
+      "--hide-notification"
+  );
+
+in
+
+{
+  meta.maintainers = [ maintainers.pmiddend ];
+
+  options = {
+    services.cbatticon = {
+      enable = mkEnableOption "cbatticon";
+
+      commandCriticalLevel = mkOption {
+        type = types.nullOr types.lines;
+        default = null;
+        example = ''
+          notify-send "battery critical!"
+        '';
+        description = ''
+          Command to execute when the critical battery level is reached.
+        '';
+      };
+
+      commandLeftClick = mkOption {
+        type = types.nullOr types.lines;
+        default = null;
+        description = ''
+          Command to execute when left clicking on the tray icon.
+        '';
+      };
+
+      iconType = mkOption {
+        type = types.nullOr (types.enum [ "standard" "notification" "symbolic" ]);
+        default = null;
+        example = "symbolic";
+        description = "Icon type to display in the system tray.";
+      };
+
+      lowLevelPercent = mkOption {
+        type = types.nullOr (types.ints.between 0 100);
+        default = null;
+        example = 20;
+        description = ''
+          Low level percentage of the battery in percent (without the
+          percent symbol).
+        '';
+      };
+
+      criticalLevelPercent = mkOption {
+        type = types.nullOr (types.ints.between 0 100);
+        default = null;
+        example = 5;
+        description = ''
+          Critical level percentage of the battery in percent (without
+          the percent symbol).
+        '';
+      };
+
+      updateIntervalSeconds = mkOption {
+        type = types.nullOr types.ints.positive;
+        default = null;
+        example = 5;
+        description = ''
+          Number of seconds between updates of the battery information.
+        '';
+      };
+
+      hideNotification = mkOption {
+        type = types.nullOr types.bool;
+        default = null;
+        description = "Hide the notification popups.";
+      };
+    };
+  };
+
+  config = mkIf cfg.enable {
+    home.packages = [ package ];
+
+    systemd.user.services.cbatticon = {
+      Unit = {
+        Description = "cbatticon system tray battery icon";
+        After = [ "graphical-session-pre.target" ];
+        PartOf = [ "graphical-session.target" ];
+      };
+
+      Install = {
+        WantedBy = [ "graphical-session.target" ];
+      };
+
+      Service = {
+        ExecStart = commandLine;
+        Restart = "on-abort";
+      };
+    };
+  };
+}