From e17b382062bfba700c0b1e4ec31406560e855ff2 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 11 May 2023 19:17:55 +0200 Subject: [PATCH] [Services/GnuPG] fix GPG issues when no tty is available (i.e. when ran by Pycharm) --- services/gnupg.nix | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/services/gnupg.nix b/services/gnupg.nix index d459b09..18a0a33 100644 --- a/services/gnupg.nix +++ b/services/gnupg.nix @@ -1,6 +1,10 @@ { config, pkgs, lib, ... }: with lib; -{ +let + gpgNoTTY = pkgs.writeShellScriptBin "gpg-no-tty" '' + exec ${pkgs.gnupg}/bin/gpg --batch --no-tty "$@" + ''; +in { config = { programs.gnupg = { @@ -13,10 +17,18 @@ with lib; }; environment.shellInit = '' - export GPG_TTY="$(tty)" - gpg-connect-agent /bye - export SSH_AUTH_SOCK="/run/user/$UID/gnupg/S.gpg-agent.ssh" + if tty --silent; then + export GPG_TTY="$(tty)" + gpg-connect-agent /bye + export SSH_AUTH_SOCK="/run/user/$UID/gnupg/S.gpg-agent.ssh" + else + alias gpg=${gpgNoTTY}/bin/gpg-no-tty + fi ''; + + environment.systemPackages = [ + gpgNoTTY + ]; systemd.user.services.gpg-agent = let pinentrySwitcher = pkgs.callPackage ../packages/pinentry.nix {};