<?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: Charles Hartmann</title>
    <description>The latest articles on DEV Community by Charles Hartmann (@charleshartmann).</description>
    <link>https://dev.to/charleshartmann</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%2F4174346%2Ff67011c1-51d9-4fa5-8db8-68b5b264b12b.png</url>
      <title>DEV Community: Charles Hartmann</title>
      <link>https://dev.to/charleshartmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charleshartmann"/>
    <language>en</language>
    <item>
      <title>A three-step smoke test for a Linux alpha in a VM</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Sat, 21 Nov 2026 18:33:08 +0000</pubDate>
      <link>https://dev.to/charleshartmann/a-three-step-smoke-test-for-a-linux-alpha-in-a-vm-4386</link>
      <guid>https://dev.to/charleshartmann/a-three-step-smoke-test-for-a-linux-alpha-in-a-vm-4386</guid>
      <description>&lt;p&gt;When a new Linux desktop is still in alpha, “the app launched” tells you less than a small, repeatable task. A VM lets you test the parts you care about without replacing a working OS. The goal isn't a benchmark or a verdict on daily-driver readiness. It's a short record that someone else can reproduce.&lt;/p&gt;

&lt;h3&gt;
  
  
  Start with a clean boundary
&lt;/h3&gt;

&lt;p&gt;Use the project’s documented VM route and create disposable test data. Note the image and app version before starting. Save a checkpoint if your VM tool supports it. A screenshot helps explain what the interface showed; it doesn't prove a feature worked. Record what was actually opened, played, saved, or reopened as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test one real workflow
&lt;/h3&gt;

&lt;p&gt;For a player, start with a feed you already know. If search misses, paste its RSS URL instead of declaring the feed broken. Play an episode, pause it, close the app, then reopen it and compare the saved position. Directory discovery and feed playback are separate checks. &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/posts/earshot-podcasts-on-luma/" rel="noopener noreferrer"&gt;Earshot's RSS walkthrough&lt;/a&gt; explains what to try and links to the public build.&lt;/p&gt;

&lt;p&gt;For an editor, create a short document, save it as an OpenDocument file, then reopen the saved file instead of trusting the window still in memory. Repeat with a simple sum and a slide if those formats matter to you. Export a PDF as a separate check and open it in a viewer. &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/posts/hartmann-office-documents-spreadsheets-slides-on-luma/" rel="noopener noreferrer"&gt;The Hartmann Office guide&lt;/a&gt; covers the app and the GitHub download; a local test does not prove round-trip compatibility with every other office app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write down what the VM didn't prove
&lt;/h3&gt;

&lt;p&gt;A passing test applies to that build, that guest, and that workflow. Don't infer native hardware support, a store listing, or production readiness from it. If you’re trying the Luma alpha, the &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/posts/luma-os-alpha-what-to-try-in-a-vm/" rel="noopener noreferrer"&gt;Homelab VM guide&lt;/a&gt; points to the official Try Luma route and helps decide what to test first.&lt;/p&gt;

&lt;p&gt;AI-assisted write-up based on the linked guides.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>testing</category>
      <category>homelab</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Payhip Pricing for Creators: When to Upgrade from Free to Plus</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Thu, 12 Nov 2026 15:00:45 +0000</pubDate>
      <link>https://dev.to/charleshartmann/payhip-pricing-for-creators-when-to-upgrade-from-free-to-plus-1g04</link>
      <guid>https://dev.to/charleshartmann/payhip-pricing-for-creators-when-to-upgrade-from-free-to-plus-1g04</guid>
      <description>&lt;p&gt;Your ebook might be working better at a lower price than you think. Here is the math on when to upgrade your storefront tier.&lt;/p&gt;

&lt;p&gt;Payhip offers three main plans for sellers, and picking the right one can save you money—especially in the early days when sales feel light but are about to pick up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Plans (as of November 2026)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Free Forever:&lt;/strong&gt; $0/month, 5% transaction fee per sale&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plus:&lt;/strong&gt; $29/month, 2% transaction fee&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pro:&lt;/strong&gt; $99/month, 0% transaction fee&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stripe or PayPal processing fees apply on top on every plan—that's just payment processing, and it doesn't change between tiers. The decision comes down to your monthly sales volume versus Payhip's platform fees.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Each Plan Pays Off&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can figure out the break-even point by setting each plan's costs against the others:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Free vs Plus:&lt;/em&gt; 5% of sales = $29 + 2% of sales → you sell through about &lt;strong&gt;$967 a month&lt;/strong&gt; in revenue when Plus becomes cheaper.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Plus vs Pro:&lt;/em&gt; $29 + 2% of sales = $99 → you need about &lt;strong&gt;$3,500 a month&lt;/strong&gt; in sales for Pro to take the lead.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Free vs Pro:&lt;/em&gt; 5% of sales = $99 → Pro beats Free at roughly &lt;strong&gt;$1,980 a month&lt;/strong&gt; in revenue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What You Actually Pay Each Month&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is what your total Payhip fees look like across different sales levels:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Monthly Sales&lt;/th&gt;
&lt;th&gt;Free Plan Fees&lt;/th&gt;
&lt;th&gt;Plus Plan Fees&lt;/th&gt;
&lt;th&gt;Pro Plan Fees&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;$200&lt;/td&gt;
&lt;td&gt;$10&lt;/td&gt;
&lt;td&gt;$33&lt;/td&gt;
&lt;td&gt;$99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;$1,000&lt;/td&gt;
&lt;td&gt;$50&lt;/td&gt;
&lt;td&gt;$49&lt;/td&gt;
&lt;td&gt;$99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;$3,500&lt;/td&gt;
&lt;td&gt;$175&lt;/td&gt;
&lt;td&gt;$99&lt;/td&gt;
&lt;td&gt;$99&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The Simple Takeaway&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stay on &lt;strong&gt;Free&lt;/strong&gt; if you're under about $1,000 a month in sales—and honestly, even stretching to that higher range still keeps Free competitive.&lt;/li&gt;
&lt;li&gt;Upgrade to &lt;strong&gt;Plus&lt;/strong&gt; once you're consistently above $1,000 a month and want lower fees as you grow.&lt;/li&gt;
&lt;li&gt;Move to &lt;strong&gt;Pro&lt;/strong&gt; only when hitting $3,500+ monthly; the flat rate starts paying off at scale.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most creators spend too long on Free because they aren't sure when it makes sense to upgrade. The math is straightforward: if your revenue justifies it, the lower transaction fee on Plus or Pro usually wins out within a month.&lt;/p&gt;

&lt;p&gt;You can check out our ebook samples and full titles over at our Payhip store: &lt;a href="https://payhip.com/HomelabtheRightWay" rel="noopener noreferrer"&gt;https://payhip.com/HomelabtheRightWay&lt;/a&gt; (this is our own store, not an affiliate link).&lt;/p&gt;

</description>
      <category>payhip</category>
      <category>ebooks</category>
      <category>selfpublishing</category>
      <category>creators</category>
    </item>
    <item>
      <title>NUT shutdown on a timer: upssched, secondaries first, and HOSTSYNC explained</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Wed, 11 Nov 2026 15:01:07 +0000</pubDate>
      <link>https://dev.to/charleshartmann/nut-shutdown-on-a-timer-upssched-secondaries-first-and-hostsync-explained-1ngj</link>
      <guid>https://dev.to/charleshartmann/nut-shutdown-on-a-timer-upssched-secondaries-first-and-hostsync-explained-1ngj</guid>
      <description>&lt;p&gt;A NUT server that shuts itself down perfectly is not enough. If the NAS was never told, or the Docker host in the corner was never told, the outage still ends in a crash. And by default NUT waits for the UPS to report &lt;strong&gt;low battery&lt;/strong&gt;, a threshold the UPS picks and an estimate that gets optimistic as the battery ages. This post connects the clients, explains the order NUT shuts things down in, and replaces "wait for low battery" with a timed shutdown that keeps most of the battery in reserve.&lt;/p&gt;

&lt;p&gt;The examples assume the UPS's USB cable is on a Proxmox host running the NUT server (&lt;code&gt;upsd&lt;/code&gt;) and the primary &lt;code&gt;upsmon&lt;/code&gt;, with the UPS named &lt;code&gt;myups&lt;/code&gt; at &lt;code&gt;10.0.10.5&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a NUT shutdown actually runs
&lt;/h2&gt;

&lt;p&gt;When the UPS goes critical (on battery &lt;strong&gt;and&lt;/strong&gt; low battery, or the forced-shutdown flag is set), this is the sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The primary &lt;code&gt;upsmon&lt;/code&gt; notices and sets the &lt;strong&gt;FSD&lt;/strong&gt; (forced shutdown) flag on the UPS in &lt;code&gt;upsd&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every secondary &lt;code&gt;upsmon&lt;/code&gt; sees FSD on its next poll, waits &lt;code&gt;FINALDELAY&lt;/code&gt; seconds and runs its own &lt;code&gt;SHUTDOWNCMD&lt;/code&gt;. As it shuts down, it disconnects from &lt;code&gt;upsd&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The primary waits until all secondaries have disconnected, or until &lt;code&gt;HOSTSYNC&lt;/code&gt; seconds have passed, whichever comes first.&lt;/li&gt;
&lt;li&gt;The primary shuts its own machine down, creating the &lt;code&gt;POWERDOWNFLAG&lt;/code&gt; file on the way.&lt;/li&gt;
&lt;li&gt;Late in the primary's shutdown, that flag tells the driver to command the UPS to cut its output after &lt;code&gt;offdelay&lt;/code&gt; seconds.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That ordering is built in: secondaries always go before the primary. Put the NAS, the other servers and any VMs that run elsewhere on secondaries, and keep the primary on the machine with the UPS cable.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Debian client
&lt;/h2&gt;

&lt;p&gt;On another Debian or Ubuntu machine that should shut down with the UPS, install only the client part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apt install nut-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the mode in &lt;code&gt;/etc/nut/nut.conf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MODE=netclient
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then point &lt;code&gt;upsmon&lt;/code&gt; at the server with a secondary user you created in &lt;code&gt;upsd.users&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# /etc/nut/upsmon.conf
MONITOR myups@10.0.10.5 1 upsmon_remote CHANGE-ME-long-random secondary

MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYFLAG ONBATT   SYSLOG+WALL
NOTIFYFLAG ONLINE   SYSLOG+WALL
NOTIFYFLAG FSD      SYSLOG+WALL
NOTIFYFLAG COMMBAD  SYSLOG+WALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lock the file down, start the monitor and check that the client can read the UPS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;chown root:nut /etc/nut/upsmon.conf
chmod 0640 /etc/nut/upsmon.conf
systemctl &lt;span class="nb"&gt;enable&lt;/span&gt; --now nut-monitor.service
upsc myups@10.0.10.5 ups.status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A secondary never has a &lt;code&gt;POWERDOWNFLAG&lt;/code&gt;: only the primary tells the UPS to cut power. Treat the secondary password as a low-value credential that only lets a client follow the UPS, and never reuse the admin password on a client.&lt;/p&gt;

&lt;h2&gt;
  
  
  TrueNAS as a client
&lt;/h2&gt;

&lt;p&gt;TrueNAS has a built-in UPS service that is NUT underneath. In TrueNAS 25.10 it is under &lt;strong&gt;System &amp;gt; Services &amp;gt; UPS&lt;/strong&gt; (the pencil icon). Set &lt;strong&gt;Identifier&lt;/strong&gt; to &lt;code&gt;myups&lt;/code&gt;, &lt;strong&gt;UPS Mode&lt;/strong&gt; to &lt;strong&gt;Slave&lt;/strong&gt; (TrueNAS still uses the old word for a secondary), &lt;strong&gt;Remote Hostname&lt;/strong&gt; and &lt;strong&gt;Remote Port&lt;/strong&gt; to the server and &lt;code&gt;3493&lt;/code&gt;, and the monitor user and password. Save, turn the service on and set it to start automatically. Menu names have moved between TrueNAS releases, so check the UPS service page in the docs for your version if the screen looks different.&lt;/p&gt;

&lt;p&gt;The NAS is usually the machine whose clean shutdown matters most and takes longest. Time it. By default a secondary &lt;code&gt;upsmon&lt;/code&gt; logs off &lt;code&gt;upsd&lt;/code&gt; as soon as it has &lt;em&gt;started&lt;/em&gt; its shutdown, so &lt;code&gt;HOSTSYNC&lt;/code&gt; only covers that hand-off, not the whole NAS shutdown. What protects the rest of it is the time the primary's own shutdown takes plus the UPS's &lt;code&gt;offdelay&lt;/code&gt;; make sure those together are longer than the NAS needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shutdown on a timer, not on low battery
&lt;/h2&gt;

&lt;p&gt;The alternative to the UPS's low-battery threshold is to decide for yourself: if the UPS has been on battery for N minutes, the outage is real, so shut everything down while the battery is still mostly full. NUT does this with &lt;code&gt;upssched&lt;/code&gt;, a timer helper that &lt;code&gt;upsmon&lt;/code&gt; calls on events.&lt;/p&gt;

&lt;p&gt;On the primary, tell &lt;code&gt;upsmon&lt;/code&gt; to call &lt;code&gt;upssched&lt;/code&gt; for the on-battery and on-line events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# add to /etc/nut/upsmon.conf on the primary
NOTIFYCMD /sbin/upssched
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the real path with &lt;code&gt;command -v upssched&lt;/code&gt; and use that. Then configure the timer. The stock &lt;code&gt;upssched.conf&lt;/code&gt; ships with &lt;code&gt;PIPEFN&lt;/code&gt; disabled on purpose, so you set it deliberately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# /etc/nut/upssched.conf
CMDSCRIPT /etc/nut/upssched-cmd
PIPEFN /run/nut/upssched/upssched.pipe
LOCKFN /run/nut/upssched/upssched.lock

AT ONBATT * START-TIMER onbatt 180
AT ONLINE * CANCEL-TIMER onbatt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;START-TIMER onbatt 180&lt;/code&gt; starts a three-minute timer when the UPS goes on battery, and &lt;code&gt;CANCEL-TIMER&lt;/code&gt; stops it if power comes back first. If the timer runs out, &lt;code&gt;upssched&lt;/code&gt; runs &lt;code&gt;CMDSCRIPT&lt;/code&gt; with the timer name as its argument. Pick the number from your own runtime at your real load, not from the box.&lt;/p&gt;

