Skip to content

Commit

Permalink
feat(nix): flake + home manager module
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Jan 27, 2024
1 parent 173cd41 commit 9164315
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
42 changes: 42 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default-linux";
};

outputs = inputs @ {
self,
nixpkgs,
systems,
...
}: let
inherit (nixpkgs) lib;
eachSystem = lib.genAttrs (import systems);
pkgsFor = eachSystem (system:
import nixpkgs {
localSystem = system;
});
in {
packages = eachSystem (system: let
pkgs = pkgsFor.${system};
in {
default = self.packages.${system}.battery-notifier;
battery-notifier = pkgs.callPackage ./default.nix {};
});

homeManagerModule.default = import ./nix/hm-module.nix self;
};
}
90 changes: 90 additions & 0 deletions nix/hm-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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
settingsModule = types.submodule {
options = {
interval_ms = mkOption {
type = types.int;
default = 700;
};

reminder_threshold = mkOption {
type = types.int;
default = 30;
};

warn_threshold = mkOption {
type = types.int;
default = 15;
};

threat_threshold = mkOption {
type = types.int;
default = 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 = builtins.length (lib.attrsets.attrValues (lib.attrsets.filterAttrs (k: v: lib.strings.hasSuffix k "threshold" && v >= 0 && v <= 100) cfg.settings)) == 0;
message = "threshold values must be greater equal than 0 and less equal than 100";
}
{
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";
}
{
assertion = cfg.settings.sleep_ms > 0;
message = "sleep time must be greater than zero";
}
];

xdg.configFile = mkIf (cfg.settings != null) {
"battery-notifier/config.toml".source = tomlFormat.generate "battery-notifier-config" cfg.settings;
};

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

Service = {
Type = "simple";
ExecStart = "${flake-pkgs.battery-notifier}/bin/battery-notifier";
Restart = "on-failure";
};

Install = {
WantedBy = ["default.target"];
};
};
};
};
}

0 comments on commit 9164315

Please sign in to comment.