Shell zsh OSTYPE check the os with a case statement

I recently came across a issue using Freebsd and Linux Jails with zsh

In the jail config for Freebsd and Ubuntu i mount /home with nullfs

Code:
mount += "/home           $path/home     nullfs          rw                      0       0";

The problem is i have zsh installed in both the Freebsd and Ubuntu jails
and they both use the /home nullfs which means they use the same zsh config files

The Freebsd jail is for Firefox running as a Wayland application,
and the Ubuntu jail is for DaVinci Resolve running as an XWayland application

So we need to export different settings in the ~/.zshenv file for each os
because the XDG_RUNTIME_DIR location on Freebsd is different than on Linux

and we also need to export some Nvidia settings on Linux for DaVinci Resolve

zsh has a feature called OSTYPE which will return the os

echo "${OSTYPE}" on Freebsd returns freebsd14.0

Code:
echo "${OSTYPE}"
freebsd14.0

echo "${OSTYPE}" on Linux returns linux-gnu

Code:
echo "${OSTYPE}"
linux-gnu

so in our ~/.zshenv we can use a case statement to check if the os is Freebsd or Linux
and then put the code for that os in the case statement

so when im running the Freebsd jail and using zsh
the code in the case statement for Freebsd is used

and when im running the Ubuntu jail and zsh
the code in the case statement for Linux is used

very cool

~/.zshenv in the /home nullfs mounted in the jails

Code:
~/.zshenv

Code:
# ~/.zshenv

# for ZSH
case "$OSTYPE" in
  freebsd*)
  # Path
  typeset -U PATH path
  path=("$path[@]")
  export PATH

  # XDG_RUNTIME_DIR
  export XDG_RUNTIME_DIR=/var/run/xdg/"${USER}"

  # wayland - uncomment to use wayland
  export WAYLAND_DISPLAY=wayland-0
  export QT_QPA_PLATFORM=wayland
  export GDK_BACKEND=wayland

  # x11 - comment out to use wayland
  #export DISPLAY=unix:0
  #export QT_QPA_PLATFORM=xcb
  #export GDK_BACKEND=x11
  ;;
  linux*)
  typeset -U PATH path
  path=("/opt/resolve/bin" "$path[@]")
  export PATH

  # XDG_RUNTIME_DIR
  export XDG_RUNTIME_DIR="/run/user/`id -u`"

  # dummy-uvm.so for access to the gpu
  #export LD_PRELOAD="$HOME/.config/gpu/dummy-uvm.so"
  export LD_PRELOAD="${HOME}"/.config/gpu/dummy-uvm.so:/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
  export __NV_PRIME_RENDER_OFFLOAD=1
  export __GLX_VENDOR_LIBRARY_NAME=nvidia

  # wayland - uncomment to use wayland
  #export WAYLAND_DISPLAY=wayland-0
  #export QT_QPA_PLATFORM=wayland
  #export GDK_BACKEND=wayland

  # x11 - comment out to use wayland
  export DISPLAY=unix:0
  export QT_QPA_PLATFORM=xcb
  export GDK_BACKEND=x11
  ;;
esac

# xdg directories
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"

# qt5
export QT_QPA_PLATFORMTHEME=qt5ct
 
Back
Top