<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>https://kalnytskyi.com</id>
  <title>Ihor Kalnytskyi</title>
  <updated>2026-10-08T22:17:30.143006+00:00</updated>
  <link href="https://kalnytskyi.com/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="https://kalnytskyi.com" rel="alternate" type="text/html"/>
  <generator uri="https://github.com/ikalnytskyi/holocron" version="0.3.1.dev283+gc4c5bab89">Holocron/v0.3.1.dev283+gc4c5bab89</generator>
  <subtitle>Ihor Kalnytskyi's thoughts &amp; writings</subtitle>
  <entry>
    <id>https://kalnytskyi.com/posts/on-tmux-osc52-support/</id>
    <title>On tmux OSC-52 support</title>
    <updated>2026-10-08T22:17:30.143738+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;Several days ago, OSC-52 support was &lt;a href="https://github.com/neovim/neovim/pull/25872"&gt;merged&lt;/a&gt; into NeoVim, and this sparkled
my interest. It was just natural, given that both NeoVim and OSC-52 are
essential part of my daily workflow.&lt;/p&gt;
&lt;p&gt;For those unfamiliar, OSC stands for &lt;em&gt;Operating System Command&lt;/em&gt;, and it's a set
of &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code"&gt;escape sequences&lt;/a&gt; originally &lt;a href="https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands"&gt;defined by Xterm&lt;/a&gt;, but now adopted by
various modern terminal emulators&lt;sup class="footnote-ref"&gt;&lt;a href="#fn1" id="fnref1"&gt;[1]&lt;/a&gt;&lt;/sup&gt;. &lt;em&gt;OSC-52&lt;/em&gt;, in particular, is an escape
sequence that allows &lt;em&gt;copying to&lt;/em&gt; and &lt;em&gt;pasting from&lt;/em&gt; the system clipboard.&lt;/p&gt;
&lt;p&gt;I have a few development environments running in virtual machines or
systemd-nspawn containers, and I typically SSH into them and run NeoVim from
within. Needless to say that a NeoVim instance running inside container has no
access to the system clipboard&lt;sup class="footnote-ref"&gt;&lt;a href="#fn2" id="fnref2"&gt;[2]&lt;/a&gt;&lt;/sup&gt;, and this is where OSC-52 comes to the
rescue!&lt;/p&gt;
&lt;!-- Generated by graphviz version 16.1.0 (0)
 --&gt;
&lt;!-- Title: G Pages: 1 --&gt;
&lt;svg width="296pt" height="140pt"
 viewBox="0.00 0.00 296.00 140.00"&gt;
&lt;g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 135.5)"&gt;
&lt;title&gt;G&lt;/title&gt;
&lt;polygon fill="white" stroke="none" points="-4,4 -4,-135.5 292,-135.5 292,4 -4,4"/&gt;
&lt;g id="clust1" class="cluster"&gt;
&lt;title&gt;cluster_Host&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="8,-8 8,-123.5 280,-123.5 280,-8 8,-8"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="144" y="-106.2" font-family="Times,serif" font-size="14.00"&gt;Host&lt;/text&gt;
&lt;/g&gt;
&lt;g id="clust2" class="cluster"&gt;
&lt;title&gt;cluster_VM&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="16,-16 16,-91.75 96,-91.75 96,-16 16,-16"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-74.45" font-family="Times,serif" font-size="14.00"&gt;VM&lt;/text&gt;
&lt;/g&gt;
&lt;g id="clust3" class="cluster"&gt;
&lt;title&gt;cluster_Container1&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="104,-16 104,-91.75 184,-91.75 184,-16 104,-16"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="144" y="-74.45" font-family="Times,serif" font-size="14.00"&gt;Container&lt;/text&gt;
&lt;/g&gt;
&lt;g id="clust4" class="cluster"&gt;
&lt;title&gt;cluster_Container2&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="192,-16 192,-91.75 272,-91.75 272,-16 192,-16"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="232" y="-74.45" font-family="Times,serif" font-size="14.00"&gt;Container&lt;/text&gt;
&lt;/g&gt;
&lt;!-- neovim1 --&gt;
&lt;g id="node1" class="node"&gt;
&lt;title&gt;neovim1&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="88,-60 24,-60 24,-24 88,-24 88,-60"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-36.58" font-family="Times,serif" font-size="14.00"&gt;NeoVim&lt;/text&gt;
&lt;/g&gt;
&lt;!-- neovim2 --&gt;
&lt;g id="node2" class="node"&gt;
&lt;title&gt;neovim2&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="176,-60 112,-60 112,-24 176,-24 176,-60"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="144" y="-36.58" font-family="Times,serif" font-size="14.00"&gt;NeoVim&lt;/text&gt;
&lt;/g&gt;
&lt;!-- neovim3 --&gt;
&lt;g id="node3" class="node"&gt;
&lt;title&gt;neovim3&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="264,-60 200,-60 200,-24 264,-24 264,-60"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="232" y="-36.58" font-family="Times,serif" font-size="14.00"&gt;NeoVim&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;p&gt;If your terminal emulator supports it, NeoVim's built-in OSC-52 clipboard
provider just works! Here's the thing though: if you're a happy tmux user,
things might not go the way you expect them to go. I quickly noticed a peculiar
behavior that I initially mistook for a bug: OSC-52 pasting didn't paste the
content of the system clipboard, but rather the content of tmux top buffer.&lt;/p&gt;
&lt;p&gt;Despite being called a multiplexer, tmux acts as a terminal emulator, which
means it implements many OSC sequences itself, including OSC-52. When it comes
to copying, tmux saves captured text to its buffer and passes the escape
sequence up to the parent terminal to set the system clipboard. Pasting, on the
other hand, always retrieves a content from a tmux buffer, forms a response and
passes it up to the parent terminal for pasting. The content of the buffer may
be a text you previously copied (and hence implicitly saved) or a text you
explicitly saved to a buffer.&lt;/p&gt;
&lt;p&gt;What does this mean practically? It means that a text you copy in NeoVim can be
pasted in a browser. However, a text that you copied in a browser cannot be
pasted to NeoVim. Lame, huh?&lt;/p&gt;
&lt;p&gt;Unfortunately, this is intentional behavior because tmux supports multiple
clients that may be attached from multiple hosts&lt;sup class="footnote-ref"&gt;&lt;a href="#fn3" id="fnref3"&gt;[3]&lt;/a&gt;&lt;/sup&gt;. It's a no-brainer with
copying: we just send a text we want to copy to all clients and let their
terminal emulators to set the system clipboard. But what should we do with
pasting? What attached client should be used as a clipboard source?&lt;/p&gt;
&lt;!-- Generated by graphviz version 16.1.0 (0)
 --&gt;
