diff --git a/modules/services/gpg-agent.nix b/modules/services/gpg-agent.nix
index f32d85af3..06ee803f4 100644
--- a/modules/services/gpg-agent.nix
+++ b/modules/services/gpg-agent.nix
@@ -27,7 +27,9 @@ let
   '' + optionalString cfg.enableSshSupport gpgSshSupportStr;
 
   gpgNushellInitStr = ''
-    $env.GPG_TTY = (tty)
+    if (is-terminal --stdin) {
+      $env.GPG_TTY = (tty)
+    }
   '' + optionalString cfg.enableSshSupport ''
     ${gpgPkg}/bin/gpg-connect-agent updatestartuptty /bye | ignore