&lt;p&gt;The script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="c"&gt;# /etc/nut/upssched-cmd&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"$1"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
  onbatt&lt;span class="o"&gt;)&lt;/span&gt;
    logger -t upssched-cmd &lt;span class="s2"&gt;"On battery for too long, forcing shutdown"&lt;/span&gt;
    /sbin/upsmon -c fsd
    &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  *&lt;span class="o"&gt;)&lt;/span&gt;
    logger -t upssched-cmd &lt;span class="s2"&gt;"Unrecognized timer: $1"&lt;/span&gt;
    &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;upsmon -c fsd&lt;/code&gt; sets the forced-shutdown flag from the primary, which starts the normal ordered shutdown: secondaries first, then the primary. Create the directory for the pipe and lock file, owned by the user &lt;code&gt;upsmon&lt;/code&gt; runs as (&lt;code&gt;nut&lt;/code&gt; on Debian), make the script executable, and restart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;install -d -o nut -g nut -m 0750 /run/nut/upssched
chmod 0750 /etc/nut/upssched-cmd
chown root:nut /etc/nut/upssched-cmd /etc/nut/upssched.conf
systemctl restart nut-monitor.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/run&lt;/code&gt; is cleared at every boot, so the directory has to be recreated each time. A one-line tmpfiles rule does it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# /etc/tmpfiles.d/nut-upssched.conf
d /run/nut/upssched 0750 nut nut -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not test the timer by pulling the plug before the script works. Run the script by hand first, with a short timer, at a time when a real shutdown costs nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The timing settings
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Where&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;th&gt;Change it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;HOSTSYNC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Primary &lt;code&gt;upsmon.conf&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;15 s&lt;/td&gt;
&lt;td&gt;Your slowest secondary needs longer to disconnect&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;FINALDELAY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every &lt;code&gt;upsmon.conf&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;5 s&lt;/td&gt;
&lt;td&gt;You want more warning before &lt;code&gt;SHUTDOWNCMD&lt;/code&gt;; keep it below the primary's &lt;code&gt;HOSTSYNC&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DEADTIME&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every &lt;code&gt;upsmon.conf&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;15 s&lt;/td&gt;
&lt;td&gt;Rarely; keep it a multiple of &lt;code&gt;POLLFREQ&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;POLLFREQ&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every &lt;code&gt;upsmon.conf&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;5 s&lt;/td&gt;
&lt;td&gt;Rarely&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;START-TIMER&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Primary &lt;code&gt;upssched.conf&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;Your own choice from your measured runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Home Assistant, for eyes only
&lt;/h2&gt;

&lt;p&gt;Home Assistant's Network UPS Tools integration does not shut anything down; it reads the NUT server so you can see the UPS on a dashboard and get a phone notification when it goes on battery. Give it its own user with no &lt;code&gt;upsmon&lt;/code&gt;, &lt;code&gt;actions&lt;/code&gt; or &lt;code&gt;instcmds&lt;/code&gt; lines in &lt;code&gt;upsd.users&lt;/code&gt;, so it can read status but change nothing, and reload &lt;code&gt;upsd&lt;/code&gt; with &lt;code&gt;systemctl reload nut-server&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Every machine that needs a clean shutdown runs a secondary &lt;code&gt;upsmon&lt;/code&gt; or its own UPS client.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;upsc myups@&amp;lt;server&amp;gt;&lt;/code&gt; works from each client.&lt;/li&gt;
&lt;li&gt;TrueNAS's UPS service is a secondary pointed at the server, enabled at boot.&lt;/li&gt;
&lt;li&gt;The primary's shutdown time plus &lt;code&gt;offdelay&lt;/code&gt; is longer than the NAS needs.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;upssched&lt;/code&gt; timer, script and tmpfiles rule are in place, and the script was tested by hand.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This is adapted from my book &lt;em&gt;Power Out, Data Safe&lt;/em&gt; (Homelab the Right Way, Book 17). The free sample is &lt;a href="https://payhip.com/b/OnKvx" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/ig2Pb" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>homelab</category>
      <category>linux</category>
      <category>proxmox</category>
      <category>sysadmin</category>
    </item>
    <item>
      <title>Gradle convention plugins for a multi-module Android app: build-logic, AGP 9 and the version catalog</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Tue, 10 Nov 2026 15:00:49 +0000</pubDate>
      <link>https://dev.to/charleshartmann/gradle-convention-plugins-for-a-multi-module-android-app-build-logic-agp-9-and-the-version-catalog-2oh7</link>
      <guid>https://dev.to/charleshartmann/gradle-convention-plugins-for-a-multi-module-android-app-build-logic-agp-9-and-the-version-catalog-2oh7</guid>
      <description>&lt;p&gt;Once an Android app has a dozen or more modules, every &lt;code&gt;build.gradle.kts&lt;/code&gt; starts to repeat the same twenty lines: the Android plugin, &lt;code&gt;compileSdk&lt;/code&gt;, &lt;code&gt;minSdk&lt;/code&gt;, Java and Kotlin options, the Compose setup, the Hilt plugin and processor, the test dependencies. Bumping &lt;code&gt;minSdk&lt;/code&gt; means editing every file, and forgetting one means a module that quietly builds differently from the rest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Convention plugins&lt;/strong&gt; fix that. A convention plugin is a small Gradle plugin you write yourself, which applies and configures other plugins the way your project wants. Each module then says &lt;em&gt;what kind&lt;/em&gt; of module it is, a library or a feature, and the plugin supplies the &lt;em&gt;how&lt;/em&gt;. Google's Now in Android sample is built this way, and Gradle's documentation recommends this approach over cross-project configuration with &lt;code&gt;subprojects {}&lt;/code&gt; and &lt;code&gt;allprojects {}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  buildSrc or build-logic
&lt;/h2&gt;

&lt;p&gt;Gradle offers two places for shared build logic.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;buildSrc&lt;/code&gt;&lt;/strong&gt; is a special directory Gradle builds automatically before your project. It works, but any change inside it invalidates the whole build's configuration, because it is on the classpath of every build script.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An included build&lt;/strong&gt;, by convention named &lt;code&gt;build-logic&lt;/code&gt;, is a separate Gradle build that your main build includes with &lt;code&gt;includeBuild&lt;/code&gt;. Its plugins are resolved by ID like any other plugin, and only the modules that apply a changed plugin are affected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post uses &lt;code&gt;build-logic&lt;/code&gt;, as Now in Android does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up build-logic
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MyApp/
├── build-logic/
│   ├── settings.gradle.kts
│   └── convention/
│       ├── build.gradle.kts
│       └── src/main/kotlin/
│           ├── AndroidLibraryConventionPlugin.kt
│           ├── AndroidLibraryComposeConventionPlugin.kt
│           ├── AndroidFeatureConventionPlugin.kt
│           ├── HiltConventionPlugin.kt
│           └── com/example/buildlogic/KotlinAndroid.kt
├── gradle/libs.versions.toml
└── settings.gradle.kts      // pluginManagement { includeBuild("build-logic") }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;build-logic/settings.gradle.kts&lt;/code&gt; shares the main project's version catalog, so plugins and libraries have one source of versions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// build-logic/settings.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;dependencyResolutionManagement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;repositories&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;google&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;mavenCentral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;versionCatalogs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"libs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;files&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"../gradle/libs.versions.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;rootProject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"build-logic"&lt;/span&gt;
&lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":convention"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The convention module is a Kotlin DSL plugin project. It puts the Android, Kotlin and KSP Gradle plugins on its classpath as &lt;code&gt;compileOnly&lt;/code&gt; dependencies, so it can reference their types without forcing versions on the main build, and registers each plugin under an ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// build-logic/convention/build.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sb"&gt;`kotlin-dsl`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;compileOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gradlePlugin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;compileOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kotlin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gradlePlugin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;compileOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gradlePlugin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;compileOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ksp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gradlePlugin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;gradlePlugin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"androidLibrary"&lt;/span&gt;&lt;span class="p"&gt;)&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="s2"&gt;"myapp.android.library"&lt;/span&gt;
            &lt;span class="n"&gt;implementationClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"AndroidLibraryConventionPlugin"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"androidFeature"&lt;/span&gt;&lt;span class="p"&gt;)&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="s2"&gt;"myapp.android.feature"&lt;/span&gt;
            &lt;span class="n"&gt;implementationClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"AndroidFeatureConventionPlugin"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// ...one register() per plugin&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write a convention plugin as a precompiled script (&lt;code&gt;myapp.android.library.gradle.kts&lt;/code&gt;, file name = plugin ID) or as a class implementing &lt;code&gt;Plugin&amp;lt;Project&amp;gt;&lt;/code&gt;. Classes are more verbose but are ordinary Kotlin, so plugins can share helper functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  One place for compileSdk and minSdk
&lt;/h2&gt;

&lt;p&gt;The shared helper holds the project-wide Android settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// KotlinAndroid.kt&lt;/span&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;configureKotlinAndroid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;commonExtension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CommonExtension&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;commonExtension&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;compileSdk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;
        &lt;span class="n"&gt;defaultConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;minSdk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;compileOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sourceCompatibility&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JavaVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION_17&lt;/span&gt;
            &lt;span class="n"&gt;targetCompatibility&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JavaVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION_17&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;KotlinAndroidProjectExtension&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;compilerOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;jvmTarget&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JvmTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JVM_17&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bumping &lt;code&gt;compileSdk&lt;/code&gt; or &lt;code&gt;minSdk&lt;/code&gt; is now a one-line change.&lt;/p&gt;

&lt;p&gt;Note the shape: &lt;code&gt;CommonExtension&lt;/code&gt; has no type arguments, and blocks are reached as properties (&lt;code&gt;defaultConfig.apply { }&lt;/code&gt;) rather than lambdas. That is an &lt;strong&gt;AGP 9 change&lt;/strong&gt;. The AGP 9.0 release notes state that the parameterization of &lt;code&gt;CommonExtension&lt;/code&gt; was removed and the block methods moved to &lt;code&gt;ApplicationExtension&lt;/code&gt; and &lt;code&gt;LibraryExtension&lt;/code&gt;. Convention plugins written for AGP 8 used &lt;code&gt;CommonExtension&amp;lt;*, *, *, *, *, *&amp;gt;&lt;/code&gt; and &lt;code&gt;defaultConfig { }&lt;/code&gt;, and need this update when you move to AGP 9.&lt;/p&gt;

&lt;p&gt;A tiny extension gives every plugin the version catalog:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;VersionCatalog&lt;/span&gt;
    &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getByType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;VersionCatalogsExtension&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;named&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"libs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The plugins themselves
&lt;/h2&gt;

&lt;p&gt;An Android library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AndroidLibraryConventionPlugin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Plugin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.android.library"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;LibraryExtension&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;configureKotlinAndroid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;defaultConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;testInstrumentationRunner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="s2"&gt;"androidx.test.runner.AndroidJUnitRunner"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"testImplementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"junit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="s2"&gt;"testImplementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kotlinx.coroutines.test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no Kotlin Android plugin to apply: AGP 9's built-in Kotlin support compiles the sources.&lt;/p&gt;

&lt;p&gt;Hilt, in the style of Now in Android's &lt;code&gt;HiltConventionPlugin&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HiltConventionPlugin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Plugin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.google.devtools.ksp"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;pluginManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.jetbrains.kotlin.jvm"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"implementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hilt.core"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="s2"&gt;"ksp"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hilt.compiler"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;pluginManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.android.base"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.google.dagger.hilt.android"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"implementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hilt.android"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="s2"&gt;"ksp"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hilt.android.compiler"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pluginManager.withPlugin&lt;/code&gt; runs its block only if that plugin is applied, so one convention plugin serves both pure Kotlin and Android modules.&lt;/p&gt;

&lt;p&gt;The feature plugin encodes your module rules in one place: it stacks the Compose library and Hilt plugins and adds what every feature needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AndroidFeatureConventionPlugin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Plugin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"myapp.android.library.compose"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"myapp.hilt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"implementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":core:data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="s2"&gt;"implementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":core:designsystem"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="s2"&gt;"implementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":core:ui"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="s2"&gt;"testImplementation"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":core:testing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What a module's build file looks like now
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// feature/notes/build.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myapp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;feature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;android&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.example.myapp.feature.notes"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything a feature always needs comes from the plugin; the module lists only what is special about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The version catalog
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;gradle/libs.versions.toml&lt;/code&gt; is where Gradle looks by default for dependency versions. It has four sections: &lt;code&gt;[versions]&lt;/code&gt;, &lt;code&gt;[libraries]&lt;/code&gt;, &lt;code&gt;[bundles]&lt;/code&gt; for groups of libraries always used together, and &lt;code&gt;[plugins]&lt;/code&gt; for Gradle plugin IDs. Your convention plugins get &lt;code&gt;[plugins]&lt;/code&gt; entries too, without versions, because they come from the included build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="k"&gt;[plugins]&lt;/span&gt;
&lt;span class="na"&gt;android-library&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;"com.android.library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"agp"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;ksp&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;"com.google.devtools.ksp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ksp"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;myapp-android-library&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;"myapp.android.library"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;myapp-android-feature&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;"myapp.android.feature"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few rules keep the catalog useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dashes become dots.&lt;/strong&gt; &lt;code&gt;androidx-navigation3-runtime&lt;/code&gt; is &lt;code&gt;libs.androidx.navigation3.runtime&lt;/code&gt; in Kotlin, and &lt;code&gt;libs.findLibrary("androidx.navigation3.runtime")&lt;/code&gt; inside a plugin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Libraries under a BOM have no version.&lt;/strong&gt; Compose entries take their versions from the Compose BOM, which the Compose convention plugin adds as a &lt;code&gt;platform()&lt;/code&gt; dependency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bundles only for libraries that are always used together.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versions live only in the catalog.&lt;/strong&gt; A version string in any &lt;code&gt;build.gradle.kts&lt;/code&gt; is a version that will be missed in the next upgrade.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Shared build logic lives in a &lt;code&gt;build-logic&lt;/code&gt; included build, not in &lt;code&gt;subprojects {}&lt;/code&gt; or &lt;code&gt;allprojects {}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;compileSdk&lt;/code&gt;, &lt;code&gt;minSdk&lt;/code&gt; and the Java and Kotlin targets are set in exactly one place.&lt;/li&gt;
&lt;li&gt;Convention plugins use AGP 9's unparameterized &lt;code&gt;CommonExtension&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each module's build file names its kind and lists only its own extras.&lt;/li&gt;
&lt;li&gt;All versions live in &lt;code&gt;gradle/libs.versions.toml&lt;/code&gt;, shared with &lt;code&gt;build-logic&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This is adapted from my book &lt;em&gt;Structure That Scales&lt;/em&gt; (Android the Right Way, Book 11). The free sample is &lt;a href="https://payhip.com/b/TH98B" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/4YgzU" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>gradle</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Troubleshooting a Linux server in a fixed order: running, listening, reachable, logs, changes</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Mon, 09 Nov 2026 15:00:48 +0000</pubDate>
      <link>https://dev.to/charleshartmann/troubleshooting-a-linux-server-in-a-fixed-order-running-listening-reachable-logs-changes-3p3f</link>
      <guid>https://dev.to/charleshartmann/troubleshooting-a-linux-server-in-a-fixed-order-running-listening-reachable-logs-changes-3p3f</guid>
      <description>&lt;p&gt;Something on the server is broken, and the instinct is to start changing things: restart the service, then the VM, then edit a config you half remember, then reinstall a package. Sometimes it comes back and you never learn why. Often you have now changed four things and broken a fifth. Good troubleshooting is boring on purpose: a fixed order of questions, each answered by a command, and no changes until you know what is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fixed order