&lt;!-- Title: G Pages: 1 --&gt;
&lt;svg width="222pt" height="244pt"
 viewBox="0.00 0.00 222.00 244.00"&gt;
&lt;g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 239.75)"&gt;
&lt;title&gt;G&lt;/title&gt;
&lt;polygon fill="white" stroke="none" points="-4,4 -4,-239.75 218,-239.75 218,4 -4,4"/&gt;
&lt;g id="clust1" class="cluster"&gt;
&lt;title&gt;cluster_Host1&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="8,-8 8,-227.75 104,-227.75 104,-8 8,-8"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-210.45" font-family="Times,serif" font-size="14.00"&gt;Host A&lt;/text&gt;
&lt;/g&gt;
&lt;g id="clust2" class="cluster"&gt;
&lt;title&gt;cluster_Host2&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" stroke-dasharray="5,2" points="112,-8 112,-227.75 206,-227.75 206,-8 112,-8"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="159" y="-210.45" font-family="Times,serif" font-size="14.00"&gt;Host B&lt;/text&gt;
&lt;/g&gt;
&lt;!-- clipboard1 --&gt;
&lt;g id="node1" class="node"&gt;
&lt;title&gt;clipboard1&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="89.5,-196 22.5,-196 22.5,-160 89.5,-160 89.5,-196"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-172.57" font-family="Times,serif" font-size="14.00"&gt;clipboard&lt;/text&gt;
&lt;/g&gt;
&lt;!-- tmux_server --&gt;
&lt;g id="node2" class="node"&gt;
&lt;title&gt;tmux_server&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="95.88,-124 16.12,-124 16.12,-88 95.88,-88 95.88,-124"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-100.58" font-family="Times,serif" font-size="14.00"&gt;tmux server&lt;/text&gt;
&lt;/g&gt;
&lt;!-- clipboard1&amp;#45;&amp;gt;tmux_server --&gt;
&lt;!-- tmux_client1 --&gt;
&lt;g id="node3" class="node"&gt;
&lt;title&gt;tmux_client1&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="94.75,-52 17.25,-52 17.25,-16 94.75,-16 94.75,-52"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="56" y="-28.57" font-family="Times,serif" font-size="14.00"&gt;tmux client&lt;/text&gt;
&lt;/g&gt;
&lt;!-- tmux_server&amp;#45;&amp;gt;tmux_client1 --&gt;
&lt;g id="edge3" class="edge"&gt;
&lt;title&gt;tmux_server&amp;#45;&amp;gt;tmux_client1&lt;/title&gt;
&lt;path fill="none" stroke="black" d="M56,-76.24C56,-68.04 56,-59.37 56,-52.1"/&gt;
&lt;polygon fill="black" stroke="black" points="52.5,-76.18 56,-86.18 59.5,-76.18 52.5,-76.18"/&gt;
&lt;/g&gt;
&lt;!-- tmux_client2 --&gt;
&lt;g id="node5" class="node"&gt;
&lt;title&gt;tmux_client2&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="197.75,-52 120.25,-52 120.25,-16 197.75,-16 197.75,-52"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="159" y="-28.57" font-family="Times,serif" font-size="14.00"&gt;tmux client&lt;/text&gt;
&lt;/g&gt;
&lt;!-- tmux_server&amp;#45;&amp;gt;tmux_client2 --&gt;
&lt;g id="edge4" class="edge"&gt;
&lt;title&gt;tmux_server&amp;#45;&amp;gt;tmux_client2&lt;/title&gt;
&lt;path fill="none" stroke="black" d="M91.05,-81.18C105.01,-71.69 120.77,-60.98 133.6,-52.26"/&gt;
&lt;polygon fill="black" stroke="black" points="89.28,-78.15 82.98,-86.67 93.22,-83.94 89.28,-78.15"/&gt;
&lt;/g&gt;
&lt;!-- clipboard2 --&gt;
&lt;g id="node4" class="node"&gt;
&lt;title&gt;clipboard2&lt;/title&gt;
&lt;polygon fill="none" stroke="#2e3440" points="192.5,-196 125.5,-196 125.5,-160 192.5,-160 192.5,-196"/&gt;
&lt;text xml:space="preserve" text-anchor="middle" x="159" y="-172.57" font-family="Times,serif" font-size="14.00"&gt;clipboard&lt;/text&gt;
&lt;/g&gt;
&lt;!-- clipboard2&amp;#45;&amp;gt;tmux_client2 --&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;p&gt;Fortunately, tmux gives us a command, &lt;code&gt;refresh-client -l&lt;/code&gt;, that can be executed
by any client to sync the content of the system clipboard to a tmux buffer,
that can subsequently be used to paste the content via OSC-52.&lt;/p&gt;
&lt;p&gt;I decided to share this information, as I spent at least an hour running tmux
under gdb, chasing a ghost that doesn't exist. I don't want anyone to repeat my
experience.&lt;/p&gt;
&lt;hr class="footnotes-sep" /&gt;
&lt;section class="footnotes"&gt;
&lt;ol class="footnotes-list"&gt;
&lt;li id="fn1" class="footnote-item"&gt;&lt;p&gt;OSC-52 is at least supported by alacritty, contour, foot, hterm, iterm2,
kitty, rxvt, st, tmux, wezterm, windows terminal and zelij. &lt;a href="#fnref1" class="footnote-backref"&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn2" class="footnote-item"&gt;&lt;p&gt;In my case, VMs and containers have no access to the wayland socket, and
I don't want them to. &lt;a href="#fnref2" class="footnote-backref"&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn3" class="footnote-item"&gt;&lt;p&gt;If I understand maintainer's &lt;a href="https://github.com/tmux/tmux/issues/1477#issuecomment-421344891"&gt;comment&lt;/a&gt; correctly. &lt;a href="#fnref3" class="footnote-backref"&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
    <link href="https://kalnytskyi.com/posts/on-tmux-osc52-support/" rel="alternate"/>
    <published>2023-11-14T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/setup-wireguard-client-systemd-networkd/</id>
    <title>Setup a WireGuard client using systemd-networkd</title>
    <updated>2026-10-08T22:17:30.143974+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;div class="note"&gt;
