Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions create-targz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ function build() {
cp "${ORIGINDIR}"/linux_files/start-systemd.sh "${TMPDIR}"/dist/usr/local/bin/start-systemd
chmod +x "${TMPDIR}"/dist/usr/local/bin/start-systemd

cp "${ORIGINDIR}"/linux_files/create_userpath.sudoers "${TMPDIR}"/dist/etc/sudoers.d/create_userpath
cp "${ORIGINDIR}"/linux_files/create_userpath.sh "${TMPDIR}"/dist/usr/local/bin/create_userpath
chmod +x "${TMPDIR}"/dist/usr/local/bin/create_userpath

cp "${ORIGINDIR}"/linux_files/wsl2-xwayland.service "${TMPDIR}"/dist/etc/systemd/system/wsl2-xwayland.service
cp "${ORIGINDIR}"/linux_files/wsl2-xwayland.socket "${TMPDIR}"/dist/etc/systemd/system/wsl2-xwayland.socket
ln -sf ../wsl2-xwayland.socket "${TMPDIR}"/dist/etc/systemd/system/sockets.target.wants/
Expand Down
61 changes: 49 additions & 12 deletions linux_files/00-remix.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,70 @@ if test -n "$XRDP_SESSION"
exit
end

if test -n "$SSH_CONNECTION"
exit
end

# check whether it is WSL1 for WSL2
if test -n "$WSL_INTEROP"
#Export an enviroment variable for helping other processes
#Export an environment variable for helping other processes
set --export WSL2 1

if test -z "$DISPLAY"
if test -n "$DISPLAY"
# WSLg support - setup /run/user directory and symlinks
set uid (id -u)
set user_path "/run/user/$uid"

if not test -d "$user_path"
sudo /usr/local/bin/create_userpath $uid 2>/dev/null
end

set wslg_runtime_dir "/mnt/wslg/runtime-dir"

ln -fs "$wslg_runtime_dir/wayland-0" "$user_path/" 2>/dev/null
ln -fs "$wslg_runtime_dir/wayland-0.lock" "$user_path/" 2>/dev/null

set pulse_path "$user_path/pulse"
set wslg_pulse_dir "$wslg_runtime_dir/pulse"

if not test -d "$pulse_path"
mkdir -p "$pulse_path" 2>/dev/null
ln -fs "$wslg_pulse_dir/native" "$pulse_path/" 2>/dev/null
ln -fs "$wslg_pulse_dir/pid" "$pulse_path/" 2>/dev/null
else if test -S "$pulse_path/native"
rm -f "$pulse_path/native" 2>/dev/null
ln -s "$wslg_pulse_dir/native" "$pulse_path/" 2>/dev/null
end

set -e uid
set -e user_path
set -e wslg_runtime_dir
set -e wslg_pulse_dir
set -e pulse_path
else
# enable external x display for WSL 2

set ipconfig_exec (wslpath 'C:\Windows\System32\ipconfig.exe')
if command -q ipconfig.exe
set ipconfig_exec (command -s ipconfig.exe)
set route_exec (wslpath 'C:\Windows\system32\route.exe')
if command -q route.exe
set route_exec (command -s route.exe)
end

set wsl2_d_tmp ($ipconfig_exec 2>/dev/null | grep -n -m 1 "Default Gateway.*: [0-9a-z]" | cut -d : -f 1)
set wsl2_d_tmp (eval "$route_exec print 2>/dev/null" | grep 0.0.0.0 | head -1 | awk '{print $4}')

if test -n "$wsl2_d_tmp"

set wsl2_d_tmp ($ipconfig_exec 2>/dev/null | sed (math $wsl2_d_tmp - 4)','(math $wsl2_d_tmp + 0)'!d' | string replace -fr '^.*IPv4.*:\s*(\S+).*$' '$1')
set --export DISPLAY "$wsl2_d_tmp:0"
else
set wsl2_d_tmp (grep nameserver /etc/resolv.conf | awk '{print $2}')
set --export DISPLAY $wsl2_d_tmp:0
set wsl2_d_tmp (ip route | grep default | awk '{print $3; exit;}')
set --export DISPLAY "$wsl2_d_tmp:0"
end

set -e wsl2_d_tmp
set -e ipconfig_exec
set -e route_exec
end

# Setup video acceleration
set --export VDPAU_DRIVER d3d12
set --export LIBVA_DRIVER_NAME d3d12
set --export GALLIUM_DRIVER d3d12
else
# enable external x display for WSL 1
set --export DISPLAY "localhost:0"
Expand Down
201 changes: 141 additions & 60 deletions linux_files/00-remix.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#!/bin/sh
# bashsupport disable=BP5007

# Only the default WSL user should run this script
if ! (id -Gn | grep -c "adm.*wheel\|wheel.*adm" >/dev/null); then
return
fi

systemd_saved_environment="$HOME/.systemd.env"

save_environment() {
{
echo "PATH='$PATH'"
echo "WSL_DISTRO_NAME='$WSL_DISTRO_NAME'"
echo "WSL_INTEROP='$WSL_INTEROP'"
echo "WSL_SYSTEMD_EXECUTION_ARGS='$WSL_SYSTEMD_EXECUTION_ARGS'"
echo "PULSE_SERVER='$PULSE_SERVER'"
echo "WAYLAND_DISPLAY='$WAYLAND_DISPLAY'"
} >"${systemd_saved_environment}"
}

Expand All @@ -38,6 +32,11 @@ setup_display() {
setup_interop
fi

unset WAYLAND_DISPLAY
if [ -n "$SYSTEMD_PID" ]; then
rm -f /run/user/"$(id -u)"/wayland* 2>/dev/null
fi

return
fi

Expand All @@ -47,34 +46,68 @@ setup_display() {

# check whether it is WSL1 or WSL2
if [ -n "${WSL_INTEROP}" ]; then
#Export an environment variable for helping other processes
export WSL2=1

if [ -n "${DISPLAY}" ]; then
#Export an environment variable for helping other processes
export WSL2=1

uid="$(id -u)"

user_path="/run/user/${uid}"
if [ ! -d "${user_path}" ]; then
sudo /usr/local/bin/create_userpath "${uid}" 2>/dev/null
fi

if [ -z "$SYSTEMD_PID" ]; then
export XDG_RUNTIME_DIR="${user_path}"
fi

wslg_runtime_dir="/mnt/wslg/runtime-dir"

ln -fs "${wslg_runtime_dir}"/wayland-0 "${user_path}"/ 2>/dev/null
ln -fs "${wslg_runtime_dir}"/wayland-0.lock "${user_path}"/ 2>/dev/null

pulse_path="${user_path}/pulse"
wslg_pulse_dir="${wslg_runtime_dir}"/pulse

if [ ! -d "${pulse_path}" ]; then
mkdir -p "${pulse_path}" 2>/dev/null

ln -fs "${wslg_pulse_dir}"/native "${pulse_path}"/ 2>/dev/null
ln -fs "${wslg_pulse_dir}"/pid "${pulse_path}"/ 2>/dev/null

elif [ -S "${pulse_path}/native" ]; then
rm -f "${pulse_path}/native" 2>/dev/null
ln -s "${wslg_pulse_dir}"/native "${pulse_path}"/ 2>/dev/null
fi

unset user_path
unset wslg_runtime_dir
unset wslg_pulse_dir
unset pulse_path
unset uid

return
fi
#Export an environment variable for helping other processes
export WSL2=1

# enable external x display for WSL 2
route_exec=$(wslpath 'C:\Windows\system32\route.exe')

ipconfig_exec=$(wslpath "C:\\Windows\\System32\\ipconfig.exe")
if (command -v ipconfig.exe >/dev/null 2>&1); then
ipconfig_exec=$(command -v ipconfig.exe)
if route_exec_path=$(command -v route.exe 2>/dev/null); then
route_exec="${route_exec_path}"
fi

wsl2_d_tmp="$(eval "$ipconfig_exec 2> /dev/null" | grep -n -m 1 "Default Gateway.*: [0-9a-z]" | cut -d : -f 1)"
wsl2_d_tmp="$(eval "$route_exec print 2> /dev/null" | grep 0.0.0.0 | head -1 | awk '{print $4}')"

if [ -n "${wsl2_d_tmp}" ]; then

wsl2_d_tmp="$(eval "$ipconfig_exec" | sed "$((wsl2_d_tmp - 4))"','"$((wsl2_d_tmp + 0))"'!d' | grep IPv4 | cut -d : -f 2 | sed -e "s|\s||g" -e "s|\r||g")"
export DISPLAY=${wsl2_d_tmp}:0
export DISPLAY="${wsl2_d_tmp}":0
else
wsl2_d_tmp="$(grep </etc/resolv.conf nameserver | awk '{print $2}')"
export DISPLAY=${wsl2_d_tmp}:0
wsl2_d_tmp="$(ip route | grep default | awk '{print $3; exit;}')"
export DISPLAY="${wsl2_d_tmp}":0
fi

unset wsl2_d_tmp
unset ipconfig_exec
unset route_exec
else
# enable external x display for WSL 1
export DISPLAY=localhost:0
Expand All @@ -84,62 +117,110 @@ setup_display() {
fi
}

setup_display
setup_dbus() {
# if dbus-launch is installed, then load it
if ! (command -v dbus-launch >/dev/null); then
return
fi

# Enabled via systemd
if [ -n "${DBUS_SESSION_BUS_ADDRESS}" ]; then
return
fi

dbus_pid="$(pidof dbus-daemon | cut -d' ' -f1)"
dbus_env_file="/tmp/dbus_env_${dbus_pid}"

# if dbus-launch is installed then load it
if (command -v dbus-launch >/dev/null 2>&1); then
eval "$(timeout 2s dbus-launch --auto-syntax)"
fi
if [ -z "${dbus_pid}" ] || [ ! -f "${dbus_env_file}" ]; then
dbus_env="$(timeout 2s dbus-launch --auto-syntax)"
eval "${dbus_env}"

# speed up some GUI apps like gedit
export NO_AT_BRIDGE=1
dbus_env_file="/tmp/dbus_env_${DBUS_SESSION_BUS_PID}"
echo "${dbus_env}" >"${dbus_env_file}"

# Fix 'clear' scrolling issues
alias clear='clear -x'
unset dbus_env
else # Running from a previous session
eval "$(cat "${dbus_env_file}")"
fi

# Custom aliases
alias ll='ls -al'
alias winget='powershell.exe winget'
alias wsl='wsl.exe'
unset dbus_pid
unset dbus_env_file
}

main() {
# Only the default WSL user should run this script
if ! (id -Gn | grep -c "adm.*wheel\|wheel.*adm" >/dev/null); then
return
fi

# Fix $PATH for Systemd
SYSTEMD_PID="$(ps -C systemd -o pid= | head -n1)"
systemd_saved_environment="$HOME/.systemd.env"

if [ -z "$SYSTEMD_PID" ]; then
SYSTEMD_PID="$(ps -C systemd -o pid= | head -n1)"

save_environment
setup_display

elif [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" -eq 1 ] && [ -f "$HOME/.systemd.env" ]; then
set -a
# shellcheck disable=SC1090
. "${systemd_saved_environment}"
set +a
if [ -z "$SYSTEMD_PID" ]; then
setup_dbus
fi

setup_interop
fi
# speed up some GUI apps like gedit
export NO_AT_BRIDGE=1

# Check if we have Windows Path
if [ -z "$WIN_HOME" ] && (command -v cmd.exe >/dev/null 2>&1); then
# Fix 'clear' scrolling issues
alias clear='clear -x'

# Create a symbolic link to the windows home
# Custom aliases
alias ll='ls -al'
alias winget='powershell.exe winget'
alias wsl='wsl.exe'

# Here have a issue: %HOMEDRIVE% might be using a custom set location
# moving cmd to where Windows is installed might help: %SYSTEMDRIVE%
wHomeWinPath=$(cmd.exe /c 'cd %SYSTEMDRIVE%\ && echo %HOMEDRIVE%%HOMEPATH%' 2>/dev/null | tr -d '\r')
if [ -n "${WSL2}" ]; then
#Setup video acceleration
export VDPAU_DRIVER=d3d12
export LIBVA_DRIVER_NAME=d3d12

if [ ${#wHomeWinPath} -le 3 ]; then #wHomeWinPath contains something like H:\
wHomeWinPath=$(cmd.exe /c 'cd %SYSTEMDRIVE%\ && echo %USERPROFILE%' 2>/dev/null | tr -d '\r')
# Setup Gallium Direct3D 12 driver
export GALLIUM_DRIVER=d3d12
fi

# shellcheck disable=SC2155
export WIN_HOME="$(wslpath -u "${wHomeWinPath}")"
if [ -z "$SYSTEMD_PID" ]; then

save_environment

elif [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" -eq 1 ] && [ -f "$HOME/.systemd.env" ] && [ -n "$WSL_SYSTEMD_EXECUTION_ARGS" ]; then
# Only if built-in systemd was started
set -a
# shellcheck disable=SC1090
. "${systemd_saved_environment}"
set +a

win_home_lnk=${HOME}/winhome
if [ ! -e "${win_home_lnk}" ]; then
ln -s -f "${WIN_HOME}" "${win_home_lnk}" >/dev/null 2>&1
setup_interop
fi

unset win_home_lnk
# Check if we have Windows Path
if [ -z "$WIN_HOME" ] && (command -v cmd.exe >/dev/null 2>&1); then

# Create a symbolic link to the Windows home

# Here have a issue: %HOMEDRIVE% might be using a custom set location
# moving cmd to where Windows is installed might help: %SYSTEMDRIVE%
wHomeWinPath=$(cmd.exe /c 'cd %SYSTEMDRIVE%\ && echo %HOMEDRIVE%%HOMEPATH%' 2>/dev/null | tr -d '\r')

if [ ${#wHomeWinPath} -le 3 ]; then #wHomeWinPath contains something like H:\
wHomeWinPath=$(cmd.exe /c 'cd %SYSTEMDRIVE%\ && echo %USERPROFILE%' 2>/dev/null | tr -d '\r')
fi

# shellcheck disable=SC2155
export WIN_HOME="$(wslpath -u "${wHomeWinPath}")"

win_home_lnk=${HOME}/winhome
if [ ! -e "${win_home_lnk}" ]; then
ln -s -f "${WIN_HOME}" "${win_home_lnk}" >/dev/null 2>&1
fi

unset win_home_lnk

fi
}

fi
main "$@"
31 changes: 31 additions & 0 deletions linux_files/create_userpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

#######################################
# Ensures the user's runtime directory exists under /run/user/<uid>.
# If the directory does not exist, it is created (parents included).
# Globals:
# None
# Arguments:
# uid: Numeric user ID whose runtime directory should exist.
# Returns:
# Exit status of the final command executed:
# - 0 if the directory already exists or was created successfully.
# - Non-zero if directory creation fails.
# Side effects:
# May create /run/user/<uid>.
# Suppresses mkdir error output (stderr) to avoid noisy logs.
#######################################
function main() {
local uid="${1}"

local user_path="/run/user/${uid}"
if [[ ! -d "${user_path}" ]]; then
mkdir -p "${user_path}" 2>/dev/null
chown -R "${uid}:${uid}" "${user_path}" 2>/dev/null

# Take advantage of the superuser and try to create other possible missing directories when systemd is not running
mkdir -p "/etc/xdg" 2>/dev/null
fi
}

main "$@"
1 change: 1 addition & 0 deletions linux_files/create_userpath.sudoers
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%wheel ALL=NOPASSWD: /usr/local/bin/create_userpath