From 0e6979f684c6585f4aba0bbb0f000dd8025aa943 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 8 Jan 2025 11:08:07 +0300 Subject: [PATCH] nix: modernize --- flake.lock | 7 ++++--- flake.nix | 5 +++-- nix/module.nix | 36 ++++++++++++++++++------------------ nix/package.nix | 17 ++++++++++++++--- nix/shell.nix | 22 +++++++++------------- 5 files changed, 48 insertions(+), 39 deletions(-) diff --git a/flake.lock b/flake.lock index d886641..8fb5d40 100644 --- a/flake.lock +++ b/flake.lock @@ -2,15 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1705003311, - "narHash": "sha256-7JtTAgeUHGKA9phwPveHtwvaQCDrta1yR5t7MYSMcCU=", + "lastModified": 1736012469, + "narHash": "sha256-/qlNWm/IEVVH7GfgAIyP6EsVZI6zjAx1cV5zNyrs+rI=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "3680fef792863c8b7bf52f0d474a16319995eabd", + "rev": "8f3e1f807051e32d8c95cd12b9b421623850a34d", "type": "github" }, "original": { "owner": "NixOS", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 8c35de4..90d044c 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,6 @@ { description = "Batmon - battery monitor"; - inputs.nixpkgs.url = "github:NixOS/nixpkgs"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; outputs = { self, @@ -11,7 +11,8 @@ pkgsForEach = nixpkgs.legacyPackages; in { nixosModules = { - batmon = ./nix/module.nix {inherit self;}; + batmon = import ./nix/module.nix self; + default = self.nixosModules.batmon; }; packages = forEachSystem (system: { diff --git a/nix/module.nix b/nix/module.nix index fba3ced..16969ff 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1,13 +1,14 @@ -{ - self, +self: { config, pkgs, lib, ... }: let - inherit (lib) mkIf mkForce types; + inherit (lib.modules) mkIf mkForce; inherit (lib.options) mkOption mkEnableOption; + inherit (lib) types; + format = pkgs.formats.json {}; cfg = config.services.batmon; in { options.services.batmon = { @@ -20,25 +21,26 @@ in { }; settings = mkOption { - description = "Settings for Batmon"; - type = types.attrs; + type = format.type; default = {}; - example = lib.literalExpression '' - { - batPaths = [ - { - path = "/sys/class/power_supply/BAT0"; - extraCommand = "notify-send 'State changed!'"; - } - ]; - } - ''; + example = { + batPaths = [ + { + path = "/sys/class/power_supply/BAT0"; + extraCommand = "notify-send 'State changed!'"; + } + ]; + }; + + description = "Settings for BatmoSettings for Batmonn"; }; }; config = mkIf cfg.enable { systemd.user.services."batmon" = { description = "Power Monitoring Service"; + wants = ["power-profiles-daemon.service"]; + wantedBy = ["default.target"]; environment.PATH = mkForce "/run/wrappers/bin:${lib.makeBinPath cfg.package}"; script = '' ${lib.getExe cfg.package} --config ${builtins.toJSON cfg.settings} @@ -47,10 +49,8 @@ in { serviceConfig = { Type = "simple"; Restart = "on-failure"; + DynamicUser = true; }; - - wants = ["power-profiles-daemon.service"]; - wantedBy = ["default.target"]; }; }; } diff --git a/nix/package.nix b/nix/package.nix index 22a179b..b87ed5e 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,11 +1,22 @@ -{buildGoModule}: +{ + lib, + buildGoModule, +}: buildGoModule { pname = "Batmon"; version = "0.1.0"; - src = ../.; + src = builtins.path { + path = ../.; + name = "batmon-src"; + }; vendorHash = null; - ldflags = ["-s" "-w"]; + + meta = { + license = lib.licenses.gpl3Only; + maintainers = [lib.maintainers.NotAShelf]; + mainProgram = "batmon"; + }; } diff --git a/nix/shell.nix b/nix/shell.nix index e3e5fea..ea268d3 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -1,16 +1,12 @@ { - packagePath, - callPackage, + mkShellNoCC, gopls, go, -}: let - mainPkg = callPackage packagePath {}; -in - mainPkg.overrideAttrs (oa: { - nativeBuildInputs = - (oa.nativeBuildInputs or []) - ++ [ - gopls - go - ]; - }) +}: +mkShellNoCC { + name = "batmon"; + packages = [ + gopls + go + ]; +}