&lt;p&gt;Please check out &lt;a href="/posts/setup-wireguard-systemd-networkd/"&gt;«Setup a WireGuard server using systemd-networkd»&lt;/a&gt;
to learn more about WireGuard in general and network topology outlined in this
post.&lt;/p&gt;
&lt;p&gt;On Feb 24, 2022, Russia began a full scale invasion of Ukraine. The terror they
brought to my country is devastating, and so publication of this post has been
postponed till better times. Only now I finally found some strength inside to
finish and publish this writing.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;WireGuard is a great VPN choice for your home, and maybe even for your company.
It's simple, fast, built into Linux kernel 5.6 and above, and can be configured
via &lt;a href="https://man.archlinux.org/man/systemd-networkd.8.en"&gt;systemd-networkd&lt;/a&gt; in no time. The systemd suite supports WireGuard
starting with v237 and is most likely installed on your Linux machine.&lt;/p&gt;
&lt;p&gt;Let us try to setup a wireguard 'client' for the following VPN network:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align:left"&gt;Option&lt;/th&gt;
&lt;th style="text-align:left"&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Network&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.0/24&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Server&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Client&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.20&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;First thing to do is to set up a virtual network device for a WireGuard tunnel.
This can be achieved by means of a &lt;a href="https://man.archlinux.org/man/systemd.netdev.5"&gt;systemd.netdev(5)&lt;/a&gt; unit
that must be created in &lt;code&gt;/etc/systemd/network/&lt;/code&gt; directory. The WireGuard
network device must know about number of things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The private key of the peer.&lt;/li&gt;
&lt;li&gt;The endpoint of the 'server' peer and its public key.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Unlike 'server', a 'client' peer doesn't require port configuration because
it's the 'client' that initiates connection not the other way round. This is
how some &lt;code&gt;/etc/systemd/network/wg0.netdev&lt;/code&gt; could look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-ini"&gt;&lt;span class="k"&gt;[NetDev]&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0&lt;/span&gt;
&lt;span class="na"&gt;Kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wireguard&lt;/span&gt;
&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0 - wireguard tunnel&lt;/span&gt;

&lt;span class="k"&gt;[WireGuard]&lt;/span&gt;
&lt;span class="na"&gt;PrivateKeyFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/etc/systemd/network/wg0.key&lt;/span&gt;
&lt;span class="na"&gt;FirewallMark&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;0x8888&lt;/span&gt;

&lt;span class="k"&gt;[WireGuardPeer]&lt;/span&gt;
&lt;span class="na"&gt;PublicKey&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;TPouHhVqvgAMfa9mNwZOh59kifUAsdn9Vtgsj2IsKVU=&lt;/span&gt;
&lt;span class="na"&gt;AllowedIPs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;0.0.0.0/0&lt;/span&gt;
&lt;span class="na"&gt;Endpoint&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;vpn.example.com:51820&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The content of &lt;code&gt;/etc/systemd/network/wg0.key&lt;/code&gt; can be generated by invoking
&lt;code&gt;$ wg genkey&lt;/code&gt; command and must be readable by the &lt;code&gt;systemd-network&lt;/code&gt; user.&lt;/p&gt;
&lt;p&gt;There are couple of things to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;FirewallMark&lt;/code&gt; option takes a number and is used to mark outgoing
WireGuard packets. Please remember that mark, it will be later used for
network configuration.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;AllowedIPs&lt;/code&gt; option is set to &lt;code&gt;0.0.0.0/0&lt;/code&gt; because we're interested to
route all the traffic via the tunnel, i.e. surfing the Internet via VPN.
If your plans for the VPN is to connect multiple devices into a single
network, you should go with the network address only, e.g. &lt;code&gt;10.0.0.0/24&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;Endpoint&lt;/code&gt; option takes a wireguard 'server' IP address or hostname,
followed by a colon, and then a port the 'server' accepts connection on. The
endpoint must be reachable from 'client' peers even when VPN is down.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Next thing to do is to use a &lt;a href="https://man.archlinux.org/man/systemd.network.5"&gt;systemd.network(5)&lt;/a&gt; unit to
setup a network. The purpose of the network is to assign a proper IP address on
the network device, set proper routes and so on.&lt;/p&gt;
&lt;p&gt;This how some &lt;code&gt;/etc/systemd/network/wg0.network&lt;/code&gt; could look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-ini"&gt;&lt;span class="k"&gt;[Match]&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0&lt;/span&gt;

&lt;span class="k"&gt;[Network]&lt;/span&gt;
&lt;span class="na"&gt;Address&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10.0.0.20/24&lt;/span&gt;
&lt;span class="na"&gt;DNSDefaultRoute&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;DNS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;1.1.1.1&lt;/span&gt;

