msys2 is the easiest and cleanest unix sub-system on Windows. It's based on Cygwin. It's tools are built on Mingw64. It provides build toolchain for Mingw64. It provides a nice package manager the pacman from ArchLinux.

With my experience so far, it makes an ideal Window utility toolbox for administration tasks. It's worth to be a default installation for every Windows machine.

Setting Home Directory to USERPROFILE

2018-8-30 note: it seems using msys2.exe with the original nsswitch.conf is enough, which has db_home windows cygwin desc. I guess the problem was it didn't work with domain joined computer. If you're not domain joined, this section isn't needed.

Do this through etc/nsswitch.conf. Set:

db_home: windows /c/Users/%U

The /c/Users/%U is to workaround an issue that db_home: windows has no effect on my domain joined machine. For detail, refer to Cygwin nsswitch.conf doc.

Making portable applications

Note that nsswitch.conf is read relative to the executable path. So to make a portable executable be aware of nsswitch.conf settings, have a structure like below. If you invoke ssh.exe from anywhere, the nsswitch.conf is respected.

ssh.bat   # wraps ssh.exe
msys/
    usr/
        bin/
            ssh.exe
            <dependency DLLs>
    etc/
        nsswitch.conf

Shell Initialization

2018-8-30 note: it seems using msys2.exe is enough - but don't use the .bat shortcuts from the start menu - for some reason they don't respect user rc scripts

Depending on the "launcher" you use. Nowadays, it has msys2.exe which takes msys2.ini containing env vars. But that's not user specific. I build a msys2.ps1 that sets env vars and launches msys2.exe. Use this and leave the ini unchanged.

For example you can set the default shell by SHELL=/usr/bin/zsh.

Figuring Executable Dependencies

To make a portable executable distribution, you need to copy not only the executable, but all its dependencies. You can do so with ldd:

$ ldd /usr/bin/rsync.exe
        ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x778c0000)
        KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x769b0000)
        KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x769b0000)
        KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x77620000)
        msys-gcc_s-1.dll => /usr/bin/msys-gcc_s-1.dll (0x6ac00000)
        msys-iconv-2.dll => /usr/bin/msys-iconv-2.dll (0x6ee80000)
        msys-2.0.dll => /usr/bin/msys-2.0.dll (0x61000000)
        msys-z.dll => /usr/bin/msys-z.dll (0x644c0000)

Note that all the DLLs located inside the msys root /usr/bin are the dependencies that need to be packed alongside the executable.

References