Skip to content

Commit b57bf0e

Browse files
committed
nix(flake): nixos options module
1 parent 6b50dab commit b57bf0e

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
systems.url = "github:nix-systems/default-linux";
55
};
66

7-
outputs = inputs @ {
7+
outputs = {
88
self,
99
nixpkgs,
1010
systems,
@@ -24,6 +24,7 @@
2424
battery-notifier = pkgs.callPackage ./default.nix {};
2525
});
2626

27+
nixosModule.default = import ./nix/nixos-module.nix self;
2728
homeManagerModule.default = import ./nix/hm-module.nix self;
2829
};
2930
}

nix/nixos-module.nix

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)