&lt;span class="k"&gt;[Link]&lt;/span&gt;
&lt;span class="na"&gt;ActivationPolicy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;manual&lt;/span&gt;

&lt;span class="k"&gt;[RoutingPolicyRule]&lt;/span&gt;
&lt;span class="na"&gt;FirewallMark&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;0x8888&lt;/span&gt;
&lt;span class="na"&gt;InvertRule&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;Table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;1000&lt;/span&gt;
&lt;span class="na"&gt;Priority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;[Route]&lt;/span&gt;
&lt;span class="na"&gt;Gateway&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10.0.0.1&lt;/span&gt;
&lt;span class="na"&gt;GatewayOnLink&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;Table&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a bunch of important stuff going on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Since &lt;code&gt;/24&lt;/code&gt; network mask is used, systemd-networkd will automatically add a
route for the whole network to be routed via the WireGuard tunnel. Without
that mask, it'd be up to a user to properly configure routing on the system.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;With &lt;code&gt;ActivationPolicy&lt;/code&gt; set to &lt;code&gt;manual&lt;/code&gt;, the VPN is not brought up on system
boot, and requires manual activation via &lt;code&gt;$ networkctl up wg0&lt;/code&gt;. One can use
the &lt;code&gt;up&lt;/code&gt; value to always activate VPN on system boot.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Both &lt;code&gt;RoutingPolicyRule&lt;/code&gt; and &lt;code&gt;Route&lt;/code&gt; come together and are only required if
you want to route all the traffic over the tunnel. The &lt;code&gt;Route&lt;/code&gt; section
essentially creates a default route that routes packets to the wireguard
server. The &lt;code&gt;RoutingPolicyRule&lt;/code&gt; section, on the other hand, says that this
new default route should be applied only for the packets not coming from the
wireguard network device (i.e. not being marked with a firewall mark).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When VPN is used as a gateway for the Internet, it's quite common to set
some non default DNS servers to resolve domain names. It could be your own
VPN server, or some third-party provider such as Cloudflare. In order to set
custom DNS servers when VPN connection is up, one can use &lt;code&gt;DNSDefaultRoute&lt;/code&gt;
and &lt;code&gt;DNS&lt;/code&gt; options. The former tells the system to use the latter to resolve
all domain names.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When both the network device and the network are configured, the only remained
step is to run &lt;code&gt;$ networkctl reload&lt;/code&gt; to pipe in and apply latest configuration
followed by &lt;code&gt;$networkctl up wg0&lt;/code&gt; to bring the VPN up.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/setup-wireguard-client-systemd-networkd/" rel="alternate"/>
    <published>2022-09-03T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/setup-wireguard-systemd-networkd/</id>
    <title>Setup a WireGuard server using systemd-networkd</title>
    <updated>2026-10-08T22:17:30.144126+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;div class="note"&gt;
&lt;p&gt;Please check out &lt;a href="/posts/setup-wireguard-client-systemd-networkd/"&gt;«Setup a WireGuard client using systemd-networkd»&lt;/a&gt;
to learn about client-side configuration of your Linux machine.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;WireGuard is an extremely simple, fast and modern VPN that is built into Linux
kernel 5.6 (released on Mar 29, 2020) and above. It mimics the model of SSH and
requires VPN peers to know each others public keys. I highly recommend to read
&lt;a href="https://www.wireguard.com"&gt;https://www.wireguard.com&lt;/a&gt; regardless of this post.&lt;/p&gt;
&lt;p&gt;When it comes to WireGuard, there's one interesting aspect: it has no notion of
'server', it's distributed by design and no blockchain is involved. Blockchain
enthusiasts may found this surprising but distributed software were invented
long before the blockchain 😅. Each peer may connect to other peers assuming
they know each other (i.e. public key, IP) and there's a connectivity between
them. The 'server' is rather a behaviour one may expect from a certain peer.
There are 3 key points that are normally expected from a 'server' peer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The peer is publicly available, so other peers may connect to it even when
behind SNAT.&lt;/li&gt;
&lt;li&gt;The peer can act as a proxy and connect other peers into a single network.&lt;/li&gt;
&lt;li&gt;The peer can act as a gateway to the Internet.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are number of ways to configure WireGuard on a Linux machine. My favorite
one is to use &lt;a href="https://man.archlinux.org/man/systemd-networkd.8.en"&gt;systemd-networkd&lt;/a&gt;, a system service that manages both networks
and network devices. It's distributed as part of systemd suite, so most likely
you have it installed, and it supports WireGuard starting with v237. It's a
good choice for a Linux server because it requires no extra software.&lt;/p&gt;
&lt;p&gt;For instance we want to setup the following VPN network:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align:left"&gt;Option&lt;/th&gt;
&lt;th style="text-align:left"&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Network&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.0/24&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Server&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Peer A&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.20&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align:left"&gt;Peer B&lt;/td&gt;
&lt;td style="text-align:left"&gt;&lt;code&gt;10.0.0.30&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;First thing to do is to set up a virtual network device for a WireGuard tunnel.
This can be achieved by means of a &lt;a href="https://man.archlinux.org/man/systemd.netdev.5"&gt;systemd.netdev(5)&lt;/a&gt; unit
that must be created in &lt;code&gt;/etc/systemd/network/&lt;/code&gt; directory. The WireGuard
network device must know about number of things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The port to accept new connections on.&lt;/li&gt;
&lt;li&gt;The private key of the server.&lt;/li&gt;
&lt;li&gt;The list of known peers and their public keys.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is how some &lt;code&gt;/etc/systemd/network/wg0.netdev&lt;/code&gt; could look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-ini"&gt;&lt;span class="k"&gt;[NetDev]&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0&lt;/span&gt;
&lt;span class="na"&gt;Kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wireguard&lt;/span&gt;
&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0 - wireguard tunnel&lt;/span&gt;

