Skip to content

Set up Windows dev environment with MSYS2

valtron edited this page Nov 24, 2019 · 30 revisions

MSYS2

Note: $VARS refer to strings you should substitute yourself. E.g. $DEV -> D:/dev

  1. Download from http://msys2.github.io
  2. Install into $DEV/msys64
  3. Run $DEV/msys2_shell.cmd
  4. Run pacman -Syuu
  5. Close the shell; reopen it, and run pacman -Syuu again, just in case :p

Pacman

pacman is the package manager bundled with msys. Use it to install useful things like gcc, flex, bison, git. The commands are pretty cryptic, so use the Pacman/Rosetta.

Set up $PATH

  • Open "Environment Variables > System Variables > Path"
  • At the end, add $DEV/msys64/usr/bin

Set up $HOME

By default, msys will use $DEV/msys64/home as your home folder. If you want to use your existing home folder, set a $HOME env variable:

  • Under "Environment Variables > User variables", add HOME -> %USERPROFILE%

Then edit $DEV/msys64/etc/nsswitch.conf to set db_home: /%H.

Also, SSH insists on using MSYS' /home, but you can get around that by adding this line to /etc/fstab:

C:/Users /home ntfs binary,noacl,auto 1 1

Git

  1. Open an msys shell
  2. Run pacman -S git
  3. If you want to use git gui/gitk,
    1. Run pacman -S mingw-w64-x86_64-tk
    2. Add $DEV/msys64/mingw64/bin to $PATH

Problems with git gui

You might get this problem with git gui; to fix, add env var GIT_GUI_LIB_DIR -> $DEV/msys64/usr/share/git-gui/lib.

If you get the Error: fatal: invalid path /home/..., you need to patch $DEV/msys64/usr/lib/git-core/git-gui:

  1. Find this section (around line 2174):
    if {!$is_submodule} {
        if {![is_bare]} {
            cd $_gitworktree
    
    and replace cd $_gitworktree with cd [exec cygpath --windows $_gitworktree]
  2. Find this section (around line 1328):
    set env(GIT_DIR) $_gitdir
    set env(GIT_WORK_TREE) $_gitworktree
    
    and, before those two lines, add set _gitworktree [exec cygpath --unix $_gitworktree]
  3. (You'll need to apply these changes every time you update Git.)

MSYS shell

$DEV/msys64/*_shell.cmd opens $DEV/msys64/usr/bin/mintty.exe, which has problems with the Python installed by the official Python installers. If you prefer, you can use $DEV/msys64/usr/bin/bash.exe by making a shortcut to it (on your desktop, for example).

To customize:

  1. (right-click shortcut) > Properties
  2. Change "Shortcut > Start in:" to be the directory you want it to open to, e.g. %HOME%/Desktop
  3. Fiddle with settings in Options, Font, Layout, and Colors

Environment Variables

Environment variables in windows are under:

  • (right-click start) > System > Advanced System Settings > Advanced > Environment Variables...

You can also set env vars in your .bash_profile/.bashrc.

Python

Dealing with PEP8-ers

Create tabspace filter:

git config --global filter.tabspace.smudge '/usr/bin/unexpand --tabs=4 --first-only'
git config --global filter.tabspace.clean '/usr/bin/expand --tabs=4 --initial'

Then in the repo's .git/info/attributes, add:

*.py  filter=tabspace

Then, in your (clean; if not, stash) repo do:

rm .git/index
git checkout HEAD -- "$(git rev-parse --show-toplevel)"

Virtualenv

To use virtualenvs without the activate/deactivate nonsense, in your .bashrc add:

PYROOT="path to your python install"

function py {
	if [ -d ".venv" ]; then
		local PRE=.venv/Scripts
	else
		local PRE=$PYROOT
	fi
	$PRE/python "$@"
}

function pyp {
	py -m pip "$@"
}

and use py instead of python (and pyp instead of pip) from now on. To help enforce this, you should also add:

function pip {
	echo "Don't use \`pip\`. Use \`pyp\` or \`py -m pip\`."
}

function python {
	echo "Don't use \`python\`. Use \`py\`."
}

Now, when you run py or pyp, it first tries to use python inside the .venv of the current directory, otherwise the system-wide one.

For executables installed by python packages (e.g. django-admin ...), figure out the equivalent py -m <module name> ... for doing the same thing.

Right-click Context Menu Entries

git-bash has a special --cd option to set the directory on startup, which regular bash doesn't. To get around that, create $DEV/bash.sh:

if [ "z$1" != "z" ]; then
	cd $1
fi
bash

Next, create a regedit file (*.reg):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\bash]
@="Bash"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"Position"="Bottom"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\bash\command]
@="$DEV\\msys64\\usr\\bin\\bash.exe"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\bash]
@="Bash"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"Position"="Bottom"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\bash\command]
@="$DEV\\msys64\\usr\\bin\\bash.exe $DEV\\bash.sh \"%1\""

Note the paths have to be Windows-style, with escaped (double) backslashes, and that the "Background" entry's command should not contain %1. Feel free to change the "Icon" and "Position" properties. Execute the regedit file and you're done.

Clone this wiki locally