<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Marin Lagacé - Operating System</title><link href="https://www.marinlagace.ca/en/" rel="alternate"></link><link href="https://www.marinlagace.ca/feeds/operating-system.atom.xml" rel="self"></link><id>https://www.marinlagace.ca/en/</id><updated>2020-06-12T21:20:00-04:00</updated><entry><title>Portable Cygwin</title><link href="https://www.marinlagace.ca/en/2020/portable-cygwin.html" rel="alternate"></link><published>2020-06-12T21:20:00-04:00</published><updated>2020-06-12T21:20:00-04:00</updated><author><name>Marin</name></author><id>tag:www.marinlagace.ca,2020-06-12:/en/2020/portable-cygwin.html</id><summary type="html">&lt;p class="first last"&gt;My own hack to make Cygwin run from a USB stick.&lt;/p&gt;
</summary><content type="html">&lt;p&gt;I do most of my software development on &lt;span class=os&gt;Linux&lt;/span&gt;. Since my employer is a
&lt;span class=os&gt;Windows&lt;/span&gt; shop, most of the software I write must run on &lt;span class=os&gt;Windows&lt;/span&gt;.
Since I use cross-platform libraries (like &lt;a href="https://www.boost.org" class="reference external"&gt;Boost&lt;/a&gt; or
&lt;a href="https://www.qt.io" class="reference external"&gt;Qt&lt;/a&gt;), I can still do most of my development work on
&lt;span class=os&gt;Linux&lt;/span&gt;. Unfortunately, at some point, I need to move to &lt;span class=os&gt;Windows&lt;/span&gt; to
compile and test the software.&lt;/p&gt;
&lt;p&gt;On &lt;span class=os&gt;Linux&lt;/span&gt;, I mainly use &lt;a href="https://www.vim.org" class="reference external"&gt;Vim&lt;/a&gt; with &lt;a href="https://github.com/tmux/tmux/wiki" class="reference external"&gt;tmux&lt;/a&gt;. I am very familiar with those tools. I
have my &lt;span class=app&gt;tmux&lt;/span&gt; sessions, windows and panels set up just the way I like
them. I also have fully configured my &lt;span class=app&gt;Vim&lt;/span&gt; plugins and key bindings. I
really would like to keep using these tools and my workspace configuration when
coding on &lt;span class=os&gt;Windows&lt;/span&gt;. And I can by using &lt;a href="https://www.cygwin.com" class="reference external"&gt;Cygwin&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;According to &lt;span class=app&gt;Wikipedia&lt;/span&gt;:&lt;/p&gt;
&lt;blockquote&gt;
Cygwin is a POSIX-compatible environment that runs natively on Microsoft
Windows. Its goal is to allow programs of Unix-like systems to be recompiled
and run natively on Windows with minimal source code modifications by
providing them with the same underlying POSIX API they would expect in those
systems.
&lt;sup id=sf-portable-cygwin-1-back&gt;&lt;a href=#sf-portable-cygwin-1 title=" https://en.wikipedia.org/wiki/Cygwin " class=simple-footnote&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;span class=app&gt;Cygwin&lt;/span&gt; is a great tool. I can use &lt;span class=app&gt;Vim&lt;/span&gt; and &lt;span class=app&gt;tmux&lt;/span&gt; without any
problem. All the plugins and configuration files I used on &lt;span class=os&gt;Linux&lt;/span&gt; work
flawlessly on &lt;span class=app&gt;Cygwin&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In my work, I have access to multiple computers. I want to use &lt;span class=app&gt;Cygwin&lt;/span&gt; on
all of them so I can have access to all my development tools. The problem is
that &lt;span class=app&gt;Cygwin&lt;/span&gt; can be long and tedious to install and configure. I would
like a version that can be run from a USB drive.&lt;/p&gt;
&lt;p&gt;There are many projects on &lt;a href="https://github.com/search?q=cygwin-portable&amp;amp;type=Repositories&amp;amp;ref=searchresults" class="reference external"&gt;GitHub&lt;/a&gt;
that hack on a local &lt;span class=app&gt;Cygwin&lt;/span&gt; installation to make it portable. Some of
them are quite old and look abandoned. Some others I find too complex for my
needs. So here is my own hack to make &lt;span class=app&gt;Cygwin&lt;/span&gt; portable.&lt;/p&gt;
&lt;p&gt;Install &lt;span class=app&gt;Cygwin&lt;/span&gt; to your hard drive. It does not matter to which directory
you install it. Here, I chose to install it to &lt;span class=path&gt;c:CygwinX86&lt;/span&gt;.&lt;/p&gt;
&lt;img src="https://www.marinlagace.ca/en/images/Cygwin_Root_Install_Dir.png" alt="Cygwin Root Install Dir" class=align-center&gt;
&lt;p&gt;Set the local package directory location to a subdirectory of the
installation directory. Here, I use the &lt;span class=path&gt;packages&lt;/span&gt; subdirectory.&lt;/p&gt;
&lt;img src="https://www.marinlagace.ca/en/images/Cygwin_Packages_Install_Dir.png" alt="Cygwin Packages Install Dir" class=align-center&gt;
&lt;p&gt;Follow the installation procedure to the end. There is no other special care to
take during the installation process.&lt;/p&gt;
&lt;p&gt;Once the installation procedure is complete, add a batch file named
&lt;span class=path&gt;PortableCygwin.bat&lt;/span&gt;
&lt;sup id=sf-portable-cygwin-2-back&gt;&lt;a href=#sf-portable-cygwin-2 title=" You can of course name it any way you like. " class=simple-footnote&gt;2&lt;/a&gt;&lt;/sup&gt;
to the &lt;span class=app&gt;Cygwin&lt;/span&gt; root directory.&lt;/p&gt;
&lt;img src="https://www.marinlagace.ca/en/images/Cygwin_Portable_Batch_File.png" alt="Cygwin Portable Batch File" class=align-center&gt;
&lt;p&gt;The file should contain the following lines:&lt;/p&gt;
&lt;div class=highlight&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=p&gt;@&lt;/span&gt;&lt;span class=k&gt;echo&lt;/span&gt; off
&lt;span class=s2&gt;"&lt;/span&gt;&lt;span class=nv&gt;%~dp0&lt;/span&gt;&lt;span class=s2&gt;\bin\mintty.exe"&lt;/span&gt; -i /Cygwin-Terminal.ico -
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This file will start the &lt;span class=app&gt;Cygwin&lt;/span&gt; terminal from whichever directory it is
installed on.&lt;/p&gt;
&lt;p&gt;&lt;span class=app&gt;Cygwin&lt;/span&gt; will create a home folder using the current &lt;span class=os&gt;Windows&lt;/span&gt; user
name. My user name changes from computer to computer. This means that the name
of my home folder will also change from computer to computer. We thus need to
set a fix home folder to make our installation more portable. This can be easily
accomplished by editing the &lt;span class=path&gt;/etc/nsswitch.conf&lt;/span&gt; file, uncommenting the
&lt;tt class="docutils literal"&gt;&lt;span class=pre&gt;db-home&lt;/span&gt;&lt;/tt&gt; line and setting its value to a fixed path:&lt;/p&gt;
&lt;div class=highlight&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;# /etc/nsswitch.conf
#
#    This file is read once by the first process in a Cygwin process tree.
#    To pick up changes, restart all Cygwin processes.  For a description
#    see https://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-mapping-nsswitch
#
# Defaults:
# passwd:   files db
# group:    files db
# db_enum:  cache builtin
db_home:  /home/foo
# db_shell: /bin/bash
# db_gecos: &amp;lt;empty&amp;gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above case, &lt;span class=app&gt;Cygwin&lt;/span&gt; will always set the home directory to
&lt;span class=path&gt;/home/foo&lt;/span&gt;. Restart your terminal and check that your home directory has
the expected path.&lt;/p&gt;
&lt;div class=highlight&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ cygpath -w ~
c:\CygwinX86\home\foo
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We need to register the proper path to the local package directory. This path is
recorded in the &lt;span class=path&gt;/etc/setup/setup.rc&lt;/span&gt; file.&lt;/p&gt;
&lt;div class=highlight&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;last-cache
        c:\CygwinX86\packages