&lt;span class="k"&gt;[WireGuard]&lt;/span&gt;
&lt;span class="na"&gt;ListenPort&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;51820&lt;/span&gt;
&lt;span class="na"&gt;PrivateKeyFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/etc/systemd/network/wg0.key&lt;/span&gt;

&lt;span class="k"&gt;[WireGuardPeer]&lt;/span&gt;
&lt;span class="na"&gt;AllowedIPs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10.0.0.20/32&lt;/span&gt;
&lt;span class="na"&gt;PublicKey&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;9vzzasvYciJLmhjrt9Aj9aQYe1gnUxI44ShVLQPrDQA=&lt;/span&gt;

&lt;span class="k"&gt;[WireGuardPeer]&lt;/span&gt;
&lt;span class="na"&gt;AllowedIPs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10.0.0.30/32&lt;/span&gt;
&lt;span class="na"&gt;PublicKey&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;9vzzasvYciJLmhjrt9Aj9aQYe1gnUxI44ShVLQPrDQA=&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The content of &lt;code&gt;/etc/systemd/network/wg0.key&lt;/code&gt; can be generated by invoking
&lt;code&gt;$ wg genkey&lt;/code&gt; command and must be readable by the &lt;code&gt;systemd-network&lt;/code&gt; user.
What's notable here is that &lt;code&gt;$ wg genkey&lt;/code&gt; can be executed anywhere, even on
your laptop, there's no need to install extra software on the server.&lt;/p&gt;
&lt;p&gt;Next thing to do is to use a &lt;a href="https://man.archlinux.org/man/systemd.network.5"&gt;systemd.network(5)&lt;/a&gt; unit to
setup a network. The purpose of the network is to assign a proper IP address on
the network device, set proper routes and so on.&lt;/p&gt;
&lt;p&gt;This how some &lt;code&gt;/etc/systemd/network/wg0.network&lt;/code&gt; could look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-ini"&gt;&lt;span class="k"&gt;[Match]&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;wg0&lt;/span&gt;

&lt;span class="k"&gt;[Network]&lt;/span&gt;
&lt;span class="na"&gt;Address&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10.0.0.1/24&lt;/span&gt;
&lt;span class="na"&gt;IPMasquerade&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;both&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are couple of things to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Since &lt;code&gt;/24&lt;/code&gt; network mask is used, systemd-networkd will automatically add a
route for the whole network to be routed via the WireGuard tunnel. Without
that mask, it'd be up to a user to properly configure routing on the system.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;IPMasquerade&lt;/code&gt; setting is only needed if the server is expected to be
used as a gateway to the Internet. Without this option, it'd be up to a user
to properly configure the firewall.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When both the network device and the network are configured, the only remained
step is to run &lt;code&gt;$ networkctl reload&lt;/code&gt; to pipe in and apply latest configuration.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/setup-wireguard-systemd-networkd/" rel="alternate"/>
    <published>2022-02-07T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/setup-cors-caddy-2/</id>
    <title>Setup CORS in Caddy 2</title>
    <updated>2026-10-08T22:17:30.144251+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;&lt;a href="https://caddyserver.com/"&gt;Caddy 2&lt;/a&gt; is an open source web server with automatic HTTPS. It's a wise choice
for pet projects or self-hosted services, since you are free from managing TLS
certs on your own and wiring things up can be super annoying.&lt;/p&gt;
&lt;p&gt;One missing feature in Caddy 2, however, is cross-origin resource sharing
(&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;CORS&lt;/a&gt;) support. For a &amp;quot;batteries included&amp;quot; web server, it's rather
surprising. Fortunately, one can use the following &lt;a href="https://caddyserver.com/docs/caddyfile/concepts#snippets"&gt;Caddy snippet&lt;/a&gt; to augment
any site with CORS headers without repeating oneself over and over again.&lt;/p&gt;
&lt;div class="note"&gt;
&lt;p&gt;You might want to update the list of headers returned by
&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt; or &lt;code&gt;Access-Control-Expose-Headers&lt;/code&gt; HTTP headers
according to your application needs. Please refer to the CORS documentation to
learn more what they are about.&lt;/p&gt;
&lt;/div&gt;
&lt;pre&gt;&lt;code&gt;(cors) {
  @cors_preflight method OPTIONS
  @cors header Origin {args.0}

  handle @cors_preflight {
    header Access-Control-Allow-Origin "{args.0}"
    header Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE"
    header Access-Control-Allow-Headers "Content-Type"
    header Access-Control-Max-Age "3600"
    respond "" 204
  }

  handle @cors {
    header Access-Control-Allow-Origin "{args.0}"
    header Access-Control-Expose-Headers "Link"
  }
}

