|
| 1 | +self: { |
| 2 | + config, |
| 3 | + pkgs, |
| 4 | + lib, |
| 5 | + ... |
| 6 | +}: |
| 7 | +with lib; let |
| 8 | + inherit (pkgs.stdenv.hostPlatform) system; |
| 9 | + tomlFormat = pkgs.formats.toml {}; |
| 10 | + flake-pkgs = self.packages.${system}; |
| 11 | +in { |
| 12 | + options.programs.battery-notifier = let |
| 13 | + boundModule = types.submodule { |
| 14 | + options = { |
| 15 | + threshold = mkOption { |
| 16 | + type = types.int; |
| 17 | + }; |
| 18 | + |
| 19 | + title = mkOption { |
| 20 | + type = types.str; |
| 21 | + default = ""; |
| 22 | + }; |
| 23 | + |
| 24 | + content = mkOption { |
| 25 | + type = types.str; |
| 26 | + default = ""; |
| 27 | + }; |
| 28 | + }; |
| 29 | + }; |
| 30 | + |
| 31 | + settingsModule = types.submodule { |
| 32 | + options = { |
| 33 | + interval_ms = mkOption { |
| 34 | + type = types.int; |
| 35 | + default = 700; |
| 36 | + }; |
| 37 | + |
| 38 | + icon_path = mkOption { |
| 39 | + type = types.str; |
| 40 | + default = ""; |
| 41 | + }; |
| 42 | + |
| 43 | + reminder = mkOption { |
| 44 | + type = boundModule; |
| 45 | + default = { |
| 46 | + threshold = 30; |
| 47 | + }; |
| 48 | + }; |
| 49 | + |
| 50 | + warn = mkOption { |
| 51 | + type = boundModule; |
| 52 | + default = { |
| 53 | + threshold = 15; |
| 54 | + }; |
| 55 | + }; |
| 56 | + |
| 57 | + threat = mkOption { |
| 58 | + type = boundModule; |
| 59 | + default = { |
| 60 | + threshold = 5; |
| 61 | + }; |
| 62 | + }; |
| 63 | + }; |
| 64 | + }; |
| 65 | + in { |
| 66 | + enable = mkEnableOption "battery-notifier"; |
| 67 | + |
| 68 | + settings = mkOption { |
| 69 | + default = null; |
| 70 | + type = types.nullOr settingsModule; |
| 71 | + }; |
| 72 | + }; |
| 73 | + |
| 74 | + config = let |
| 75 | + cfg = config.programs.battery-notifier; |
| 76 | + in |
| 77 | + mkIf cfg.enable { |
| 78 | + assertions = mkIf (cfg.settings != null) [ |
| 79 | + { |
| 80 | + assertion = let |
| 81 | + greatEq0LowEq100 = v: v >= 0 && v <= 100; |
| 82 | + inherit (cfg.settings) reminder warn threat; |
| 83 | + in |
| 84 | + greatEq0LowEq100 reminder.threshold && greatEq0LowEq100 warn.threshold && greatEq0LowEq100 threat.threshold; |
| 85 | + message = "threshold values must be greater equal than 0 and less equal than 100"; |
| 86 | + } |
| 87 | + { |
| 88 | + assertion = cfg.settings.interval_ms > 0; |
| 89 | + message = "'interval_ms' must be greater than zero"; |
| 90 | + } |
| 91 | + { |
| 92 | + assertion = cfg.settings.reminder.threshold > cfg.settings.warn.threshold; |
| 93 | + message = "'reminder' threshold must be greater than 'warn' threshold"; |
| 94 | + } |
| 95 | + { |
| 96 | + assertion = cfg.settings.warn.threshold > cfg.settings.threat.threshold; |
| 97 | + message = "'warn' threshold must be greater than 'threat' threshold"; |
| 98 | + } |
| 99 | + ]; |
| 100 | + |
| 101 | + systemd.user.services = { |
| 102 | + battery-notifier = { |
| 103 | + description = "A very useful battery notifier for window managers"; |
| 104 | + |
| 105 | + serviceConfig = { |
| 106 | + Type = "simple"; |
| 107 | + ExecStart = let |
| 108 | + pname = "battery-notifier"; |
| 109 | + configFile = tomlFormat.generate "${pname}-user-config" cfg.settings; |
| 110 | + in "${flake-pkgs.battery-notifier}/bin/${pname} --config-file=${configFile}"; |
| 111 | + Restart = "on-failure"; |
| 112 | + }; |
| 113 | + |
| 114 | + wantedBy = ["default.target"]; |
| 115 | + }; |
| 116 | + }; |
| 117 | + }; |
| 118 | +} |
0 commit comments