<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>usher.dev</title><description>Tom Usher is a Web Developer &amp; DevOps Engineer based in lovely North Wales, UK. This is his blog.</description><link>https://usher.dev/</link><item><title>Using Claude Code to Personalise the Xteink X4</title><link>https://usher.dev/posts/2026-02-18-using-claude-code-to-personalise-xteink-x4/</link><guid isPermaLink="true">https://usher.dev/posts/2026-02-18-using-claude-code-to-personalise-xteink-x4/</guid><pubDate>Wed, 18 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
I recently picked up the widely hyped &lt;a href=&quot;https://www.xteink.com/products/xteink-x4&quot;&gt;Xteink X4&lt;/a&gt; 
e-reader, in one of those moments where I can happily justify purchasing more tech in pursuit of improving my life.

&lt;p&gt;Of course I immediately installed the open-source &lt;a href=&quot;https://github.com/crosspoint-reader/crosspoint-reader&quot;&gt;Crosspoint Reader&lt;/a&gt; firmware on the device, which led to a novel realisation. (heh)&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I love reading on the X4 - it feels great to hold, the eInk screen is crisp and readable, and it fits in my pocket so I can pull it out rather than reaching for my phone.&lt;/p&gt;
&lt;p&gt;&amp;lt;Image
    src={import(&quot;./xteink-x4.jpg&quot;)}
    alt=&quot;Hand holding an Xteink X4 reader in front of a laptop screen showing Claude Code&quot;
