Skip to content

Commit

Permalink
nix(flake): nixos options module
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Apr 4, 2024
1 parent 6b50dab commit ade73e2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
systems.url = "github:nix-systems/default-linux";
};

outputs = inputs @ {
outputs = {
self,
nixpkgs,
systems,
Expand All @@ -24,6 +24,7 @@
battery-notifier = pkgs.callPackage ./default.nix {};
});

nixosModules.default = import ./nix/nixos-module.nix self;
homeManagerModule.default = import ./nix/hm-module.nix self;
};
}
118 changes: 118 additions & 0 deletions nix/nixos-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
self: {
config,
pkgs,
lib,
...
}:
with lib; let
inherit (pkgs.stdenv.hostPlatform) system;
tomlFormat = pkgs.formats.toml {};
flake-pkgs = self.packages.${system};
in {
options.programs.battery-notifier = let
boundModule = types.submodule {
options = {
threshold = mkOption {
type = types.int;
};

title = mkOption {
type = types.str;
default = "";
};

content = mkOption {
type = types.str;
default = "";
};
};
};

settingsModule = types.submodule {
options = {
interval_ms = mkOption {
type = types.int;
default = 700;
};

icon_path = mkOption {
type = types.str;
default = "";
};

reminder = mkOption {
type = boundModule;
default = {
threshold = 30;
};
};

warn = mkOption {
type = boundModule;
default = {
threshold = 15;
};
};

threat = mkOption {
type = boundModule;
default = {
threshold = 5;
};
};
};
};
in {
enable = mkEnableOption "battery-notifier";

settings = mkOption {
default = null;
type = types.nullOr settingsModule;
};
};

config = let
cfg = config.programs.battery-notifier;
in
mkIf cfg.enable {
assertions = mkIf (cfg.settings != null) [
{
assertion = let
greatEq0LowEq100 = v: v >= 0 && v <= 100;
inherit (cfg.settings) reminder warn threat;
in
greatEq0LowEq100 reminder.threshold && greatEq0LowEq100 warn.threshold && greatEq0LowEq100 threat.threshold;
message = "threshold values must be greater equal than 0 and less equal than 100";
}
{
assertion = cfg.settings.interval_ms > 0;
message = "'interval_ms' must be greater than zero";
}
{
assertion = cfg.settings.reminder.threshold > cfg.settings.warn.threshold;
message = "'reminder' threshold must be greater than 'warn' threshold";
}
{
assertion = cfg.settings.warn.threshold > cfg.settings.threat.threshold;
message = "'warn' threshold must be greater than 'threat' threshold";
}
];

systemd.user.services = {
battery-notifier = {
description = "A very useful battery notifier for window managers";

serviceConfig = {
Type = "simple";
ExecStart = let
pname = "battery-notifier";
configFile = tomlFormat.generate "${pname}-user-config" cfg.settings;
in "${flake-pkgs.battery-notifier}/bin/${pname} --config-file=${configFile}";
Restart = "on-failure";
};

wantedBy = ["default.target"];
};
};
};
}

0 comments on commit ade73e2

Please sign in to comment.