iTranslated by AI
Building dotfiles compatible with both Windows and WSL
Recently, I have been using Windows Subsystem for Linux (WSL) for my development.
When I created my dotfiles for development on WSL, I encountered some WSL-specific issues, so I've summarized them here.
Note that these dotfiles are intended to be executed on WSL.
Placing files from WSL into the Windows home directory
Sometimes you want to place files like .wslconfig in the Windows home directory.
Windows files are mounted in WSL at locations like /mnt/c, and the home directory path can be obtained as follows:
WINHOME="$(wslpath "$(wslvar USERPROFILE)")"
cp -f .wslconfig $WINHOME/
By passing USERPROFILE, the environment variable for the Windows home directory, to wslvar, you can get a value like C:\\Users\\username.
Then, wslpath is used to convert the obtained Windows-style path into a WSL-compatible format like /mnt/c/User/username.
Determining if the script is running in WSL
There seem to be several ways to determine if a shell script is running in WSL.
For example, the script at https://github.com/docker/docker-install, published via https://get.docker.com, determines this by checking if the string Microsoft or microsoft is contained in the output of uname -r.
Since uname -r might yield the same result when running in a container, I personally check for the existence of the wslpath command.
if [ -n "$(which wslpath)" ]; then
# Process to be executed only on WSL
fi
Sharing Git credentials between WSL and Windows
I basically do my development on WSL, but I share Git credentials so that I can also work on the Windows side.
The method is as described in the following article:
Using the WINHOME mentioned earlier, and assuming Git is installed on the Windows side via Scoop, I set it up as follows:
git config --global credential.helper "${WINHOME}/scoop/apps/git/current/mingw64/libexec/git-core/git-credential-manager-core.exe"
Conclusion
I've written about various points regarding maintaining dotfiles on WSL. I hope this serves as a reference for those who are starting to build theirs.
Discussion