nix: modernize

This commit is contained in:
raf 2025-01-08 11:08:07 +03:00
parent 514eb6617e
commit 0e6979f684
No known key found for this signature in database
GPG key ID: EED98D11B85A2819
5 changed files with 48 additions and 39 deletions

View file

@ -2,15 +2,16 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1705003311, "lastModified": 1736012469,
"narHash": "sha256-7JtTAgeUHGKA9phwPveHtwvaQCDrta1yR5t7MYSMcCU=", "narHash": "sha256-/qlNWm/IEVVH7GfgAIyP6EsVZI6zjAx1cV5zNyrs+rI=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "3680fef792863c8b7bf52f0d474a16319995eabd", "rev": "8f3e1f807051e32d8c95cd12b9b421623850a34d",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View file

@ -1,6 +1,6 @@
{ {
description = "Batmon - battery monitor"; description = "Batmon - battery monitor";
inputs.nixpkgs.url = "github:NixOS/nixpkgs"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { outputs = {
self, self,
@ -11,7 +11,8 @@
pkgsForEach = nixpkgs.legacyPackages; pkgsForEach = nixpkgs.legacyPackages;
in { in {
nixosModules = { nixosModules = {
batmon = ./nix/module.nix {inherit self;}; batmon = import ./nix/module.nix self;
default = self.nixosModules.batmon;
}; };
packages = forEachSystem (system: { packages = forEachSystem (system: {

View file

@ -1,13 +1,14 @@
{ self: {
self,
config, config,
pkgs, pkgs,
lib, lib,
... ...
}: let }: let
inherit (lib) mkIf mkForce types; inherit (lib.modules) mkIf mkForce;
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
inherit (lib) types;
format = pkgs.formats.json {};
cfg = config.services.batmon; cfg = config.services.batmon;
in { in {
options.services.batmon = { options.services.batmon = {
@ -20,25 +21,26 @@ in {
}; };
settings = mkOption { settings = mkOption {
description = "Settings for Batmon"; type = format.type;
type = types.attrs;
default = {}; default = {};
example = lib.literalExpression '' example = {
{
batPaths = [ batPaths = [
{ {
path = "/sys/class/power_supply/BAT0"; path = "/sys/class/power_supply/BAT0";
extraCommand = "notify-send 'State changed!'"; extraCommand = "notify-send 'State changed!'";
} }
]; ];
} };
'';
description = "Settings for BatmoSettings for Batmonn";
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
systemd.user.services."batmon" = { systemd.user.services."batmon" = {
description = "Power Monitoring Service"; description = "Power Monitoring Service";
wants = ["power-profiles-daemon.service"];
wantedBy = ["default.target"];
environment.PATH = mkForce "/run/wrappers/bin:${lib.makeBinPath cfg.package}"; environment.PATH = mkForce "/run/wrappers/bin:${lib.makeBinPath cfg.package}";
script = '' script = ''
${lib.getExe cfg.package} --config ${builtins.toJSON cfg.settings} ${lib.getExe cfg.package} --config ${builtins.toJSON cfg.settings}
@ -47,10 +49,8 @@ in {
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
Restart = "on-failure"; Restart = "on-failure";
DynamicUser = true;
}; };
wants = ["power-profiles-daemon.service"];
wantedBy = ["default.target"];
}; };
}; };
} }

View file

@ -1,11 +1,22 @@
{buildGoModule}: {
lib,
buildGoModule,
}:
buildGoModule { buildGoModule {
pname = "Batmon"; pname = "Batmon";
version = "0.1.0"; version = "0.1.0";
src = ../.; src = builtins.path {
path = ../.;
name = "batmon-src";
};
vendorHash = null; vendorHash = null;
ldflags = ["-s" "-w"]; ldflags = ["-s" "-w"];
meta = {
license = lib.licenses.gpl3Only;
maintainers = [lib.maintainers.NotAShelf];
mainProgram = "batmon";
};
} }

View file

@ -1,16 +1,12 @@
{ {
packagePath, mkShellNoCC,
callPackage,
gopls, gopls,
go, go,
}: let }:
mainPkg = callPackage packagePath {}; mkShellNoCC {
in name = "batmon";
mainPkg.overrideAttrs (oa: { packages = [
nativeBuildInputs =
(oa.nativeBuildInputs or [])
++ [
gopls gopls
go go
]; ];
}) }