Like many others, I own a gaming PC with a GPU far more powerful than my needs.
Thanks to recent advances in open source AI, running and even training models locally has become far more accessible, and my GPU, tired of running the same old games, found itself a new purpose.

After initially trying to install the toolchains and software on my Windows operating system, I gave up quickly and turned to Linux. Probably due to my lack of experience with recent Windows systems, this kind of work still feels like an afterthought compared to simply running a Linux distro.

After giving up on Windows and deciding to install Linux, to my surprise, or rather lack thereof, Windows bootloader still doesn’t support secondary operating systems. Hence GRUB takes the throne as the UEFI-chosen bootloader in my system.

Now I had a dilemma: which operating system should be the default option to boot?

On one hand, most of the time I use my Linux installation headless and just SSH to it via my laptop, so making it default and manually picking Windows during boot whenever I want to game sounds like a good option. However, Windows likes to restart itself to finish updates, and the system booting to Linux when it restarts isn’t ideal. On the other hand, when I decide to game, hitting the power button, going to the kitchen to make coffee while my Windows boots, and coming back to my PC ready to game is appealing.

After countless unnecessary reboots, one day I realized that my GPU was using almost 16 megabytes of my now very precious VRAM to render the useless login screen. I realized unplugging the DisplayPort cable before booting was the most logical solution—in hindsight it probably wasn’t, but I ended up doing so.

Suddenly my workflow became simple:

  1. If there is a display connected: I want to game, boot to Windows
  2. If there are no displays connected: I want to generate cat images, boot to Linux

I then quickly realized that since GRUB should be able to tell when there is a monitor connected, it could solve my original problem.

The solution was simply adding a check to GRUB in /etc/grub.d/40_custom

insmod all_video
insmod gfxterm

set gfxmode=auto
set gfxpayload=keep

if terminal_output gfxterm; then
    set default=2 # Windows 11
else
    set default=0 # Linux
fi

This simply tries to initialize a graphical terminal, and if it succeeds, boots to Windows, since that implies the existence of a monitor.

Silly, but it solves my problem and will probably work until the ports on my GPU break from frequent use.