&lt;/h2&gt;

&lt;p&gt;Stop at the first unexpected answer and work there.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Is it running?&lt;/strong&gt; &lt;code&gt;systemctl status app&lt;/code&gt;, or &lt;code&gt;docker ps&lt;/code&gt; for containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Is it listening?&lt;/strong&gt; &lt;code&gt;sudo ss -tulpn | grep 8080&lt;/code&gt; shows the port and the address it is bound to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Can you reach it?&lt;/strong&gt; From the server: &lt;code&gt;curl -v http://127.0.0.1:8080/&lt;/code&gt;. From your laptop: the same with the server's address. The difference between those two answers is a bind address, a firewall or the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What do the logs say?&lt;/strong&gt; &lt;code&gt;journalctl -u app -n 100&lt;/code&gt;, then &lt;code&gt;journalctl -p err -b&lt;/code&gt; for everything else that went wrong this boot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What changed?&lt;/strong&gt; Most outages follow a change. Find it:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 30 /var/log/apt/history.log             &lt;span class="c"&gt;# recent package changes&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;find /etc &lt;span class="nt"&gt;-mtime&lt;/span&gt; &lt;span class="nt"&gt;-2&lt;/span&gt; &lt;span class="nt"&gt;-type&lt;/span&gt; f 2&amp;gt;/dev/null    &lt;span class="c"&gt;# config changed in 2 days&lt;/span&gt;
journalctl &lt;span class="nt"&gt;--list-boots&lt;/span&gt; | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-3&lt;/span&gt;               &lt;span class="c"&gt;# did it reboot?&lt;/span&gt;
journalctl &lt;span class="nt"&gt;-b&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 20 &lt;span class="nt"&gt;--no-pager&lt;/span&gt;               &lt;span class="c"&gt;# end of the previous boot&lt;/span&gt;
&lt;span class="nb"&gt;uptime&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Debian 13 the &lt;code&gt;last&lt;/code&gt; command is gone; the previous boot's journal answers the same question.&lt;/p&gt;

&lt;p&gt;The rule that makes the whole order work: &lt;strong&gt;write down every change you make while troubleshooting&lt;/strong&gt;, with the time. A text file in your home directory is enough. If the fix does not work, you can undo exactly what you did. And before changing anything on a VM you care about, take a snapshot, so a bad "fix" is two seconds from undone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is the machine itself healthy?
&lt;/h2&gt;

&lt;p&gt;When the problem is "everything is slow" rather than "one thing is broken", look at the whole machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;uptime&lt;/span&gt;          &lt;span class="c"&gt;# how long it has run, and the load averages&lt;/span&gt;
top             &lt;span class="c"&gt;# live: CPU, memory, processes (q to quit)&lt;/span&gt;
free &lt;span class="nt"&gt;-h&lt;/span&gt;         &lt;span class="c"&gt;# memory and swap&lt;/span&gt;
&lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt;           &lt;span class="c"&gt;# disk space per filesystem&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;iotop &lt;span class="nt"&gt;-o&lt;/span&gt;   &lt;span class="c"&gt;# who is using the disk right now (sudo apt install iotop)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Load average, honestly
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ uptime
 10:42:17 up 12 days,  3:05,  2 users,  load average: 3.84, 2.10, 0.95
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three numbers are the average number of processes that were running or waiting to run over the last 1, 5 and 15 minutes. On Linux, processes waiting for disk I/O count too. Read them against the number of cores (&lt;code&gt;nproc&lt;/code&gt;): on a 4-core VM, a load around 4 means the CPUs are fully busy, and well above it means work is queueing. The trend matters as much as the value: &lt;code&gt;3.84, 2.10, 0.95&lt;/code&gt; is climbing, so something started recently. A high load with idle CPUs in &lt;code&gt;top&lt;/code&gt; (low &lt;code&gt;us&lt;/code&gt; and &lt;code&gt;sy&lt;/code&gt;, high &lt;code&gt;wa&lt;/code&gt;) points at the disk, not the processor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory and swap
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       2.9Gi       121Mi        12Mi       1.0Gi       880Mi
Swap:          975Mi       410Mi       565Mi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A small &lt;code&gt;free&lt;/code&gt; value is not a problem: Linux uses spare memory as disk cache and gives it back when programs need it. The column that matters is &lt;strong&gt;available&lt;/strong&gt;. Swap in use is not a problem by itself; swap that keeps growing while &lt;code&gt;available&lt;/code&gt; stays low means the machine needs more memory or a process is leaking. In &lt;code&gt;top&lt;/code&gt;, press &lt;code&gt;M&lt;/code&gt; to sort by memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the kernel kills something
&lt;/h2&gt;

&lt;p&gt;When memory runs out completely, the kernel's out-of-memory killer picks a process and kills it. The service just "dies", sometimes restarted by systemd, and its own log says nothing useful. The kernel log does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;journalctl &lt;span class="nt"&gt;-k&lt;/span&gt; &lt;span class="nt"&gt;-b&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="s1"&gt;'out of memory|oom'&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;dmesg &lt;span class="nt"&gt;-T&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="s1"&gt;'killed process|oom'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is never "turn off the OOM killer". Give the VM more memory, cap the hungry service (&lt;code&gt;MemoryMax=&lt;/code&gt; in a systemd drop-in, or a memory limit on the container), or move it somewhere with room.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disk full
&lt;/h2&gt;

