<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shiah Kwhrow</title>
    <description>The latest articles on DEV Community by Shiah Kwhrow (@shiiah).</description>
    <link>https://dev.to/shiiah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4012746%2Fa241f06b-7b03-4771-8294-322778a62aa9.jpg</url>
      <title>DEV Community: Shiah Kwhrow</title>
      <link>https://dev.to/shiiah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shiiah"/>
    <language>en</language>
    <item>
      <title>How I built a local-first habit tracker with Go and SQLite (zero friction, zero cloud)</title>
      <dc:creator>Shiah Kwhrow</dc:creator>
      <pubDate>Sun, 05 Jul 2026 11:38:46 +0000</pubDate>
      <link>https://dev.to/shiiah/how-i-built-a-local-first-habit-tracker-with-go-and-sqlite-zero-friction-zero-cloud-4blp</link>
      <guid>https://dev.to/shiiah/how-i-built-a-local-first-habit-tracker-with-go-and-sqlite-zero-friction-zero-cloud-4blp</guid>
      <description>&lt;p&gt;Every focus tracker I tried had the same fatal flaw: to record that I was focused, it first made me &lt;em&gt;unfocus&lt;/em&gt;. Open a browser tab, log in, dismiss the upsell. The fire selling extinguishers.&lt;/p&gt;

&lt;p&gt;So I built &lt;a href="https://9482969453720.gumroad.com/l/aopuhv" rel="noopener noreferrer"&gt;&lt;strong&gt;focusd&lt;/strong&gt;&lt;/a&gt; on the opposite principle: &lt;strong&gt;the tool goes to where the work happens.&lt;/strong&gt; My work happens in tmux and Neovim, so my focus scoreboard lives in the tmux status bar and the Neovim statusline. This post is the technical tour: one Go daemon, one SQLite file, and the handful of design decisions that made it feel invisible.&lt;/p&gt;

&lt;h2&gt;
  
  
  The architecture in one diagram
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                     ┌───────────────────────────┐
tmux status-line ──▶ │                           │
Neovim (lualine) ──▶ │   focusd  ·  127.0.0.1    │ ──▶ ~/.focusd/focus.db
HTMX dashboard   ──▶ │   Go · Echo · embed.FS    │      (SQLite, WAL)
your own scripts ──▶ │                           │
                     └───────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One daemon. One database. Everything else — the tmux bar, the nvim plugin, the web dashboard — is just a window onto the same state. The timer belongs to the &lt;em&gt;server&lt;/em&gt;, not the editor: start a focus in Neovim, close everything, and tmux keeps counting. One source of truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQLite done right: WAL, one connection, prepared statements
&lt;/h2&gt;

&lt;p&gt;The daemon opens SQLite in WAL mode with a &lt;strong&gt;single connection&lt;/strong&gt; and immediate transactions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sqlite3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"file:focus.db?_journal_mode=WAL&amp;amp;_busy_timeout=5000&amp;amp;_txlock=immediate"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetMaxOpenConns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions hiding in that snippet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WAL&lt;/strong&gt; lets the status-line endpoints read while a write is in flight. Readers never block.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SetMaxOpenConns(1)&lt;/code&gt;&lt;/strong&gt; sounds like a limitation; for a localhost daemon it's a superpower. &lt;code&gt;SQLITE_BUSY&lt;/code&gt; simply cannot happen between my own goroutines — the pool serializes access, and prepared statements stay valid forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;_txlock=immediate&lt;/code&gt;&lt;/strong&gt; makes write transactions take the lock at &lt;code&gt;BEGIN&lt;/code&gt; instead of at first write, converting a class of mid-transaction busy errors into a clean wait at the door.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Killing a TOCTOU with a CHECK constraint
&lt;/h2&gt;

&lt;p&gt;"Only one active focus session at a time" is a classic check-then-act race: two &lt;code&gt;POST /focus/start&lt;/code&gt; requests both read "no active focus", both insert. Instead of a mutex, I let the schema enforce the invariant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;active_focus&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;       &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;habit_id&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;habits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;since&lt;/span&gt;    &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The table physically cannot hold two rows. The second &lt;code&gt;INSERT&lt;/code&gt; fails with a constraint violation that maps to a clean HTTP 409. No locks in application code, no race, and the invariant survives even a rogue script writing to the database directly — which is the whole point of local-first: the file &lt;em&gt;is&lt;/em&gt; the API of last resort.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule that shaped everything: never cost the user focus
&lt;/h2&gt;