example.com {
  import cors https://example.com
  reverse_proxy localhost:8080
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The nice part about this snippet is that CORS headers are only returned for
HTTP requests with the &lt;code&gt;Origin&lt;/code&gt; HTTP header. That header is normally used by
browsers only, which means you won't see CORS headers in responses for requests
sent by &lt;code&gt;curl&lt;/code&gt; or &lt;em&gt;your-programming-language-of-choice&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I've been successfully using this snippet for quite a while now &lt;a href="https://github.com/xsnippet/xsnippet-infra/blob/1d583a6868597cb71bb2ae08f60bc42ac4364e91/roles/xsnippet_api/templates/caddy.j2#L1-L17"&gt;to protect&lt;/a&gt;
&lt;a href="https://api.xsnippet.org"&gt;api.xsnippet.org&lt;/a&gt;, so it can be accessed by &lt;a href="https://xsnippet.org"&gt;xsnippet.org&lt;/a&gt;.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/setup-cors-caddy-2/" rel="alternate"/>
    <published>2022-01-12T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/setup-postgres-linux-windows-macos-gh-actions/</id>
    <title>Setup PostgreSQL for Linux, Windows and macOS using GitHub Actions</title>
    <updated>2026-10-08T22:17:30.144376+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;GitHub Actions is a CI/CD platform that is widely used among open-source
software hosted on GitHub. If you happened to host your software there, you may
end up needing a SQL server to test your application. PostgreSQL is the most
common choice nowadays.&lt;/p&gt;
&lt;p&gt;As of today (Nov 24, 2021), there's only one &lt;a href="https://github.com/marketplace/actions/setup-postgresql-for-linux-macos-windows"&gt;action on the marketplace&lt;/a&gt; to
setup a PostgreSQL server for Linux, Windows and macOS action runners. If you
among those who want to test their software on all major platforms, you have no
option but to use &lt;a href="https://github.com/ikalnytskyi/action-setup-postgres"&gt;ikalnytskyi/action-setup-postgres&lt;/a&gt;. Below is the typical
usage example:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-yaml"&gt;&lt;span class="nt"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p p-Indicator"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;Setup PostgreSQL&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;ikalnytskyi/action-setup-postgres@v4&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;postgres&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p p-Indicator"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;Run tests using connection URI&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;CONNECTION_STR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;${{ steps.postgres.outputs.connection-uri }}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;pytest -vv tests/&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p p-Indicator"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;Run tests using connection kv-string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;CONNECTION_STR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;service=${{ steps.postgres.outputs.service-name }}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;pytest -vv tests/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So why use that exact action and no other?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Runs on Linux, macOS and Windows action runners.&lt;/li&gt;
&lt;li&gt;Adds PostgreSQL &lt;a href="https://www.postgresql.org/docs/current/reference-client.html"&gt;client applications&lt;/a&gt; to &lt;code&gt;PATH&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Uses PostgreSQL binaries baked into &lt;a href="https://github.com/actions/runner-images"&gt;GitHub Actions Runner Images&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Easy &lt;a href="https://github.com/ikalnytskyi/action-setup-postgres/blob/v4/action.yml"&gt;to prove&lt;/a&gt; that it DOES NOT contain malicious code.&lt;/li&gt;
&lt;/ul&gt;
</content>
    <link href="https://kalnytskyi.com/posts/setup-postgres-linux-windows-macos-gh-actions/" rel="alternate"/>
    <published>2021-11-24T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/gnome-terminal-csd/</id>
    <title>Enable CSD in GNOME Terminal</title>
    <updated>2026-10-08T22:17:30.144502+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;Client-side decoration (&lt;a href="https://wiki.gnome.org/Initiatives/CSD"&gt;CSD&lt;/a&gt;) is the concept of allowing a graphical
application software to be responsible for drawing its own window decorations,
historically the responsibility of the window manager. GNOME applications is
slowly migrating to client-side decoration. While some applications use CSD by
default, others draw them only while running in GNOME session.&lt;/p&gt;
&lt;p&gt;&lt;img src="headerbar.png" alt="gnome-terminal with csd" /&gt;&lt;/p&gt;
&lt;p&gt;Unfortunately, CSD is not used by GNOME Terminal if you launch it outside of
GNOME session. Fortunately, there's a configuration option you can use to
explicitly enable it. In order to do that just run the following command in
your terminal:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;$&lt;span class="w"&gt; &lt;/span&gt;gsettings&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;org.gnome.Terminal.Legacy.Settings&lt;span class="w"&gt; &lt;/span&gt;headerbar&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class="note"&gt;
&lt;p&gt;The setting won't be applied as long as there's a running copy of
&lt;code&gt;gnome-terminal-server&lt;/code&gt; process. You have to terminate it and restart the
terminal.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The configuration option is tested with &lt;code&gt;gnome-terminal 3.40&lt;/code&gt;. It might or
might not work with other versions.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/gnome-terminal-csd/" rel="alternate"/>
    <published>2021-10-24T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/assert-str-matches-regex-in-pytest/</id>
    <title>Assert that str matches regex in pytest</title>
    <updated>2026-10-08T22:17:30.144634+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;Once in a while every Pythonista faces a need to test that some string value
matches a regex pattern. When it comes to it we have no option but to use &lt;code&gt;re&lt;/code&gt;
module directly.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;re&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_something_very_useful&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_some_string&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\d+"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While it works perfectly fine in case of a lone string, it's not always
convenient to do the same when a string is a part of more complex data
structure (e.g. &lt;code&gt;dict&lt;/code&gt;) because you need to extract the string, check it and
only then get back to check rest attributes.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;re&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_something_even_more_useful&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_some_structure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# inconvenient and boring! :(&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s2"&gt;"value"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\d+"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# rest assertion&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"foo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fortunately, it's pretty easy to write a convenient helper that would check a
string against some pattern when comparing complex data structures.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_something_even_more_useful&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_some_structure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;mapping&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"foo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;pytest_regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\d+"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Looks better, huh? :) The trick can be achieved by implementing a class wrapper
that implements both &lt;code&gt;__eq__&lt;/code&gt; and &lt;code&gt;__repr__&lt;/code&gt; dunder methods. The first method
is used to implement matching while the other – to return the value that will
be shown in error message in case of errors.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;re&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;pytest_regex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""Assert that a given string meets some expectations."""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__eq__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_regex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_regex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, actually, it's not by any means tied to &lt;code&gt;pytest&lt;/code&gt; and could be used even
in non-test production code, though I found this rather queer.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/assert-str-matches-regex-in-pytest/" rel="alternate"/>
    <published>2018-04-30T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/farewell-2015/</id>
    <title>Farewell, 2015!</title>
    <updated>2026-10-08T22:17:30.147884+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;I'm a little late to sum up, but better now than never. :) Well, 2015 was a
year of travels. I've never visited before so many places in one year, just
look at the record:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Istanbul, Turkey&lt;/li&gt;
&lt;li&gt;Minsk, Belarus&lt;/li&gt;
&lt;li&gt;Zürich, Switzerland&lt;/li&gt;
&lt;li&gt;Barcelona, Spain&lt;/li&gt;
&lt;li&gt;Toronto, Canada&lt;/li&gt;
&lt;li&gt;Niagara Falls, Canada&lt;/li&gt;
&lt;li&gt;Silicon Valley, USA&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I'm so excited about them, and looking forward to have even more in 2016!&lt;/p&gt;
&lt;p&gt;As for my programming life, nothing special has happened except, perhaps,
that I started doing less coding that I'd want to. You see, in the end of
fall two events took place:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I've been elected for Fuel-Python component lead position.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wiki.openstack.org/wiki/Fuel"&gt;Fuel&lt;/a&gt; has joined OpenStack Big Tent umbrella.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So now my main focus is to make design decisions and choose direction to go.
I barely have time for code review, but I hope it'll be resolved soon.&lt;/p&gt;
&lt;p&gt;Besides that, &lt;a href="https://holocron.readthedocs.org"&gt;Holocron&lt;/a&gt; - an extendable static site generator behind this
blog - was finally released. Thanks a lot to my friend &lt;a href="https://www.facebook.com/andrii.gamaiunov"&gt;Andrii Gamaiunov&lt;/a&gt;
for putting effort to make this happen. I'm glad to see how it's going despite
lack of time, though there're few major things to be done before I can say
&lt;em&gt;«It's 1.0 release»&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Summarizing: 2015 was a good year, and I expect 2016 to be at least as good
as 2015 was, but hope it will be even better!&lt;/p&gt;
&lt;p&gt;Happy Holidays!&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/farewell-2015/" rel="alternate"/>
    <published>2016-01-02T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/mad-map/</id>
    <title>Mad Map: Python Road</title>
    <updated>2026-10-08T22:17:30.148042+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;I've been using Python for several years and I used to think there was nothing
in the language that can surprise me. It was so until recently. The story I'm
about to tell is about how even so simple and well-known thing like &lt;code&gt;map()&lt;/code&gt;
function can surprise you after years of using, and what led me to believe
that it's poorly designed.&lt;/p&gt;
&lt;p&gt;So, what's wrong with &lt;code&gt;map()&lt;/code&gt;? We use it everyday and everywhere, and it seems
OK, and none of us have met any problems. Yes, indeed, &lt;code&gt;map()&lt;/code&gt; works just fine
if you use it how it's intended to be used. For instance, if you use &lt;code&gt;map()&lt;/code&gt;
to multiply each element of some sequence by 2 -&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just keep going, everything's ok. But let me show another usage example -&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Can you tell now what is &lt;code&gt;x&lt;/code&gt;, ha? I always thought that &lt;code&gt;map()&lt;/code&gt; function
receives precisely two arguments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a function to apply;&lt;/li&gt;
&lt;li&gt;a sequence to be processed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Can you imagine how surprised I was when it turned out that I was wrong?
Python has always been about readability, and even if you don't know the
language you can pretty much guess most of its parts intuitively. So I started
thinking what could possible be returned in this case? What &lt;code&gt;None&lt;/code&gt; may mean in
this context? I recalled that if &lt;code&gt;None&lt;/code&gt; is passed as a function argument to
&lt;code&gt;filter()&lt;/code&gt; then only &lt;em&gt;truthy&lt;/em&gt; elements are returned. Hm, maybe &lt;code&gt;map()&lt;/code&gt; behaves
similar to &lt;code&gt;filter()&lt;/code&gt; and returns a list of booleans? Let's check.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No, it's not :( So I went to the documentation and learned that if a function
argument is &lt;code&gt;None&lt;/code&gt;, the identity function is assumed. In other words it's
equal to the following call -&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wait a minute. &lt;code&gt;None&lt;/code&gt; means identity?! Does it make sense? Perhaps if we're
talking about default behaviour, but it makes no sense from a user point of
view. You know, it looks like &amp;quot;pass &lt;code&gt;None&lt;/code&gt; if you want to get identity
function behaviour&amp;quot;. But let's go on.&lt;/p&gt;
&lt;p&gt;I continued guessing about how &lt;code&gt;map()&lt;/code&gt; should deal with two iterables? It was
obvious to me that &lt;code&gt;map()&lt;/code&gt; should chain them: when first is over, the second
will be used.&lt;/p&gt;
&lt;p&gt;Let's take a loot at the original snippet one more time.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What is &lt;code&gt;x&lt;/code&gt;? Taking into account that was written above I was expecting &lt;code&gt;x&lt;/code&gt; to
be -&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Boom! I missed again, because &lt;code&gt;x&lt;/code&gt; was -&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-python"&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I stunned again because that means these two iterables were zipped, not
chained. Why zipped? I never asked for this! Is this really Python? I always
liked the language because of good design and good intentions. And when I
didn't know something it always fitted my expectations, but this was something
really weird.&lt;/p&gt;
&lt;p&gt;I agree that someone may expect iterables to be zipped, but I'm so much
disappointed that that one may stuck upon expression that could not be
correctly guessed. I wish &lt;code&gt;map()&lt;/code&gt; to be dead simple: receive one function and
one iterable, and leave zipping or chaining up to programmers. Moreover, I
wish a function argument to be always a function, no way to fallback to
default behaviour. If someone wants identity behaviour let's pass it
explicitly, no way to do it through &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The most frustrating thing is that it hasn't been removed in Python 3, but
changed! First, you can't use &lt;code&gt;None&lt;/code&gt; as identity function anymore (alleluia).
If you try to do that you'll fall with &lt;code&gt;TypeError&lt;/code&gt; exception:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;TypeError: 'NoneType' object is not callable
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Second, iterables are zipped by &lt;code&gt;zip()&lt;/code&gt;, not &lt;code&gt;zip_longest()&lt;/code&gt;. If the first
change seems rational, the second one is mad. It doesn't provide any benefits,
but may cause a lot of pain for those who porting some software to Python.
Why? You see, if two iterables are equal in size the behavior is still the
same -&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;language / expression&lt;/th&gt;
&lt;th&gt;&lt;code&gt;map(lambda x: x, ['a', 'b'], [1, 2])&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Python 2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[('a', 1), ('b', 2)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python 3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[('a', 1), ('b', 2)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;if not, it's different -&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;language / expression&lt;/th&gt;
&lt;th&gt;&lt;code&gt;map(lambda x: x, ['a', 'b', 'c'], [1, 2])&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Python 2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[('a', 1), ('b', 2), ('c', None)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python 3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[('a', 1), ('b', 2)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I'm really really sad about such a mad &lt;code&gt;map()&lt;/code&gt; and I wish I never knew about
that. I don't know why it was designed so, but I hope none of pythonistas will
use it and one day it will be removed from the language.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/mad-map/" rel="alternate"/>
    <published>2015-06-14T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://kalnytskyi.com/posts/docker-for-pythonistas/</id>
    <title>Docker For Pythonistas</title>
    <updated>2026-10-08T22:17:30.148163+00:00</updated>
    <author>
      <name>Ihor Kalnytskyi</name>
    </author>
    <content type="html">&lt;p&gt;&lt;a href="https://www.docker.com"&gt;Docker&lt;/a&gt;'s popularity is growing day by day and every last man wrote about
it. Well, I'm a slowpoke... so sorry if I wrote something you already know.
Anyway, if you're already familiar with Docker I'm not sure if that makes
sense for you to waste time and read this post; otherwise - you're welcome.&lt;/p&gt;
&lt;p&gt;My acquaintance with Docker began almost a year ago, when we decided to use
it as an upgrade mechanism in &lt;a href="https://wiki.openstack.org/wiki/Fuel"&gt;Fuel for OpenStack&lt;/a&gt;. For sure I learned a lot
about Docker since that time: I know about its limitations, pitfalls and
bugs; I even know how to run the latest version (v1.4.1) on CentOS 6.5 with
old Linux kernel.&lt;/p&gt;
&lt;p&gt;In this post I'm not going to tell you about all possible use cases for
Docker or about its internals and limitations. Instead, I'll try to focus on
the daily usage flow and why I found it useful for myself and why it might
be useful for Pythonistas in general.&lt;/p&gt;
&lt;p&gt;I’ll try to be straight to the point, but before we go further let's answer
the following questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is the most common &amp;quot;action&amp;quot; for programmers?&lt;/li&gt;
&lt;li&gt;What is the second most common &amp;quot;action&amp;quot; for programmers?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Well, obviously, an answer to the first question is &lt;em&gt;to write code&lt;/em&gt;.
Programmers are supposed to write code, they're doing it almost every day
and it takes a lot of their time.&lt;/p&gt;
&lt;p&gt;And what about the second question? The answer is &lt;em&gt;to run tests&lt;/em&gt; in order to
be sure that nothing is broken. Unfortunately, it may not be as easy as it
seems. You know, a lot of Python software is supposed to be run on various
Python interpreters: it may be a set of Py2 interpreters or even both Py2
and Py3 interpreters. So for proper testing the tests should be run against
a certain set of Python interpreters, but what to do if some of them are
unavailable for your Linux distributive?&lt;/p&gt;
&lt;p&gt;This is where Docker is going to help us.&lt;/p&gt;
&lt;p&gt;Docker is a platform for building, shipping and running containers. In other
words it allows you to prepare an image with some software, push it to
docker registry (sort of repository) and pull it wherever you want to run.&lt;/p&gt;
&lt;p&gt;I came to it when I needed to run tests against Python 2.6 and this
interpreter wasn't available for Debian Jessie. So I spent some time and
built a &lt;a href="https://github.com/ikalnitsky/pythonista"&gt;pythonista docker image&lt;/a&gt; that contained the most popular Python
interpreters. It's very convenient since anyone can pull this image and use
it to run tests. If you aren't satisfied by my image, you can always build
your own - it's not a challenge.&lt;/p&gt;
&lt;p&gt;Tests could be run in the container with just a single command, no
pre-configuration is required, but pull the image. The whole workflow could
look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;$&lt;span class="w"&gt; &lt;/span&gt;docker&lt;span class="w"&gt; &lt;/span&gt;pull&lt;span class="w"&gt; &lt;/span&gt;ikalnitsky/pythonista
$&lt;span class="w"&gt; &lt;/span&gt;docker&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;-v&lt;span class="w"&gt; &lt;/span&gt;/path/to/src/:/src&lt;span class="w"&gt; &lt;/span&gt;-w&lt;span class="w"&gt; &lt;/span&gt;/src&lt;span class="w"&gt; &lt;/span&gt;ikalnitsky/pythonista&lt;span class="w"&gt; &lt;/span&gt;tox
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For those who aren't familiar with Docker, the first command retrieves the
image from the Docker registry and the second one runs a container created
from it. The arguments are&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-v /path/to/src:/src&lt;/code&gt; - mounts &lt;code&gt;/path/to/src&lt;/code&gt; from your host machine
into &lt;code&gt;/src&lt;/code&gt; inside your container&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-w /src&lt;/code&gt; - change current working directory inside container to &lt;code&gt;/src&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ikalnitsky/pythonista&lt;/code&gt; - obviously, use this image for container&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tox&lt;/code&gt; - run tox (a test runner tool)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So why Docker? What does it bring to you? It saves your time and nerves
because you don't need to compile missed Python interpreters on every
machine you're working on. Still, this is one of possible Docker use cases,
so don't hesitate to find your own.&lt;/p&gt;
</content>
    <link href="https://kalnytskyi.com/posts/docker-for-pythonistas/" rel="alternate"/>
    <published>2015-02-03T00:00:00+00:00</published>
  </entry>
</feed>