&lt;p&gt;A full root filesystem breaks things in strange ways: services fail to start, logins hang, APT cannot write its lists, databases go read-only. Find where the space went, from the top down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt;                                          &lt;span class="c"&gt;# which filesystem is full?&lt;/span&gt;
&lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;                                          &lt;span class="c"&gt;# or out of inodes?&lt;/span&gt;
&lt;span class="nb"&gt;sudo du&lt;/span&gt; &lt;span class="nt"&gt;-xh&lt;/span&gt; &lt;span class="nt"&gt;--max-depth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 / | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; | &lt;span class="nb"&gt;tail&lt;/span&gt;   &lt;span class="c"&gt;# which top-level directory?&lt;/span&gt;
&lt;span class="nb"&gt;sudo du&lt;/span&gt; &lt;span class="nt"&gt;-xh&lt;/span&gt; &lt;span class="nt"&gt;--max-depth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 /var | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; | &lt;span class="nb"&gt;tail
sudo &lt;/span&gt;find / &lt;span class="nt"&gt;-xdev&lt;/span&gt; &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-size&lt;/span&gt; +500M &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-lh&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; + 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The usual suspects:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where&lt;/th&gt;
&lt;th&gt;What fills it&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/var/log/journal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The journal with no size cap&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;journalctl --vacuum-size=200M&lt;/code&gt;, then cap it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/var/cache/apt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Downloaded package files&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sudo apt clean&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/var/lib/docker&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Old images, stopped containers, build cache&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker system df&lt;/code&gt;, then prune carefully&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/var/log/*.log&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;An app logging in a loop&lt;/td&gt;
&lt;td&gt;Fix the loop, then truncate the file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deleted but open&lt;/td&gt;
&lt;td&gt;A deleted file a process still writes to&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;sudo lsof +L1&lt;/code&gt;, restart that process&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Do not delete a log file that a running service is still writing to: the space is not freed until the service closes it. Truncate it instead with &lt;code&gt;sudo truncate -s 0 /var/log/app.log&lt;/code&gt;. And never delete files in &lt;code&gt;/var/lib/docker&lt;/code&gt; by hand; use Docker's own prune commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptom to first command
&lt;/h2&gt;

&lt;p&gt;Where to start looking for the most common complaints. It is a starting point, not a diagnosis.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;First command&lt;/th&gt;
&lt;th&gt;Look for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Service will not start&lt;/td&gt;
&lt;td&gt;&lt;code&gt;journalctl -u app -n 50&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The first real error line&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works locally, not from LAN&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sudo ss -tulpn&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Bound to 127.0.0.1, or a firewall&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Connection refused"&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sudo ss -tlpn 'sport = :PORT'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Nothing listening on that port&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timeout connecting&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sudo nft list ruleset&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A drop rule, here or on the router&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Name does not resolve&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;dig name&lt;/code&gt; and &lt;code&gt;dig @router name&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Which DNS server is wrong&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Service dies randomly&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;journalctl -k -b&lt;/code&gt; with grep oom&lt;/td&gt;
&lt;td&gt;The OOM killer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cannot write files&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;df -h&lt;/code&gt; and &lt;code&gt;df -i&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;A full filesystem or no inodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Permission denied"&lt;/td&gt;
&lt;td&gt;&lt;code&gt;namei -l /path&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The missing permission on the path&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Broke after an update&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tail /var/log/apt/history.log&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What changed, and when&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSH refused&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;sudo sshd -t&lt;/code&gt; from the console&lt;/td&gt;
&lt;td&gt;A config error after an edit&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Asking for help well
&lt;/h2&gt;

&lt;p&gt;When the answer is in a forum or a bug tracker, the people there can only help with what you show them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The exact command you ran and its exact output, copied, not paraphrased.&lt;/li&gt;
&lt;li&gt;The relevant log lines (&lt;code&gt;journalctl -u app -n 50 --no-pager&lt;/code&gt;), with secrets removed.&lt;/li&gt;
&lt;li&gt;Versions: &lt;code&gt;cat /etc/debian_version&lt;/code&gt;, &lt;code&gt;uname -r&lt;/code&gt;, and the app's version.&lt;/li&gt;
&lt;li&gt;What you already tried, and what changed before it broke.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Writing that down often solves the problem before you post it, and it is exactly the note you will want next time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Follow the order: running, listening, reachable, logs, changes.&lt;/li&gt;
&lt;li&gt;Write down every change you make while troubleshooting.&lt;/li&gt;
&lt;li&gt;Snapshot before trying a fix on anything important.&lt;/li&gt;
&lt;li&gt;Read load average against &lt;code&gt;nproc&lt;/code&gt;, and memory by the &lt;code&gt;available&lt;/code&gt; column.&lt;/li&gt;
&lt;li&gt;Check the kernel log for OOM kills when a service dies without a reason.&lt;/li&gt;
&lt;li&gt;Truncate live logs instead of deleting them.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This is adapted from my book &lt;em&gt;Linux Without the Guesswork&lt;/em&gt; (Homelab the Right Way, Book 16). The free sample is &lt;a href="https://payhip.com/b/YLEK5" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/LV4qz" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
      <category>sysadmin</category>
      <category>homelab</category>
    </item>
    <item>
      <title>Android reminders that actually fire: exact alarms, SCHEDULE_EXACT_ALARM and rescheduling after reboot</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Sun, 08 Nov 2026 15:02:05 +0000</pubDate>
      <link>https://dev.to/charleshartmann/android-reminders-that-actually-fire-exact-alarms-scheduleexactalarm-and-rescheduling-after-264d</link>
      <guid>https://dev.to/charleshartmann/android-reminders-that-actually-fire-exact-alarms-scheduleexactalarm-and-rescheduling-after-264d</guid>
      <description>&lt;p&gt;A reminder is a promise with a time on it. The user said "remind me at 9:00", and if the notification arrives at 9:40, or after the next reboot, or never, the promise is broken. Android gives you several ways to run code later, and each one trades precision for battery. This post picks the right one for each job and makes reminders survive the three things that kill them: Doze, reboots and permission changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The menu of "later"
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Need&lt;/th&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Precision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Deferrable work: sync, cleanup, a daily digest&lt;/td&gt;
&lt;td&gt;WorkManager&lt;/td&gt;
&lt;td&gt;Minutes to hours; runs when constraints allow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Around this time"&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;AlarmManager.set()&lt;/code&gt; / &lt;code&gt;setAndAllowWhileIdle()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Within an hour on Android 12+, unless battery saving delays it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Within this window"&lt;/td&gt;
&lt;td&gt;&lt;code&gt;AlarmManager.setWindow()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Inside the window; short windows are usually stretched to 10 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"At this minute", user-set&lt;/td&gt;
&lt;td&gt;&lt;code&gt;setExactAndAllowWhileIdle()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Nearly exact, even in Doze; needs exact alarm access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;An alarm clock&lt;/td&gt;
&lt;td&gt;&lt;code&gt;setAlarmClock()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exact; shown to the user as an upcoming alarm&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Those figures come from Google's alarm documentation. For &lt;code&gt;set()&lt;/code&gt; and &lt;code&gt;setAndAllowWhileIdle()&lt;/code&gt;, the alarm never fires early, and on Android 12 (API 31) and higher the system fires it within one hour of the trigger time unless battery saver or Doze is in effect. For &lt;code&gt;setWindow()&lt;/code&gt; on apps targeting Android 12+, windows under 600,000 milliseconds are typically clipped to 10 minutes.&lt;/p&gt;

&lt;p&gt;An hour late is fine for "back up my notes overnight" and useless for "remind me before the meeting". A user-set reminder at a specific minute is exactly what exact alarms exist for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two permissions, very different terms
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;SCHEDULE_EXACT_ALARM&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;USE_EXACT_ALARM&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Granted by&lt;/td&gt;
&lt;td&gt;The user, in "Alarms &amp;amp; reminders"&lt;/td&gt;
&lt;td&gt;Automatically at install&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can the user revoke it?&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who should declare it&lt;/td&gt;
&lt;td&gt;Apps with a user-facing feature that needs precise timing&lt;/td&gt;
&lt;td&gt;Calendar and alarm clock apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Play&lt;/td&gt;
&lt;td&gt;Allowed for genuine precise-timing features&lt;/td&gt;
&lt;td&gt;Restricted by Play policy to limited use cases&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two details matter most. On Android 14 and higher, &lt;code&gt;SCHEDULE_EXACT_ALARM&lt;/code&gt; is no longer pre-granted to most newly installed apps that target Android 13 or higher; it starts out denied. And without the access, &lt;code&gt;setExact()&lt;/code&gt;, &lt;code&gt;setExactAndAllowWhileIdle()&lt;/code&gt; and &lt;code&gt;setAlarmClock()&lt;/code&gt; throw a &lt;code&gt;SecurityException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A notes app with a reminder feature is not a calendar or an alarm clock. It should declare &lt;code&gt;SCHEDULE_EXACT_ALARM&lt;/code&gt;, ask for it in context, and fall back to an inexact alarm when the user says no. Declaring &lt;code&gt;USE_EXACT_ALARM&lt;/code&gt; just to skip the question puts the Play listing at risk for a feature that works without it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.SCHEDULE_EXACT_ALARM"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.RECEIVE_BOOT_COMPLETED"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Asking for "Alarms &amp;amp; reminders"
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;SCHEDULE_EXACT_ALARM&lt;/code&gt; is a special app access, not a runtime permission. There is no dialog: you send the user to a settings page and check the result when they come back. Ask at the moment it matters, when the user sets a reminder for a specific time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;canScheduleExact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SDK_INT&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;am&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSystemService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;canScheduleExactAlarms&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;requestExactAlarmAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SDK_INT&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ACTION_REQUEST_SCHEDULE_EXACT_ALARM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromParts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"package"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;packageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explain why in your own UI first, then send the intent. One line under the time picker is enough: "Reminders may arrive up to an hour late. Allow exact reminders?" with an Allow button. If the user ignores it, the reminder is still saved and still fires, just less precisely.&lt;/p&gt;

&lt;p&gt;When the user grants access, the system sends &lt;code&gt;AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED&lt;/code&gt;. Listen for it, call &lt;code&gt;canScheduleExactAlarms()&lt;/code&gt; again rather than trusting the broadcast alone, and reschedule pending reminders as exact ones. The user can also revoke access at any time, and the system then cancels all of the app's future exact alarms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database first, alarm second
&lt;/h2&gt;

&lt;p&gt;Store every reminder in your database first (note ID, trigger time, state), then schedule the alarm. The database is the source of truth; the alarm is only the wake-up call. That split is what makes rescheduling possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReminderScheduler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;am&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSystemService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Reminder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pi&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;firePendingIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;at&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;triggerAt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toEpochMilli&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;canScheduleExact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setExactAndAllowWhileIdle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RTC_WAKEUP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RTC_WAKEUP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000L&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;firePendingIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;firePendingIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBroadcast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ReminderFireReceiver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fieldnotes://note/$noteId"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUri&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
        &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_UPDATE_CURRENT&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_IMMUTABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;RTC_WAKEUP&lt;/code&gt; uses wall-clock time and wakes the device, which is right for "9:00 tomorrow". For "in 20 minutes", &lt;code&gt;ELAPSED_REALTIME_WAKEUP&lt;/code&gt; with &lt;code&gt;SystemClock.elapsedRealtime()&lt;/code&gt; is immune to the user changing the clock. The data URI makes each reminder's PendingIntent unique, so &lt;code&gt;cancel()&lt;/code&gt; cancels exactly one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setExact*&lt;/code&gt; can still throw if access was revoked between your check and your call. Wrap the exact call in &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; and fall back to the inexact path in the &lt;code&gt;catch&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Surviving a reboot
&lt;/h2&gt;

&lt;p&gt;Google's alarm documentation puts it plainly: "By default, all alarms are canceled when a device shuts down." The fix is to reschedule from your database when the system finishes booting, and also when the clock, the time zone or the exact-alarm access changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;receiver&lt;/span&gt;
    &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;".reminders.RescheduleReceiver"&lt;/span&gt;
    &lt;span class="na"&gt;android:exported=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;intent-filter&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;action&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.intent.action.BOOT_COMPLETED"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;action&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.intent.action.TIME_SET"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;action&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;&lt;span class="s"&gt;"android.intent.action.TIMEZONE_CHANGED"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;action&lt;/span&gt; &lt;span class="na"&gt;android:name=&lt;/span&gt;
            &lt;span class="s"&gt;"android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/intent-filter&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/receiver&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The receiver must be exported because the system sends these broadcasts, and the boot broadcast reaches your app only if the user has launched it at least once. Whatever the trigger, it does one thing: read every pending reminder and schedule it again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RescheduleReceiver&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BroadcastReceiver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onReceive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ACTION_BOOT_COMPLETED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ACTION_TIME_CHANGED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ACTION_TIMEZONE_CHANGED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;AlarmManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pending&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;goAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;app&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;applicationContext&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;FieldNotesApp&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ReminderScheduler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;now&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reminders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;triggerAt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reminders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;markMissed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One trap: the action string for a clock change is &lt;code&gt;android.intent.action.TIME_SET&lt;/code&gt;, and the Kotlin constant that holds it is &lt;code&gt;Intent.ACTION_TIME_CHANGED&lt;/code&gt;. The names differ, which catches people out. Reminders whose time passed while the phone was off are marked missed and shown in the app rather than fired late.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the denied path
&lt;/h2&gt;

&lt;p&gt;On an emulator, turn the app's access off in Settings, Apps, Special app access, Alarms &amp;amp; reminders. The app should fall back to inexact alarms and show its one-line explanation, and &lt;code&gt;canScheduleExactAlarms()&lt;/code&gt; should report &lt;code&gt;false&lt;/code&gt;. Then reboot the emulator and confirm pending reminders were rescheduled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each job uses the least precise scheduler that still keeps its promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SCHEDULE_EXACT_ALARM&lt;/code&gt; is requested in context, never &lt;code&gt;USE_EXACT_ALARM&lt;/code&gt; for a non-calendar app.&lt;/li&gt;
&lt;li&gt;Reminders are stored first and scheduled second.&lt;/li&gt;
&lt;li&gt;Exact calls are wrapped in &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; with an inexact fallback.&lt;/li&gt;
&lt;li&gt;A receiver reschedules on boot, clock, time zone and access changes.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This is adapted from my book &lt;em&gt;Notify Them Right&lt;/em&gt; (Android the Right Way, Book 10). The free sample is &lt;a href="https://payhip.com/b/NnqQE" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/Y21by" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>mobile</category>
      <category>notifications</category>
    </item>
    <item>
      <title>Frigate recording retention: continuous, motion and alerts, and how big a disk you need</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Sat, 07 Nov 2026 15:20:33 +0000</pubDate>
      <link>https://dev.to/charleshartmann/frigate-recording-retention-continuous-motion-and-alerts-and-how-big-a-disk-you-need-2aa2</link>
      <guid>https://dev.to/charleshartmann/frigate-recording-retention-continuous-motion-and-alerts-and-how-big-a-disk-you-need-2aa2</guid>
      <description>&lt;p&gt;Recording is the easy part of a Frigate NVR to switch on and the hard part to size. Without a plan, one of two things happens. Either retention is set to "30 days of everything" and the disk fills in a week, at which point Frigate starts deleting the oldest footage on its own; or retention is so short that the clip you needed from last Tuesday is already gone. A retention plan is a short calculation and a few numbers in &lt;code&gt;config.yml&lt;/code&gt;. This post walks through both.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Frigate keeps
&lt;/h2&gt;

&lt;p&gt;Frigate records in short segments and keeps three kinds of footage, each with its own retention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Continuous&lt;/strong&gt;: all video, whether or not anything moved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Motion&lt;/strong&gt;: only segments in which motion was detected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alerts and detections&lt;/strong&gt;: segments that overlap a review item, with a few seconds before and after (&lt;code&gt;pre_capture&lt;/code&gt; and &lt;code&gt;post_capture&lt;/code&gt;, both 5 seconds by default).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each kind has its own number of days. A segment that overlaps an alert is the one you most want to keep, so give alerts the most days. Retention for alerts and detections also has a &lt;strong&gt;mode&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;What is kept for the review item's retention&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;all&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every segment that overlaps the review item&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;motion&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Only overlapping segments with motion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;active_objects&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Only overlapping segments with an active (moving) tracked object&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Three plans from the documentation
&lt;/h2&gt;

&lt;p&gt;Frigate's recording documentation gives three starting points. Pick the one that matches what you would need after an incident.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep everything for a while.&lt;/strong&gt; All video for 3 days, motion for 7 days, and anything overlapping an alert or detection for 30 days:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;record&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;continuous&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;motion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;
  &lt;span class="na"&gt;alerts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;all&lt;/span&gt;
  &lt;span class="na"&gt;detections&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reduced storage.&lt;/strong&gt; No continuous retention; motion for 3 days; alerts and detections for 30 days, keeping only their segments with motion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;record&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;motion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;alerts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;motion&lt;/span&gt;
  &lt;span class="na"&gt;detections&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;motion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Alerts only.&lt;/strong&gt; Video is discarded unless an alert is ongoing; alerts are kept for 30 days:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;record&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;continuous&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
  &lt;span class="na"&gt;alerts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;days&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;motion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Continuous footage is what answers "what happened in the driveway at 3 a.m." when nothing was detected, which is why the first plan is a good default if the disk allows it. Retention values can be set globally, as above, and overridden per camera. Days may be decimals, so &lt;code&gt;0.5&lt;/code&gt; keeps twelve hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Storage arithmetic
&lt;/h2&gt;

&lt;p&gt;The disk you need depends on the bitrate of each camera's main stream and the plan you picked. The upper bound is continuous recording:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GB per day per camera = bitrate (Mbit/s) x 86,400 / 8 / 1,000

  2 Mbit/s  -&amp;gt;  21.6 GB/day
  4 Mbit/s  -&amp;gt;  43.2 GB/day
  6 Mbit/s  -&amp;gt;  64.8 GB/day
  8 Mbit/s  -&amp;gt;  86.4 GB/day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the first plan, estimate each layer separately and add them. Continuous days are the full rate. Motion days and alert days are a fraction of it, and that fraction depends entirely on the scene: a camera on a quiet back garden may see motion a small part of the day, one facing a street much more. A worked example, arithmetic only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 cameras at 4 Mbit/s

Continuous, 3 days:      4 x 43.2 GB x 3          = 518 GB
Motion days 4-7:         4 x 43.2 GB x 4 x 0.20   = 138 GB   (assume 20% motion)
Alerts/detections 8-30:  4 x 43.2 GB x 23 x 0.05  = 199 GB   (assume 5% activity)
                                           Total  ~ 855 GB  -&amp;gt; plan for a 2 TB disk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 20 and 5 percent figures are assumptions to replace with your own. After a few days, open &lt;strong&gt;System &amp;gt; Storage&lt;/strong&gt; in Frigate: it shows how much space each camera's recordings use, which gives you a real per-day figure. That page counts Frigate's recordings only; for the disk itself, use &lt;code&gt;df -h&lt;/code&gt; on the host.&lt;/p&gt;

&lt;p&gt;Leave at least 20 percent of the disk free in the plan. Bitrates rise at night and in rain, cameras get added, and the emergency cleanup below works from the disk's real free space.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the disk fills anyway
&lt;/h2&gt;

&lt;p&gt;Frigate watches the free space of the disk holding &lt;code&gt;/media/frigate/recordings&lt;/code&gt;. When less than roughly one hour of recording space remains, estimated from the current bitrate, it deletes the oldest recordings first, &lt;strong&gt;regardless of your retention settings&lt;/strong&gt;, and logs a message. Two consequences follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Anything else filling that disk, even files that are not Frigate's, can trigger deletion of your oldest recordings. Keep the recordings disk for recordings only.&lt;/li&gt;
&lt;li&gt;Frequent emergency cleanups mean your retention is larger than the disk. Reduce the days until normal retention keeps up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also check from inside the container that &lt;code&gt;/media/frigate&lt;/code&gt; really is the disk you think it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec &lt;/span&gt;frigate &lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; /media/frigate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a disk or NFS share is not actually mounted there, Frigate fills whatever filesystem is underneath, usually the system disk. If you record to a NAS, a dedicated dataset with its own quota keeps a runaway Frigate from filling the rest of the NAS, and keeps the NAS's other files from triggering Frigate's cleanup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snapshots and exports
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Snapshots&lt;/strong&gt; are still images of tracked objects. They are what Home Assistant attaches to notifications, and they are off by default. Turn them on and give them their own retention:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;snapshots&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;bounding_box&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;retain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exports&lt;/strong&gt; are how you keep a specific clip beyond retention. Use the Export button in the History view (or right-click a review item), choose the time range and give it a name. Exports are saved separately and are not removed by retention. Download an export to hand a clip to someone instead of copying files out of the recordings folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  H.265 recordings on Apple devices
&lt;/h2&gt;

&lt;p&gt;If a camera records an H.265 main stream, Safari on macOS and iPhone may refuse to play its recordings. Frigate has a per-camera setting for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;cameras&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;driveway&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ffmpeg&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;apple_compatibility&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test playback from every device that will view recordings, including the Home Assistant app, before you rely on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to back up
&lt;/h2&gt;

&lt;p&gt;The recordings themselves are rarely backed up: they are large, they expire by design, and the important clips are exports. What you must back up is small:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;config.yml&lt;/code&gt; (your whole configuration)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;frigate.db&lt;/code&gt; (review items, tracked objects, users), copied while Frigate is stopped or with SQLite's backup command&lt;/li&gt;
&lt;li&gt;your &lt;code&gt;compose.yaml&lt;/code&gt; and &lt;code&gt;.env&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the exports you want to keep, copied off the Frigate disk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Frigate runs in a Proxmox VM, a VM backup covers all of it. Untick &lt;strong&gt;Backup&lt;/strong&gt; on the recordings disk in the VM's Hardware tab so the backup skips the footage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Plan chosen and written down: continuous, motion and alert/detection days.&lt;/li&gt;
&lt;li&gt;Storage estimate done with your real bitrates, plus 20 percent headroom.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;record&lt;/code&gt; block in &lt;code&gt;config.yml&lt;/code&gt; matches the plan.&lt;/li&gt;
&lt;li&gt;Snapshots enabled with retention.&lt;/li&gt;
&lt;li&gt;The recordings disk holds nothing but Frigate media.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker exec frigate df -h /media/frigate&lt;/code&gt; shows the right disk.&lt;/li&gt;
&lt;li&gt;Storage page checked after a week, retention adjusted.&lt;/li&gt;
&lt;li&gt;Config, database and compose files are in your backups.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This is adapted from my book &lt;em&gt;Cameras You Control&lt;/em&gt; (Homelab the Right Way, Book 15). The free sample is &lt;a href="https://payhip.com/b/Vz6dk" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/rPynw" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>homelab</category>
      <category>selfhosted</category>
      <category>security</category>
      <category>docker</category>
    </item>
    <item>
      <title>Will this model fit on your GPU? Size a local LLM on paper before you pull it</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Tue, 03 Nov 2026 15:00:30 +0000</pubDate>
      <link>https://dev.to/charleshartmann/will-this-model-fit-on-your-gpu-size-a-local-llm-on-paper-before-you-pull-it-1aop</link>
      <guid>https://dev.to/charleshartmann/will-this-model-fit-on-your-gpu-size-a-local-llm-on-paper-before-you-pull-it-1aop</guid>
      <description>&lt;p&gt;You download the biggest model your disk can hold, ask it a question, and wait. The answer arrives a word at a time, the CPU fans spin up, and &lt;code&gt;ollama ps&lt;/code&gt; says &lt;code&gt;45%/55% CPU/GPU&lt;/code&gt;. The model did not fit in the card's memory, so part of it runs from system RAM, and the whole thing moves at the speed of that slow part. The opposite mistake is just as common: a tiny model that fits easily but cannot follow a two-step instruction.&lt;/p&gt;

&lt;p&gt;Sizing is arithmetic, not guesswork. Three numbers decide whether a model fits: its parameters, its quantization, and the context you give it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parameters: what the "8b" means
&lt;/h2&gt;

&lt;p&gt;A model's name usually carries its size: &lt;code&gt;llama3.1:8b&lt;/code&gt;, &lt;code&gt;qwen3:14b&lt;/code&gt;, &lt;code&gt;gemma3:27b&lt;/code&gt;. The number is the parameter count in billions, the weights the model learned during training. More parameters generally means more knowledge and better reasoning, and always means more memory and slower answers on the same hardware.&lt;/p&gt;

&lt;p&gt;At full 16-bit precision each parameter takes 2 bytes, so an 8-billion-parameter model is about 16 GB of weights. That is why the Ollama library lists &lt;code&gt;llama3.1:8b-instruct-fp16&lt;/code&gt; at 16 GB.&lt;/p&gt;

&lt;p&gt;Mixture-of-experts models such as &lt;code&gt;qwen3:30b&lt;/code&gt; or &lt;code&gt;gpt-oss:20b&lt;/code&gt; hold all their parameters in memory but use only part of them for each word, so they can answer faster than a dense model of the same size. For sizing, what matters is the full size of the file you load.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quantization: smaller numbers, smaller model
&lt;/h2&gt;

&lt;p&gt;Quantization stores each weight with fewer bits. A model quantized to about 4 bits per weight is roughly a quarter the size of the 16-bit original, and in practice loses surprisingly little quality on most tasks. Ollama and llama.cpp use the &lt;strong&gt;GGUF&lt;/strong&gt; file format, and the quantization is part of the tag:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Q8_0&lt;/strong&gt;: about 8 bits per weight. Closest to the original, twice the size of Q4.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Q6_K&lt;/strong&gt;, &lt;strong&gt;Q5_K_M&lt;/strong&gt;: the middle ground.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Q4_K_M&lt;/strong&gt;: about 4 to 5 bits per weight. The common default, and what Ollama gives you for a plain tag like &lt;code&gt;llama3.1:8b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Q3_K_M&lt;/strong&gt;, &lt;strong&gt;Q2_K&lt;/strong&gt;: smaller still, with a visible quality cost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fp16&lt;/strong&gt; or &lt;strong&gt;bf16&lt;/strong&gt;: no quantization at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start at Q4_K_M. Move up only if you have memory to spare and can see a difference on your own prompts. Dropping to Q3 or Q2 to squeeze in a bigger model is usually worse than running a smaller model at Q4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context: the memory everyone forgets
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;context window&lt;/strong&gt; is how many tokens the model can see at once: the system prompt, the conversation so far, any retrieved documents and the answer it is writing. Ollama picks a default from the card's memory; its context-length page lists 4k tokens below 24 GiB of VRAM, 32k from 24 to 48 GiB and 256k at 48 GiB or more.&lt;/p&gt;

&lt;p&gt;Context costs memory because the model keeps a &lt;strong&gt;KV cache&lt;/strong&gt;: for every token in the window it stores a key and a value vector in every layer. The size per token comes straight from the model's architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KV bytes per token = 2 (key and value) x layers x KV heads x head size x bytes per number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Llama 3.1 8B's published configuration has 32 layers, 8 key-value heads and a head size of 128. With a 16-bit cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 x 32 x 8 x 128 x 2 bytes = 131,072 bytes, about 0.13 MB per token
  4,096 tokens  -&amp;gt;  about 0.54 GB
  8,192 tokens  -&amp;gt;  about 1.07 GB
 32,768 tokens  -&amp;gt;  about 4.29 GB
131,072 tokens  -&amp;gt;  about 17.2 GB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last line is the full context Llama 3.1 supports, and its cache alone is bigger than the 16-bit model. Long context is not free.&lt;/p&gt;

&lt;p&gt;Two settings change the picture. Ollama can store the cache in 8-bit numbers: its FAQ lists &lt;code&gt;f16&lt;/code&gt; as the default, &lt;code&gt;q8_0&lt;/code&gt; as about half the memory with a very small loss in precision, and &lt;code&gt;q4_0&lt;/code&gt; as smaller still with a more noticeable loss. A quantized cache needs flash attention, which Ollama enables automatically where the hardware supports it. And parallel requests multiply context: with &lt;code&gt;OLLAMA_NUM_PARALLEL=4&lt;/code&gt;, the FAQ says required memory scales with the number of parallel requests times the context length, so four parallel 8K conversations reserve as much cache as one 32K conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The estimate
&lt;/h2&gt;

&lt;p&gt;Add three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Weights&lt;/strong&gt;: the file size of the tag you pull.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KV cache&lt;/strong&gt;: per-token size times context length (times parallel requests).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overhead&lt;/strong&gt;: compute buffers, the CUDA runtime and whatever else sits on the card. It varies by model, backend and driver; the examples below assume about 1 GB, and you confirm the real number on your own machine.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Llama 3.1 8B with an 8,192-token context, using the library's file sizes. These are estimates, rounded to a tenth of a gigabyte:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tag&lt;/th&gt;
&lt;th&gt;Weights&lt;/th&gt;
&lt;th&gt;KV cache (8K, f16)&lt;/th&gt;
&lt;th&gt;Overhead (assumed)&lt;/th&gt;
&lt;th&gt;Estimate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;8b-instruct-q4_K_M&lt;/td&gt;
&lt;td&gt;4.9 GB&lt;/td&gt;
&lt;td&gt;1.1 GB&lt;/td&gt;
&lt;td&gt;1.0 GB&lt;/td&gt;
&lt;td&gt;about 7.0 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8b-instruct-q5_K_M&lt;/td&gt;
&lt;td&gt;5.7 GB&lt;/td&gt;
&lt;td&gt;1.1 GB&lt;/td&gt;
&lt;td&gt;1.0 GB&lt;/td&gt;
&lt;td&gt;about 7.8 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8b-instruct-q8_0&lt;/td&gt;
&lt;td&gt;8.5 GB&lt;/td&gt;
&lt;td&gt;1.1 GB&lt;/td&gt;
&lt;td&gt;1.0 GB&lt;/td&gt;
&lt;td&gt;about 10.6 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8b-instruct-fp16&lt;/td&gt;
&lt;td&gt;16 GB&lt;/td&gt;
&lt;td&gt;1.1 GB&lt;/td&gt;
&lt;td&gt;1.0 GB&lt;/td&gt;
&lt;td&gt;about 18.1 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The same model at Q4_K_M as the context grows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Context&lt;/th&gt;
&lt;th&gt;KV cache&lt;/th&gt;
&lt;th&gt;Estimate (f16 cache)&lt;/th&gt;
&lt;th&gt;Estimate (q8_0 cache)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;4,096&lt;/td&gt;
&lt;td&gt;0.5 GB&lt;/td&gt;
&lt;td&gt;about 6.4 GB&lt;/td&gt;
&lt;td&gt;about 6.2 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8,192&lt;/td&gt;
&lt;td&gt;1.1 GB&lt;/td&gt;
&lt;td&gt;about 7.0 GB&lt;/td&gt;
&lt;td&gt;about 6.4 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32,768&lt;/td&gt;
&lt;td&gt;4.3 GB&lt;/td&gt;
&lt;td&gt;about 10.2 GB&lt;/td&gt;
&lt;td&gt;about 8.0 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the tables against your card. An 8 GB card runs the Q4_K_M model comfortably at the default context and gets tight at 8K. A 12 GB card has room for 8K at Q5_K_M, or 32K with a quantized cache. A 16 GB card can hold Q8_0 at 8K. Leave a margin: if the estimate is within a gigabyte of the card's total, expect Ollama to spill part of the model to system RAM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check it against reality
&lt;/h2&gt;

&lt;p&gt;An estimate is a starting point. Load the model with the context you plan to use, then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ollama ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The SIZE column is what Ollama actually allocated, the CONTEXT column is the context it used, and the PROCESSOR column must say &lt;code&gt;100% GPU&lt;/code&gt;. If it shows a CPU/GPU split, pick a smaller quantization, a smaller context or a smaller model, and check again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking the file
&lt;/h2&gt;

&lt;p&gt;The Ollama library at ollama.com/library lists every model's sizes, context window and capabilities, and each tags page lists every quantization with its file size. &lt;code&gt;ollama pull qwen3:8b&lt;/code&gt; downloads the default quantization; &lt;code&gt;ollama pull llama3.1:8b-instruct-q5_K_M&lt;/code&gt; asks for a specific one.&lt;/p&gt;

&lt;p&gt;Hugging Face hosts many more GGUF files, and Ollama can run them directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Q4_K_M is used by default when the repository has it&lt;/span&gt;
ollama run hf.co/bartowski/Llama-3.2-1B-Instruct-GGUF

&lt;span class="c"&gt;# Ask for a specific quantization by its tag&lt;/span&gt;
ollama run hf.co/bartowski/Llama-3.2-1B-Instruct-GGUF:Q8_0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stick to repositories from the model's publisher or well-known quantizers, and read the model card, including the license, before you build anything on top. "Open" models come with very different licenses: as listed on Hugging Face in October 2026, Llama 3.1 uses the Llama 3.1 Community License and Gemma 3 the Gemma Terms of Use, while Qwen3, gpt-oss and Mistral 7B are Apache 2.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You know your card's memory before you pick a model.&lt;/li&gt;
&lt;li&gt;You start at Q4_K_M and move up only with memory to spare.&lt;/li&gt;
&lt;li&gt;You estimate weights + KV cache + overhead before you pull.&lt;/li&gt;
&lt;li&gt;You know the context you need, and that parallel requests multiply it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ollama ps&lt;/code&gt; shows the SIZE you expected and &lt;code&gt;100% GPU&lt;/code&gt; after loading.&lt;/li&gt;
&lt;li&gt;You have read the license of every model you build on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is adapted from my book &lt;em&gt;Private AI at Home&lt;/em&gt; (Homelab the Right Way, Book 14). The free sample is &lt;a href="https://payhip.com/b/NfDLe" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/71LQ2" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>homelab</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>Your fingerprint prompt is a speed bump, not a lock: Android biometrics with a CryptoObject</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Mon, 02 Nov 2026 15:24:01 +0000</pubDate>
      <link>https://dev.to/charleshartmann/your-fingerprint-prompt-is-a-speed-bump-not-a-lock-android-biometrics-with-a-cryptoobject-16jc</link>
      <guid>https://dev.to/charleshartmann/your-fingerprint-prompt-is-a-speed-bump-not-a-lock-android-biometrics-with-a-cryptoobject-16jc</guid>
      <description>&lt;p&gt;A fingerprint prompt feels like security. The user touches the sensor, a little animation plays, the app opens. But look at what most implementations do with the result: &lt;code&gt;onAuthenticationSucceeded()&lt;/code&gt; sets a boolean, and the boolean opens the screen. Anyone who can change that boolean, with a debugger on a rooted phone or by patching the APK, opens the screen without a fingerprint. Worse, the data behind the screen was never protected at all; it was sitting in storage the whole time.&lt;/p&gt;

&lt;p&gt;A biometric check is only as strong as what it unlocks. This post shows the difference between a prompt that decides a branch in your code and a prompt that releases a cryptographic key, and how to build the second kind with &lt;code&gt;androidx.biometric&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The library
&lt;/h2&gt;

&lt;p&gt;Use the Jetpack Biometric library rather than the platform &lt;code&gt;BiometricPrompt&lt;/code&gt; directly. It works back to older Android versions, handles device differences and gives you one API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"androidx.biometric:biometric:1.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;1.1.0 is the stable release in October 2026. The 1.4.0 alpha line adds an activity-result API, a &lt;code&gt;biometric-compose&lt;/code&gt; artifact and &lt;code&gt;KeyAgreement&lt;/code&gt; support in &lt;code&gt;CryptoObject&lt;/code&gt;; everything below uses the stable &lt;code&gt;BiometricPrompt&lt;/code&gt; API, which the alpha keeps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strong, weak and the screen lock
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;BiometricManager.Authenticators&lt;/code&gt; defines three types, and the difference between the first two is the whole story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BIOMETRIC_STRONG&lt;/strong&gt;: a Class 3 biometric under the Android Compatibility Definition. Strong enough to unlock Keystore keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BIOMETRIC_WEAK&lt;/strong&gt;: a Class 2 biometric. Can confirm presence, cannot unlock keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DEVICE_CREDENTIAL&lt;/strong&gt;: the screen lock (PIN, pattern or password).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The device maker decides which sensor is Class 2 or Class 3, not you. Many face unlock implementations are Class 2 and many fingerprint sensors are Class 3, but do not assume; ask the system.&lt;/p&gt;

&lt;p&gt;A few rules from the documentation shape everything:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You cannot call &lt;code&gt;setNegativeButtonText()&lt;/code&gt; together with &lt;code&gt;DEVICE_CREDENTIAL&lt;/code&gt;; when the screen lock is allowed, the system provides its own fallback button.&lt;/li&gt;
&lt;li&gt;On Android 10 (API level 29) and lower, &lt;code&gt;DEVICE_CREDENTIAL&lt;/code&gt; alone and &lt;code&gt;BIOMETRIC_STRONG or DEVICE_CREDENTIAL&lt;/code&gt; are not supported combinations. Check for a screen lock with &lt;code&gt;KeyguardManager.isDeviceSecure()&lt;/code&gt; instead.&lt;/li&gt;
&lt;li&gt;Crypto-based authentication is incompatible with Class 2 biometrics, and before Android 11 with the device credential too. Passing a &lt;code&gt;CryptoObject&lt;/code&gt; with those authenticators throws &lt;code&gt;IllegalArgumentException&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Check before you prompt
&lt;/h2&gt;

&lt;p&gt;Call &lt;code&gt;canAuthenticate()&lt;/code&gt; with exactly the combination you will pass to the prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;biometricState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;BiometricState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BiometricManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;canAuthenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;BiometricManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BIOMETRIC_SUCCESS&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BiometricState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ready&lt;/span&gt;
        &lt;span class="nc"&gt;BiometricManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BIOMETRIC_ERROR_NONE_ENROLLED&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BiometricState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NeedsEnrollment&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BiometricState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unavailable&lt;/span&gt;    &lt;span class="c1"&gt;// no hardware, unavailable, update needed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When nothing is enrolled, on Android 11 (API level 30) and higher you can send the user to &lt;code&gt;Settings.ACTION_BIOMETRIC_ENROLL&lt;/code&gt;, passing the authenticators you accept in &lt;code&gt;Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED&lt;/code&gt;. When biometrics are unavailable, fall back to your normal sign-in rather than hiding the feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  The prompt without crypto, and what it proves
&lt;/h2&gt;

&lt;p&gt;The common version calls &lt;code&gt;showNotes()&lt;/code&gt; from &lt;code&gt;onAuthenticationSucceeded()&lt;/code&gt;. A successful callback proves only that the system said the user passed, and your code then trusts a callback inside your own process. With a patched APK, or on a device where an attacker controls your process, that callback can be faked, and the notes were readable all along. It is fine for confirming that the person holding an already-unlocked phone is probably the owner before showing something mildly sensitive. It is not a lock.&lt;/p&gt;

&lt;h2&gt;
  
  
  The prompt with crypto: unlocking a key
&lt;/h2&gt;

&lt;p&gt;The fix is to make the prompt release something the app cannot get any other way. Create a key in the Android Keystore that can only be used after a strong biometric check, and encrypt the sensitive data with it. Now the only way to decrypt is to pass the prompt, because the Keystore enforces the check outside your process, often in secure hardware.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: an authentication-bound key
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;KEY_ALIAS&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"fieldnotes_vault"&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createVaultKey&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;spec&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyGenParameterSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nc"&gt;KEY_ALIAS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PURPOSE_ENCRYPT&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PURPOSE_DECRYPT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBlockModes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BLOCK_MODE_GCM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setEncryptionPaddings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ENCRYPTION_PADDING_NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKeySize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setUserAuthenticationRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// 0 = authenticate for every use; API 30+ form of the setting&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setUserAuthenticationParameters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AUTH_BIOMETRIC_STRONG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setInvalidatedByBiometricEnrollment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nc"&gt;KeyGenerator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KEY_ALGORITHM_AES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"AndroidKeyStore"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;generateKey&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setUserAuthenticationRequired(true)&lt;/code&gt; makes the key unusable until the user authenticates.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setUserAuthenticationParameters(timeout, type)&lt;/code&gt;, added in API level 30, sets how long a successful authentication keeps the key usable (0 means every operation needs its own prompt) and which kinds count. &lt;code&gt;AUTH_BIOMETRIC_STRONG&lt;/code&gt; accepts only Class 3 biometrics; add &lt;code&gt;or KeyProperties.AUTH_DEVICE_CREDENTIAL&lt;/code&gt; to accept the screen lock too. Below API 30, the older &lt;code&gt;setUserAuthenticationValidityDurationSeconds()&lt;/code&gt; does the same job, with &lt;code&gt;-1&lt;/code&gt; meaning per-use.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setInvalidatedByBiometricEnrollment(true)&lt;/code&gt; (the default, API 24 and higher) permanently invalidates the key when the user enrolls a new fingerprint or face. Someone who learns the PIN cannot add their own finger and then decrypt.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2: prompt with a CryptoObject
&lt;/h3&gt;

&lt;p&gt;Initialize a &lt;code&gt;Cipher&lt;/code&gt; with the key, wrap it in a &lt;code&gt;CryptoObject&lt;/code&gt; and pass it to &lt;code&gt;authenticate()&lt;/code&gt;. On success, the cipher in the result is authorized for one operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;cipher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ENCRYPT_MODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;vaultKey&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;prompt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BiometricPrompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMainExecutor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;BiometricPrompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationCallback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onAuthenticationSucceeded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BiometricPrompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;c&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cryptoObject&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
            &lt;span class="nf"&gt;onDone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EncryptedBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;BiometricPrompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PromptInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Protect your vault"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAllowedAuthenticators&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BIOMETRIC_STRONG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setNegativeButtonText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cancel"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nc"&gt;BiometricPrompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CryptoObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Decryption is the same with &lt;code&gt;Cipher.DECRYPT_MODE&lt;/code&gt; and a &lt;code&gt;GCMParameterSpec(128, blob.iv)&lt;/code&gt;. Store the IV next to the ciphertext; it is not secret, but GCM needs it, and the Keystore generates a fresh random IV for each encryption by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: handle a dead key
&lt;/h3&gt;

&lt;p&gt;When the user adds a new fingerprint or removes the screen lock, the key is gone for good and &lt;code&gt;cipher.init()&lt;/code&gt; throws &lt;code&gt;KeyPermanentlyInvalidatedException&lt;/code&gt;. That is the system doing its job. Catch it, delete the key alias and the ciphertext it protected, and ask the user to sign in again the normal way. Never treat it as a crash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cipherOrNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;vaultKey&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;vaultKey&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KeyPermanentlyInvalidatedException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;KeyStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AndroidKeyStore"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;deleteEntry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KEY_ALIAS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;vaultStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;null&lt;/span&gt;   &lt;span class="c1"&gt;// caller falls back to a full sign-in&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where it belongs
&lt;/h2&gt;

&lt;p&gt;Good uses: unlocking a stored refresh token so a returning user signs in with a fingerprint; confirming a sensitive action such as deleting the account, where the strongest version signs a server nonce with a biometric-bound private key; and a private vault inside the app with its own authentication-bound key.&lt;/p&gt;

&lt;p&gt;Bad uses: a prompt in front of the whole app while the data sits unencrypted behind it, or a prompt treated as a replacement for server-side authentication. After too many failed attempts the system locks biometrics out and reports it through &lt;code&gt;onAuthenticationError()&lt;/code&gt;; offer the screen lock or your normal sign-in instead of looping the prompt. And if what you want is "sign in with my fingerprint", passkeys are the better answer, since the credential provider already uses the screen lock and biometrics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;canAuthenticate()&lt;/code&gt; is called with the same authenticators the prompt uses.&lt;/li&gt;
&lt;li&gt;Anything sensitive is encrypted with a Keystore key that requires &lt;code&gt;BIOMETRIC_STRONG&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The prompt passes a &lt;code&gt;CryptoObject&lt;/code&gt;, and only the authorized cipher touches the data.&lt;/li&gt;
&lt;li&gt;Keys are invalidated by new biometric enrollment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;KeyPermanentlyInvalidatedException&lt;/code&gt; leads to a clean re-sign-in, not a crash.&lt;/li&gt;
&lt;li&gt;A failed or locked-out prompt falls back to the screen lock or normal sign-in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is adapted from my book &lt;em&gt;Sign-In and Security Done Right&lt;/em&gt; (Android the Right Way, Book 9). The free sample is &lt;a href="https://payhip.com/b/01OcP" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/oZl1J" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>security</category>
      <category>kotlin</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Proxmox VE 9 high availability: HA rules, fencing and a failover you have actually tested</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Sun, 01 Nov 2026 15:18:31 +0000</pubDate>
      <link>https://dev.to/charleshartmann/proxmox-ve-9-high-availability-ha-rules-fencing-and-a-failover-you-have-actually-tested-4n05</link>
      <guid>https://dev.to/charleshartmann/proxmox-ve-9-high-availability-ha-rules-fencing-and-a-failover-you-have-actually-tested-4n05</guid>
      <description>&lt;p&gt;High availability is the feature most people build a Proxmox VE cluster for: a node dies at 3 a.m. and its important guests are running again on another node before anyone wakes up. Proxmox VE does this with two daemons, a watchdog and a set of rules. This post explains how those pieces fit together, adds guests to HA, writes placement rules (which changed in Proxmox VE 9), walks through a node failure step by step, and covers planned shutdowns and maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap
&lt;/h2&gt;

&lt;p&gt;HA gets switched on for a cluster that is not ready for it: two nodes and no QDevice, or guests whose disks live on local storage with no replica. When a node fails, nothing restarts, or the surviving node fences itself.&lt;/p&gt;

&lt;p&gt;The second trap is using HA as a substitute for backups. HA restarts the guest. It does not undo whatever was wrong with it.&lt;/p&gt;

&lt;p&gt;The documented requirements are short: at least three cluster nodes (or two plus a QDevice) for reliable quorum, shared storage for the guests (or replicated storage, with a data-loss window back to the last replica), hardware redundancy, and a watchdog.&lt;/p&gt;

&lt;h2&gt;
  
  
  The HA stack in two daemons
&lt;/h2&gt;

&lt;p&gt;Every node runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pve-ha-lrm&lt;/strong&gt;, the local resource manager. It starts and stops HA guests on its own node, following the state the CRM requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pve-ha-crm&lt;/strong&gt;, the cluster resource manager. One node's CRM is the active master at any time. It tells the LRMs what to run, moves guests when something fails and handles fencing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both rely on locks in the cluster file system and on a &lt;strong&gt;watchdog&lt;/strong&gt;. While a node is healthy and quorate, its daemons keep resetting the watchdog timer. If the node loses quorum or the HA daemons stop working, the timer runs out and the watchdog resets the node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why fencing matters
&lt;/h3&gt;

&lt;p&gt;Before a guest from a failed node can start somewhere else, the cluster must be certain the failed node is not still running it, perhaps cut off from the network but still writing to shared storage. Starting a second copy would corrupt the disk. Making sure the node is really off is called fencing.&lt;/p&gt;

&lt;p&gt;Proxmox VE fences with watchdog timers. A node outside the quorate part of the cluster cannot reset its watchdog, so the watchdog resets it. Once the CRM master can take the failed node's lock, it knows the node has fenced itself and can recover its guests.&lt;/p&gt;

&lt;p&gt;Without a configured hardware watchdog, Proxmox VE uses the Linux kernel's &lt;strong&gt;softdog&lt;/strong&gt;, which the docs describe as reliable but not independent of the server's hardware. Hardware watchdog modules are blocked by default; to use one, name its module and reboot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# /etc/default/pve-ha-manager&lt;/span&gt;
&lt;span class="nv"&gt;WATCHDOG_MODULE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;iTCO_wdt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One warning from the docs worth repeating: never kill &lt;code&gt;pve-ha-crm&lt;/code&gt;, &lt;code&gt;pve-ha-lrm&lt;/code&gt; or &lt;code&gt;watchdog-mux&lt;/code&gt; by hand. They manage the watchdog, and killing them can cause an immediate reset of the node.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding guests to HA
&lt;/h2&gt;

&lt;p&gt;An HA resource is a type and an ID: &lt;code&gt;vm:100&lt;/code&gt;, &lt;code&gt;ct:110&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ha-manager add vm:100
ha-manager add ct:110 &lt;span class="nt"&gt;--state&lt;/span&gt; started &lt;span class="nt"&gt;--max_restart&lt;/span&gt; 1 &lt;span class="nt"&gt;--max_relocate&lt;/span&gt; 1
ha-manager config
ha-manager status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the web interface it is &lt;strong&gt;Datacenter → HA → Resources → Add&lt;/strong&gt;, or &lt;strong&gt;More → Manage HA&lt;/strong&gt; on a guest.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--state&lt;/code&gt; is the requested state: &lt;code&gt;started&lt;/code&gt;, &lt;code&gt;stopped&lt;/code&gt;, &lt;code&gt;disabled&lt;/code&gt; or &lt;code&gt;ignored&lt;/code&gt;. Once HA manages a guest, ordinary &lt;code&gt;qm start&lt;/code&gt; and &lt;code&gt;qm stop&lt;/code&gt; simply change its requested state. &lt;code&gt;max_restart&lt;/code&gt; is how often HA retries starting it on the same node after a failed start, and &lt;code&gt;max_relocate&lt;/code&gt; how often it tries another node; both default to 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ha-manager &lt;span class="nb"&gt;set &lt;/span&gt;vm:100 &lt;span class="nt"&gt;--state&lt;/span&gt; stopped     &lt;span class="c"&gt;# stop and keep stopped&lt;/span&gt;
ha-manager migrate vm:100 pve2            &lt;span class="c"&gt;# live migration&lt;/span&gt;
ha-manager relocate vm:100 pve2           &lt;span class="c"&gt;# stop, move, start&lt;/span&gt;
ha-manager remove vm:100                  &lt;span class="c"&gt;# stop managing (does not stop it)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before you add a guest, check it can run on every node HA might pick: disks on shared or replicated storage, bridges with the same names on every node, and no passthrough devices. If a dependency exists on only some nodes, restrict the guest to them with a strict node affinity rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  HA groups became HA rules in Proxmox VE 9
&lt;/h2&gt;

&lt;p&gt;Earlier releases used &lt;strong&gt;HA groups&lt;/strong&gt; to say which nodes a guest prefers. Proxmox VE 9.0 deprecated them in favour of &lt;strong&gt;HA rules&lt;/strong&gt;, stored in &lt;code&gt;/etc/pve/ha/rules.cfg&lt;/code&gt;. Existing groups are migrated to node affinity rules automatically once every node runs Proxmox VE 9; if anything looks wrong afterwards, the upgrade guide points to the active CRM's log, &lt;code&gt;journalctl -eu pve-ha-crm&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node affinity: which nodes a guest runs on
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Prefer pve1 for the firewall VM; any node is fine if pve1 is down&lt;/span&gt;
ha-manager rules add node-affinity fw-on-pve1 &lt;span class="nt"&gt;--resources&lt;/span&gt; vm:100 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--nodes&lt;/span&gt; pve1
&lt;span class="c"&gt;# Make it a hard requirement instead&lt;/span&gt;
ha-manager rules &lt;span class="nb"&gt;set &lt;/span&gt;node-affinity fw-on-pve1 &lt;span class="nt"&gt;--strict&lt;/span&gt; 1
&lt;span class="c"&gt;# Ordered preference: pve1 first, then pve2 or pve3 equally&lt;/span&gt;
ha-manager rules add node-affinity ha-core &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--resources&lt;/span&gt; vm:101,ct:110 &lt;span class="nt"&gt;--nodes&lt;/span&gt; &lt;span class="s2"&gt;"pve1:2,pve2:1,pve3:1"&lt;/span&gt;
&lt;span class="c"&gt;# Keep these guests off pve3 (the small node)&lt;/span&gt;
ha-manager rules add node-affinity not-pve3 &lt;span class="nt"&gt;--affinity&lt;/span&gt; negative &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--resources&lt;/span&gt; ct:200,vm:300 &lt;span class="nt"&gt;--nodes&lt;/span&gt; pve3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node affinity rules are &lt;strong&gt;not strict by default&lt;/strong&gt;: if none of the listed nodes is available, the guest may run elsewhere. With &lt;code&gt;--strict 1&lt;/code&gt; it is restricted to the listed nodes and stopped if none of them is up. Priorities are relative; higher numbers win, and nodes without a number get 0. &lt;code&gt;failback&lt;/code&gt;, a per-resource property that defaults to on, moves a guest back to its highest-priority node when that node returns.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resource affinity: guests together or apart
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Two DNS servers should never share a node&lt;/span&gt;
ha-manager rules add resource-affinity dns-apart &lt;span class="nt"&gt;--affinity&lt;/span&gt; negative &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--resources&lt;/span&gt; ct:120,ct:121
&lt;span class="c"&gt;# An app and its database should stay together&lt;/span&gt;
ha-manager rules add resource-affinity app-with-db &lt;span class="nt"&gt;--affinity&lt;/span&gt; positive &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--resources&lt;/span&gt; vm:130,vm:131
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resource affinity rules are the opposite: strict by default. A negative rule cannot list more resources than there are nodes.&lt;/p&gt;

&lt;p&gt;Rules are checked for conflicts before they apply. A resource can be in only one node affinity rule, for example, and two resources cannot be kept both together and apart. A conflicting rule is disabled until you resolve it, so run &lt;code&gt;ha-manager rules config&lt;/code&gt; after every edit.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when a node dies
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Node &lt;code&gt;pve2&lt;/code&gt; loses power. Corosync on &lt;code&gt;pve1&lt;/code&gt; and &lt;code&gt;pve3&lt;/code&gt; stops hearing from it and forms a new membership without it. Two of three votes: still quorate.&lt;/li&gt;
&lt;li&gt;The CRM master marks &lt;code&gt;pve2&lt;/code&gt;'s HA guests as waiting for fencing (state &lt;code&gt;fence&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;pve2&lt;/code&gt; were still running but cut off, it would be in a minority without quorum and unable to reset its watchdog, and the watchdog (60 seconds, per the docs) would reset it. A node with no power is already fenced in practice.&lt;/li&gt;
&lt;li&gt;Once the CRM can take &lt;code&gt;pve2&lt;/code&gt;'s lock, the node counts as fenced and its guests move to &lt;code&gt;recovery&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The CRM picks a node for each guest from the online, quorate nodes, narrowed by the affinity rules, and chooses the one with the lowest load. The guests start there.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;pve2&lt;/code&gt; comes back it rejoins the cluster, and guests with failback move back if a rule prefers it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With Ceph or shared storage the guests start with every completed write intact. With ZFS replication they start from the last replica. And every guest that ran on the failed node now runs on fewer nodes, which the docs warn can overload small clusters: size each node so the others can carry its HA guests, or mark only what truly matters as HA.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test it with the power cord
&lt;/h3&gt;

&lt;p&gt;Before you trust HA with the firewall, test it with a disposable VM: add it to HA, then pull the power cord (not a clean shutdown) of the node running it. Watch &lt;code&gt;ha-manager status&lt;/code&gt; on another node until the VM shows as started on its new home. A clean shutdown tests something else, the shutdown policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Planned shutdowns: pick a shutdown policy
&lt;/h2&gt;

&lt;p&gt;A planned shutdown is not a failure, and HA handles it according to the cluster's shutdown policy, set in &lt;strong&gt;Datacenter → Options → HA Settings&lt;/strong&gt; or in &lt;code&gt;datacenter.cfg&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ha: shutdown_policy=migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;conditional&lt;/code&gt; (the default): on shutdown, stop the guests and let other nodes take them over; on reboot, freeze them and start them again on the same node.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;migrate&lt;/code&gt;: mark the node unavailable and live-migrate its HA guests away first; they return when it is back.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;failover&lt;/code&gt;: stop the guests and recover them elsewhere if the node is not back soon.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;freeze&lt;/code&gt;: stop and freeze the guests; nothing moves until the node returns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The docs note that &lt;code&gt;conditional&lt;/code&gt; is the default for backward compatibility and that some users find &lt;code&gt;migrate&lt;/code&gt; behaves more as expected. One catch with &lt;code&gt;migrate&lt;/code&gt;: a guest restricted by a strict node affinity rule to unavailable nodes makes the shutdown hang until you step in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintenance mode for one node
&lt;/h2&gt;

&lt;p&gt;For work on a single node, maintenance mode is the cleanest tool. It migrates the node's HA guests away and brings them back when you disable it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ha-manager crm-command node-maintenance &lt;span class="nb"&gt;enable &lt;/span&gt;pve2
ha-manager status          &lt;span class="c"&gt;# wait until no HA guests remain on pve2&lt;/span&gt;
&lt;span class="c"&gt;# ... patch, reboot, replace hardware ...&lt;/span&gt;
ha-manager crm-command node-maintenance disable pve2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can run from any node. The LRM shows maintenance as soon as the request is picked up, before every guest has moved, so watch the guest list rather than the status word. Maintenance mode survives a reboot and ends only when you disable it.&lt;/p&gt;

&lt;p&gt;For work that could briefly break quorum everywhere, such as re-cabling the corosync network, Proxmox VE 9.2 added a cluster-wide switch: &lt;code&gt;ha-manager crm-command disarm-ha freeze&lt;/code&gt;, then &lt;code&gt;ha-manager crm-command arm-ha&lt;/code&gt; when you are done. While HA is disarmed it protects nothing, so keep the window short.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Quorum survives one node down (three nodes, or two plus a QDevice).&lt;/li&gt;
&lt;li&gt;Every HA guest's disks are on shared, Ceph or replicated storage.&lt;/li&gt;
&lt;li&gt;Every rule in &lt;code&gt;ha-manager rules config&lt;/code&gt; is enabled (no conflicts).&lt;/li&gt;
&lt;li&gt;The shutdown policy is a deliberate choice, not the default by accident.&lt;/li&gt;
&lt;li&gt;A pulled-power failover test of a disposable VM succeeded.&lt;/li&gt;
&lt;li&gt;Each node can carry the HA guests of any one failed node.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is adapted from my book &lt;em&gt;Cluster Without Fear&lt;/em&gt; (Homelab the Right Way, Book 13). The free sample is &lt;a href="https://payhip.com/b/Fkx2c" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/cb4j1" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>proxmox</category>
      <category>homelab</category>
      <category>selfhosted</category>
      <category>linux</category>
    </item>
    <item>
      <title>Test your Compose ViewModels in milliseconds: fakes, runTest, the Main dispatcher and Turbine</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Sat, 31 Oct 2026 14:00:57 +0000</pubDate>
      <link>https://dev.to/charleshartmann/test-your-compose-viewmodels-in-milliseconds-fakes-runtest-the-main-dispatcher-and-turbine-2gg4</link>
      <guid>https://dev.to/charleshartmann/test-your-compose-viewmodels-in-milliseconds-fakes-runtest-the-main-dispatcher-and-turbine-2gg4</guid>
      <description>&lt;p&gt;The ViewModel is where a Compose screen's behavior lives: what the screen shows while loading, what happens when the user saves, which error appears when the network fails. It is plain Kotlin and runs on the JVM, so its tests take milliseconds and need no emulator. That makes it the best-value test in an Android app. This post tests a small note editor's ViewModel with a fake repository, coroutine test dispatchers, Turbine and &lt;code&gt;SavedStateHandle&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code under test
&lt;/h2&gt;

&lt;p&gt;The editor loads a note by id, lets the user change the title and body, and saves. Its state is a single &lt;code&gt;StateFlow&amp;lt;EditorUiState&amp;gt;&lt;/code&gt;, and the important parts look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EditorViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NotesRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"noteId"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EditorUiState&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EditorUiState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;note&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getNote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"draftTitle"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orEmpty&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orEmpty&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                    &lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;current&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Title is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;SaveResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ok&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;SaveResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// onTitleChange() also writes savedStateHandle["draftTitle"]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;NotesRepository&lt;/code&gt; is an interface. That one decision makes the ViewModel testable: a test can hand it anything that implements the interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fakes, not mocks, by default
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;mock&lt;/strong&gt; (MockK, for example) is configured per test: you tell it what to return and afterwards verify which calls were made. A &lt;strong&gt;fake&lt;/strong&gt; is a small, real implementation written by hand, usually backed by a map in memory. Google's testing guidance recommends fakes as the default test double, for practical reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A fake behaves like the real thing, so tests check behavior ("after saving, the note is in the repository") rather than implementation details ("&lt;code&gt;save&lt;/code&gt; was called once with these arguments").&lt;/li&gt;
&lt;li&gt;A fake is written once and reused by every test. Mocks are configured again in every test.&lt;/li&gt;
&lt;li&gt;Refactoring the ViewModel does not break tests that use a fake, as long as the behavior stays the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep mocks for the edges: a third-party class you cannot implement easily, or a check that something was &lt;em&gt;not&lt;/em&gt; called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FakeNotesRepository&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NotesRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;notes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableMapOf&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;nextId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1L&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;failNextSave&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;saveDelayMs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0L&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;vararg&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;it&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="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;nextId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;maxOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="mi"&gt;0L&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getNote&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="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notes&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="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;save&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="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;SaveResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;saveDelayMs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;// virtual time in tests&lt;/span&gt;
        &lt;span class="n"&gt;failNextSave&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;failNextSave&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SaveResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;note&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Note&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="n"&gt;nextId&lt;/span&gt;&lt;span class="p"&gt;++,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;note&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="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SaveResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;note&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: replace the Main dispatcher
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;viewModelScope&lt;/code&gt; launches on &lt;code&gt;Dispatchers.Main&lt;/code&gt;. On the JVM there is no Android main looper, so a test that creates this ViewModel fails with an error about the Main dispatcher missing. The fix is &lt;code&gt;Dispatchers.setMain&lt;/code&gt; and &lt;code&gt;Dispatchers.resetMain&lt;/code&gt; from kotlinx-coroutines-test, and the Android Developers coroutine testing guide recommends wrapping them in a JUnit rule so no test forgets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainDispatcherRule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;testDispatcher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TestDispatcher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UnconfinedTestDispatcher&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TestWatcher&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;starting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setMain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testDispatcher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;finished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetMain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every ViewModel test then starts with one line: &lt;code&gt;@get:Rule val mainDispatcherRule = MainDispatcherRule()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: understand runTest and the two dispatchers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;runTest&lt;/code&gt; runs the test body as a coroutine with &lt;strong&gt;virtual time&lt;/strong&gt;: a &lt;code&gt;delay(5_000)&lt;/code&gt; inside it completes instantly, while the test still sees five seconds pass. There are two test dispatchers, and the difference matters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;StandardTestDispatcher&lt;/code&gt; queues new coroutines. They do not run until the test yields, with &lt;code&gt;advanceUntilIdle()&lt;/code&gt;, &lt;code&gt;advanceTimeBy()&lt;/code&gt;, &lt;code&gt;runCurrent()&lt;/code&gt; or by suspending. It is the default in &lt;code&gt;runTest&lt;/code&gt;, and it lets you check intermediate states such as "saving is in progress".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UnconfinedTestDispatcher&lt;/code&gt; starts new coroutines eagerly until their first suspension. Simple tests read shorter, but you cannot observe the state between "launched" and "finished".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;code&gt;MainDispatcherRule&lt;/code&gt; installs a test dispatcher as Main, &lt;code&gt;runTest&lt;/code&gt; reuses its scheduler, so &lt;code&gt;advanceUntilIdle()&lt;/code&gt; in the test also drives coroutines launched in &lt;code&gt;viewModelScope&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: the first tests
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EditorViewModelTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Rule&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;mainDispatcherRule&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MainDispatcherRule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardTestDispatcher&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FakeNotesRepository&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EditorViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mapOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"noteId"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;noteId&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;saving_blank_note_shows_error_and_saves_nothing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;vm&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onTitleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"   "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEqualTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Title is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;saving_shows_progress_then_marks_saved&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;saveDelayMs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2_000&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;vm&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onTitleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Groceries"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;runCurrent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                       &lt;span class="c1"&gt;// the save starts, then suspends in delay()&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                 &lt;span class="c1"&gt;// two virtual seconds pass instantly&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The classic mistake is asserting before advancing. With &lt;code&gt;StandardTestDispatcher&lt;/code&gt;, the coroutine launched in &lt;code&gt;init&lt;/code&gt; has not run when the constructor returns, so a test that reads &lt;code&gt;state.value.title&lt;/code&gt; straight away sees the default empty title and fails, or worse, passes for the wrong reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: sequences of states with Turbine
&lt;/h2&gt;

&lt;p&gt;When the order of states is the behavior, Turbine collects the flow and lets you await each item:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;save_emits_saving_then_saved&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;saveDelayMs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt;         &lt;span class="c1"&gt;// a suspension between the two updates&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;vm&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onTitleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Packing list"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;awaitItem&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isFalse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;// current state&lt;/span&gt;
        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;awaitItem&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;isSaving&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;awaitItem&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isTrue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;cancelAndIgnoreRemainingEvents&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;StateFlow&lt;/code&gt; is &lt;strong&gt;conflated&lt;/strong&gt;: if two updates happen before the collector runs, it sees only the latest, and equal consecutive values are dropped. A test that expects a state that was immediately replaced waits and times out. That is why the fake has a delay: the suspension between "saving" and "saved" lets the collector see both. Assert on the states that matter, and make sure something suspends between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stateIn trap
&lt;/h2&gt;

&lt;p&gt;A flow exposed with &lt;code&gt;stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())&lt;/code&gt; runs its upstream only while someone collects it. Reading &lt;code&gt;.value&lt;/code&gt; in a test with nobody collecting returns the initial empty list forever. Collect it in &lt;code&gt;backgroundScope&lt;/code&gt;, which &lt;code&gt;runTest&lt;/code&gt; cancels for you at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;backgroundScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UnconfinedTestDispatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testScheduler&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Process death, at the ViewModel level
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;SavedStateHandle&lt;/code&gt; has a public constructor that takes a map, so you can simulate the system recreating the ViewModel: write a value through the first instance, build a second one with a handle holding the same values, and assert the state comes back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;restored&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EditorViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mapOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"noteId"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"draftTitle"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"draftTitle"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;restored&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEqualTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Half-typed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The ViewModel depends on interfaces, and each has a reusable fake.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;MainDispatcherRule&lt;/code&gt; replaces &lt;code&gt;Dispatchers.Main&lt;/code&gt; in every ViewModel test.&lt;/li&gt;
&lt;li&gt;Tests advance (&lt;code&gt;advanceUntilIdle()&lt;/code&gt;, &lt;code&gt;runCurrent()&lt;/code&gt;) before asserting.&lt;/li&gt;
&lt;li&gt;Turbine tests have a real suspension between the states they expect.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stateIn&lt;/code&gt; flows are collected in &lt;code&gt;backgroundScope&lt;/code&gt; before reading &lt;code&gt;.value&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Saved state is tested by rebuilding the ViewModel from a &lt;code&gt;SavedStateHandle&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is adapted from my book &lt;em&gt;Test It Before They Do&lt;/em&gt; (Android the Right Way, Book 8). The free sample is &lt;a href="https://payhip.com/b/I905N" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/Fpyu8" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>testing</category>
      <category>jetpackcompose</category>
    </item>
    <item>
      <title>Real HTTPS for your homelab Kubernetes apps without opening a port: k3s Traefik, cert-manager and a DNS-01 wildcard</title>
      <dc:creator>Charles Hartmann</dc:creator>
      <pubDate>Fri, 30 Oct 2026 14:05:06 +0000</pubDate>
      <link>https://dev.to/charleshartmann/real-https-for-your-homelab-kubernetes-apps-without-opening-a-port-k3s-traefik-cert-manager-and-a-2ek4</link>
      <guid>https://dev.to/charleshartmann/real-https-for-your-homelab-kubernetes-apps-without-opening-a-port-k3s-traefik-cert-manager-and-a-2ek4</guid>
      <description>&lt;p&gt;Every web app in a homelab cluster should answer at a proper hostname with a certificate your browser trusts, and you should not have to forward a single port from the internet to get there. On k3s you already have most of the pieces: the Traefik ingress controller ships with it. Add cert-manager, one DNS-01 issuer and one wildcard certificate, and every app you route gets real HTTPS automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap
&lt;/h2&gt;

&lt;p&gt;There are two traps, one on each side.&lt;/p&gt;

&lt;p&gt;The first is living with certificate warnings: self-signed certificates on every app, a browser you have trained to click "proceed anyway", and a password manager that refuses to autofill. That habit is exactly what an attacker on your network would rely on.&lt;/p&gt;

&lt;p&gt;The second is fixing it the obvious way: opening port 80 to the internet so Let's Encrypt can run an HTTP-01 challenge. That publishes your lab to every scanner on the internet for the sake of a padlock icon.&lt;/p&gt;

&lt;p&gt;The DNS-01 challenge avoids both. You prove you control the domain by creating a DNS record through your DNS provider's API. Nothing on your network has to be reachable from outside.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a request reaches a pod
&lt;/h2&gt;

&lt;p&gt;When something does not load, walk this path in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your browser asks LAN DNS for &lt;code&gt;paperless.home.example.com&lt;/code&gt; and gets the private address of Traefik's LoadBalancer Service (for example &lt;code&gt;192.168.40.200&lt;/code&gt;, handed out by MetalLB).&lt;/li&gt;
&lt;li&gt;The node that holds that address receives the connection and hands it to a Traefik pod.&lt;/li&gt;
&lt;li&gt;Traefik terminates TLS, matches the hostname against its routes, and forwards the request to the app's ClusterIP Service.&lt;/li&gt;
&lt;li&gt;The Service sends it to one of the app's ready pods.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: check the Traefik that k3s installed
&lt;/h2&gt;

&lt;p&gt;k3s installs Traefik from a bundled Helm chart into &lt;code&gt;kube-system&lt;/code&gt; and registers an IngressClass called &lt;code&gt;traefik&lt;/code&gt;. Because it is the only ingress controller in the cluster, it picks up standard Ingress objects without being told.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl get ingressclass
kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system get deploy,svc traefik
kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system get helmchart traefik &lt;span class="nt"&gt;-o&lt;/span&gt; yaml | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change Traefik's settings with a &lt;code&gt;HelmChartConfig&lt;/code&gt; named &lt;code&gt;traefik&lt;/code&gt;, never by editing the generated chart manifest. One gotcha when you copy older snippets: the chart version k3s ships moved the Service type from &lt;code&gt;service.type&lt;/code&gt; to &lt;code&gt;service.spec.type&lt;/code&gt;, and the k3s documentation warns that the old key is silently ignored.&lt;/p&gt;

&lt;p&gt;While you are there, redirect plain HTTP to HTTPS everywhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;helm.cattle.io/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HelmChartConfig&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;traefik&lt;/span&gt;
  &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;kube-system&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;valuesContent&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|-&lt;/span&gt;
    &lt;span class="s"&gt;ports:&lt;/span&gt;
      &lt;span class="s"&gt;web:&lt;/span&gt;
        &lt;span class="s"&gt;http:&lt;/span&gt;
          &lt;span class="s"&gt;redirections:&lt;/span&gt;
            &lt;span class="s"&gt;entryPoint:&lt;/span&gt;
              &lt;span class="s"&gt;to: websecure&lt;/span&gt;
              &lt;span class="s"&gt;scheme: https&lt;/span&gt;
              &lt;span class="s"&gt;permanent: true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; traefik-config.yaml
kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system get pods &lt;span class="nt"&gt;-l&lt;/span&gt; app.kubernetes.io/name&lt;span class="o"&gt;=&lt;/span&gt;traefik &lt;span class="nt"&gt;-w&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;k3s re-runs the chart with the merged values and Traefik restarts.&lt;/p&gt;

&lt;p&gt;A note if you are following an older tutorial that installs ingress-nginx first: in November 2025 Kubernetes SIG Network and the Security Response Committee announced the retirement of ingress-nginx, with best-effort maintenance until March 2026 and no releases or security fixes after that. k3s already gives you Traefik. Translate those guides' Ingress objects to the &lt;code&gt;traefik&lt;/code&gt; class, and review any &lt;code&gt;nginx.ingress.kubernetes.io/*&lt;/code&gt; annotations, because Traefik ignores them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: install cert-manager
&lt;/h2&gt;

&lt;p&gt;cert-manager watches &lt;code&gt;Certificate&lt;/code&gt; objects, gets certificates from an issuer such as Let's Encrypt, stores them in Secrets and renews them before they expire. The project publishes its Helm chart in an OCI registry (v1.21.3 at the time of writing; check for the current release):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;helm &lt;span class="nb"&gt;install &lt;/span&gt;cert-manager oci://quay.io/jetstack/charts/cert-manager &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--version&lt;/span&gt; v1.21.3 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--namespace&lt;/span&gt; cert-manager &lt;span class="nt"&gt;--create-namespace&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--set&lt;/span&gt; crds.enabled&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true
&lt;/span&gt;kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; cert-manager get pods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;crds.enabled=true&lt;/code&gt; lets the chart install and upgrade cert-manager's CustomResourceDefinitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: a DNS-01 issuer
&lt;/h2&gt;

&lt;p&gt;You need a domain you own, hosted at a DNS provider cert-manager can talk to. It supports several directly (Cloudflare, Route 53, Google Cloud DNS, Azure DNS, DigitalOcean and others) and many more through webhooks. With Cloudflare, follow cert-manager's documentation: create an API &lt;strong&gt;token&lt;/strong&gt;, not the global API key, with &lt;strong&gt;Zone - DNS - Edit&lt;/strong&gt; and &lt;strong&gt;Zone - Zone - Read&lt;/strong&gt;, limited to your zone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; cert-manager create secret generic cloudflare-api-token &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--from-literal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;api-token&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'PASTE-THE-TOKEN'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then a ClusterIssuer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cert-manager.io/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ClusterIssuer&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;letsencrypt-prod&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;acme&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://acme-v02.api.letsencrypt.org/directory&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;you@example.com&lt;/span&gt;
    &lt;span class="na"&gt;privateKeySecretRef&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;letsencrypt-prod-account&lt;/span&gt;
    &lt;span class="na"&gt;solvers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;dns01&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;cloudflare&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;apiTokenSecretRef&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
              &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cloudflare-api-token&lt;/span&gt;
              &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;api-token&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a second issuer named &lt;code&gt;letsencrypt-staging&lt;/code&gt; with the server &lt;code&gt;https://acme-staging-v02.api.letsencrypt.org/directory&lt;/code&gt;. Staging has much more generous rate limits, so get everything working there first. The token secret lives in the &lt;code&gt;cert-manager&lt;/code&gt; namespace because, for a ClusterIssuer, cert-manager looks for referenced secrets in its own namespace by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: one wildcard for every internal name
&lt;/h2&gt;

&lt;p&gt;You could request one certificate per app. A single wildcard for &lt;code&gt;*.home.example.com&lt;/code&gt; is simpler and has a privacy benefit: every certificate Let's Encrypt issues is published in public Certificate Transparency logs, and a wildcard publishes &lt;code&gt;*.home.example.com&lt;/code&gt; instead of a list of every app you run. Wildcards can only be issued through DNS-01, which is one more reason to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cert-manager.io/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Certificate&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;wildcard-home&lt;/span&gt;
  &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;kube-system&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;secretName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;wildcard-home-tls&lt;/span&gt;
  &lt;span class="na"&gt;issuerRef&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;letsencrypt-staging&lt;/span&gt;        &lt;span class="c1"&gt;# switch to letsencrypt-prod when it works&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ClusterIssuer&lt;/span&gt;
  &lt;span class="na"&gt;dnsNames&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;home.example.com"&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*.home.example.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; wildcard.yaml
kubectl &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system get certificate wildcard-home &lt;span class="nt"&gt;-w&lt;/span&gt;     &lt;span class="c"&gt;# READY becomes True&lt;/span&gt;
kubectl describe certificaterequest &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system          &lt;span class="c"&gt;# if it does not&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the staging certificate shows &lt;code&gt;Ready&lt;/code&gt;, change &lt;code&gt;issuerRef.name&lt;/code&gt; to &lt;code&gt;letsencrypt-prod&lt;/code&gt;, apply again, and delete the old secret so cert-manager issues a fresh one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: make it Traefik's default certificate
&lt;/h2&gt;

&lt;p&gt;The certificate lives in &lt;code&gt;kube-system&lt;/code&gt;, but an Ingress can only reference Secrets in its own namespace. Instead of copying the secret into every namespace, make it Traefik's default. Traefik serves the default certificate for any TLS connection that no more specific certificate matches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;traefik.io/v1alpha1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;TLSStore&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;default&lt;/span&gt;
  &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;kube-system&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;defaultCertificate&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;secretName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;wildcard-home-tls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now any Ingress under &lt;code&gt;home.example.com&lt;/code&gt; that asks for TLS gets the wildcard automatically, with no &lt;code&gt;secretName&lt;/code&gt; at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;networking.k8s.io/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Ingress&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;whoami&lt;/span&gt;
  &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;whoami&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;tls&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;whoami.home.example.com"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;whoami.home.example.com&lt;/span&gt;
      &lt;span class="na"&gt;http&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
            &lt;span class="na"&gt;pathType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Prefix&lt;/span&gt;
            &lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
              &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;whoami&lt;/span&gt;
                &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
                  &lt;span class="na"&gt;number&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain Ingress is a fine choice here. The k3s documentation states that the traditional Ingress API is still supported and not planned for deprecation, and most Helm charts still generate it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep internal names internal
&lt;/h2&gt;

&lt;p&gt;DNS-01 means the certificate is public but the service is not. Nothing in public DNS points at your lab; the only public record cert-manager creates is a temporary TXT record during issuance. &lt;code&gt;*.home.example.com&lt;/code&gt; resolves only through your LAN resolver's override to a private address, so from outside your network the names do not resolve and the address is unreachable.&lt;/p&gt;

&lt;p&gt;For access from outside the house, use a VPN instead of a port forward. Once your phone is connected, it uses the LAN resolver and every app works exactly as it does at home.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;kubectl get ingressclass&lt;/code&gt; shows &lt;code&gt;traefik&lt;/code&gt;, and Traefik settings live in one &lt;code&gt;HelmChartConfig&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Plain HTTP redirects to HTTPS.&lt;/li&gt;
&lt;li&gt;cert-manager is installed with &lt;code&gt;crds.enabled=true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The DNS provider credential is a scoped API token, stored in the &lt;code&gt;cert-manager&lt;/code&gt; namespace.&lt;/li&gt;
&lt;li&gt;Staging and production ClusterIssuers exist, and you proved the setup on staging first.&lt;/li&gt;
&lt;li&gt;One wildcard &lt;code&gt;Certificate&lt;/code&gt; is &lt;code&gt;Ready&lt;/code&gt; and set as Traefik's default in a &lt;code&gt;TLSStore&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No port is forwarded from the internet; remote access goes through a VPN.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is adapted from my book &lt;em&gt;Kubernetes Without the Overwhelm&lt;/em&gt; (Homelab the Right Way, Book 12). The free sample is &lt;a href="https://payhip.com/b/XGIMw" rel="noopener noreferrer"&gt;here&lt;/a&gt; and the full book is &lt;a href="https://payhip.com/b/uyY03" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want the week's homelab articles, videos and one tip from the books in your inbox every Sunday? &lt;a href="https://chartmann1590.github.io/homelab-the-right-way/newsletter/" rel="noopener noreferrer"&gt;Get the free weekly email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drafted with AI help and edited by the author; based on his book.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>homelab</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
  </channel>
</rss>