&lt;p&gt;A focus tracker that adds latency to your editor is self-defeating. Two integrations, one rule — &lt;strong&gt;the daemon must be allowed to die without anyone noticing&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;tmux&lt;/strong&gt; polls with a hard budget:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--silent&lt;/span&gt; &lt;span class="nt"&gt;--max-time&lt;/span&gt; 0.4 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$FOCUSD_URL&lt;/span&gt;&lt;span class="s2"&gt;/status"&lt;/span&gt; 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the daemon is down, the component prints nothing and tmux renders a clean bar. 400ms worst case, once a second, and the &lt;code&gt;/status&lt;/code&gt; endpoint returns plain text — no JSON parsing in shell.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Neovim&lt;/strong&gt; never blocks on I/O. The lualine component reads a cached string synchronously; a libuv timer refreshes that cache in the background by spawning &lt;code&gt;curl&lt;/code&gt; (&lt;code&gt;vim.loop.spawn&lt;/code&gt;), reading stdout, and waiting for the exit+EOF handshake before trusting the buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- statusline reads the cache; the cache is refreshed off the main loop&lt;/span&gt;
&lt;span class="kd"&gt;local&lt;/span&gt; &lt;span class="n"&gt;status_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="kd"&gt;local&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;spawn_curl&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="s2"&gt;"--max-time"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"0.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BASE_URL&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="s2"&gt;"/status"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;status_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;pcall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"redrawstatus"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Editor hiccups: zero. If the daemon is stopped the cache goes empty, and the statusline segment collapses to nothing. Silence when idle is a feature: no gray icon, no "0m", no cognitive noise.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTMX + embed.FS: a dashboard with no build step
&lt;/h2&gt;

&lt;p&gt;The web panel is server-rendered HTML with &lt;a href="https://htmx.org" rel="noopener noreferrer"&gt;htmx&lt;/a&gt; for fragment updates — and htmx itself is embedded in the Go binary with &lt;code&gt;embed.FS&lt;/code&gt;, so the dashboard works fully offline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;//go:embed views/* static/*&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;viewsFS&lt;/span&gt; &lt;span class="n"&gt;embed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating a habit returns an HTML fragment plus an out-of-band swap that updates the side panel in the same response. No JSON API for the UI, no npm, no bundler — &lt;code&gt;go build&lt;/code&gt; is the whole frontend pipeline. The panel is themed Catppuccin Mocha to match the terminal, because opening it should feel like your terminal grew a second screen, not like leaving home.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boring operational correctness
&lt;/h2&gt;

&lt;p&gt;The unglamorous parts that make a daemon trustworthy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;signal.NotifyContext&lt;/code&gt;&lt;/strong&gt; for graceful shutdown: SIGTERM drains in-flight requests, then checkpoints the WAL so &lt;code&gt;focus.db&lt;/code&gt; is always a clean single-file backup (&lt;code&gt;cp&lt;/code&gt; is the backup strategy).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bind &lt;code&gt;127.0.0.1&lt;/code&gt; only.&lt;/strong&gt; A personal tracker has no business listening on your LAN.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The ctl script health-checks pid + port together&lt;/strong&gt; (via &lt;code&gt;lsof&lt;/code&gt;), so &lt;code&gt;focusd status&lt;/code&gt; can't be fooled by a stale pidfile or an unrelated process squatting on 8080.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I deliberately didn't build
&lt;/h2&gt;

&lt;p&gt;No accounts, no sync, no telemetry, no Electron. Every one of those would trade the user's focus or the user's data for my convenience. Backup is &lt;code&gt;cp&lt;/code&gt;, export is &lt;code&gt;sqlite3&lt;/code&gt;, auditing is opening the file. Software you can hold in your head ages well.&lt;/p&gt;




&lt;p&gt;focusd is $20, one-time, lifetime — prebuilt binaries for macOS (ARM/Intel) and Linux (static): &lt;a href="https://9482969453720.gumroad.com/l/aopuhv" rel="noopener noreferrer"&gt;https://9482969453720.gumroad.com/l/aopuhv&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy to go deeper on any of this in the comments — especially the single-connection SQLite take, which I know is spicy.&lt;/p&gt;

</description>
      <category>go</category>
      <category>sqlite</category>
      <category>htmx</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
