···22{
33 perSystem =
44 { pkgs, ... }:
55+ let
66+ # Function to detect if we're running in a terminal environment
77+ terminalDetectionFunction = ''
88+ is_terminal_environment() {
99+ # Check if we're running in a TTY
1010+ if [ -t 0 ] || [ -t 1 ]; then
1111+ return 0 # True, we're in a terminal
1212+ fi
1313+1414+ # Check if our parent process is a terminal
1515+ parent_process=$(ps -o comm= $PPID 2>/dev/null || true)
1616+ terminal_programs="bash zsh fish sh ksh csh tcsh dash xterm konsole gnome-terminal alacritty kitty terminal terminator urxvt rxvt xfce4-terminal iterm2 Terminal"
1717+ for term in $terminal_programs; do
1818+ if echo "$parent_process" | grep -q "$term"; then
1919+ return 0 # True, parent is a terminal
2020+ fi
2121+ done
2222+2323+ # Check if we're in an interactive environment
2424+ if [ -n "$TERM" ] && [ "$TERM" != "dumb" ]; then
2525+ # Check if we're in an SSH session
2626+ if [ -n "$SSH_TTY" ] || [ -n "$SSH_CONNECTION" ] || [ -n "$SSH_CLIENT" ]; then
2727+ return 0 # True, SSH session implies terminal
2828+ fi
2929+ fi
3030+3131+ return 1 # False, not in a terminal
3232+ }
3333+ '';
3434+ in
535 {
636 packages.menu =
737 if pkgs.stdenv.hostPlatform.isDarwin then
···939 name = "menu";
1040 runtimeInputs = [
1141 pkgs.choose-gui
4242+ pkgs.fzf
4343+ pkgs.procps
1244 ];
1345 text = ''
1446 set -efu
1515- # TODO check if a terminal is available and use fzf in that case
1616- exec choose "$@"
4747+4848+ # Terminal detection function
4949+ ${terminalDetectionFunction}
5050+5151+ # Decide which menu program to use
5252+ if is_terminal_environment; then
5353+ # Use terminal-based menu
5454+ cat | fzf "$@"
5555+ else
5656+ # Use GUI-based menu
5757+ cat | choose "$@"
5858+ fi
1759 '';
1860 })
1961 else if pkgs.stdenv.hostPlatform.isLinux then
···2264 runtimeInputs = [
2365 pkgs.rofi
2466 pkgs.wofi
6767+ pkgs.fzf
6868+ pkgs.procps
2569 ];
2670 text = ''
2771 set -efu
2828- # TODO check if a terminal is available and use fzf in that case
2929- if [[ -n $WAYLAND_DISPLAY ]]; then
3030- exec wofi "$@"
7272+7373+ # Terminal detection function
7474+ ${terminalDetectionFunction}
7575+7676+ # Decide which menu program to use
7777+ if is_terminal_environment; then
7878+ # Use terminal-based menu
7979+ cat | fzf "$@"
8080+ elif [[ -n $WAYLAND_DISPLAY ]]; then
8181+ # Use wofi in Wayland
8282+ cat | wofi -d "$@"
3183 elif [[ -n $DISPLAY ]]; then
3232- exec rofi "$@"
8484+ # Use rofi in X11
8585+ cat | rofi -dmenu "$@"
8686+ else
8787+ # No terminal or GUI environment, fallback to fzf
8888+ cat | fzf "$@"
3389 fi
3490 '';
3591