From 7b9cd178ecdb420b273dd8572d2a402a55bc99af Mon Sep 17 00:00:00 2001 From: Shahab Dogar Date: Fri, 14 Feb 2025 21:39:55 +0500 Subject: [PATCH] feat: HomeManager | enable notifications for yubikey touch required --- flake.nix | 1 + home-manager/home.nix | 2 + .../home-manager/yubikey-touch-detector.nix | 65 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 modules/home-manager/yubikey-touch-detector.nix diff --git a/flake.nix b/flake.nix index 5d09d78..ca4db6e 100644 --- a/flake.nix +++ b/flake.nix @@ -69,6 +69,7 @@ inherit pkgs; modules = [ ./home-manager/home.nix + ./modules/home-manager/yubikey-touch-detector.nix ]; }; }; diff --git a/home-manager/home.nix b/home-manager/home.nix index dbd06d9..eefd14e 100644 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -123,6 +123,8 @@ in { # Enable home-manager programs.home-manager.enable = true; + services.yubikey-touch-detector.enable = true; + # Nicely reload system units when changing configs systemd.user.startServices = "sd-switch"; } diff --git a/modules/home-manager/yubikey-touch-detector.nix b/modules/home-manager/yubikey-touch-detector.nix new file mode 100644 index 0000000..100ca67 --- /dev/null +++ b/modules/home-manager/yubikey-touch-detector.nix @@ -0,0 +1,65 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.yubikey-touch-detector; +in +{ + options.services.yubikey-touch-detector = { + enable = mkEnableOption "a tool to detect when your YubiKey is waiting for a touch"; + + package = mkOption { + type = types.package; + default = pkgs.yubikey-touch-detector; + defaultText = "pkgs.yubikey-touch-detector"; + description = '' + Package to use. Binary is expected to be called "yubikey-touch-detector". + ''; + }; + + socket.enable = mkEnableOption "starting the process only when the socket is used"; + + extraArgs = mkOption { + type = types.listOf types.str; + default = [ "--libnotify" ]; + defaultText = literalExpression ''[ "--libnotify" ]''; + description = '' + Extra arguments to pass to the tool. The arguments are not escaped. + ''; + }; + }; + + config = mkIf cfg.enable { + home.packages = [ cfg.package ]; + + # Service description licensed under ISC + # See https://github.com/maximbaz/yubikey-touch-detector/blob/c9fdff7163361d6323e2de0449026710cacbc08a/LICENSE + # Author: Maxim Baz + systemd.user.sockets.yubikey-touch-detector = mkIf cfg.socket.enable { + Unit.Description = "Unix socket activation for YubiKey touch detector service"; + Socket = { + ListenFIFO = "%t/yubikey-touch-detector.sock"; + RemoveOnStop = true; + SocketMode = "0660"; + }; + Install.WantedBy = [ "sockets.target" ]; + }; + + # Same license thing for the description here + systemd.user.services.yubikey-touch-detector = { + Unit = { + Description = "Detects when your YubiKey is waiting for a touch"; + Requires = optionals cfg.socket.enable [ "yubikey-touch-detector.socket" ]; + }; + Service = { + ExecStart = "${cfg.package}/bin/yubikey-touch-detector ${concatStringsSep " " cfg.extraArgs}"; + Environment = [ "PATH=${lib.makeBinPath [ pkgs.gnupg ]}" ]; + Restart = "on-failure"; + RestartSec = "1sec"; + }; + Install.Also = optionals cfg.socket.enable [ "yubikey-touch-detector.socket" ]; + Install.WantedBy = [ "default.target" ]; + }; + }; +}