Shell zsh check os with ostype and uname

i have 3 jails that use nullfs to mount the home directory

each jail has zsh installed and so they all use the zsh config files

i need a way to check the os,
so i can have different shell config for each jail

so they can have different shell paths
XDG_RUNTIME_DIR directories, or use X11 or wayland

this is done using the ~/.zshenv file
with 2 case statements

the first case statement uses OSTYPE to check if the jail is running Freebsd
the second case statement use uname -a to check if the jail is running Ubuntu or Rocky linux


Code:
vi ~/.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
  ;;
esac

case "$(uname -a)" in
  *ubuntu*)
  typeset -U PATH path
  path=( "$HOME/.cargo/bin" "/opt/resolve/bin" "/bin" "/usr/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

  # 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=:0
  export QT_QPA_PLATFORM=xcb
  export GDK_BACKEND=x11

  . "$HOME/.cargo/env"
  ;;
  *rocky*)
  typeset -U PATH path
  path=( "/usr/local/cuda-12.4/bin" "/bin" "/usr/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

  # 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=: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

just using OSTYPE or uname on its own doesnt work
you have to use them both
 
Would this help?

Code:
% sh
$ . /etc/os-release
$ echo $NAME
FreeBSD
$ echo $ID
freebsd
$ echo $VERSION
14.2-RELEASE-p2
$ echo $VERSION_ID
14.2

Should work on Linux too.
 
Would this help?

Code:
% sh
$ . /etc/os-release
$ echo $NAME
FreeBSD
$ echo $ID
freebsd
$ echo $VERSION
14.2-RELEASE-p2
$ echo $VERSION_ID
14.2

Should work on Linux too.
This does work perfectly well on FreeBSD & Linux.

I use this very technique for OS and distribution specific zsh configuration.

${ID} seems to be the best distinguisher (and, occasionally, ${ID_LIKE}, with closely related linux distros) for my purposes.

There are a couple github repos out there with os-release files from many os's so you can compare them as needed:

Also, Chris Siebenmann (Wandering Thoughts) has somewhat recently published a blog entry on managing shell environments on different systems with shared config files, but I can't find the specific post right now.
 
Back
Top