mirrors-lst
        http://cygwin.mirror.constant.com/;cygwin.mirror.constant.com;United States;New Jersey
        http://cygwin.mirror.hoobly.com/;cygwin.mirror.hoobly.com;United States;Pennsylvania
        http://mirrors.koehn.com/cygwin/;mirrors.koehn.com;United States;Missouri
        http://mirrors.metapeer.com/cygwin/;mirrors.metapeer.com;United States;Washington
        ...
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We need to replace the second line with the path to the local package directory
on our USB key. Adding the following line to your &lt;span class=path&gt;.profile&lt;/span&gt; file will do
the trick.&lt;/p&gt;
&lt;div class=highlight&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;sed -i &lt;span class=s1&gt;'2s|.*|\t'&lt;/span&gt;&lt;span class=k&gt;$(&lt;/span&gt;cygpath -m /&lt;span class=k&gt;)&lt;/span&gt;&lt;span class=s1&gt;'/packages|'&lt;/span&gt; /etc/setup/setup.rc
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is important to specify the proper subdirectory in the command. Here we
specified &lt;span class=path&gt;packages&lt;/span&gt; since this is the subdirectory we used during
installation earlier. Now, every time we start &lt;span class=app&gt;Cygwin&lt;/span&gt;, the
&lt;span class=path&gt;.profile&lt;/span&gt; file will be run and the &lt;span class=path&gt;setup.rc&lt;/span&gt; file will be updated
with the actual path to the local package directory.&lt;/p&gt;
&lt;p&gt;Finally, copy the whole installation directory anywhere on your USB drive.
Use the &lt;span class=path&gt;PortableCygwin.bat&lt;/span&gt; file to start &lt;span class=app&gt;Cygwin&lt;/span&gt; and enjoy your
portable POSIX experience.&lt;/p&gt;
&lt;!-- vim: ft=pelican.rst --&gt;
&lt;ol class=simple-footnotes&gt;&lt;li id=sf-portable-cygwin-1&gt; &lt;a href="https://en.wikipedia.org/wiki/Cygwin" class="reference external"&gt;https://en.wikipedia.org/wiki/Cygwin&lt;/a&gt;  &lt;a href=#sf-portable-cygwin-1-back class=simple-footnote-back&gt;↩&lt;/a&gt;&lt;/li&gt;&lt;li id=sf-portable-cygwin-2&gt; You can of course name it any way you like.  &lt;a href=#sf-portable-cygwin-2-back class=simple-footnote-back&gt;↩&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;</content><category term="cygwin"></category></entry></feed>