iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

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.

https://github.com/mkizka/dotfiles

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.

https://github.com/docker/docker-install/blob/fa5e8eaa2b048065a151f805d451903099144e25/install.sh#L133-L139

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:
https://qiita.com/snaka/items/cc83553a81e00b2d26f3

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.

GitHubで編集を提案

Discussion