I’m Blake. I write about probability, stochastic processes, Python tooling, and the practical side of building things with code. More about me →

  • Secure Home Server Access with Caddy, Tailscale, and Cloudflare

    To serve multiple services on a node on my Tailnet, I’m going to use Caddy as a reverse proxy. I’ve rented a domain name, now I just need to connect that domain name to my server in a secure manner that allows me to access my self-hosted services.

    I ended up on a relatively simple solution that matched all my criteria, but it was not without a few investigations in leads that didn’t pan out

    To keep this home server secure, I’m only exposing the IP of the server via my Tailnet. Only those machines logged into the Tailnet will be able to access that IP address.

    Initial attempt: tailscale serve

    • Tailscale only allows one domain per node, and subdomains aren’t supported
    • I could tailscale serve <port> to broadcast the service
      • But this will only work for one service

    Proposed solution: Expose Caddy, use URI paths to resolve to different services

    Limitation: Most web servers assume they’re on a root path <domain>/ instead of <domain>/<name-of-app>. It’s frustrating and sometimes impossible to configure this for each service.

    Regardless of how much fiddling with the reverse_proxy to strip paths, there was always some caveat that was a total pain to debug.

    No matter how far you’ve gone down the wrong path: turn back now.

    Container communication with docker network

    I’ve created the dockernet network so my hosted containers can communicate with each other

    docker network create dockernet

    Make sure to run before spinning using the compose files throughout this post up so we don’t accidentally create a network for each service.

    I’m using this docker compose to create a container for syncthing:

    services:
      syncthing:
        image: syncthing/syncthing:latest
        container_name: syncthing
        restart: unless-stopped
        hostname: syncthing
        environment:
          - TZ=${TZ:-Etc/UTC}
        ports: # These ports are for local LAN traffic
          - "22000:22000/tcp"
          - "22000:22000/udp"
          - "21027:21027/udp"
        volumes:
          - /opt/syncthing/config:/var/syncthing/config
          - /mnt/path/to/drive:/storage
        networks:
          - dockernet
    networks:
      dockernet:
        external: true

    Some notes on this:

    • The Syncthing web UI (port 8384) doesn’t need to be published — Containers can reach it over dockernet
    • /mnt/path/to/drive is where the actual synced data lives

    Accepted solution: subdomains and the Cloudflare DNS

    Using subdomains allows each service to have it’s own root path, so no more fiddling with paths thankfully!

    So now: I map each subdomain to the same IP address on the Tailnet, and I’m going to use a reverse proxy to direct traffic to each requested service.

    On the Cloudflare dashboard under domains then DNS, I’ve added type A records for syncthing that point to the Tailscale IP address (found on the Tailscale dashboard) with Proxy type: DNS only - reserved IP and TTL: Auto

    Note that MacgicDNS should be enabled on the Tailscale dashboard!

    Caddy is a wonderfully simple reverse proxy. It’s configured for automatic HTTPS but using the default HTTP-01 challenge from “Let’s Encrypt”. But since the server sits behind Tailscale (a private network IP), Let’s Encrypt can’t reach http://syncthing.<domain>/.well-known/acme-challenge/... to verify domain ownership.

    Instead of HTTP-01, we’ll switch Caddy to the DNS-01 challenge using the Cloudflare DNS plugin (for which there’s a docker image for). This lets Caddy prove domain ownership by creating a TXT record in Cloudflare (which “Let’s Encrypt” can verify without ever hitting the server)

    services:
      caddy:
        image: ghcr.io/serfriz/caddy-cloudflare:latest
        container_name: caddy
        restart: unless-stopped
        ports:
          - "80:80"
          - "443:443"
        dns:
          - 1.1.1.1
          - 8.8.8.8
        env_file: .env # This should have CLOUDFLARE_API_TOKEN=... and DOMAIN=...
        volumes:
          - opt/caddy/config:/etc/caddy
        networks_advanced:
          name: dockernet
    networks:
      dockernet:
        external: true

    Make sure to create your .env file:

    CLOUDFLARE_API_TOKEN=your_token_here
    DOMAIN=example.com

    Now let’s create and edit the config at /opt/caddy/config/Caddyfile:

    syncthing.{env.DOMAIN} {
        reverse_proxy syncthing:8384
        tls {
            dns cloudflare {env.CLOUDFLARE_API_TOKEN}
        }
    }

    Now start up the caddy service:

    docker compose up -d
    # After changes to Caddyfile, use
    # `docker exec caddy caddy reload --config /etc/caddy/Caddyfile`

    We can double check the certificates have been handled correctly with inspecting the logs:

    docker logs caddy 2>&1 | grep -i "certificate"

    Once the DNS changes have propagated (which you can test with nslookup syncthing.<domain>, or test Cloudflare DNS specifically with nslookup syncthing.<domain> 1.1.1.1), you should be able to test it with curl:

    curl https://syncthing.<domain>

    And now, we can access https://syncthing.<domain> in the browser from a device connected to the Tailnet!

    If this error appears in the browser:

    no valid A records found for `syncthing.<domain>`

    Double check the SSL/Cloudflare plugin instructions above.

  • TIL: Install Docker Ubuntu

    Constantly something I have to search on the web for: posting it here so I can come back to it immediately next time I need it

    Source

    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
    
    
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
    
    sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
    
    sudo usermod -aG docker ${USER}
  • Switching to an iPhone after 15 years of Android

    My first smartphone was an iPhone 4 — a well-crafted, great device, but one where software and customisation were limited. Jailbreaking iOS 4 was pretty much the only way I could make my phone work the way I wanted it to.

    To this day, my jailbroken iPhone 4 was one of the greatest phones I’ve ever used. My killer feature: having my calendars and todo list (via 2do) on my lock screen. All my classes and tasks were available at a glance at anytime. No fiddling with unlocking and tapping on apps or anything!

    My gripe with Apple at the time was how precious they were (are) with their walled garden. It felt like my device wasn’t really mine, and frankly I found this attitude of theirs pretty arrogant. In retrospect I’ve come to understand where they’re coming from: to deliver the best experience for everybody requires extensive testing and a meticulously crafted strategy for managing the hardware and software. But of all things though, the thing that I found most irritating: not being able to place icons wherever I wanted on the Home Screen! It really doesn’t matter in the grand scheme of things, but it’s such an inconsequential thing, it was baffling this wasn’t considered!

    Then I discovered the open source philosophy. Android offered a more advanced and hands-on experience with phone software. Even stock android had support for different app launchers, widgets, and replacing default apps (Firefox and the unlock plugin are incredible!)

    But I lost that killer feature of the organiser lockscreen on my previous phone. I tried to replace it with widgets, but it still just wasn’t quite the same

    In the 15 years since, iOS has had tremendous updates! And google has consistently been declining from my good graces. From their unsatisfactory privacy stance, to their insistence on shoving AI without any way to opt out: I finally had it! I changed to Kagi search, and subscribed to proton. But I was still on android… who knows what other data is being funneled there. Apple has a stronger and clearer stance on privacy, maybe it’s time to given them another go, and see how far they’ve come.

    I bought a second hand iPhone 13 from eBay, with the intent this would be a short trial period where I try and fail to adapt to the iPhone as my daily driver. But after setting up this device, the plan had changed dramatically within the day: I’ve switched to it permanently

    Migrating from android to iOS was surprisingly seamless. And to my surprise: all of my previous gripes seem to have been addressed

    • App icons anywhere on the Home Screen
    • Change default web browser and many other apps
    • Widgets are now on the Home Screen, and can be viewed from the lock screen

    And so, my organisation lock screen was back in full force — without keeping up with the jailbreaking arms race!

    Apples design, while it’s not perfect, seems to make settings easy to navigate and play with. And with how intuitive the interface is, I’ve had a surprising number of moments where I think “I wonder if I can do X?”, then amazed to see that I can do X with my first guess!

    It’s such a pleasant experience to use, and I’m amazed how many papercuts I forgot about from android once I migrated. Fifteen years later, I’m glad I gave the walled garden another chance.

  • Using git to manage dotfiles

    An educational procrastination technique is managing your dotfiles. There’s some useful lessons to pick up on over the years, and it’s genuinely useful when you have multiple machines you use frequently. I’ve used a couple of tools (stow, then chezmoi), but my latest refactor brought me back to plain old git

    Dotfiles tools are fantastic, but git is pre-installed on most systems and is purpose built for tracking file changes and syncing with remote repos. chezmoi embraces this, but I want to lean away from complexity when possible. chezmoi has many features, and I felt I wasn’t even using half of them. Plus the mental model for applying and adding files can be a bit cumbersome. git is simpler, more transparent, and harder to outsmart yourself with.

    So here’s what I did for my latest episode of “practical procrastination”

    Bare git repos

    To avoid clobbering $HOME with a .git directory (which can result it git walking through every single file, which can take a while), we can use a bare repo:

    git init --bare $HOME/.dotfiles

    Now we need to write a .gitignore that functions as a whitelist rather than a blacklist:

    echo '*' > .gitignore

    This will prevent git from wasting time walking though $HOME, and also prevent committing secrets in $HOME. While git is a very explicit system, the muscle memory of add . && commit -m "..." to just stage and commit all changes is far too tempting — in this case it would commit everything in $HOME!

    Now to use this bare repo, we need to pass --git-dir and --work-tree on every single git command. This is because a bare repo is the directory with tracking metadata in a --git-dir and nothing more. It doesn’t assume the working tree is outside the directory. Hence, we must tell git where the --git-dir is (since it’s not named .git) and where the work tree is.

    We’ll use this git alias:

    git config --global alias.dotfiles '!f() { git --git-dir=$HOME/.dotfiles --work-tree=$HOME "$@"; }; f'

    Tooling such as the starship prompt and vim-fugitive expects to find a .git directory, let’s create a quick pointer so these tools work

    echo "gitdir: $HOME/.dotfiles" > $HOME/.git

    Set up your remote repo as you normally would, and commit the .gitignore and push it to a remote repo as you normally would (I created a dotfiles repo on my Github for this).

    Clone on new machine

    On another machine with git, we can deploy our dotfiles straight away with

    git clone --bare git@github.com:<USERNAME>/dotfiles.git $HOME/.dotfiles
    git config --global alias.dotfiles '!f() { git --git-dir=$HOME/.dotfiles --work-tree=$HOME "$@"; }; f'
    echo "gitdir: $HOME/.dotfiles" > $HOME/.git
    git dotfiles checkout

    If there’s conflicts, use git dotfiles checkout --force to obliterate the local state and replace it. If you want to preserve local state though:

    mkdir -p .config-backup
    git dotfiles checkout 2>&1 | grep -E "^\s+\." | awk '{print $1}' | while read -r file; do
      mkdir -p ".config-backup/$(dirname "$file")"
      mv "$file" ".config-backup/$file"
    done
    git dotfiles checkout

    Tracking dotfiles

    Now, to add new files:

    echo "!/path/to/file" >> .gitignore
    git dotfiles add /path/to/file
    git dotfiles commit -m "feat: Add file"

    The ! Directive will exclude it from the catch-all ignore on the first line.

    Caveats

    Nested directories must be exempted first before exempting the files in them — and they must not have a trailing slash on their ignore lines. For example:

    *
    !.config
    !.config/opencode
    !.config/opencode/opencode.json
    !.config/opencode/AGENTS.md

    .gitignore patterns should relative to $HOME

    To be safe and prevent leaking credentials, always inspect your diffs! Or better yet, use a pre-commit step that uses gitleaks! Here’s .pre-commit-config.yaml:

    repos:
      - repo: https://github.com/gitleaks/gitleaks
        rev: v8.24.2
        hooks:
          - id: gitleaks
    # Install pre-commit however you prefer
    pre-commit install

    Or use a gitignore in the nested directory — I wrote about that tip here and provided an over-engineered alias for this, but the simple oneliner for this situation:

    echo -e '*\n!.gitignore' >> path/to/dir/.gitignore

    For example, here’s how I manage the config for the llm cli:

    # .config/llm/.gitignore
    logs.db
    keys.json
    openrouter_models.json
    openrouter_models_structured_outputs.json
    # ~/.gitignore
    *
    !.config
    !.config/llm
    !.config/llm/.gitignore
    !.config/llm/**/*

    If you’re confident in your nested directory, you could add a trailing /* (or /**/*) wildcard to the directory ignore line in the top-level ignore. But always inspect those diffs!

    Now add commit push. You’re all set up and good to go!