/&amp;gt;&lt;/p&gt;
&lt;p&gt;I put it to sleep when it goes in my pocket, and wake it up when I want to read. That&apos;s all good, and stops me accidentally pressing buttons to switch pages in my pocket, but there is a noticeable (2-3 second) delay when waking the device up that adds a tiny bit of friction to an otherwise lovely process.&lt;/p&gt;
&lt;p&gt;Wondering whether there was an alternative, I searched the repo, issues and pull requests for any signs
of an alternative, maybe a button lock feature so I could keep the page I&apos;m reading but lock the buttons from being pressed in my pockets. No such luck.&lt;/p&gt;
&lt;p&gt;My C++ knowledge is rusty at best, and my time to learn a new codebase, even one as well organised as Crosspoint, is limited, so I was not jumping at the chance to contribute myself. However, having seen a number of pull requests on the repo that were already AI-coded, I thought I&apos;d try my chances with Claude Code.&lt;/p&gt;
&lt;p&gt;So I opened up Claude in plan mode (shift-tab twice) and gave it a very short prompt:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I want to add a &apos;button lock&apos; feature to this firmware (i.e. double tap power button to
lock buttons), how would I approach that?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Within moments it had a plan, which on review didn&apos;t touch as much code as I&apos;d expected it to. I let Claude do its thing and flashed the updated firmware to the device (&lt;code&gt;uvx --with intelhex platformio run --target upload&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;There were a few issues - the device didn&apos;t quite refresh properly after locking (a follow-up prompt resolved this), and there&apos;s no visual indicator that the buttons are locked (maybe something for another time), but it worked.&lt;/p&gt;
&lt;p&gt;I&apos;ve used Claude Code and other LLM-coding tools enough to not be super surprised that it was able to do this, but I was surprised that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I can own well-built minimal hardware that is great at its one job&lt;/li&gt;
&lt;li&gt;I can install open-source, community maintained firmware on the device&lt;/li&gt;
&lt;li&gt;I can customise that device to do what I want it to do, without necessarily knowing how the firmware is built and structured&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In a time where we don&apos;t own or have control over much of the tech in our lives, being able to feel like I can fully customise a device I own felt strangely liberating.&lt;/p&gt;
&lt;p&gt;Oh and maybe I&apos;ll tidy this change up and submit a pull request, if I can get over my hesistance to subject other people to vibe-coded PRs.&lt;/p&gt;
</content:encoded></item><item><title>AI Can&apos;t Replace Us - We Know Too Much</title><link>https://usher.dev/posts/2025-05-27-ai-cant-replace-us-we-know-too-much/</link><guid isPermaLink="true">https://usher.dev/posts/2025-05-27-ai-cant-replace-us-we-know-too-much/</guid><pubDate>Tue, 27 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
It&apos;s hard to avoid the refrain that AI is out for our jobs. That all developers will be replaced by LLMs and we&apos;ll all have to brush up on our prompt engineering or just become gardeners (tempting, to be honest...).

&lt;p&gt;But developers don&apos;t just write code, we also know how everything &lt;em&gt;actually&lt;/em&gt; works. Which systems are held together with wishes and crossed fingers. We&apos;re deeply embedded in how companies operate, and more often than not, we&apos;re what&apos;s keeping the lights on.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;The AI doesn&apos;t know:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which of the seven Google Docs actually describes the current infrastructure (and which one was just hopes &amp;amp; dreams).&lt;/li&gt;
&lt;li&gt;That we&apos;re halfway through migrating from Jira to Notion to Linear, and will probably back to Jira by the time that&apos;s done, just with more grey hair.&lt;/li&gt;
&lt;li&gt;Why you&apos;ve already ruled out that vendor and don&apos;t need another two hour rambling demo call and 5 day implementation consultation.&lt;/li&gt;
&lt;li&gt;Which Slack thread from six months ago contains the final decision about the logging library. (Not the one in the spec, or the pinned message - Brian didn&apos;t like that one.)&lt;/li&gt;
&lt;li&gt;That one server on a non-standard cloud provider, running a lonely cron job, is the only thing holding your data stack together.&lt;/li&gt;
&lt;li&gt;That restarting the prod server breaks the firewall config unless you remember to run &lt;code&gt;fixfirewall.sh&lt;/code&gt; and pray.&lt;/li&gt;
&lt;li&gt;That you have to divide that metric by two to get the &lt;em&gt;actual&lt;/em&gt; value. No one remembers why.&lt;/li&gt;
&lt;li&gt;How that one undocumented supplier API works, and why it only returns 14 pages of data unless you pass the &lt;code&gt;&quot;more&quot;&lt;/code&gt; flag.&lt;/li&gt;
&lt;li&gt;That one special customer with a custom setup, maintained in a fork of a fork in a separate GitHub org. Don&apos;t forget to update it.&lt;/li&gt;
&lt;li&gt;That the contractor who built the weird glue layer between two ancient vendor systems is gone. And so is their documentation. And maybe their LinkedIn.&lt;/li&gt;
&lt;li&gt;That orders over £100 get free shipping unless they contain an item in the Fresh Produce category, or involves anyone with the first name Angela.&lt;/li&gt;
&lt;li&gt;Why RG needs to FFL the TNO on Fridays or the RT will get PO&apos;d. No, the &lt;em&gt;other&lt;/em&gt; TNO.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;AI will give you a boost. It&apos;ll speed up development, often take entire problems off your hands, and point out awkward phrasing in my writing (which I may choose to ignore...). It&apos;s like pairing with an overconfident intern who read a blog post and now wants to rewrite everything in Rust.&lt;/p&gt;
&lt;p&gt;But developers aren&apos;t just coders. We&apos;re archives of institutional knowledge, tape dispensers holding together years of compromises, and the experience that holds the messy, mismatched parts of reality in place.&lt;/p&gt;
&lt;p&gt;If you&apos;re not already embedded in that mess - well, it might be time.&lt;/p&gt;
&lt;p&gt;Oh and yeah, sure - we should document this stuff. But let&apos;s be honest: we won&apos;t. Not properly.&lt;/p&gt;
&lt;p&gt;Which is exactly why we&apos;re sticking around.&lt;/p&gt;

I&apos;d love to hear your examples of that small bit of undocumented knowledge that holds everything together. &lt;a href=&quot;/contact&quot;&gt;Get in touch&lt;/a&gt; and let me know!
</content:encoded></item><item><title>Here&apos;s how I use LLMs to help me write code</title><link>https://usher.dev/posts/2025-03-11-using-llms-for-code/</link><guid isPermaLink="true">https://usher.dev/posts/2025-03-11-using-llms-for-code/</guid><pubDate>Tue, 11 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://simonwillison.net/2025/Mar/11/using-llms-for-code/&quot;&gt;The king of LLM blogging, Simon Willison, has a great overview on using LLMs to write code&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Using LLMs to write code is &lt;strong&gt;difficult&lt;/strong&gt; and &lt;strong&gt;unintuitive&lt;/strong&gt;. It takes significant effort to figure out the sharp and soft edges of using them in this way, and there’s precious little guidance to help people figure out how best to apply them.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Even if you&apos;re not sold on how LLMs can boost your development, I&apos;d highly recommend that learning to manipulate them to your benefit is a skill worth learning.&lt;/p&gt;
</content:encoded></item><item><title>Kill your Feeds - Stop letting algorithms dictate how you think</title><link>https://usher.dev/posts/2025-03-08-kill-your-feeds/</link><guid isPermaLink="true">https://usher.dev/posts/2025-03-08-kill-your-feeds/</guid><pubDate>Sat, 08 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
We are being boiled like frogs. It happened gradually, one algorithmic tweak at a time. What started as a way to connect with friends has become a system that gives the corporations that run social media control over what we consume and the ability to subtly shape how we think.
&lt;/div&gt;

&lt;p&gt;We used to control apps like Facebook and Instagram with our own choices. They became daily comforts, making the world seem a little bit smaller and closer by bringing the people that we cared about together in to one place.&lt;/p&gt;
&lt;p&gt;But from the perspective of these companies, that’s a problem. Our personal worlds, our friends, family, and connections, are finite. Once we’ve caught up, we put the app down. That’s bad for business.&lt;/p&gt;
&lt;p&gt;Social media companies need us flicking through their apps as long as they can keep us there. More eyes on ads is more money. So they play the system a bit. You&apos;ve lingered on enough photos of cute puppies, they know what you like.&lt;/p&gt;
&lt;p&gt;Before long those feeds of finite content are replaced by infinite algorithmic content pulled from millions of users trying to optimise their posts to be picked up by the omnipotent algorithms. Algorithms which are completely opaque to us.&lt;/p&gt;
&lt;p&gt;Sci-fi imagines megacorporations controlling our minds with brain implants. Some worry that companies are already listening in. But they don’t have to - they already control our eyes.&lt;/p&gt;
&lt;p&gt;The creators of TikTok, Instagram etc. have gained control over exactly what we see. What we see strongly influences how we think. &lt;a href=&quot;https://en.m.wikipedia.org/wiki/2021_Facebook_leak&quot;&gt;They know&lt;/a&gt; that their feeds make us angry, they know the negative effects on our mental health (particularly that of teens), and they know that they have an influence on our opinion.&lt;/p&gt;
&lt;p&gt;With the power to shape what we see comes the power to shape what we believe. Whether through deliberate manipulation or the slow creep of algorithmic recommendations, engagement is fueled by outrage, and outrage breeds extremism. The result is a feedback loop that isolates users, reinforces beliefs, and deprioritises opposing viewpoints.&lt;/p&gt;
&lt;p&gt;We live in times where being able to form our own opinion is more important than ever. Where knowing how to source and identify truthful information is a critical skill.&lt;/p&gt;
&lt;p&gt;Our reliance on being spoon fed ideas is destroying those abilities, Alec of Technology Connections calls this &lt;a href=&quot;https://youtu.be/QEJpZjg8GuA&quot;&gt;algorithmic complacency&lt;/a&gt;, referencing our increasing inability to look outside our algorithmically created bubble. The social media companies don&apos;t care, the only person who has any interest in fixing this is you.&lt;/p&gt;
&lt;h2&gt;Take the Power Back&lt;/h2&gt;
&lt;p&gt;It&apos;s time to take back control of how we think. We&apos;ve identified the problem, now it&apos;s time to take action.&lt;/p&gt;
&lt;p&gt;We don&apos;t all have the freedom, interest or willpower to delete social media from our lives entirely. It&apos;s still where our friends are, an occasional distraction from reality and a source of entertainment. You don&apos;t have to become a digital outcast to hold back this influence.&lt;/p&gt;
&lt;p&gt;So what can we do?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go directly to the source - if you like a particular TikTok creator, Facebook page or YouTube channel, skip the feed and go directly to their pages. Consider bookmarking their profiles individually.&lt;/li&gt;
&lt;li&gt;Learn to find information and entertainment without a feed - try to find a creator making videos or writing about a topic of interest without having to stumble across them in a feed.&lt;/li&gt;
&lt;li&gt;Use platforms and platform features that let you control your experience - Instagram&apos;s &apos;Following&apos; feed, YouTube&apos;s Subscriptions page, &lt;a href=&quot;https://bsky.app&quot;&gt;Bluesky&lt;/a&gt;, &lt;a href=&quot;https://mastodon.social&quot;&gt;Mastodon&lt;/a&gt; and &lt;a href=&quot;https://zapier.com/blog/how-to-use-rss-feeds/&quot;&gt;RSS feeds&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Be mindful of engagement traps - recognise how algorithmic feeds are designed to keep you engaged and scrolling. Take a breath and stop the cycle.&lt;/li&gt;
&lt;li&gt;Talk about it - if you&apos;re reading this you a already know this is a problem. Your friends and family may not be aware of how their feeds are manipulating their attention and beliefs. Without intervention, the radicalisation of opinions, and the consequences we’re already seeing, will only escalate.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The internet should serve you, not the other way around. Take back control. Kill your feeds before they kill your ability to think independently.&lt;/p&gt;
</content:encoded></item><item><title>Confessions about my smart home</title><link>https://usher.dev/posts/2025-02-24-confessions-about-my-smart-home/</link><guid isPermaLink="true">https://usher.dev/posts/2025-02-24-confessions-about-my-smart-home/</guid><pubDate>Mon, 24 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://frenck.dev/confessions-about-my-smart-home/&quot;&gt;Frenck (lead engineer of Home Assistant) breaks down the state of his smart home&lt;/a&gt;. While you might assume the lead engineer of a home automation platform has everything figured out, it&apos;s fun to hear that&apos;s far from the truth.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;...my dashboards are so bad that if I turn on the &quot;Coffee machine&quot; switch, it actually powers on my 3D Printer. 🤦‍♂️&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I keep seeing examples of this - lead developers and experts in their field often don&apos;t have the perfect environment - they are focused on the things they want to do and often the pursuit of the ideal environment falls by the wayside.&lt;/p&gt;
&lt;p&gt;This is something I often need to remind myself about when I go on a yak shaving adventure tweaking my editor setup or finding the perfect tool for a job.&lt;/p&gt;
</content:encoded></item><item><title>When Imperfect Systems are Good, Actually: Bluesky&apos;s Lossy Timelines</title><link>https://usher.dev/posts/2025-02-19-why-imperfect-systems-are-good/</link><guid isPermaLink="true">https://usher.dev/posts/2025-02-19-why-imperfect-systems-are-good/</guid><pubDate>Wed, 19 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://jazco.dev/2025/02/19/imperfection/&quot;&gt;I love reading technical deep-dives in to system design and scaling&lt;/a&gt;. This post from Jaz (infrastructure at Bluesky) is a great read on how they balanced user needs with performance by finding where they could be &apos;imperfect&apos;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;By specifying the limits of reasonable user behavior and embracing imperfection for users who go beyond it, we can continue to provide service that meets the expectations of users without sacrificing scalability of the system.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There&apos;s always sacrifices to be made in system design - I like the idea of defining a &apos;reasonable limit&apos; for any one user and building optimisations around that limit.&lt;/p&gt;
</content:encoded></item><item><title>Minimal GitOps-like Deployment Tool</title><link>https://usher.dev/posts/2025-02-17-minimal-cluster-deployment-tool/</link><guid isPermaLink="true">https://usher.dev/posts/2025-02-17-minimal-cluster-deployment-tool/</guid><pubDate>Mon, 17 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
If you have more than 1 server and a need to deploy multiple applications &apos;somewhere&apos; on that cluster without having to micromanage - don&apos;t reinvent the wheel, just use Kubernetes. It&apos;s the industry standard for a reason, and you&apos;re not going to have a hard time finding help when you need it.

&lt;p&gt;However, it&apos;s easy to forget that there&apos;s a class of service/business where this doesn&apos;t make sense. Many small businesses (if they&apos;re not using a PaaS like Fly), don&apos;t have the resources to run a Kubernetes cluster and 95% of the time, just need an app to run on a single VPS.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Sometimes, maybe during an event or a promotional period, it might make sense to add another server to the mix, load balancing traffic somewhere (DigitalOcean load balancers are pretty effective).&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Assuming&lt;/em&gt; we have some nice way to spin up more servers running the app (a nice &lt;a href=&quot;https://github.com/ansible/ansible&quot;&gt;Ansible Playbook&lt;/a&gt; perhaps?), and &lt;em&gt;assuming&lt;/em&gt; we have some nice CD pipeline to deploy the app, we need some way to make sure all the servers are running the latest version.&lt;/p&gt;
&lt;p&gt;One pattern I like for this is a simplified GitOps pull model (think ArgoCD or Flux but held together with duct tape) where we:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Have a repo which indicates what app releases should currently be deployed.&lt;/li&gt;
&lt;li&gt;Get your servers to query this repo to determine what it should be running and automatically update the app on the server to match.&lt;/li&gt;
&lt;li&gt;Make your deployment pipeline update the repo with a reference to the new release.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This way, your deployment pipeline doesn&apos;t need to know anything about the servers it&apos;s deploying to - it just needs to know how to update the repo with a reference to the new release.&lt;/p&gt;
&lt;p&gt;In one case, I have a repo called &apos;ops&apos; which along with all the ansible playbooks, has a set of &lt;code&gt;env.yaml&lt;/code&gt; files, one for each app which contain (along with other settings), a line like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;image: my-registry-image:latest
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I can then run a script like the following on each server, which pulls down the &lt;code&gt;ops&lt;/code&gt; repo, checks each app and updates the systemd service file to point to the latest release.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash
set -euo pipefail

cd /srv/ops
git pull

# For each file in /srv/deploy
for file in /srv/deploy/*; do
  if [ -f &quot;$file&quot; ]; then
    # Read the file and split it into an array
    IFS=&apos; &apos; read -r -a array &amp;lt;&amp;lt;&amp;lt; &quot;$(cat &quot;$file&quot;)&quot;
    CONFIG_PATH=${array[0]}
    SERVICE=${array[1]}

    echo &quot;Checking $SERVICE in $CONFIG_PATH for updates...&quot;

    IMAGE=$(grep &apos;^image:&apos; /srv/ops/config/&quot;$CONFIG_PATH&quot;/env.yaml | awk &apos;{print $2}&apos;)

    if [ -z &quot;$IMAGE&quot; ]; then
        echo &quot;No image found in env.yaml&quot;
        continue
    fi

    CURRENT_IMAGE=$(grep &apos;^Environment=IMAGE=&apos; /etc/systemd/system/&quot;$SERVICE&quot;.service | awk &apos;{split($0,a,&quot;=&quot;); print a[3]}&apos;)

    if [ &quot;$IMAGE&quot; == &quot;$CURRENT_IMAGE&quot; ]; then
        echo &quot;Image is already up to date&quot;
        continue
    fi

    echo &quot;Updating $SERVICE to $IMAGE&quot;

    docker pull &quot;$IMAGE&quot;

    # Update the systemd service file
    sed -i&apos;&apos; -r &apos;s@Environment=IMAGE=.+$@Environment=IMAGE=&apos;&quot;$IMAGE&quot;&apos;@&apos; /etc/systemd/system/&quot;$SERVICE&quot;.service

    # Reload the systemd service
    systemctl daemon-reload

    # If the systemd service is running
    if [ &quot;$(systemctl is-active &quot;$SERVICE&quot;)&quot; ]; then
        # Restart the systemd service
        echo &quot;Restarting $SERVICE&quot;
        systemctl restart &quot;$SERVICE&quot;

        # Wait for the service to start
        timeout 2m bash -c &quot;until [[ \$(docker inspect -f \{\{.State.Running\}\} &quot;$SERVICE&quot; || echo false) == true ]]; do sleep 2; done;&quot;

        # Run migrate
        echo &quot;Running migrate&quot;
        docker exec &quot;$SERVICE&quot; ./app/manage.py migrate
    fi
  fi
done
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For cases where Kubernetes and a full GitOps solution is overkill, this is a nice way to get a simple deployment pipeline setup.&lt;/p&gt;
</content:encoded></item><item><title>Why Blog if Nobody Reads It?</title><link>https://usher.dev/posts/2025-02-10-why-blog-if-nobody-reads-it/</link><guid isPermaLink="true">https://usher.dev/posts/2025-02-10-why-blog-if-nobody-reads-it/</guid><pubDate>Mon, 10 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://andysblog.uk/why-blog-if-nobody-reads-it/&quot;&gt;Andy Hawthorne writes&lt;/a&gt; a lovely summary of why it&apos;s worth blogging:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When you write, you think better. When you think better, you create better.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&apos;ve just added &lt;a href=&quot;https://simonwillison.net/2024/Dec/22/link-blog/&quot;&gt;Simon Willison&apos;s link blog pattern&lt;/a&gt; to this site, hoping it will encourage me to write more, even if it&apos;s just fleeting thoughts on a link like this one.&lt;/p&gt;
</content:encoded></item><item><title>Django on Fly.io with Litestream/LiteFS</title><link>https://usher.dev/posts/2022-10-27-django-on-flyio-with-litestream/</link><guid isPermaLink="true">https://usher.dev/posts/2022-10-27-django-on-flyio-with-litestream/</guid><pubDate>Thu, 27 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
I&apos;ve been playing with Fly.io a lot recently (see my series on [deploying Wagtail to Fly.io](../2022-08-30-wagtail-on-flyio/part-1)).

&lt;p&gt;One of the neat things that has come out of Fly is a renewed interest across the dev world in SQLite - an embedded database that doesn&apos;t need any special servers or delicate configuration.&lt;/p&gt;
&lt;p&gt;Some part of this interest comes from the thought that if you had an SQLite database that sat right next to your application, in the same VM, with no network latency, that&apos;s probably going to be pretty quick and pretty easy to deploy.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Although in some ways it feels like this idea comes full circle back to the days of running a MySQL Server alongside our PHP application on a single VPS, we&apos;re also in an era where we need to deal with things like geographic distribution, ephemeral filesystems and scale-to-zero.&lt;/p&gt;
&lt;p&gt;So we want to run our apps in a nice PaaS, and also quite like the idea of our database being local to our application code, but there&apos;s a few conflicts here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PaaS tools like Heroku/Fly tend to offer ephemeral storage, or no guarantees on the safety of storage. Trying to keep an SQLite database around on this sort of storage just won&apos;t work out.&lt;/li&gt;
&lt;li&gt;A common approach to scaling is to &quot;scale out&quot; - start up more instances of your application and load balance between them. How would that work with SQLite? Even if you could access the same database file from each instance, we&apos;re re-introducing latency and as SQLite can&apos;t be written to by multiple processes at once, we&apos;re probably slowing our app down too.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thankfully Fly have been funding the development of some interesting tools; &lt;a href=&quot;https://litestream.io/&quot;&gt;Litestream&lt;/a&gt; and &lt;a href=&quot;https://fly.io/docs/litefs/&quot;&gt;LiteFS&lt;/a&gt; which aim to solve this.&lt;/p&gt;
&lt;p&gt;The difference between these tools is not particularly obvious; so to summarise:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Litestream was Ben Johnson&apos;s first attempt at solving this problem, and is now focused primarily on disaster recovery. It&apos;s a tool to stream all the changes made to your SQLite database to some remote storage, like S3, and then recover from it when you need to.&lt;/p&gt;
&lt;p&gt;This is great, and it nicely solves our first conflict. Our application can be configured to restore the database from remote storage when it starts, and we can be safe knowing that any changes are being backed up as our application runs.&lt;/p&gt;
&lt;p&gt;Unfortunately, it doesn&apos;t solve our second problem, replicating our databases to other instances of our app if we decide to scale out. While there were plans (and an initial implementation) for this in Litestream, live replication was instead moved to the second project, LiteFS.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;LiteFS &lt;a href=&quot;https://fly.io/blog/introducing-litefs/&quot;&gt;does some magic with FUSE&lt;/a&gt; to allow it to intercept SQLite transactions and then replicate to multiple instances of your application. It&apos;s a little more complicated as you need additional tools like Consul so that it knows where to find the primary instance (where it will direct queries that write to the database), but it solves our second conflict!&lt;/p&gt;
&lt;p&gt;Alas, our first conflict isn&apos;t yet solved by LiteFS - if all your nodes go away, there&apos;s nowhere to replicate your database from so it too will disappear. S3 replication like in Litestream is on the roadmap however, so it seems like LiteFS is fixed to solve all our problems!&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So we know what these tools do, let&apos;s experiment with getting our Django applications running with them on Fly.io&lt;/p&gt;
&lt;h1&gt;Litestream&lt;/h1&gt;
&lt;p&gt;For Litestream, we&apos;ll need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An S3-compatible storage bucket and access keys&lt;/li&gt;
&lt;li&gt;Our django app, ideally configured with &lt;code&gt;dj-database-url&lt;/code&gt; for convenience&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;litestream&lt;/code&gt; binary available to our application. I have:&lt;pre&gt;&lt;code&gt;wget https://github.com/benbjohnson/litestream/releases/download/v0.3.8/litestream-v0.3.9-linux-amd64.deb \
 &amp;amp;&amp;amp; dpkg -i litestream-v0.3.9-linux-amd64.deb
&lt;/code&gt;&lt;/pre&gt;
in my Dockerfile.&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Prepare your Fly application with &lt;code&gt;flyctl launch&lt;/code&gt; (we don&apos;t need a Postgres database if it asks).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set all the environment variables we&apos;re going to need by creating a new file (call it something like &lt;code&gt;env-vars&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;LITESTREAM_ACCESS_KEY_ID=[your S3-compatible access key]
LITESTREAM_SECRET_ACCESS_KEY=[your S3 compatible secret key]
DB_DIR=/db
DATABASE_URL=sqlite:////db/sqlite.db
S3_DB_URL=s3://[your-bucket-name]/db
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;DB_DIR&lt;/code&gt; will be the directory where the database is replicated to, &lt;code&gt;DATABASE_URL&lt;/code&gt; is the path where Django and &lt;code&gt;dj-database-url&lt;/code&gt; can find your database file, and &lt;code&gt;S3_DB_URL&lt;/code&gt; is the path to your S3-compatible bucket.&lt;/p&gt;
&lt;p&gt; Run&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl secrets import &amp;lt; env-vars
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; to import these values in to your Fly environment.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;litestream.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;exec: uwsgi
dbs:
    - path: &quot;$DB_DIR/db.sqlite&quot;
      replicas:
        - url: &quot;$S3_DB_URL&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Replace your &lt;code&gt;exec&lt;/code&gt; section with whatever you you normally run to start your web server. Litestream will do its stuff and conveniently run our own application, exiting when our server exits.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a script, &lt;code&gt;start.sh&lt;/code&gt;, that will run on application start to make sure all our directories are created:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/usr/bin/env bash
if [[ -z &quot;$DB_DIR&quot; ]]; then
    echo &quot;DB_DIR env var not specified - this should be a path of the directory where the database file should be stored&quot;
    exit 1
fi
if [[ -z &quot;$S3_DB_URL&quot; ]]; then
    echo &quot;S3_DB_URL env var not specified - this should be an S3-style URL to the location of the replicated database file&quot;
    exit 1
fi

mkdir -p &quot;$DB_DIR&quot;

litestream restore -if-db-not-exists -if-replica-exists -o &quot;$DB_DIR/db.sqlite&quot; &quot;$S3_DB_URL&quot;

./manage.py migrate --noinput
./manage.py createcachetable

chmod -R a+rwX &quot;$DB_DIR&quot;

exec litestream replicate -config litestream.yml
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Checks important environment variables are set.&lt;/li&gt;
&lt;li&gt;Creates a database directory and makes sure it&apos;s open enough for the app to read/write to it (you might choose to tighten this up if appropriate).&lt;/li&gt;
&lt;li&gt;Restores the database using litestream if it doesn&apos;t already exist.&lt;/li&gt;
&lt;li&gt;Runs migrate to make sure the database is up to date (or creates it if there wasn&apos;t anything to restore).&lt;/li&gt;
&lt;li&gt;Runs &lt;code&gt;litestream replicate&lt;/code&gt; which will in turn run the &lt;code&gt;exec&lt;/code&gt; command in the litestream config, starting the application.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; Update your Docker &lt;code&gt;CMD&lt;/code&gt; to run this &lt;code&gt;start.sh&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once deployed with &lt;code&gt;flyctl deploy&lt;/code&gt;, Litestream will start backing up your database. Careful, if you try to scale out by adding more instances, at best you&apos;ll see out of sync data, at worst you&apos;ll end up with a corrupt database.&lt;/p&gt;
&lt;h1&gt;LiteFS&lt;/h1&gt;

The following is not battle-tested and is not suitable for production, but if you&apos;re interested in experimenting, it&apos;s a bit of fun.


&lt;p&gt;For LiteFS, we&apos;ll need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Our django app, ideally configured with &lt;code&gt;dj-database-url&lt;/code&gt; for convenience&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;litefs&lt;/code&gt; binary available to our application. I have:&lt;pre&gt;&lt;code&gt;RUN ARCH=&quot;amd64&quot; &amp;amp;&amp;amp; \
VERSION=&quot;0.2.0&quot; &amp;amp;&amp;amp; \
cd $(mktemp --directory) &amp;amp;&amp;amp; \
wget &quot;https://github.com/superfly/litefs/releases/download/v${VERSION}/litefs-v${VERSION}-linux-${ARCH}.tar.gz&quot; &amp;amp;&amp;amp; \
tar xvf &quot;litefs-v${VERSION}-linux-${ARCH}.tar.gz&quot; &amp;amp;&amp;amp; \
mv litefs /usr/local/bin
&lt;/code&gt;&lt;/pre&gt;
in my Dockerfile (alternatively, copy the binary from the &lt;a href=&quot;https://github.com/superfly/litefs-example/blob/main/Dockerfile&quot;&gt;&lt;code&gt;litefs&lt;/code&gt; image&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Some way to make sure our write requests only end up with the primary (we&apos;ll come back to this).&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Prepare your Fly application with &lt;code&gt;flyctl launch&lt;/code&gt; (we don&apos;t need a Postgres database if it asks).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set all the environment variables we&apos;re going to need by creating a new file (call it something like &lt;code&gt;env-vars&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;DB_DIR=/db
DATABASE_URL=sqlite:////db/sqlite.db
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;DB_DIR&lt;/code&gt; will be the directory where the database is replicated to and &lt;code&gt;DATABASE_URL&lt;/code&gt; is the path where Django and &lt;code&gt;dj-database-url&lt;/code&gt; can find your database file.&lt;/p&gt;
&lt;p&gt; Run&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl secrets import &amp;lt; env-vars
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; to import these values in to your Fly environment.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In your &lt;code&gt;fly.toml&lt;/code&gt;, add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[experimental]
    enable_consul = true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This gives us access to the shared Fly.io-managed Consul instance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;litefs.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;exec: ./run.sh

mount-dir: &quot;$DB_DIR&quot;
data-dir: &quot;/db-data&quot;

http:
  addr: &quot;:20202&quot;

consul:
  url: &quot;${FLY_CONSUL_URL}&quot;
  advertise-url: &quot;http://${FLY_REGION}.${FLY_APP_NAME}.internal:20202&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Replace your &lt;code&gt;exec&lt;/code&gt; section with whatever you you normally run to start your web server.&lt;/p&gt;
&lt;p&gt; The &lt;code&gt;mount-dir&lt;/code&gt; is where LiteFS will create its filesystem (where the database will live), &lt;code&gt;data-dir&lt;/code&gt; is where it keeps files it needs for replication. The &lt;code&gt;http&lt;/code&gt; and &lt;code&gt;consul&lt;/code&gt; blocks tell LiteFS how to talk to each other and where to find the Fly.io managed Consul instance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create the &lt;code&gt;run.sh&lt;/code&gt; that is started by LiteFS. We need things like migrations to run after LiteFS has set up its filesystem, so we do those in this script@:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/usr/bin/env bash
set -e
echo &quot;Starting app&quot;

./manage.py migrate --noinput || true
./manage.py createcachetable
chmod -R a+rwX &quot;$DB_DIR&quot;

exec /venv/bin/uwsgi
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a script, &lt;code&gt;start.sh&lt;/code&gt;, that will run on application start to make sure all our directories are created:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/usr/bin/env bash
if [[ -z &quot;$DB_DIR&quot; ]]; then
    echo &quot;DB_DIR env var not specified - this should be a path of the directory where the database file should be stored&quot;
    exit 1
fi

mkdir -p &quot;$DB_DIR&quot;

exec litefs -config litefs.yml
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This:&lt;/p&gt;
&lt;p&gt; Update your Docker &lt;code&gt;CMD&lt;/code&gt; to run this &lt;code&gt;start.sh&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We&apos;re not there yet. We need to make sure database writes &lt;em&gt;only&lt;/em&gt; go to our primary. To do this, we&apos;ll register a database &lt;code&gt;execute_wrapper&lt;/code&gt; which intercepts any write queries. I&apos;ve got this in my &lt;code&gt;base&lt;/code&gt; app&apos;s &lt;code&gt;__init__.py&lt;/code&gt; (heavily based on &lt;a href=&quot;https://github.com/adamchainz/django-read-only&quot;&gt;Adam Johnson&apos;s &lt;code&gt;django-read-only&lt;/code&gt;&lt;/a&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import os
import os.path
from pathlib import Path
from typing import Any, Callable, Generator

from django.apps import AppConfig
from djang.conf import settings
from django.db import connections
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.signals import connection_created

read_only = False

class BaseConfig(AppConfig):
    name = &quot;bakerydemo.base&quot;
    verbose_name = &quot;base&quot;

    def ready(self) -&amp;gt; None:
        db_name = settings.DATABASES[&apos;default&apos;][&apos;NAME&apos;]
        db_dir = Path(db_name).parent
        primary_path = db_dir / &quot;.primary&quot;
        if not primary_path.is_file():
            return
        for alias in connections:
            connection = connections[alias]
            install_hook(connection)
        connection_created.connect(install_hook)


def install_hook(connection: BaseDatabaseWrapper, **kwargs: object) -&amp;gt; None:
    if blocker not in connection.execute_wrappers:
        connection.execute_wrappers.insert(0, blocker)


class QueriesAttemptedError(Exception):
    pass


def blocker(
    execute: Callable[[str, str, bool, dict[str, Any]], Any],
    sql: str,
    params: str,
    many: bool,
    context: dict[str, Any],
) -&amp;gt; Any:
    if should_block(sql):
        raise QueriesAttemptedError(msg)
    return execute(sql, params, many, context)


def should_block(sql: str) -&amp;gt; bool:
    return not sql.lstrip(&quot; \n(&quot;).startswith(
        (
            &quot;EXPLAIN &quot;,
            &quot;PRAGMA &quot;,
            &quot;ROLLBACK TO SAVEPOINT &quot;,
            &quot;RELEASE SAVEPOINT &quot;,
            &quot;SAVEPOINT &quot;,
            &quot;SELECT &quot;,
            &quot;SET &quot;,
        )
    ) and sql not in (&quot;BEGIN&quot;, &quot;COMMIT&quot;, &quot;ROLLBACK&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This will raise an exception if the query will write to the database, and if the &lt;code&gt;.primary&lt;/code&gt; file created by LiteFS exists (meaning this is not the primary).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need something to intercept this exception, so add some middleware:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from django.http import HttpResponse
from djang.conf import settings
from . import QueriesAttemptedError

def replay_middleware(get_response):
    def middleware(request):
        try:
            response = get_response(request)
        except QueriesAttemptedError:
            res = HttpResponse()
            # Find the name of the primary instance by reading the .primary file
            db_name = settings.DATABASES[&apos;default&apos;][&apos;NAME&apos;]
            db_dir = Path(db_name).parent
            primary_path = db_dir / &quot;.primary&quot;
            primary = primary_path.read_text()
            res.headers[&apos;fly-replay&apos;] = f&quot;instance={primary}&quot;
            return res

        return response

return middleware
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; and register it in your &lt;code&gt;MIDDLEWARE&lt;/code&gt; settings.&lt;/p&gt;
&lt;p&gt; This catches the exception raised by the previously registered &lt;code&gt;execute_wrapper&lt;/code&gt;, finds out where the primary database is hosted and returns a &lt;code&gt;fly-replay&lt;/code&gt; header telling Fly.io; &quot;Sorry, I can&apos;t handle this request, please replay it to this database primary&quot;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once deployed with &lt;code&gt;flyctl deploy&lt;/code&gt;, LiteFS will start replicating your database!&lt;/p&gt;
&lt;h1&gt;The Future&lt;/h1&gt;
&lt;p&gt;These are fun tools to play with for now, but there&apos;s clearly a lot of work to get them working with our normal apps.&lt;/p&gt;
&lt;p&gt;I&apos;m excited about how they could make getting a Django/Wagtail app deployed much more accessible, easier and cheaper, but they&apos;re still some work to be done to make that a reality.&lt;/p&gt;
&lt;p&gt;The LiteFS roadmap includes things like S3 replication (so we get similar backup features to Litestream), and write forwarding (so writes to read-replicas will automatically be forwarded to the primary). There&apos;s a lot of promise there and I can&apos;t wait to make more use of it!&lt;/p&gt;
</content:encoded></item><item><title>Deploying a Wagtail site to Fly.io Part 1: The Plan</title><link>https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-1/</link><guid isPermaLink="true">https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-1/</guid><pubDate>Tue, 30 Aug 2022 00:00:00 GMT</pubDate><content:encoded>
Not interested in the preamble? Jump to [part 2](../part-2).

&lt;p&gt;Migrating from Heroku or similar? Your app is probably ready to go. Jump to &lt;a href=&quot;../part-5&quot;&gt;part 5&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Need a hand? I offer Wagtail, hosting and DevOps consulting services - &lt;a href=&quot;/contact&quot;&gt;drop me an email&lt;/a&gt; and we&apos;ll have a chat.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;div&gt;
I&apos;ve been playing with Fly.io quite a bit recently.

&lt;p&gt;Yes I&apos;m drawn to &lt;a href=&quot;https://fly.io/docs/reference/architecture/&quot;&gt;the exciting tech&lt;/a&gt;, the &lt;a href=&quot;https://fly.io/blog/sqlite-internals-btree/&quot;&gt;fascinating blog posts&lt;/a&gt;, the &lt;a href=&quot;https://fly.io/docs/about/pricing/&quot;&gt;attractive pricing&lt;/a&gt; and generous free tier (for now...), but more importantly, Fly are one of the few new breed &apos;PaaS&apos; tools that realise what Heroku got right with its excellent developer experience, and are on a fast-track to replicating and eventually exceeding what Heroku offers.&lt;/p&gt;
&lt;p&gt;While Fly is quickly establishing itself as my go-to hosting platform for personal/hobby projects (even before the &quot;sunsetting&quot; of Heroku&apos;s free tier), &lt;a href=&quot;https://github.com/wagtail/wagtail/&quot;&gt;Wagtail&lt;/a&gt; has been my go-to platform for almost all the sites I build for quite a while.&lt;/p&gt;
&lt;/div&gt;


Although I&apos;m referencing Wagtail in particular in this post, almost everything I discuss here is also relevant to &quot;pure Django&quot; applications.


&lt;p&gt;In this series, I&apos;ll run through how to get a Wagtail site running on Fly.io.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;We&apos;ll need to sort out a few things before our application is ready to be served from Fly:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How do we configure our application so its easily ported between different environments? (dev, staging, production, etc.)&lt;/li&gt;
&lt;li&gt;How are we going to bundle up our application ready for pushing to Fly?&lt;/li&gt;
&lt;li&gt;How are we going to serve our &apos;static&apos; files (CSS, JavaScript, etc.)&lt;/li&gt;
&lt;li&gt;Where are our media files (or Wagtail Images/Documents) going to be stored?&lt;/li&gt;
&lt;li&gt;Where&apos;s our database going to live?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There are, as always, many answers to these questions but we&apos;ll pick a few answers and run with them.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For configuration (and for everything else for that matter), we&apos;ll be following the &lt;a href=&quot;https://12factor.net/&quot;&gt;Twelve-Factor App&lt;/a&gt; methodology. This means storing any configuration that might vary in the environment - &lt;a href=&quot;https://fly.io/docs/reference/secrets/&quot;&gt;Fly&apos;s Secrets&lt;/a&gt; in this case.&lt;/li&gt;
&lt;li&gt;We&apos;ll bundle up the application using a &lt;code&gt;Dockerfile&lt;/code&gt; - while there are certainly merits to Fly&apos;s other option, &lt;a href=&quot;https://fly.io/docs/reference/builders/#buildpacks&quot;&gt;Buildpacks&lt;/a&gt;, I consider having a good Docker set up to be super useful for portability and a great local development experience.&lt;/li&gt;
&lt;li&gt;Django has no built-in way (outside of development) to serve static files - it expects you to figure out how to serve these yourself with a separate web server or from a separate cloud service. We&apos;ll use the very useful &lt;a href=&quot;https://github.com/evansd/whitenoise&quot;&gt;WhiteNoise&lt;/a&gt; package to give Django the ability to easily and safely serve its own static files.&lt;/li&gt;
&lt;li&gt;When Fly runs your application, it gets a bit of the hosts&apos; file system to write to, but whatever you write to it is &apos;ephemeral&apos; (fancy for &apos;not gonna last&apos;). When that application stops, your data is gone. That&apos;s no good when you want to store things like images or documents uploaded by users.&lt;/li&gt;
&lt;li&gt;Our database is going to live on Fly too! We&apos;ll use their built-in Postgres support for this project, but do be aware it is largely &apos;unmanaged&apos; - meaning you don&apos;t get backups, disaster recovery and the other niceties you&apos;d get with a managed service.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr /&gt;
&lt;p&gt;Before we kick-off, a few final things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We&apos;re going to be preparing and deploying a fresh Wagtail site in this series. If you have your own site ready to go, these steps should all be transferrable to whatever you&apos;re building.&lt;/li&gt;
&lt;/ul&gt;

If you want to experiment with a more fully-featured Wagtail site, follow along with [bakerydemo](https://github.com/wagtail/bakerydemo). It&apos;s a great example of a Wagtail site that will demonstrate all the features we&apos;ll be dealing with.

&lt;p&gt;Any &lt;code&gt;bakerydemo&lt;/code&gt;-specific changes or notes will be in these 🍞-boxes.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This series focuses on preparing and deploying a Wagtail site - I won&apos;t be going in to detail about setting up your development environment, installing dependencies or potential differences when working with different operating systems.&lt;/li&gt;
&lt;li&gt;Some knowledge of &lt;code&gt;git&lt;/code&gt;, Wagtail/Django, managing &lt;code&gt;requirements&lt;/code&gt; files and general command line usage is expected.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a href=&quot;../part-2&quot;&gt;Let&apos;s get started in Part 2.&lt;/a&gt;&lt;/p&gt;
</content:encoded></item><item><title>Deploying a Wagtail site to Fly.io Part 2: Configuration</title><link>https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-2/</link><guid isPermaLink="true">https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-2/</guid><pubDate>Tue, 30 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
To kick everything off, you&apos;ll need a Wagtail site configured in a local development environment. The [Wagtail Getting Started guide](https://docs.wagtail.org/en/stable/getting_started/tutorial.html) is a great place to start.

&lt;p&gt;Alternatively, grab a checkout of &lt;a href=&quot;https://github.com/wagtail/bakerydemo&quot;&gt;&lt;code&gt;bakerydemo&lt;/code&gt;&lt;/a&gt;, or find your own existing project.&lt;/p&gt;
&lt;p&gt;We&apos;ll start by getting your application in to a state where it is easily configurable with environment variables.&lt;/p&gt;
&lt;/div&gt;
---

&lt;p&gt;When building an application for any platform, following the &lt;a href=&quot;https://12factor.net/&quot;&gt;Twelve-Factor App&lt;/a&gt; methodology is highly recommended for reasons that are well explained on the linked site.&lt;/p&gt;
&lt;p&gt;In particular, factor 3 (&apos;Config&apos;) is about storing configuration that is likely to vary on the host, rather than in your codebase.&lt;/p&gt;

The word &apos;environment&apos; is a bit overloaded. It can mean both &apos;an environment something runs on&apos; e.g. Fly, Heroku, a Docker container etc, or it can mean &apos;a system&apos;s environment variables&apos;.

&lt;p&gt;I&apos;ll refer to the former as a &apos;host&apos; for simplicity here, even though that could be a local dev setup, CI, or something else.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you&apos;ve seen a few Django projects, you&apos;ve probably seen projects with multiple configuration files like &lt;code&gt;prod.py&lt;/code&gt;, &lt;code&gt;dev.py&lt;/code&gt;, etc. This can create a tight coupling between your codebase and the host it runs on. Rather than your host being able to inform the application about things like database credentials, domains and secrets, the codebase needs to be &apos;aware&apos; of the details of the host it&apos;s going to run on before it gets there!&lt;/p&gt;
&lt;p&gt;Wagtail&apos;s default starter template is guilty of this, but we can focus on the &lt;code&gt;settings/base.py&lt;/code&gt;, treating it as our only settings file and varying any settings that might change with environment variables.&lt;/p&gt;
&lt;p&gt;While there are a lot of nice fully-featured packages that help you do this (&lt;a href=&quot;https://django-environ.readthedocs.io/en/latest/&quot;&gt;django-environ&lt;/a&gt;, &lt;a href=&quot;https://github.com/dynaconf/dynaconf&quot;&gt;Dynaconf&lt;/a&gt;, &lt;a href=&quot;https://github.com/henriquebastos/python-decouple/&quot;&gt;python-decouple&lt;/a&gt;), we&apos;ll start simple by pulling values directly from the environment using Python&apos;s &lt;code&gt;os&lt;/code&gt; module.&lt;/p&gt;
&lt;p&gt;There&apos;s a few settings we&apos;ll want to convert to environment variables (or add in if they don&apos;t exist). I&apos;ll usually end up with something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import os

DEBUG = os.getenv(&quot;DEBUG&quot;, &quot;false&quot;).lower() == &quot;true&quot;
# ...
SECRET_KEY = os.getenv(&quot;SECRET_KEY&quot;)
# ...
ALLOWED_HOSTS = os.getenv(&quot;ALLOWED_HOSTS&quot;, &quot;&quot;).split(&quot;,&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, the hosts&apos; environment variables specify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Whether debug mode is enabled&lt;/li&gt;
&lt;li&gt;The Django secret key&lt;/li&gt;
&lt;li&gt;A comma-separated list of values for &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The other important thing that we need to pull from the environment are the database credentials. In 12-factor apps, these are often expressed in a URL format (e.g. &lt;code&gt;postgres://username:password@host:5432&lt;/code&gt;). This can take a bit of parsing to get the separate values Django expects, so a package like &lt;a href=&quot;https://github.com/jazzband/dj-database-url&quot;&gt;dj-database-url&lt;/a&gt; is very useful here.&lt;/p&gt;
&lt;p&gt;Configuring Django to use a &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable using this package can be done by replacing your &lt;code&gt;DATABASES&lt;/code&gt; settings value with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import dj_database_url

# ...

DATABASES = {
    &quot;default&quot;: dj_database_url.config(conn_max_age=600)
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other things that vary can include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;API keys and other secrets&lt;/li&gt;
&lt;li&gt;Locations of external resources like S3 buckets (we&apos;ll come back to this)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most of these values can be easily parsed from the simple strings we are limited to with environment variables.&lt;/p&gt;

We don&apos;t need to make any immediate configuration changes to `bakerydemo` - it&apos;s already in a 12-factor-enough state for us to work with, and includes `dj-database-url`. You can take a closer look at [`production.py`](https://github.com/wagtail/bakerydemo/blob/main/bakerydemo/settings/production.py) to see a few more examples of settings being pulled from the environment.


&lt;hr /&gt;
&lt;p&gt;&lt;a href=&quot;../part-3&quot;&gt;On to the complicated bit...&lt;/a&gt;&lt;/p&gt;
</content:encoded></item><item><title>Deploying a Wagtail site to Fly.io Part 3: Dockerising &amp; Requirements</title><link>https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-3/</link><guid isPermaLink="true">https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-3/</guid><pubDate>Tue, 30 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
For Fly to know how to run our application, we need to package it up in a way that it understands. We need to tell it things like:

&lt;ul&gt;
&lt;li&gt;How to acquire all our project&apos;s dependencies&lt;/li&gt;
&lt;li&gt;What application server to use and how to configure it&lt;/li&gt;
&lt;li&gt;What files need to be made available to the platform&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Fly currently supports &lt;a href=&quot;https://fly.io/docs/reference/builders/&quot;&gt;3 ways of doing this&lt;/a&gt;; Dockerfiles, Buildpacks and Nixpacks. The latter two are a great way to get started if you&apos;re not familiar with Docker - they provide a pre-built way for the platform to build and run your application without the developer having to do much extra work.&lt;/p&gt;
&lt;/div&gt;
Dockerfile&apos;s are a bit more complicated but they are my preference because they offer complete flexibility over what we&apos;re deploying. In our case, Wagtail&apos;s starter project template already comes with a pretty decent [Dockerfile](https://github.com/wagtail/wagtail/blob/main/wagtail/project_template/Dockerfile), so we&apos;ll focus on using that.

&lt;p&gt;If you don&apos;t have a Dockerfile, grab a copy of the &lt;a href=&quot;https://github.com/wagtail/wagtail/blob/main/wagtail/project_template/Dockerfile&quot;&gt;Wagtail template file&lt;/a&gt;, place it in the root of your project and replace any occurrences of &lt;code&gt;{% raw %}{{ project_name }}{% endraw %}&lt;/code&gt; with the name of the folder your &lt;code&gt;wsgi.py&lt;/code&gt; is found in.&lt;/p&gt;

You can skip the following changes if you&apos;re using `bakerydemo`&apos;s Dockerfile, as it is already in a fully usable state.


&lt;hr /&gt;
&lt;p&gt;Looking at this &lt;code&gt;Dockerfile&lt;/code&gt;, you&apos;ll probably notice a big &lt;code&gt;WARNING&lt;/code&gt; comment at the bottom, before the line that starts with &lt;code&gt;CMD&lt;/code&gt;. This line is what Fly will execute when it starts running our application, and the warning is telling us that running &lt;code&gt;migrate&lt;/code&gt; at this point is not best practice.&lt;/p&gt;

For most simple use cases, running `migrate` at this stage is unlikely to be a problem. Issues are likely to arise when running multiple simultaneous copies of the site (scaling out), as it is possible that they will each try to try run `migrate` at the same time potentially causing conflicts or errors.


&lt;p&gt;Because Fly gives us a &lt;code&gt;release&lt;/code&gt; phase in which we can run migrations (which we&apos;ll come back to), we can simplify this line to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;CMD gunicorn myproject.wsgi:application
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the only thing that Fly will do when your application launches is start the &lt;code&gt;gunicorn&lt;/code&gt; application server.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Because we&apos;ll be using PostgreSQL for our site, we need to make sure the package that Django uses to talk to PostgreSQL databases will be installed.&lt;/p&gt;
&lt;p&gt;To do this, add &lt;code&gt;psycopg2==2.9.3&lt;/code&gt; to your &lt;code&gt;requirements.txt&lt;/code&gt; file.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a href=&quot;../part-4&quot;&gt;Next up, what do to with all static/media files...&lt;/a&gt;&lt;/p&gt;
</content:encoded></item><item><title>Deploying a Wagtail site to Fly.io Part 4: Static &amp; Media Files</title><link>https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-4/</link><guid isPermaLink="true">https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-4/</guid><pubDate>Tue, 30 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
Our Wagtail app needs to serve two different &apos;categories&apos; of files:

&lt;ul&gt;
&lt;li&gt;Static files - things that are in our codebase like CSS and JS and don&apos;t change unless we deploy a new version of the site.&lt;/li&gt;
&lt;li&gt;Media files - images and documents uploaded by our site users and content editors.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We&apos;ll use a couple of very useful Django packages to handle these.&lt;/p&gt;
&lt;/div&gt;


`bakerydemo` comes preconfigured with both, so if you&apos;re just here to deploy that, skip to the `Media Files` section below.


&lt;h2&gt;Static Files&lt;/h2&gt;
&lt;p&gt;As covered in Part 1 (but that was too many words ago), Django has no production-ready approach to serving static files.&lt;/p&gt;
&lt;p&gt;Thankfully, &lt;a href=&quot;https://github.com/evansd/whitenoise&quot;&gt;WhiteNoise&lt;/a&gt; exists and offers a nice simple way to get the job done without any additional servers or services.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;http://whitenoise.evans.io/en/stable/django.html&quot;&gt;Using WhiteNoise with Django&lt;/a&gt; docs already serve as a perfect starting point, so I won&apos;t repeat them here. I recommend you only follow the setup up to step 3 for now - everything following may be useful in some situations but likely won&apos;t be for this process.&lt;/p&gt;
&lt;p&gt;You&apos;ll need to add &lt;code&gt;whitenoise==6.2.0&lt;/code&gt; to your &lt;code&gt;requirements.txt&lt;/code&gt; and update your settings to include something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# ...
MIDDLEWARE = [
    # ...
    &quot;django.middleware.security.SecurityMiddleware&quot;,
    &quot;whitenoise.middleware.WhiteNoiseMiddleware&quot;,
    # ...
]

# ...

STATICFILES_STORAGE = &quot;whitenoise.storage.CompressedManifestStaticFilesStorage&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&apos;ve now told WhiteNoise to get involved whenever we ask for one of our &apos;static&apos; files.&lt;/p&gt;

Fly does support a [`statics`](https://fly.io/docs/reference/configuration/#the-statics-sections) configuration which is an alternative approach to serving static files. However, WhiteNoise gives us a bit more flexibility allowing us to manage cache headers and serve compressed content.


&lt;h2&gt;Media Files&lt;/h2&gt;
&lt;p&gt;Media files are a little more complicated. By default, Django&apos;s going to want to store any user uploaded files on the local filesystem.&lt;/p&gt;
&lt;p&gt;With Fly.io (and many other similar services like Heroku), the filesystem given to your application is ephemeral - it will vanish whenever your application is shut down or re-deployed. That&apos;s not great when you&apos;re dealing with files your users are expecting to stick around.&lt;/p&gt;

Fly does have an option for persistent storage in [Volumes](https://fly.io/docs/reference/volumes/), however these are tied to a region and server, so it limits how you can scale and distribute your application across regions, and means there&apos;s a decent chance you could lose your data if that server goes away.

&lt;p&gt;Unless you have a nice way to replicate your media across volumes, an external provider like we use here is recommended.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;To solve this, we&apos;re going to want to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Store those files somewhere a bit a more permanent.&lt;/li&gt;
&lt;li&gt;Tell Django to write and read files from that place.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The magic combination here is &lt;a href=&quot;https://django-storages.readthedocs.io/en/latest/&quot;&gt;django-storages&lt;/a&gt; and Amazon S3.&lt;/p&gt;
&lt;p&gt;Django Storages adds a set of custom &apos;storage backends&apos; which give Django&apos;s &lt;code&gt;FileField&lt;/code&gt;s the ability to store and retrieve files from external locations. While it supports options like Google Cloud Storage and Azure Storage, we&apos;ll be using Amazon S3 here.&lt;/p&gt;
&lt;h3&gt;Creating an S3 Bucket&lt;/h3&gt;
&lt;p&gt;First up, you&apos;ll need an &lt;a href=&quot;https://aws.amazon.com/&quot;&gt;AWS account&lt;/a&gt;. If your account is new, you&apos;ll get some 5GB of free storage for 12 months. If it&apos;s not, 5GB is going to cost around $0.12 a month.&lt;/p&gt;
&lt;p&gt;Once your account is set up and logged in, click your name in the top right and go to &lt;em&gt;Security Credentials&lt;/em&gt;. Go to the &lt;em&gt;Access keys&lt;/em&gt; section and click &lt;em&gt;Create new access key&lt;/em&gt;. Take note of the generated Access Key ID and Secret Key in your password manager of choice.&lt;/p&gt;
&lt;p&gt;A place to put files in S3 terminology is called a &apos;bucket&apos;. We&apos;ll need to create a new one such that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Our site is allowed to put files in to it&lt;/li&gt;
&lt;li&gt;Anyone on the internet can read files from it&lt;/li&gt;
&lt;/ul&gt;

Yes, anyone will be able to read files from it. This is _usually_ OK, but if you have a site where users might upload private documents or images that they expect are only accessible through your site (and not by visiting the link directly), this could be a problem.

&lt;p&gt;Consider a package like &lt;a href=&quot;https://github.com/torchbox/wagtail-storages&quot;&gt;wagtail-storages&lt;/a&gt; if you&apos;d like a bit more control.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Setting this up can be a little daunting if you&apos;re not familiar with AWS/S3, so we&apos;ll take a little shortcut and use the handy &lt;a href=&quot;https://github.com/simonw/s3-credentials&quot;&gt;s3-credentials&lt;/a&gt; tool which you can &lt;code&gt;pip install&lt;/code&gt; in to your dev environment.&lt;/p&gt;
&lt;p&gt;Once installed, we can get everything we need in one command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;s3-credentials create my-bakerydemo-media -c --public --access-key [your Access Key ID] --secret-key [your secret key]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(as S3 bucket names must be unique you&apos;ll need to replace &lt;code&gt;my-bakerydemo-media&lt;/code&gt; with a more unique name here).&lt;/p&gt;
&lt;p&gt;This will go ahead and create:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A bucket which is publicly accessible&lt;/li&gt;
&lt;li&gt;An AWS user which has write access to the bucket&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;and give you an output like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Created bucket: my-bakerydemo-media
Attached bucket policy allowing public access
Created  user: &apos;s3.read-write.my-bakerydemo-media&apos; with permissions boundary: &apos;arn:aws:iam::aws:policy/AmazonS3FullAccess&apos;
Attached policy s3.read-write.my-bakerydemo-media to user s3.read-write.my-bakerydemo-media
Created access key for user: s3.read-write.my-bakerydemo-media
{
    &quot;UserName&quot;: &quot;s3.read-write.my-test-bakerydemo-media&quot;,
    &quot;AccessKeyId&quot;: &quot;ABCDEFGHJIJKLMNOPQRS&quot;,
    &quot;Status&quot;: &quot;Active&quot;,
    &quot;SecretAccessKey&quot;: &quot;AbCdEFGHiJkLmNOPqrsTUVwxYZ12345678901234&quot;,
    &quot;CreateDate&quot;: &quot;2022-08-31 14:12:04+00:00&quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&apos;ll want to note down your bucket name, &lt;code&gt;AccessKeyID&lt;/code&gt; and &lt;code&gt;SecretAccessKey&lt;/code&gt;.&lt;/p&gt;

If you&apos;re working with `bakerydemo`, you&apos;re done - it&apos;s already configured to work with `django-storages` so we&apos;ll revisit this when we come to deploying the app.


&lt;p&gt;Next, you&apos;ll need to add &lt;code&gt;django-storages[s3]&lt;/code&gt; to your requirements and update your settings to use the &lt;code&gt;django-storages&lt;/code&gt; S3 backend:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if &quot;AWS_STORAGE_BUCKET_NAME&quot; in os.environ:
    AWS_STORAGE_BUCKET_NAME = os.getenv(&quot;AWS_STORAGE_BUCKET_NAME&quot;)
    AWS_DEFAULT_ACL = &quot;public-read&quot;
    AWS_QUERYSTRING_AUTH = False

    INSTALLED_APPS.append(&quot;storages&quot;)
    DEFAULT_FILE_STORAGE = &quot;storages.backends.s3boto3.S3Boto3Storage&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&apos;re making sure to use environment variables here so that if you specify an &lt;code&gt;AWS_STORAGE_BUCKET_NAME&lt;/code&gt; variable this config is going to apply.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a href=&quot;../part-5&quot;&gt;That was a big one. Let&apos;s move on...&lt;/a&gt;&lt;/p&gt;
</content:encoded></item><item><title>Deploying a Wagtail site to Fly.io Part 5: Deploying</title><link>https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-5/</link><guid isPermaLink="true">https://usher.dev/posts/2022-08-30-wagtail-on-flyio/part-5/</guid><pubDate>Tue, 30 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
With all that prep work done we can finally do what we said we&apos;d do at the start - deploy a site to Fly.io.

&lt;p&gt;To start, we&apos;ll need to install the Fly command line tool, &lt;code&gt;flyctl&lt;/code&gt;. Follow steps 1-3 over on the &lt;a href=&quot;https://fly.io/docs/hands-on/install-flyctl/&quot;&gt;Fly.io docs&lt;/a&gt; and then come back. I&apos;ll wait.&lt;/p&gt;
&lt;/div&gt;
---

&lt;p&gt;Great, so now you have a Fly.io account and the &lt;code&gt;flyctl&lt;/code&gt; tool. Everything on Fly can be managed with this tool (not quite the nice GUI experience you get with Heroku, but it&apos;s getting there).&lt;/p&gt;
&lt;p&gt;Next up, let&apos;s kick off the application on Fly:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl launch --dockerfile Dockerfile --name my-bakerydemo
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(You&apos;ll need to come up with your own alternative to &lt;code&gt;my-bakerydemo&lt;/code&gt; as app names need to be unique.)&lt;/p&gt;
&lt;p&gt;You&apos;ll be prompted for a region - pick your preference. You&apos;ll also be asked whether you want to setup a Postgresql database, type &lt;code&gt;Y&lt;/code&gt; and select the &apos;Development&apos; configuration. Finally, you&apos;ll be asked whether you want to deploy your application. Type &lt;code&gt;N&lt;/code&gt; to say no for now.&lt;/p&gt;
&lt;p&gt;In one command, Fly&apos;s done a lot of work for you here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An empty app has been added to your Fly account ready to receive a deployment&lt;/li&gt;
&lt;li&gt;A Postgres database has been configured and attached to your app so that the &lt;code&gt;DATABASE_URL&lt;/code&gt; secret (and therefore environment variable) contains all the information your app needs to connect to the database.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;fly.toml&lt;/code&gt; file has been added with all the details Fly needs to deploy and manage your app.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One problem, we need to make sure your database is populated with the application models when it&apos;s first deployed (and when migrations are added in the future). To do this, edit the &lt;code&gt;fly.toml&lt;/code&gt; and add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[deploy]
    release_command = &quot;python manage.py migrate&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now whenever Fly creates a new release, it will run Django&apos;s &lt;code&gt;migrate&lt;/code&gt; command.&lt;/p&gt;
&lt;p&gt;While we&apos;re editing this file, you&apos;ll also want to change the &lt;code&gt;internal_port&lt;/code&gt; line from &lt;code&gt;8080&lt;/code&gt; to &lt;code&gt;8000&lt;/code&gt; as this is the port the &lt;code&gt;Dockerfile&lt;/code&gt; we&apos;re working with serves the application on.&lt;/p&gt;

bakerydemo&apos;s Docker image runs Python inside a virtualenv. To use the correct `python` instance, you&apos;ll need to replace this with:

&lt;pre&gt;&lt;code&gt;/venv/bin/python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;We&apos;re also going to need to set a few other environment variables to make sure everything we works as we expect. In Fly, we set these using the &apos;Secrets&apos; feature and the related &lt;code&gt;flyctl secrets&lt;/code&gt; commands.&lt;/p&gt;
&lt;p&gt;I find the easiest way to do this is to prepare a file called &lt;code&gt;secrets&lt;/code&gt; with all the environment variables you want to set. It&apos;ll look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ALLOWED_HOSTS=myappname.fly.dev
DJANGO_SECRET_KEY=my_secret_key
AWS_ACCESS_KEY_ID=[AWS Access Key ID generated by s3-credentials]
AWS_SECRET_ACCESS_KEY=[AWS secret key generated by s3-credentials]
AWS_STORAGE_BUCKET_NAME=[storage bucket name you defined]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The value of &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt; should be the name you gave your Fly app, followed by &lt;code&gt;.fly.dev&lt;/code&gt;.&lt;/p&gt;

Generate a more secure secret key than I have by running:

&lt;pre&gt;&lt;code&gt;python -c &quot;import secrets; print(secrets.token_urlsafe())&quot;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Then run&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl secrets import &amp;lt; secrets
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will create each line from your file as a Fly secret attached to your application. The application will be able to access each secret as an environment variable when it&apos;s running.&lt;/p&gt;
&lt;p&gt;Finally, we can deploy our app:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl deploy
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Visiting &lt;code&gt;[yourappname].fly.dev&lt;/code&gt; will now reveal your site running on Fly!&lt;/p&gt;
&lt;p&gt;You&apos;ll probably want to set yourself up with a superuser. We can do this by accessing the server running your application and running the &lt;code&gt;createsuperuser&lt;/code&gt; management command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;flyctl ssh console
&amp;gt;&amp;gt; /app/manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;

If you&apos;re following along with `bakerydemo`, you&apos;ll need to run your commands preceded with `/venv/bin/python` so we use the `python` instance installed in the virtual environment. You&apos;ll also need to replace the `/app` directory with `/code`.

&lt;p&gt;You&apos;re also probably expecting a bit more than just a boring &apos;Welcome to your new Wagtail site!&apos;. We need to get &lt;code&gt;bakerydemo&lt;/code&gt; to generate its content, so let&apos;s do that.&lt;/p&gt;
&lt;p&gt;In a &lt;code&gt;flyctl ssh console&lt;/code&gt; session, run &lt;code&gt;/venv/bin/python /code/manage.py load_initial_data&lt;/code&gt; to prepare our content. Refresh the site and you should have a fully functional bakery!
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Where Next?&lt;/h2&gt;
&lt;p&gt;Congratulations! You&apos;ve got a Wagtail site running on Fly!&lt;/p&gt;
&lt;p&gt;There&apos;s a few things to consider now we&apos;re here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You might want to point your own domain at Fly, which will generate SSL certificates for you. Take a look at the &lt;a href=&quot;https://fly.io/docs/app-guides/custom-domains-with-fly/&quot;&gt;custom domains docs&lt;/a&gt;, and don&apos;t forget to update your &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There&apos;s currently no super-easy way to run scheduled tasks on Fly. Things like Wagtail&apos;s &lt;code&gt;publish_scheduled_content&lt;/code&gt; or Django&apos;s &lt;code&gt;clearsessions&lt;/code&gt;. This is something I&apos;d love to see Fly add, but there are workarounds that are a topic for a future blog post.&lt;/li&gt;
&lt;li&gt;Fly&apos;s Postgres service is very handy but lacks managed features like backups, rollbacks, disaster recovery and support. If you need these things, consider hosting your database somewhere else like &lt;a href=&quot;https://supabase.com/&quot;&gt;Supabase&lt;/a&gt;, &lt;a href=&quot;https://www.crunchydata.com/&quot;&gt;CrunchyData&lt;/a&gt;, or one of the big cloud providers&apos; database services (AWS RDS, Google Cloud SQL).&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>August 2021 Braindump</title><link>https://usher.dev/posts/2021-08-27-august-braindump/</link><guid isPermaLink="true">https://usher.dev/posts/2021-08-27-august-braindump/</guid><pubDate>Fri, 27 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
What I&apos;ve been thinking about in August 2021
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;🎲 We&apos;re slowly getting back in to weekly game nights with a smaller group. It&apos;s been great to dive in to some things that have been sitting around waiting to be played; &lt;a href=&quot;https://boardgamegeek.com/boardgame/291572/oath-chronicles-empire-and-exile&quot;&gt;Oath&lt;/a&gt;, &lt;a href=&quot;https://boardgamegeek.com/boardgame/311193/anno-1800&quot;&gt;Anno 1800&lt;/a&gt;, &lt;a href=&quot;https://boardgamegeek.com/boardgame/300322/hallertau&quot;&gt;Hallertau&lt;/a&gt; and the lovely &lt;a href=&quot;https://boardgamegeek.com/boardgame/279537/search-planet-x&quot;&gt;The Search for Planet X&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;🎲 We released tickets for &lt;a href=&quot;https://www.bastionwales.com/&quot;&gt;Bastion&lt;/a&gt;, a small (around 100 people) board game meet-up I run with some friends. They sold out in 2 days - clearly everyone else is as eager as we are to get back to it after missing a year!&lt;/li&gt;
&lt;li&gt;🏡 The new raised beds are finally finished. Next up, filling them. We&apos;re aiming for a &lt;a href=&quot;https://en.wikipedia.org/wiki/H%C3%BCgelkultur&quot;&gt;hügelkultur&lt;/a&gt;-inspired approach to minimise the cost and labour required to fill up the deeper beds. We&apos;ll see how that goes.
{% image &quot;src/posts/2021-08-27-august-braindump/raised_beds.jpg&quot;, &quot;Finished raised beds&quot; %}&lt;/li&gt;
&lt;li&gt;📹 I wrote a blog post about my adventures in &lt;a href=&quot;/posts/make-your-webcam-look-slightly-more-professional/&quot;&gt;background blurring my webcam on Linux&lt;/a&gt;. It&apos;s been fun to play around with &lt;a href=&quot;https://github.com/tensorflow/tfjs-models/tree/master/body-pix&quot;&gt;BodyPix&lt;/a&gt; - now I have far too many fun ideas on what to do with this in conjunction with &lt;a href=&quot;https://www.home-assistant.io/&quot;&gt;Home Assistant&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>Make Your Webcam Look Slightly More Professional (mainly on Linux)</title><link>https://usher.dev/posts/2021-08-26-make-your-webcam-look-slightly-professional/</link><guid isPermaLink="true">https://usher.dev/posts/2021-08-26-make-your-webcam-look-slightly-professional/</guid><pubDate>Thu, 26 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
(alternate title: Overcomplicating Things That Aren&apos;t That Important)

&lt;p&gt;Being annoyed with Zoom&apos;s lack of smart-background blurring on Linux led me down a rabbit-hole out of which I have only just emerged, covered in who-knows-what and left with a sense that I should probably have left the poor rabbits alone.&lt;/p&gt;
&lt;p&gt;Do you want to make this questionably improved transformation to your cheap webcam:&lt;/p&gt;
&lt;/div&gt;






&lt;p&gt;With nothing but a browser, broadcasting software, a reasonable understanding of launch flags, a high tolerance for frustration and a decent chunk of your valuable time? Well have I got the blog post for you!&lt;/p&gt;
&lt;h2&gt;Blurring it Out&lt;/h2&gt;

First up, this process does require running [Open Broadcaster Software (OBS)](https://obsproject.com/). This will give us the tools we need to import all sorts of sources (like your webcam), apply filters to them and create a new &apos;Virtual&apos; webcam with those filters applied.


&lt;p&gt;The most complicated part of this process is getting blurring working. There are plenty of options out there (such as &lt;a href=&quot;https://github.com/royshil/obs-backgroundremoval&quot;&gt;OBS Background Removal&lt;/a&gt; - if you fancy compiling your own OBS plugin.), but none were quite as easy to use as I&apos;d like.&lt;/p&gt;
&lt;p&gt;For a while, I used the very useful &lt;a href=&quot;https://vdo.ninja/&quot;&gt;VDO.ninja&lt;/a&gt;, which lets you broadcast your camera from one browser instance to any other browser, applying filters such as background removal on the way.&lt;/p&gt;
&lt;p&gt;By opening the &apos;viewing&apos; URL using OBS&apos; embedded browser source, you can pull together multiple video streams, ideal for composing group calls inside OBS.&lt;/p&gt;
&lt;p&gt;Where this comes in handy in this case is that you can use this for a single stream, but it does mean leaving the &apos;server&apos; browser tab open and active.&lt;/p&gt;
&lt;p&gt;Taking heavy inspiration from VDO.ninja, I scraped together a single-page-application, &lt;a href=&quot;https://github.com/tomusher/lazy-webcam-blur&quot;&gt;Lazy Webcam Blur&lt;/a&gt; which uses some of the same methods of blurring (&lt;a href=&quot;https://github.com/tensorflow/tfjs-models/tree/master/body-pix&quot;&gt;BodyPix&lt;/a&gt; + &lt;a href=&quot;https://www.tensorflow.org/js&quot;&gt;TensorflowJS&lt;/a&gt;) as VDO.ninja, but doesn&apos;t do anything else - no UI and no interactions needed.&lt;/p&gt;

You can find a hosted version of Lazy Webcam Blur over at [`https://lazy-webcam-blur.pages.dev/`](https://lazy-webcam-blur.pages.dev)


&lt;p&gt;This page can be added as a &apos;Browser&apos; source in OBS to bring in a pre-blurred webcam video stream.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In OBS, under Sources, click the &apos;+&apos; and select &apos;Browser&apos;
&amp;lt;Image src={import(&quot;./obs_add_source.png&quot;)} alt=&quot;Adding a browser source&quot; /&amp;gt;&lt;/li&gt;
&lt;li&gt;Give your source a useful name and click OK.&lt;/li&gt;
&lt;li&gt;Enter the &lt;code&gt;https://lazy-webcam-blur.pages.dev&lt;/code&gt; in the URL and set your target resolution - I use 1280x720.
&amp;lt;Image src={import(&quot;./obs_browser_source.png&quot;)} alt=&quot;Browser source settings&quot; /&amp;gt;&lt;/li&gt;
&lt;li&gt;After adding the source, stretch it to fill the scene.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Unfortunately, out of the box, this &lt;strong&gt;probably won&apos;t work&lt;/strong&gt;. The embedded browser is waiting for you to confirm permissions to use your webcam, but there&apos;s no UI to let you do that.&lt;/p&gt;
&lt;p&gt;To solve this, we&apos;re going to need to add the &lt;code&gt;--use-fake-ui-for-media-stream&lt;/code&gt; flag when launching OBS. How this happens will depend on your distribution/how you&apos;ve installed OBS, but you have two main options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Launch OBS manually with the flag appended, &lt;code&gt;obs-studio --use-fake-ui-for-media-stream&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Update/make a copy of the &lt;code&gt;.desktop&lt;/code&gt; file used to launch OBS and append the flag to the &lt;code&gt;Exec&lt;/code&gt; value.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Creating a new camera, from your camera&lt;/h2&gt;
&lt;p&gt;Now your slightly-nicer-looking-camera is added as a source in OBS, we can let other applications use it using OBS&apos; built-in &apos;Virtual Camera&apos;.&lt;/p&gt;
&lt;p&gt;As long as you&apos;re using a recent release of OBS, this should be a click of a button:&lt;/p&gt;
&lt;p&gt;&amp;lt;Image src={import(&quot;./obs_start_virtual_camera.png&quot;)} alt=&quot;Start Virtual Camera button&quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;You&apos;ll then be able to select &quot;OBS Virtual Camera&quot; as a webcam in your video conferencing tool-of-choice to get whatever OBS is outputting.&lt;/p&gt;
&lt;h2&gt;Improving the Colours&lt;/h2&gt;
&lt;p&gt;OBS gives us tools to do all sorts of post-processing on sources, but the one I&apos;ve found most useful for improving webcam video is the &quot;Apply LUT&quot; filter. A LUT is a colour-mapping preset generally used for colour correction - but in this case we&apos;re going to take the non-scientific approach of &apos;just making it look nice&apos;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Grab some LUT files - I use &lt;a href=&quot;https://obsproject.com/forum/resources/free-lut-filter-pack.594/&quot;&gt;this pack&lt;/a&gt; from OBS user Jordan Wages.&lt;/li&gt;
&lt;li&gt;Right click your browser source in OBS and select &apos;Filters&apos;.&lt;/li&gt;
&lt;li&gt;Under &apos;Effect Filters&apos;, click the &apos;+&apos; symbol and select &apos;Apply LUT&apos;.&lt;/li&gt;
&lt;li&gt;Browse to where you have downloaded/extracted your LUT files and experiment with them to find the one you like the best. I like &lt;code&gt;webcam_preset_3.png&lt;/code&gt; from Jordan&apos;s pack.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;It is Done&lt;/h2&gt;
&lt;p&gt;Success! A slightly improved webcam image with far too much effort, relying on an entire browser engine running in the background.&lt;/p&gt;
&lt;p&gt;Yes, it&apos;s not perfect - I&apos;d love to see simpler tools for this process pop up in the future - do give me a shout if you come across anything!&lt;/p&gt;
</content:encoded></item><item><title>July 2021 Braindump</title><link>https://usher.dev/posts/2021-08-06-july-braindump/</link><guid isPermaLink="true">https://usher.dev/posts/2021-08-06-july-braindump/</guid><pubDate>Fri, 06 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
Some stuff that happened in July 2021
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🪶 &lt;a href=&quot;https://github.com/wagtail/wagtail&quot;&gt;Wagtail&lt;/a&gt;, the open-source CMS on which I&apos;ve built my livelihood recently started a &lt;a href=&quot;https://github.com/wagtail/wagtail/wiki/Performance-team&quot;&gt;performance working group&lt;/a&gt;. Already some great ideas as to how we can improve the CMS in the long-term - very much looking forward to seeing what comes out of this.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🎲 Attended the UK Games Expo 2021 show. It&apos;s been great to work with the team here building their new site and infrastructure over the past couple of years. Even with the major setback of having to build a &apos;Virtual&apos; version of the Expo in 2020, I feel like we&apos;ve made some big improvements.&lt;/p&gt;
&lt;p&gt;It was great to see this all come together at the show. While definitely scaled back from the past few years, there&apos;s still something about being amongst other board gamers that reminds you why the hobby is great.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🎮 There was a lot of buzz around Final Fantasy XIV (the MMO) recently, initially after the poor reception to the latest WoW patches, and then after the awful stuff that came out of Activision/Blizzard.&lt;/p&gt;
&lt;p&gt;Despite not having committed to an MMO in many years (only dropping in to the WoW expansions when they release), I still have a soft spot for the genre, so I thought I&apos;d give it a go. Pretty pleased I did right now - very much enjoying the slower pace, the story elements and just exploring a different world.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🏡 Had the exciting experience of hiring a digger and going to town on our garden with it. Not &lt;em&gt;entirely&lt;/em&gt; for entertainment purposes (although my son may have thought otherwise). We&apos;re replacing our slowly rotting raised vegetable beds with new, fresh, sleeper-based ones.&lt;/p&gt;
&lt;p&gt;{% image &quot;src/posts/2021-08-06-july-braindump/digger.jpg&quot;, &quot;Building raised beds&quot; %}&lt;/p&gt;
&lt;p&gt;Despite the painful cost of wood at the moment, I think these will be a huge improvement. Always surprising how much you can learn from a &quot;simple&quot; project like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Designing a wood structure in Fusion 360 (I like to be able to visualise these things).&lt;/li&gt;
&lt;li&gt;How to drive a digger, and how not to tip it over when working on slopes.&lt;/li&gt;
&lt;li&gt;You can buy &lt;em&gt;really&lt;/em&gt; long timber screws.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>May 2021 Braindump</title><link>https://usher.dev/posts/2021-05-18-may-braindump/</link><guid isPermaLink="true">https://usher.dev/posts/2021-05-18-may-braindump/</guid><pubDate>Tue, 18 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
What I&apos;ve been playing, watching and building in May 2021
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🎮 Been enjoying Disco Elysium quite a lot - a really engaging world, story and characters - although it does take the right frame of mind to pick up. Satisfactory has been this month&apos;s mindless &apos;go-to&apos;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;📺 Found &lt;a href=&quot;https://www.youtube.com/channel/UCcXhhVwCT6_WqjkEniejRJQ&quot;&gt;Wintergatan&lt;/a&gt;, who is building a music-playing marble machine. A fascinating constantly evolving piece of engineering.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;⌨️ I finally built my first mechanical keyboard. A GMMK Tenkeyless with Gateron yellows, if you care. It&apos;s nice, but it&apos;s definitely taking some getting used to.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;{% image &quot;src/posts/2021-05-18-may-braindump/keyboard.jpg&quot;, &quot;The fancy new keyboard&quot; %}&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🎲 Boardgaming has sadly taken a backseat recently (I &lt;em&gt;suppose&lt;/em&gt; there&apos;s a good reason), but I was excited to receive my copy of &lt;a href=&quot;https://boardgamegeek.com/boardgame/291572/oath-chronicles-empire-and-exile&quot;&gt;Oath: Chronicles of Empire and Exile&lt;/a&gt; - the new release from Cole Wehrle, the designer of &lt;a href=&quot;https://boardgamegeek.com/boardgame/237182/root&quot;&gt;Root&lt;/a&gt; (one of my favourites in recent years).&lt;/p&gt;
&lt;p&gt;Oath promises to chronicle the history of its world over repeated plays, each game causing consequences for future games. Looking forward to diving in.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>Exposing Home Assistant using Cloudflare Tunnel</title><link>https://usher.dev/posts/2021-04-21-exposing-homeassistant-using-cloudflare-tunnel/</link><guid isPermaLink="true">https://usher.dev/posts/2021-04-21-exposing-homeassistant-using-cloudflare-tunnel/</guid><pubDate>Wed, 21 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
I use the wonderful [Home Assistant](https://www.home-assistant.io/) on our home network for a variety of weird and wonderful automations and as a nice dashboard to all the devices in our home.

&lt;p&gt;Nothing on my home network can be reached from the outside world without a VPN.
Unfortunately, that presents a few issues with Home Assistant:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Accessing the Home Assistant UI from out-and-about is a pain.&lt;/li&gt;
&lt;li&gt;The Home Assistant app can&apos;t report useful information such as location data unless the device is connected to the VPN.&lt;/li&gt;
&lt;li&gt;There are a number of integrations which use webhooks or similar to communicate data to your HA instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So far, I&apos;ve been living with these problems. Exposing my entire HA instance to the world isn&apos;t something I&apos;m comfortable with.&lt;/p&gt;
&lt;/div&gt;
There is a solution for this in the form of [Home Assistant Cloud](https://www.nabucasa.com/) - a paid solution from the creators of Home Assistant.

&lt;p&gt;&lt;strong&gt;If you&apos;re not comfortable with your networking and security knowledge, stop here and go ahead and subscribe to Home Assistant Cloud. It&apos;s very good and a great way to support Home Assistant.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you&apos;re interested in managing a solution for this yourself, read on.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Cloudflare&apos;s &apos;Argo Tunnel&apos; product has been around for a while, providing a tool to create a secure tunnel from any network in to the Cloudflare network, but they&apos;ve recently rebranded it to Cloudflare Tunnel and made it &lt;a href=&quot;https://blog.cloudflare.com/tunnel-for-everyone/&quot;&gt;free to everyone&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here&apos;s how I set it up to expose my Home Assistant instance.&lt;/p&gt;

Before you start, you&apos;ll need a domain set up with DNS managed by Cloudflare.


&lt;h2&gt;Use &lt;code&gt;cloudflared&lt;/code&gt; to configure a tunnel&lt;/h2&gt;
&lt;p&gt;On your home server, use the &lt;code&gt;cloudflared&lt;/code&gt; utility to login to Cloudflare and download a certificate.&lt;/p&gt;
&lt;p&gt;I use the &lt;code&gt;cloudflared&lt;/code&gt; docker container, so to do this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a folder for your &lt;code&gt;cloudflared&lt;/code&gt; configuration to live, I use &lt;code&gt;/etc/cloudflared&lt;/code&gt; on the host.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;docker run --rm -it \
 -v &quot;/etc/cloudflared:/home/nonroot/.cloudflared&quot; \
 --user 1000:1000 \
 cloudflare/cloudflared:2021.4.0 \
 tunnel login
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Replacing &lt;code&gt;--user 1000:1000&lt;/code&gt; with a user/group ID that has access to read and write from your &lt;code&gt;/etc/cloudflared&lt;/code&gt; directory.&lt;/p&gt;
&lt;p&gt; This will provide you with a link to follow to authorise with Cloudflare and to choose a domain to authorise. Once that&apos;s done, &lt;code&gt;cloudflared&lt;/code&gt; will downloaded the generated certificate and place it in your mounted volume at &lt;code&gt;/etc/cloudflared&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create your tunnel:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;docker run --rm -it \
 -v &quot;/etc/cloudflared:/etc/cloudflared&quot; \
 --user 1000:1000 \
 cloudflare/cloudflared:2021.4.0 \
 tunnel create homeassistant
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; This will create a new tunnel named &lt;code&gt;homeassistant&lt;/code&gt; and drop a config file for it in your configuration directory.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a configuration file to route your tunnel to your Home Assistant instance. In &lt;code&gt;/etc/cloudflared/config.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;---

url: homeassistant:8123
tunnel: 12345678-9012-3456-7890-123456789012
credentials-file: /etc/cloudflared/12345678-9012-3456-7890-123456789012.json
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; replacing the &lt;code&gt;tunnel&lt;/code&gt; ID and &lt;code&gt;credentials-file&lt;/code&gt; with a reference to the config file you got from step 3, and replacing the &lt;code&gt;url&lt;/code&gt; with the URL for your Home Assistant instance.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Set up a DNS record for the tunnel&lt;/h2&gt;
&lt;p&gt;In the Cloudflare DNS panel, add a new &lt;code&gt;CNAME&lt;/code&gt; from the subdomain you want your instance to be accessible at, to &lt;code&gt;12345678-9012-3456-7890-123456789012.cfargotunnel.com&lt;/code&gt; - where the ID in the target is the same as the tunnel ID you created previously.&lt;/p&gt;
&lt;p&gt;&amp;lt;Image src={import(&quot;./cloudflare-tunnel-ui.png&quot;)} alt=&quot;Cloudflare DNS UI&quot; /&amp;gt;&lt;/p&gt;
&lt;h2&gt;Start the tunnel&lt;/h2&gt;
&lt;p&gt;You&apos;ll need some way to start your tunnel and keep it running - I&apos;m doing this using &lt;code&gt;docker-compose&lt;/code&gt;, with a &lt;code&gt;docker-compose.yml&lt;/code&gt; that looks a bit like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;version: &apos;3.9&apos;
services:
  cloudflared:
    image: cloudflare/cloudflared:2021.4.0
    user: 1000:1000
    volumes:
      - &quot;/etc/cloudflared:/etc/cloudflared&quot;
    command: &quot;tunnel --config /etc/cloudflared/config.yml run homeassistant&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run &lt;code&gt;docker-compose up -d&lt;/code&gt; to bring up the tunnel.&lt;/p&gt;
&lt;h2&gt;Configuring access&lt;/h2&gt;
&lt;p&gt;The first thing we need to do is give Cloudflare a way to authenticate you so we can make sure access is restricted.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Head over to the &lt;a href=&quot;https://dash.teams.cloudflare.com&quot;&gt;Cloudflare Teams Dashboard&lt;/a&gt; to start configuring access to your tunnel.&lt;/li&gt;
&lt;li&gt;Start at Configuration -&amp;gt; Authentication.&lt;/li&gt;
&lt;li&gt;Click &apos;+ Add&apos; next to Login methods to add your first login method.&lt;/li&gt;
&lt;li&gt;The easiest to get started with here is &apos;One-time PIN&apos;, so choose and enable that.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Next up, we need to configure the tunnel to use this login provider:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to Access -&amp;gt; Applications on the left&lt;/li&gt;
&lt;li&gt;Click &apos;Add an application&apos; and choose &apos;Self-hosted&apos; from the options.&lt;/li&gt;
&lt;li&gt;Give your application a name and provide the domain you set up previously.&lt;/li&gt;
&lt;li&gt;In the next step, create a rule for &apos;Emails&apos; which includes your email address:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;  &amp;lt;Image src={import(&quot;./cloudflare-rule-ui.png&quot;)} alt=&quot;Cloudflare Application Rule UI&quot; /&amp;gt;
5. Leave the &apos;setup&apos; settings as they are and finalise setup.&lt;/p&gt;
&lt;p&gt;Once this is done, you should be able to visit the domain you&apos;ve setup where you&apos;ll be prompted to follow the One-time PIN sign in process. If the entered email matches the one you provided in your rule, you&apos;ll have remote access to your Home Assistant instance!&lt;/p&gt;
&lt;h2&gt;Exposing webhooks&lt;/h2&gt;
&lt;p&gt;Many Home Assistant integrations expose a webhook URL to allow external applications (and mobile apps) to update sensors.
These applications won&apos;t be able to negotiate through the Cloudflare Access authentication process, so to work around this we&apos;ll add a bypass rule specifically for webhooks.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create another application as above, but when prompted for the application domain, enter &lt;code&gt;/api/webhook/*&lt;/code&gt; in the path:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;  &amp;lt;Image src={import(&quot;./cloudflare-webhook-path.png&quot;)} alt=&quot;Setting the webhook path in the Cloudflare UI&quot; /&amp;gt;
2. When setting rules, create a rule with the &apos;Rule action&apos; set to Bypass&apos; and an &apos;Include&apos; rule set to &apos;Everyone&apos;. This will allow anonymous users to bypass authentication.
  &amp;lt;Image src={import(&quot;./cloudflare-rule-include-everyone.png&quot;)} alt=&quot;Setting a rule to bypass everyone in the Cloudflare UI&quot; /&amp;gt;&lt;/p&gt;

Many webhooks are now configured automatically by Home Assistant. To make sure they point to the tunnel URL rather than your internal URL, head over to _Configuration -&amp;gt; General_ in your Home Assistant UI and set the &apos;External URL&apos; value to that of the tunnel you&apos;ve set up.


&lt;p&gt;To set up your Home Assistant mobile app to route sensor data through the tunnel, you&apos;ll need to set up a separate URL for external and internal use. On Android, this is done by setting the &lt;em&gt;Home Assistant URL&lt;/em&gt; setting to the external/tunnel URL, and the &lt;em&gt;Internal Connection URL&lt;/em&gt; to the URL you use while connected to the networks listed in &lt;em&gt;Home Network WiFi SSID&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&amp;lt;Image src={import(&quot;./android-ui.png&quot;)} alt=&quot;Setting up an external URL in the Home Assistant android app&quot; /&amp;gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I&apos;m still experimenting with this so this solution isn&apos;t entirely complete. It works to help limit the exposure of your Home Assistant instance, but it isn&apos;t perfect:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Some integrations don&apos;t use webbooks as a means to communicate with HA, so you may find you need to expose different URLs - this isn&apos;t typically well documented so you&apos;ll need to dive in to the code to figure out what you need to configure.&lt;/li&gt;
&lt;li&gt;You&apos;re still exposing part of your Home Assistant instance to the world - if there&apos;s a vulnerability exploitable through the webhook endpoint, this won&apos;t help you.&lt;/li&gt;
&lt;li&gt;The dashboard in the Home Assistant app won&apos;t work with Cloudflare Access in front of it.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.nabucasa.com/&quot;&gt;Home Assistant Cloud&lt;/a&gt; is better, easier, and super cheap. Go and sign up.&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>April 2021 Braindump</title><link>https://usher.dev/posts/2021-04-12-april-braindump/</link><guid isPermaLink="true">https://usher.dev/posts/2021-04-12-april-braindump/</guid><pubDate>Mon, 12 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
A brief summary of what I got up to in April 2021
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🎮 Finished NieR: Automata - when this first came out I was intrigued but not convinced I&apos;d be able to put the 40-odd hours in to get through it.&lt;/p&gt;
&lt;p&gt;When it appeared on Game Pass I thought I&apos;d give it a chance, and I&apos;m very glad I did. An excellent combination of thought-provoking story, fun characters, engaging combat and &lt;em&gt;fantastic&lt;/em&gt; music, sprinkled with a few tropey JRPG bits and a slight lack of respect for the player&apos;s time.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🎲 We&apos;ve been playing quite a bit of &lt;a href=&quot;https://boardgamegeek.com/boardgame/318977/micromacro-crime-city&quot;&gt;MicroMacro: Crime City&lt;/a&gt;, a game that presents you with a large city map and a set of crimes you need to solve by spotting clues sprinkled throughout the city. A lovely unique concept that I&apos;m excited to see more of.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;📺 Discovered &lt;a href=&quot;https://www.youtube.com/channel/UCTGZGNZAZDfCQ2hpIddbfNw&quot;&gt;That Van Jolene&lt;/a&gt; via &lt;a href=&quot;https://www.youtube.com/channel/UCW5OrUZ4SeUYkUg1XqcjFYA&quot;&gt;GeoWizard&lt;/a&gt;. A well-presented and entertaining YouTube series on a couple&apos;s adventures in converting a van in to a campervan. Now I&apos;m tempted to buy a van.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;📖 Currently reading &lt;a href=&quot;https://www.goodreads.com/book/show/101438.Skunk_Works&quot;&gt;Skunk Works: A Personal Memoir of My Years at Lockheed&lt;/a&gt;. I&apos;m not usually interested in aerospace or the military, but this book&apos;s story of the engineering effort that goes in to building one of these machines is amazing. There&apos;s a few interesting lessons on security here too!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🏠 I bought a &lt;a href=&quot;https://coral.ai/&quot;&gt;Coral Edge TPU&lt;/a&gt; and set it up with &lt;a href=&quot;https://github.com/blakeblackshear/frigate&quot;&gt;Frigate&lt;/a&gt; on our home server to do real-time object detection on my cameras.&lt;/p&gt;
&lt;p&gt;I&apos;ve done a lot of of experimenting with various self-hosted camera monitoring tools (&lt;a href=&quot;https://github.com/ccrisan/motioneye&quot;&gt;MotionEye&lt;/a&gt;, &lt;a href=&quot;https://shinobi.video/&quot;&gt;Shinobi&lt;/a&gt;, &lt;a href=&quot;https://zoneminder.com/&quot;&gt;ZoneMinder&lt;/a&gt;, &lt;a href=&quot;https://blueirissoftware.com/&quot;&gt;Blue Iris&lt;/a&gt;) - while they all have their advantages, they all fall short on motion/object detection. Offloading this to an Edge TPU with Frigate is working out great.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🌻 Our greenhouse has been in a state of disrepair for a while - so we finally got around to replacing all the broken glass panels with corrugated PVC sheets (not quite as pretty, but way more rugged for our windy garden).&lt;/p&gt;
&lt;p&gt;We also installed fancy automatic window openers to control the temperature in there. These are such clever, simple pieces of engineering made up of a piston filled with wax which expands and contracts based on the temperature. Magic.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;💻 I discovered and switched to &lt;a href=&quot;https://asdf-vm.com&quot;&gt;&lt;code&gt;asdf&lt;/code&gt;&lt;/a&gt; which replaces tools like &lt;code&gt;pyenv&lt;/code&gt;, &lt;code&gt;nvm&lt;/code&gt; and &lt;code&gt;rbenv&lt;/code&gt; with a single tool. Very happy with it so far.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>GPT Board Game Titles</title><link>https://usher.dev/posts/2021-04-08-gpt-board-game-titles/</link><guid isPermaLink="true">https://usher.dev/posts/2021-04-08-gpt-board-game-titles/</guid><pubDate>Thu, 08 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
It is 2021, a blog can&apos;t exist without an &quot;I trained an AI&quot; post like these trailblazers:

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://janellecshane.substack.com/p/gpt-3-tries-pickup-lines&quot;&gt;GPT-3 tries pickup lines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://twitter.com/minimaxir/status/1064604986951163905&quot;&gt;Max Woolf&apos;s Buzzfeed YouTube video titles&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So here we are.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;When we were building out Virtually Expo (UK Games Expo &apos;gone virtual&apos; for a pandemic-stricken world), we built a Discord bot that let visitors join one of UKGEs famed queues.&lt;/p&gt;
&lt;p&gt;One of the things this bot did was to give you a random, AI generated board game when you reached the &apos;front&apos; of the queue. These were generated in advance by training a GPT-2 model (with the aid of Max Woolf&apos;s fantastic &lt;a href=&quot;https://github.com/minimaxir/gpt-2-simple&quot;&gt;gpt-2-simple&lt;/a&gt;) on the top 2,000 board games from &lt;a href=&quot;https://www.boardgamegeek.com&quot;&gt;BoardGameGeek&lt;/a&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;First up; if you&apos;re not familiar with the board game world - there&apos;s a &lt;em&gt;lot&lt;/em&gt; of games named after places. From &lt;a href=&quot;https://boardgamegeek.com/boardgame/822/carcassonne&quot;&gt;Carcassonne&lt;/a&gt;, through &lt;a href=&quot;https://boardgamegeek.com/boardgame/164928/orleans&quot;&gt;Orléans&lt;/a&gt; and &lt;a href=&quot;https://boardgamegeek.com/boardgame/172386/mombasa&quot;&gt;Mombasa&lt;/a&gt;, to &lt;a href=&quot;https://boardgamegeek.com/boardgame/136888/bruges&quot;&gt;Bruges&lt;/a&gt; and &lt;a href=&quot;https://boardgamegeek.com/boardgame/119432/snowdonia&quot;&gt;Snowdonia&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The model seemed to pick up on this trend and continued the theme with ideas such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;France&lt;/li&gt;
&lt;li&gt;Monmouth Tycoon&lt;/li&gt;
&lt;li&gt;Exeter&lt;/li&gt;
&lt;li&gt;High Wycombe&lt;/li&gt;
&lt;li&gt;Choose Your Own Adventure: Argentina&lt;/li&gt;
&lt;li&gt;Turning Point: Swansea&lt;/li&gt;
&lt;li&gt;Crisis in Northumberland&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Then there&apos;s the expansion trend. Carcassonne has somewhere between 10 and 134 expansions, depending on how you&apos;re counting. If the publisher ever runs out of ideas, this model has them covered:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Carcassonne: Yard Edition&lt;/li&gt;
&lt;li&gt;Carcassonne: New York&lt;/li&gt;
&lt;li&gt;Carcassonne: Vale of the Fallen Dragons of War&lt;/li&gt;
&lt;li&gt;Carcassonne: Abyss – The Lost Chronicles&lt;/li&gt;
&lt;li&gt;Carcassonne: Asteroid Derby&lt;/li&gt;
&lt;li&gt;Carcassonne: The Cards are on the Rise&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;along with positive vibes from the appropriately titled:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pandemic: Alive!&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;There&apos;s the expansions that promise to change the game quite significantly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ticket to Ride: The Quest for the Galaxy&lt;/li&gt;
&lt;li&gt;Rallyman Gettysburg&lt;/li&gt;
&lt;li&gt;Animal Upon Animal: The Camelot Campaign&lt;/li&gt;
&lt;li&gt;Unlock! The Impacts of Crime&lt;/li&gt;
&lt;li&gt;Rory&apos;s Story Cubism&lt;/li&gt;
&lt;li&gt;Smash Up: First Person Shooter&lt;/li&gt;
&lt;li&gt;Power Grid: The Next Phase of Electronic Warfare&lt;/li&gt;
&lt;li&gt;Zooloretto: Run!&lt;/li&gt;
&lt;li&gt;Ticket to Ride: The Official Route&lt;/li&gt;
&lt;li&gt;Field Commander: Cthulhu&lt;/li&gt;
&lt;li&gt;Catan: All the Pretty Things&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I am tickled by the idea of &lt;a href=&quot;https://boardgamegeek.com/boardgame/27588/zooloretto&quot;&gt;Zooloretto&lt;/a&gt;, a game in which you carefully plan a zoo filled with cute animals to attract visitors, turned on its head by the addition of the &apos;Run!&apos; expansion.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;There&apos;s the philosophical:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Who Will Win? Modern Magic: The Gathering&lt;/li&gt;
&lt;li&gt;Where Did All the Money Go?&lt;/li&gt;
&lt;li&gt;If There Were No Dragon...&lt;/li&gt;
&lt;li&gt;Something Big Is Bluffling the Earth&lt;/li&gt;
&lt;li&gt;Mythic Tales: All the President&apos;s Cards Are Spell Cards!&lt;/li&gt;
&lt;li&gt;Fascism!&lt;/li&gt;
&lt;li&gt;Long Story Short: You Can&apos;t Always Get What You Want&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I checked, &apos;bluffling&apos; isn&apos;t a word, but I can definitely agree that something big is indeed bluffling the Earth.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;There&apos;s the games I&apos;d play in an instant:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Valley of the Uzis&lt;/li&gt;
&lt;li&gt;I Cast a Spell: The Card Game&lt;/li&gt;
&lt;li&gt;Accursed Gratuitous Sarcasm&lt;/li&gt;
&lt;li&gt;Einstein: Son of a crab and his business partner embark on a daring mission&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;and the ones that are perhaps for a more niche audience:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Battle Masters: The Fate of Meddle and Sump-Hole goats&lt;/li&gt;
&lt;li&gt;Cottage Cheese&lt;/li&gt;
&lt;li&gt;My Wife Is Alive!&lt;/li&gt;
&lt;li&gt;Quirky Fate: You Win! War of Entrails&lt;/li&gt;
&lt;li&gt;Funky Poop&lt;/li&gt;
&lt;li&gt;10 Days, No Jumpers&lt;/li&gt;
&lt;li&gt;Pyromania Board Game&lt;/li&gt;
&lt;li&gt;Nuns on the Underground&lt;/li&gt;
&lt;li&gt;Rent a Bear In The Night&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;The &apos;creative use of an IP&apos; award goes to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Warhammer 40,000: The Futon War&lt;/li&gt;
&lt;li&gt;Bob Ross: Guitar Hero&lt;/li&gt;
&lt;li&gt;Dice Throne: Season Two – Star Wars Battle for Aragorn&lt;/li&gt;
&lt;li&gt;Hannibal: Family&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;It seems to have a slightly confusing tendency to generate fish-themed games:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fish Prince&lt;/li&gt;
&lt;li&gt;Piranha Detective&lt;/li&gt;
&lt;li&gt;My Fish is Fish&lt;/li&gt;
&lt;li&gt;Flying Carp&lt;/li&gt;
&lt;li&gt;Rolling Fishes&lt;/li&gt;
&lt;li&gt;Death to the Lobster&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;of which, &apos;My Fish is Fish&apos; raises questions I do not have answers for.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Then there&apos;s the titles that cause genuine concern about the future of AI:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;EXPLOSION: A New World&lt;/li&gt;
&lt;li&gt;Our Souls Are Marketable&lt;/li&gt;
&lt;li&gt;Unconditional Surrender-era&lt;/li&gt;
&lt;li&gt;No Peace Without Fire&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Yep. We should probably stop doing this before it&apos;s too late.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Finally, a few I liked:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Road to Wales&lt;/li&gt;
&lt;li&gt;Zombie and Screamin&apos; Zombies&lt;/li&gt;
&lt;li&gt;Explosive Substances Control 4: Harry the Accuser&lt;/li&gt;
&lt;li&gt;TATOOOMPI&lt;/li&gt;
&lt;li&gt;Capital Letters&lt;/li&gt;
&lt;li&gt;Get Off My Lawn! Unfairly Impeded&lt;/li&gt;
&lt;li&gt;Do Something Crazy&lt;/li&gt;
&lt;li&gt;Microsoft Word&lt;/li&gt;
&lt;li&gt;Fabio&apos;s Garage&lt;/li&gt;
&lt;li&gt;There&apos;s Something Shifting In Paris&lt;/li&gt;
&lt;li&gt;Escape Goat &amp;amp; Cat with Guest Cat&lt;/li&gt;
&lt;li&gt;Band-Aid Man&lt;/li&gt;
&lt;li&gt;Billionaire Bus Driver&lt;/li&gt;
&lt;li&gt;Bulk Spaghetti&lt;/li&gt;
&lt;li&gt;Cryptocop&lt;/li&gt;
&lt;li&gt;The Captain Is, But He&apos;s Bigger&lt;/li&gt;
&lt;li&gt;Pie&lt;/li&gt;
&lt;li&gt;Gravy Lobby&lt;/li&gt;
&lt;li&gt;Whoopi Goldberg&lt;/li&gt;
&lt;li&gt;YAM!&lt;/li&gt;
&lt;li&gt;Tesla and the Other Robots&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>My Email Setup with Notmuch and Alot</title><link>https://usher.dev/posts/2021-03-17-my-email-setup/</link><guid isPermaLink="true">https://usher.dev/posts/2021-03-17-my-email-setup/</guid><pubDate>Wed, 17 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;div&gt;
Email is annoying. Doubly so when you&apos;re a contractor and have a separate inbox for every client.
Having to keep 4 Gmail tabs open to keep track of incoming requests feels like a waste of brain power and space.
&lt;/div&gt;

&lt;p&gt;I&apos;ve spent a long time looking for a solution that solves all my needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Gives me a unified inbox across all my email accounts on my Linux desktop&lt;/li&gt;
&lt;li&gt;Lets me receive notifications for important emails anywhere (desktop, laptop or phone)&lt;/li&gt;
&lt;li&gt;Allows me to quickly scan and archive emails that I need to see, but don&apos;t need to action (notifications, alerts, etc.)&lt;/li&gt;
&lt;li&gt;Supports authentication with Gmail accounts via OAuth&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first and most obvious solution is a GUI mail client. Mailspring, Thunderbird, Evolution and many of others have crossed my desktop. These are all great tools, but ultimately I find that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Configuring them with Gmail OAuth is awkward or not possible&lt;/li&gt;
&lt;li&gt;They are slow, especially with a lot of emails cached locally&lt;/li&gt;
&lt;li&gt;Search performance isn&apos;t great - I end up having to login to Gmail whenever I need to search&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&apos;m not the sort of person who lives in the terminal, I like a good GUI when it serves a purpose and having previously ventured in to to trying &lt;code&gt;mutt&lt;/code&gt; and not wanting to invest the time to learn the keyboard shortcuts, I wasn&apos;t particularly inclined to go down that route again. How naive I was.&lt;/p&gt;
&lt;p&gt;Some time ago I had configured &lt;a href=&quot;https://isync.sourceforge.io/&quot;&gt;&lt;code&gt;isync&lt;/code&gt;&lt;/a&gt; to keep local backups of my mail. If all my mail was being regularly synced for backups in &lt;code&gt;maildir&lt;/code&gt; format, couldn&apos;t I read it directly from there?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Thunderbird has &lt;a href=&quot;https://support.mozilla.org/en-US/kb/maildir-thunderbird&quot;&gt;Maildir support&lt;/a&gt; but has some scary warnings about it being an experimental feature.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mutt&lt;/code&gt;/&lt;code&gt;neomutt&lt;/code&gt; are nice, but the aforementioned fear of learning a bunch of new keybindings kept me away - it&apos;s also a bit of a pain to configure with multiple inboxes and search is still slow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Eventually, I rediscovered &lt;code&gt;notmuch&lt;/code&gt; and a well-supported client, &lt;code&gt;alot&lt;/code&gt; - which I&apos;ve been using for a few months very happily. Let&apos;s tackle the setup:&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;Step 1: Grabbing email&lt;/h2&gt;
&lt;p&gt;The first thing I need is to grab all my email from my various mail providers and copy those emails locally. This gives me local backups, the ability to search (I&apos;ll come back to that in a bit), and a way to quickly access, read and manipulate email locally.&lt;/p&gt;
&lt;p&gt;To do this, I use &lt;a href=&quot;https://isync.sourceforge.io/&quot;&gt;&lt;code&gt;isync&lt;/code&gt;&lt;/a&gt; (the tool is called &lt;code&gt;isync&lt;/code&gt;, the binary is called &lt;code&gt;mbsync&lt;/code&gt;) to synchronise a local copy with each IMAP server. My config looks a bit like this:&lt;/p&gt;
&lt;h4&gt;~/.config/mbsync/mbsyncrc&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;Expunge None
Create Near
SyncState *

IMAPAccount my-email@gmail.com
Host imap.gmail.com
User my-email@gmail.com
AuthMechs XOAUTH2
PassCmd &quot;oauth2get google my-email@gmail.com&quot;
SSLType IMAPS
SSLVersions TLSv1.1 TLSv1.2

IMAPStore my-email@gmail.com-remote
Account my-email@gmail.com

MaildirStore my-email@gmail.com-local
Path ~/mail/my-email@gmail.com/
Inbox ~/mail/my-email@gmail.com/Inbox
SubFolders Verbatim

MaildirStore my-email@gmail.com-archive
Path ~/mail/archive-my-email@gmail.com/

Channel my-email@gmail.com-archive
Far &quot;:my-email@gmail.com-remote:[Google Mail]/All Mail&quot;
Near &quot;:my-email@gmail.com-archive:Archive&quot;
Sync All

Channel my-email@gmail.com-trash
Far &quot;:my-email@gmail.com-remote:[Google Mail]/Bin&quot;
Near &quot;:my-email@gmail.com-archive:Trash&quot;
Sync Pull

Channel my-email@gmail.com-drafts
Far &quot;:my-email@gmail.com-remote:[Google Mail]/Drafts&quot;
Near &quot;:my-email@gmail.com-local:Drafts&quot;
Sync Pull
Expunge Both

Channel my-email@gmail.com-sent
Far &quot;:my-email@gmail.com-remote:[Google Mail]/Sent Mail&quot;
Near &quot;:my-email@gmail.com-local:Sent&quot;
Sync Pull
Expunge Both

Channel my-email@gmail.com-inbox
Far &quot;:my-email@gmail.com-remote:INBOX&quot;
Near &quot;:my-email@gmail.com-local:Inbox&quot;
Sync All
Expunge Both

Group my-email@gmail.com
Channel my-email@gmail.com-trash
Channel my-email@gmail.com-inbox
Channel my-email@gmail.com-drafts
Channel my-email@gmail.com-sent
Channel my-email@gmail.com-archive

# ... repeat for every mail account

Group all
Channel my-email@gmail.com-trash
Channel my-email@gmail.com-inbox
Channel my-email@gmail.com-drafts
Channel my-email@gmail.com-sent
Channel my-email@gmail.com-archive
# ... repeat for every mail account
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each of my email accounts, this defines:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;An &lt;code&gt;IMAPAccount&lt;/code&gt; containing the server details and credentials for the account.&lt;/li&gt;
&lt;li&gt;An &lt;code&gt;IMAPStore&lt;/code&gt; for that account. This tool syncs between two stores - so we define this to give it an entity from which to sync.&lt;/li&gt;
&lt;li&gt;Two &lt;code&gt;MaildirStore&lt;/code&gt; entries - one for &apos;active&apos; email, and one for the archived email - keeping them separate makes it easier to backup only the archives and gives me the option to access only the &apos;active&apos; emails if I want to use clients like &lt;code&gt;mutt&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A set of &lt;code&gt;Channels&lt;/code&gt; - the config entry used for defining how two stores are synchronised. I create a seperate channel for each key folder within my remote IMAP store so that I can keep them consistently named locally across different mail providers.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Group&lt;/code&gt; listing all my defined channels, so that running &lt;code&gt;mbsync all&lt;/code&gt; syncs everything.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I have &lt;code&gt;mbsync all&lt;/code&gt; configured to run on a systemd timer every 2 minutes:&lt;/p&gt;
&lt;h4&gt;~/.config/systemd/user/mbsync-run.service&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;[Unit]
Description=Sync mail with mbsync
OnFailure=status-email-user@%n.service

[Service]
Type=oneshot
Nice=10
ExecStart=/usr/bin/mbsync --config /home/tom/.config/mbsync/mbsyncrc all
Environment=&quot;HOME=/home/tom&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;~/.config/systemd/user/mbsync-run.timer&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;[Unit]
Description=Sync emails with mbsync on schedule

[Timer]
OnBootSec=2m
OnUnitActiveSec=2m
Persistent=true

[Install]
WantedBy=timers.target
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2&gt;Step 2: Indexing&lt;/h2&gt;
&lt;p&gt;Now all my emails are being synced locally on a schedule, I need some way to make sense of them. This is where &lt;a href=&quot;https://notmuchmail.org/&quot;&gt;&lt;code&gt;notmuch&lt;/code&gt;&lt;/a&gt; comes in.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;notmuch&lt;/code&gt; scans your &lt;code&gt;maildir&lt;/code&gt; formatted directory and adds everything in to a Xapian index giving you a nice CLI tool to search through all your emails &lt;em&gt;really&lt;/em&gt; quickly.&lt;/p&gt;
&lt;p&gt;Running &lt;code&gt;notmuch new&lt;/code&gt; will walk you through getting it all set up - you can then edit your config file as you need.&lt;/p&gt;


&lt;p&gt;notmuch doesn&apos;t read its config from &lt;code&gt;XDG_CONFIG_HOME&lt;/code&gt; by default, but it will read a config location from the &lt;code&gt;NOTMUCH_CONFIG&lt;/code&gt; environment variable.
If you like to keep your home directory clean, add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;export NOTMUCH_CONFIG=$XDG_CONFIG_HOME/notmuch/config
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to your &lt;code&gt;.zshenv&lt;/code&gt;, &lt;code&gt;.bash_profile&lt;/code&gt; or equivalent.
&lt;/p&gt;&lt;p&gt;&lt;/p&gt;
&lt;h4&gt;~/.config/notmuch/config&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;[database]
path=/home/tom/mail

[user]
name=Tom Usher
primary_email=tom@tomusher.com
other_email=my-email@gmail.com

[new]
tags=new;
ignore=

[search]
exclude_tags=deleted;spam;

[maildir]
synchronize_flags=true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most important thing here for me is the &lt;code&gt;tags&lt;/code&gt; definition in the &lt;code&gt;[new]&lt;/code&gt; section - this tells Notmuch that when it finds new mail, it should mark it in its internal tagging system as &lt;code&gt;new&lt;/code&gt;. The default option here is to set both the &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;unread&lt;/code&gt; tags, but as I may have read this email using a different client, I prefer the unread flag to by synchronised from the maildir using the &lt;code&gt;synchronize_flags&lt;/code&gt; setting.&lt;/p&gt;
&lt;p&gt;To tell &lt;code&gt;notmuch&lt;/code&gt; to index new mail when it comes in, I add an &lt;code&gt;ExecStartPost&lt;/code&gt; command to my &lt;code&gt;systemd&lt;/code&gt; unit:&lt;/p&gt;
&lt;h4&gt;~/.config/systemd/user/mbsync-run.service&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;[Unit]
Description=Sync mail with mbsync
OnFailure=status-email-user@%n.service

[Service]
Type=oneshot
Nice=10
ExecStart=/usr/bin/mbsync --config /home/tom/.config/mbsync/mbsyncrc all
ExecStartPost=/usr/bin/notmuch new
Environment=&quot;HOME=/home/tom&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can do all sorts of handy things with the &lt;code&gt;notmuch&lt;/code&gt; CLI:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;notmuch search tag:unread&lt;/code&gt; - list all the emails I haven&apos;t read&lt;/li&gt;
&lt;li&gt;&lt;code&gt;notmuch tag -unread -- tag:unread&lt;/code&gt; - remove the &lt;code&gt;unread&lt;/code&gt; tag from all the mail tagged &lt;code&gt;unread&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;notmuch tag important -- tag:unread and critical&lt;/code&gt; - tag anything unread containing the term &apos;critical&apos; as &lt;code&gt;important&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;notmuch search date:2020-12-01..2020-12-31 and from:auto-confirm@amazon.co.uk&lt;/code&gt; - find all my order receipts from Amazon in December 2020.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is useful, but it&apos;s not the nicest way to regularly read email. That&apos;s where the number of available &lt;a href=&quot;https://notmuchmail.org/frontends/&quot;&gt;notmuch frontends&lt;/a&gt; comes in.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;3. Reading Email&lt;/h2&gt;
&lt;p&gt;Of the frontend applications supporting &lt;code&gt;notmuch&lt;/code&gt; that I&apos;ve tried, I ended up sticking with &lt;a href=&quot;https://github.com/pazz/alot&quot;&gt;&lt;code&gt;alot&lt;/code&gt;&lt;/a&gt; which I find to be a great balance between simplicity and customisability.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;alot&lt;/code&gt; has a bunch of neat features, but it does what I want particularly well:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A simple overview of mail across all my inboxes in one panel&lt;/li&gt;
&lt;li&gt;Very quick to start and flick through emails&lt;/li&gt;
&lt;li&gt;Lets me archive emails quickly with the default &lt;code&gt;a&lt;/code&gt; keybind - which toggles the &lt;code&gt;inbox&lt;/code&gt; flag&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I don&apos;t customise it much, other than defining an &apos;account&apos; for sending email, and adding a useful &lt;a href=&quot;https://github.com/pazz/alot/wiki/Contrib-Hooks#open-html-emails-in-external-browser&quot;&gt;open in browser&lt;/a&gt; custom hook - which lets me press a key to view an email in the browser - useful for those complicated HTML emails.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[accounts]
    [[personal]]
        realname = Tom Usher
        address = tom@tomusher.com
        sendmail_command = msmtp --account=personal -t
        sent_box = maildir:///home/tom/mail/my-email@gmail.com/Sent
        draft_box = maildir:///home/tom/mail/my-email@gmail.com/Drafts

[bindings]
  [[thread]]
  b = call hooks.open_in_browser(ui)
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2&gt;4. Sending email&lt;/h2&gt;
&lt;p&gt;I don&apos;t actually do much email sending - for better or for worse most of my communication has moved to messaging platforms.&lt;/p&gt;
&lt;p&gt;It is handy to have something to easily send emails without launching Gmail, and for that I use &lt;a href=&quot;https://marlam.de/msmtp/&quot;&gt;&lt;code&gt;msmtp&lt;/code&gt;&lt;/a&gt;. This can be configured using a relatively straightforward config file:&lt;/p&gt;
&lt;h4&gt;~/.config/msmtp/config&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;defaults
auth    on
tls     on
tls_trust_file  /etc/ssl/certs/ca-certificates.crt
host        smtp.gmail.com
port        587
auth        oauthbearer

# Personal
account     personal
from        tom@tomusher.com
user        my-email@gmail.com
passwordeval    oauth2get google my-email@gmail.com

# ... repeat for each account

account default : personal
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I can then use the &apos;account&apos; I previously configured in &lt;code&gt;alot&lt;/code&gt; to send and reply to emails.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;5. Making it work with Gmail&lt;/h2&gt;
&lt;p&gt;Most clients I work with use GSuite for email - it&apos;s quick to set up, works well and provides an excellent web-based client.&lt;/p&gt;
&lt;p&gt;Because I&apos;ve gone down this whole weird route, I do need to make some adjustments to cope with Gmail&apos;s oddities.&lt;/p&gt;
&lt;p&gt;First up; &apos;archiving&apos; an email via IMAP in Gmail might not be what you expect. Incoming emails end up in both your &lt;code&gt;All Mail&lt;/code&gt; folder as well as your &lt;code&gt;Inbox&lt;/code&gt;. To archive something, you need to delete it from your &lt;code&gt;Inbox&lt;/code&gt;, leaving the copy in &lt;code&gt;All Mail&lt;/code&gt; intact.&lt;/p&gt;
&lt;p&gt;I have a script which does that on the local copy of my inboxes by searching for files in any &lt;code&gt;Inbox&lt;/code&gt; folder that no longer have the tag &lt;code&gt;inbox&lt;/code&gt; (i.e. I&apos;ve archived them in &lt;code&gt;alot&lt;/code&gt;), use &lt;code&gt;grep&lt;/code&gt; to filter the results to only show mail from my &lt;code&gt;Inbox&lt;/code&gt; (as &lt;code&gt;notmuch search&lt;/code&gt; will return every copy of the email it found, including the one in my &lt;code&gt;Archive&lt;/code&gt; folder), then delete the remaining items.&lt;/p&gt;
&lt;p&gt;Once they&apos;ve been removed, &lt;code&gt;mbsync&lt;/code&gt; will synchronise those changes back to Gmail, removing the copy from my Gmail &lt;code&gt;Inbox&lt;/code&gt;, effectively archiving the email.&lt;/p&gt;
&lt;h4&gt;~/.mail/scripts/notmuch-move.sh&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash
echo &quot;Removing emails deleted from inbox&quot;
notmuch new --no-hooks
notmuch search --output=files --format=text folder:/Inbox/ -tag:inbox | grep Inbox | xargs --no-run-if-empty rm
notmuch new --no-hooks
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next problem is that if I have the Gmail web client open, or am checking emails on my phone, I&apos;m likely to have read and archived an email before it hits my computer. All new incoming email will be marked as &lt;code&gt;new&lt;/code&gt; by &lt;code&gt;notmuch&lt;/code&gt;, even if it has already been read or archived.&lt;/p&gt;
&lt;p&gt;To resolve that, I treat the &lt;code&gt;new&lt;/code&gt; tag as &apos;to be processed&apos;, and do all my post-processing on emails with that tag. For example, this script tags everything &lt;code&gt;new&lt;/code&gt; and in an &lt;code&gt;Inbox&lt;/code&gt; folder with the &lt;code&gt;inbox&lt;/code&gt; tag, and then removes the &lt;code&gt;new&lt;/code&gt; tag from everything:&lt;/p&gt;
&lt;h4&gt;~/.mail/scripts/notmuch-tag.sh&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash
echo &quot;Tagging new inboxed mail&quot;
notmuch tag +inbox -- folder:/Inbox/ and tag:new
notmuch tag -new -- tag:new
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both these run as part of my &lt;code&gt;systemd&lt;/code&gt; unit file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[Unit]
Description=Sync mail with mbsync
OnFailure=status-email-user@%n.service

[Service]
Type=oneshot
Nice=10
ExecStartPre=/home/tom/mail/scripts/notmuch-move.sh
ExecStart=/usr/bin/mbsync --config /home/tom/.config/mbsync/mbsyncrc all
ExecStartPost=/usr/bin/notmuch new
ExecStartPost=/home/tom/mail/scripts/notmuch-tag.sh
Environment=&quot;HOME=/home/tom&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, there&apos;s the problem of authorization. For Google accounts, I prefer to authorize with OAuth so that I can make use of 2FA and avoid creating insecure app passwords. To do this, I use the &lt;a href=&quot;https://pypi.org/project/oauth2token/&quot;&gt;&lt;code&gt;oauth2token&lt;/code&gt;&lt;/a&gt; project, which provides two binaries to create and get an OAuth token from local storage. &lt;code&gt;mbsync&lt;/code&gt; and &lt;code&gt;msmtp&lt;/code&gt; are then configured using &lt;code&gt;PassCmd&lt;/code&gt; and &lt;code&gt;passwordeval&lt;/code&gt; respectively to fetch the token using &lt;code&gt;oauth2get&lt;/code&gt;, which will automatically refresh the token when required.&lt;/p&gt;
&lt;p&gt;The XOAUTH2 mechanism requires an implementation of SASL with a XOAUTH2 plugin installed; I use &lt;a href=&quot;https://www.cyrusimap.org/sasl/&quot;&gt;&lt;code&gt;cyrus-sasl&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/moriyoshi/cyrus-sasl-xoauth2&quot;&gt;&lt;code&gt;cyrus-sasl-xoauth2&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This also requires creating an OAuth2 credential in Google Cloud Console - which is a bit outside the scope of this post, but be aware you&apos;ll need to add the &lt;code&gt;https://mail.google.com/&lt;/code&gt; scope to your application, which is a &apos;restricted scope&apos; and makes it extra awkward to do things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can keep the application in testing mode, but your auth tokens will expire after 7 days, so you&apos;ll need to re-authorize annoyingly frequently.&lt;/li&gt;
&lt;li&gt;In testing mode, you&apos;ll get an error if you try to authorize and are logged in with more than one Google account.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To fix those issues, you can choose to &apos;Publish` the application, which will prompt for manual verification by Google, but you don&apos;t actually need to do this - you&apos;ll only get a scary warning whenever you try to login.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;6. Extra Fun Things&lt;/h2&gt;
&lt;p&gt;Now you have a nice CLI for querying your mail, you can make it work for you.&lt;/p&gt;
&lt;p&gt;For example, in my personal &lt;code&gt;notmuch-tag&lt;/code&gt; script, I add the &lt;code&gt;notify&lt;/code&gt; tag to every email that&apos;s unread, new and in my inbox. I have the following script running in an &lt;code&gt;ExecStartPost&lt;/code&gt; on my &lt;code&gt;systemd&lt;/code&gt; unit which notifies me of these new emails, and then removes the notify tag so I don&apos;t get duplicate notifications:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash
SEARCH=&quot;tag:notify&quot;

NOTIFY_COUNT=$(notmuch count &quot;$SEARCH&quot;)

if [ &quot;$NOTIFY_COUNT&quot; -gt 0 ]; then
  RESULTS=$(notmuch search --format=json --output=summary --limit=3 --sort=&quot;newest-first&quot; &quot;$SEARCH&quot; | jq -r &apos;.[] | &quot;\(.authors): \(.subject)&quot;&apos;)
  notify-send &quot;$NOTIFY_COUNT New Emails:&quot; &quot;$RESULTS&quot;
fi

notmuch tag -notify -- tag:notify
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another handy tool is my count of new emails in my &lt;code&gt;polybar&lt;/code&gt;:&lt;/p&gt;
&lt;h4&gt;~/.config/polybar/scripts/mail.sh&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash

EMPTY_INBOX_ICON=&quot;%{T3}%{T-}&quot;
UNREAD_INBOX_ICON=&quot;%{T2}%{T-}&quot;

UNREAD=$(notmuch count is:inbox and is:unread)
if [[ $1 = &quot;count&quot; ]]; then
   if [ $UNREAD = &quot;0&quot; ]; then
       echo $EMPTY_INBOX_ICON $UNREAD
   else
       echo $UNREAD_INBOX_ICON $UNREAD
   fi
elif [[ $1 = &quot;alot&quot; ]]; then
   $TERMINAL -e alot &amp;amp;
fi
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;~/.config/polybar/config&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;# ...

[module/mailcount]
type = custom/script
interval = 1
format-padding = 0
format = &amp;lt;label&amp;gt;

exec = ~/.config/polybar/scripts/mail.sh count
click-left = ~/.config/polybar/scripts/mail.sh alot

# ...
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;While this was a fun thing to set up and a great way to play with some more open source goodies, I&apos;ve found it works well for me for a number of reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Being able to quickly scan and archive all my emails across 4+ email accounts every morning has absolutely saved me time and frustration.&lt;/li&gt;
&lt;li&gt;I still get all the benefit of being able to set up mobile email clients with IMAP and everything syncs as you&apos;d expect.&lt;/li&gt;
&lt;li&gt;When I&apos;m actually sitting at my desk, this approach lets me deal with emails quickly with minimal distractions without having to flick through various tabs of different webmail providers.&lt;/li&gt;
&lt;li&gt;Even though I&apos;m still a little tied to Gmail, it feels liberating to have all my emails locally, backed up and safely stored.&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item></channel></rss>