<?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: Xkoder</title>
    <description>The latest articles on DEV Community by Xkoder (@ikskoder).</description>
    <link>https://dev.to/ikskoder</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%2F4174620%2F9cfabee0-07f7-41d7-9142-d709ae2fef21.png</url>
      <title>DEV Community: Xkoder</title>
      <link>https://dev.to/ikskoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ikskoder"/>
    <language>en</language>
    <item>
      <title>Mistakes Will Be Made: series overview</title>
      <dc:creator>Xkoder</dc:creator>
      <pubDate>Thu, 26 Nov 2026 20:27:04 +0000</pubDate>
      <link>https://dev.to/ikskoder/mistakes-will-be-made-series-overview-26bk</link>
      <guid>https://dev.to/ikskoder/mistakes-will-be-made-series-overview-26bk</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;🎧 &lt;strong&gt;Prefer to listen?&lt;/strong&gt; The &lt;a href="https://ikskoder.dev/blog/mistakes-will-be-made-overview/" rel="noopener noreferrer"&gt;original post on my blog&lt;/a&gt; has an AI-narrated audio version (~13 min). You can set the playback speed to whatever suits you, and the text scrolls along with the narration, highlighting the sentence being read.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the overview of &lt;strong&gt;Mistakes Will Be Made: designing safer software for real people&lt;/strong&gt;, a series in 6 parts.&lt;/p&gt;

&lt;p&gt;We moved banking, healthcare and public services into apps, then left ordinary people to spot industrial-scale deception on their own. When they fail, we call it user error – largely because the people who built the trap aren’t the ones paying for it. Fixing that means designing for real, distracted humans: block the predictable slip where you can, limit what one mistake can reach, and save warnings for the few choices a person genuinely has to make.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who will find this post useful:&lt;/strong&gt; developers, designers, product managers and platform owners, and anyone told to “just be careful” online who suspected that wasn’t the whole answer. If you just have to live with those products, there’s something here for you too: why one slip isn’t a personal failing, and what to ask the companies behind your apps for.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Numbers marked ≈ are rounded to keep them easy to take in, never by more than 5%. The linked sources have the exact figures.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  TL;DR
  &lt;ul&gt;
&lt;li&gt;Much of what gets called user error is a design failure. Prevent the mistake where practical, contain the damage, then help with the decisions that remain.&lt;/li&gt;
&lt;li&gt;Going online is no longer optional, attackers route around strong logins through help desks and users, and annual training showed no measurable effect in a study of nearly 20,000 employees, though sustained programmes can help at work.&lt;/li&gt;
&lt;li&gt;Security steps cost time, attention and sometimes money, and the bill lands hardest on people with older devices, less confidence or nobody to ask.&lt;/li&gt;
&lt;li&gt;Interfaces set many of the traps: wrong defaults, confirmations that repeat what you typed instead of showing what will happen, and fakes that look exactly like the real thing.&lt;/li&gt;
&lt;li&gt;Protection can become the risk. Security software has sold its users’ data, crashed millions of machines and passed to new owners who control its updates, so it should ask for no more data or access than the job needs.&lt;/li&gt;
&lt;li&gt;Skipped protection comes back as bills and harm to people who never chose the product, breaches have bankrupted whole companies, and AI-written code doesn’t change who’s responsible.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;/p&gt;




&lt;p&gt;An electronic immobilizer stops a car starting without the right key. A &lt;a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC12559002/" rel="noopener noreferrer"&gt;2025 study&lt;/a&gt; found that US models fitted with one saw theft fall roughly 80% against comparable models without it. The improvement didn’t require the driver to become a detective.&lt;/p&gt;

&lt;p&gt;Much of what we call user error in software is a design failure that shows up later, when someone makes an ordinary mistake. People will click the wrong link, trust a familiar name and approve a payment while thinking about dinner. That’s predictable, so the series asks: &lt;strong&gt;why do we still build software where one ordinary mistake can break everything, and what can we do about it?&lt;/strong&gt; It looks at why being careful isn’t enough, what security advice costs and who pays for it, how screens and even protection software set traps of their own, who’s responsible when protection is skipped, and what to build instead.&lt;/p&gt;

&lt;p&gt;The short answer to the first half: the people who build software often aren’t the ones who pay for the mistake. A carmaker that skips a known protection faces recalls and lawsuits. Software makers mostly don’t, and EU liability rules &lt;a href="https://ikskoder.dev/blog/you-ship-it-you-own-it/#who-carries-the-cost" rel="noopener noreferrer"&gt;treat software as a product&lt;/a&gt; only from December 2026. A &lt;a href="https://www.acsac.org/2001/papers/110.pdf" rel="noopener noreferrer"&gt;2001 paper&lt;/a&gt; said it in one line: expect trouble wherever the party able to protect a system isn’t the party that suffers when it fails.&lt;/p&gt;

&lt;p&gt;As for what to do about it, I’d borrow the order &lt;a href="https://www.fda.gov/regulatory-information/search-fda-guidance-documents/applying-human-factors-and-usability-engineering-medical-devices" rel="noopener noreferrer"&gt;medical-device safety guidance&lt;/a&gt; already uses, with design first and warnings last: prevent the dangerous action where practical, contain the damage when it happens, and help people with the decisions that remain.&lt;/p&gt;

&lt;h2&gt;
  
  
  People are the way in
&lt;/h2&gt;

&lt;p&gt;Bank branches close. Government services move into apps. Paper forms disappear. Millions of people are expected to use systems they didn’t choose, and their details also sit in other people’s systems, beyond their control.&lt;/p&gt;

&lt;p&gt;Strong technical protection doesn’t close every route through a person. A help desk can be persuaded to reset access. A user can be talked into running a command or approving a payment. AI makes the lure cheaper and more convincing. And people fall fast: in phishing tests, the typical person who falls for it types in their data about 50 seconds after opening the email.&lt;/p&gt;

&lt;p&gt;Training doesn’t fill the gap on its own. A &lt;a href="https://today.ucsd.edu/story/cybersecurity-training-programs-dont-prevent-employees-from-falling-for-phishing-scams" rel="noopener noreferrer"&gt;2025 UC San Diego study&lt;/a&gt; of nearly 20,000 employees found no measurable benefit from annual training, and a small benefit from short lessons shown right after a test click. A typical fake campaign fooled about one employee in ten, yet by the eighth month more than half had clicked at least once, and an attacker needs only one. Well-run continuous programmes can help at work, but someone using a banking app at home can’t depend on an employer running one. Fear appeals and gotcha tests can push people away from security, and shame stops many victims reporting, so reporting a mistake has to stay easy and safe. Constant expert vigilance can’t be a condition of ordinary use. &lt;a href="https://ikskoder.dev/blog/gone-in-50-seconds/" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt; has the attacks and the studies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advice has a price
&lt;/h2&gt;

&lt;p&gt;Every instruction costs attention. An active user can have more than a hundred accounts and faces hundreds of tips nobody ranked for them, and stress, which scammers create on purpose, leaves even less attention to spend. A 2009 Microsoft Research paper argued that ignoring much of that advice is economically rational: the time it costs everyone can outweigh the direct losses it prevents. It’s a rough illustration, not a full account, but the point stands: advice has a price, and someone pays it.&lt;/p&gt;

&lt;p&gt;The same step can cost much more to someone with an old device, little technical confidence or nobody to ask. Hardware keys, phones that stop getting updates and protection sold only on pricier plans cost real money too. Growing up online doesn’t make anyone immune: younger adults report losing money to scams more often than older ones, though older victims lose more when they do.&lt;/p&gt;

&lt;p&gt;So universal advice has to justify its cost, and the better move is to make the secure path the short one. When Google enrolled accounts in two-step verification automatically, compromised accounts among those enrolled fell by half. &lt;a href="https://ikskoder.dev/blog/the-price-is-wrong/" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt; counts the bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  The screen sets the trap
&lt;/h2&gt;

&lt;p&gt;In 2020 Citibank sent ≈$900 million to Revlon’s lenders by mistake, past three reviewers and a warning, and nothing on the screen or in the warning said how much would actually leave the bank. A payment screen that repeats the name you typed gives your guess the look of a checked identity. The screen should show what the system has checked and what the action will do.&lt;/p&gt;

&lt;p&gt;Many defaults ship wrong. Smart home devices needed a UK law to stop shipping with the same factory password, and protection against scam calls can sit several menus deep or not exist in your country.&lt;/p&gt;

&lt;p&gt;Fakes are made to look real. In 2017 a domain written in Cyrillic letters displayed as apple.com. Attackers plant crypto addresses that match the first and last characters of one you’ve paid before, because wallets show only those. On Teams, anyone can call themselves Help Desk. In 2022 an eight-dollar blue check let an account posing as a real drugmaker announce free insulin, and the next day the company’s market value fell by about $15 billion.&lt;/p&gt;

&lt;p&gt;Every interruption spends attention, and people stop seeing warnings they meet too often. So a warning should appear where a specific harmful action happens, at the moment it happens, and teams should count its false alarms as well as the attacks it stops. Clearer fonts and long codes split into groups are meant to help someone who does check. They still need testing, and they assist a decision rather than prevent the attack. &lt;a href="https://ikskoder.dev/blog/seeing-is-deceiving/" rel="noopener noreferrer"&gt;Part 3&lt;/a&gt; separates what has evidence from what still needs testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The protector can become the risk
&lt;/h2&gt;

&lt;p&gt;Consumer security often asks for deep access to your device and your data, and that trust has been abused. Avast sold browsing data collected through its antivirus to more than 100 companies.&lt;/p&gt;

&lt;p&gt;And software with that much access fails big when it fails. A faulty CrowdStrike update crashed 8.5 million Windows machines and grounded airlines. Ransomware gangs, who lock up files and demand payment, use buggy but trusted drivers from security vendors, the parts of a security product that run deep in the system, to switch protection off. And an update carries every permission already granted, even after the product is sold, hijacked or swapped by force: after the US banned Kaspersky, about a million US customers were moved to a different antivirus, which on Windows replaced Kaspersky without asking them.&lt;/p&gt;

&lt;p&gt;Protection should ask for no more data, access or trust than the job needs. Free protection built into the system is often good enough. Chrome checks sites against a list of dangerous pages without Google learning who opened which page, and many useful checks run on data the device already holds. Default protection also has to work on older hardware, keep false alarms low and offer a clear way out when a block is wrong. &lt;a href="https://ikskoder.dev/blog/big-brother-is-protecting-you/" rel="noopener noreferrer"&gt;Part 4&lt;/a&gt; covers both sides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Skipped protection comes back as a bill
&lt;/h2&gt;

&lt;p&gt;When Hyundai and Kia left the protection from the opening out of many US models, videos showed how to start them with a USB cable. The theft wave that followed was linked to deaths and cost more than $200 million in settlements and fixes. In software the bill often lands on people who never chose the product: a ransomware attack on a pathology lab serving London hospitals postponed thousands of appointments and operations. Breaches have bankrupted whole companies, from a company browsers trusted to vouch for websites to a psychotherapy provider.&lt;/p&gt;

&lt;p&gt;Many customers stay after a breach because they have nowhere else to go. If your customers can’t leave for a competitor, that’s no excuse to skip protection. Security can also pay up front: Microsoft reports 98% of sign-ins with passkeys succeed, against 32% with passwords. And the law is moving the cost back to providers, with EU rules under which a vulnerability can make a product legally defective and UK banks now required to refund most victims who were talked into sending money.&lt;/p&gt;

&lt;p&gt;So security belongs on the release plan: measure defences after launch, and narrow or pause a release when a serious risk remains. AI changes the workflow, not who’s responsible. Generated code still introduces flaws at high rates, and the team that ships the app owns its access rules and the passwords and keys an AI agent can reach. &lt;a href="https://ikskoder.dev/blog/you-ship-it-you-own-it/" rel="noopener noreferrer"&gt;Part 5&lt;/a&gt; has the cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I’d do
&lt;/h2&gt;

&lt;p&gt;For the people who build a product, I’d start with the flow where a foreseeable mistake can hurt people most, and go in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prevent the dangerous action where practical.&lt;/strong&gt; Use sign-in that won’t work on a fake site, such as passkeys, protect the recovery route, keep basic protection free, and don’t let a display name alone establish authority.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contain the damage.&lt;/strong&gt; Limit permissions and sensitive data, delay risky or unusual actions, especially on a new device, keep backups out of a hacked account’s reach, and test restoring them. Singapore’s banks let customers lock savings away from online transfers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make necessary decisions honest and readable.&lt;/strong&gt; Check who really gets the money instead of repeating what was typed, and show addresses and codes so they’re easy to compare. Test warnings with the actual audience, including people using assistive technology and older devices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limit the protector too.&lt;/strong&gt; Minimise data and privileges, review behaviour changes before they ship, plan for failed updates, and give people a way back when a block is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the work.&lt;/strong&gt; Review AI-generated code explicitly, check fixes with suitable tools and expertise, and limit what an agent can reach in live systems, whatever the code review says.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Measure lockouts and false alarms after release, not just blocked attacks. New ideas, such as showing how often you’ve written to a sender before, belong in experiments before they become requirements. &lt;a href="https://ikskoder.dev/blog/back-to-the-drawing-board/" rel="noopener noreferrer"&gt;Part 6&lt;/a&gt; separates what product teams can ship now from ideas for operating systems, browsers, mail and phone networks to test, such as comparing a file or account number with a trusted copy in one click, or a warning when a pasted command came from a website.&lt;/p&gt;

&lt;p&gt;Users can help by asking for specific protections by name: passkeys, a clear recipient before you pay, a usable way back into your account. But if many of them have to ask, the product is already late, and that’s a problem to fix, not a complaint to manage. And when the app fails, people need a way to reach a human. Services people can’t do without should also keep a route that doesn’t depend on the app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A predictable human mistake shouldn’t be enough to break everything.&lt;/strong&gt; Design for tired, busy people, including the person forced online in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read the series
&lt;/h2&gt;

&lt;p&gt;Each part reads on its own; all six together take about 1.5 hours.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/gone-in-50-seconds/" rel="noopener noreferrer"&gt;Gone in 50 Seconds&lt;/a&gt;: human routes around technical defences, and the limits of training.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/the-price-is-wrong/" rel="noopener noreferrer"&gt;The Price Is Wrong&lt;/a&gt;: the unequal cost of advice, devices and protection.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/seeing-is-deceiving/" rel="noopener noreferrer"&gt;Seeing Is Deceiving&lt;/a&gt;: wrong defaults, misleading screens and fakes that look real.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/big-brother-is-protecting-you/" rel="noopener noreferrer"&gt;Big Brother Is Protecting You&lt;/a&gt;: limits on data, privilege and changing behaviour.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/you-ship-it-you-own-it/" rel="noopener noreferrer"&gt;You Ship It, You Own It&lt;/a&gt;: responsibility, business exposure and harm passed to others.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ikskoder.dev/blog/back-to-the-drawing-board/" rel="noopener noreferrer"&gt;Back to the Drawing Board&lt;/a&gt;: priorities for teams, experiments for platforms and proposals for standards.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>security</category>
      <category>ux</category>
      <category>privacy</category>
    </item>
    <item>
      <title>Pinning NixOS generations to the commits that built them</title>
      <dc:creator>Xkoder</dc:creator>
      <pubDate>Sun, 20 Sep 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/ikskoder/pinning-nixos-generations-to-the-commits-that-built-them-1cbf</link>
      <guid>https://dev.to/ikskoder/pinning-nixos-generations-to-the-commits-that-built-them-1cbf</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://ikskoder.dev/blog/nixos-generations-git-commits/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;, where you can also listen to a narrated version.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; You keep your NixOS configuration in a Git repository and build it as a flake, and you want a generation in the boot menu to name the commit that built it. On a channel-based machine that edits &lt;code&gt;/etc/nixos/configuration.nix&lt;/code&gt; in place there's no commit to pin yet, and version control comes first.&lt;/p&gt;




&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;system.configurationRevision&lt;/code&gt; to the flake's &lt;code&gt;self.rev&lt;/code&gt;, and every generation records the commit that built it.&lt;/li&gt;
&lt;li&gt;Put the short revision in &lt;code&gt;system.nixos.tags&lt;/code&gt;, so it shows up in the boot menu and in &lt;code&gt;nixos-rebuild list-generations&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Refuse to build from a dirty tree, in the rebuild wrapper and in the configuration itself: &lt;code&gt;self.dirtyRev&lt;/code&gt; records that the tree was dirty, not what was in it.&lt;/li&gt;
&lt;li&gt;Hold each generation's commit with a &lt;code&gt;refs/generations/&amp;lt;host&amp;gt;/&amp;lt;n&amp;gt;&lt;/code&gt; ref, so Git GC can't take the source of a system you can still boot. A short script keeps the refs in step with the generations.&lt;/li&gt;
&lt;li&gt;I wrote a hook to block history rewrites, then deleted it: its refusal lands after &lt;code&gt;reset --hard&lt;/code&gt; has already moved your files.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;One day you update your machine, reboot, and something is wrong. If you run NixOS, recovery can be painless: reboot again, pick the previous generation from the boot menu, and everything works.&lt;/p&gt;

&lt;p&gt;The harder part comes afterwards. The boot menu says "Generation 677". The Git history says "gnome: patch nautilus". Unless you recorded the connection, the working system doesn't tell you which commit to return to.&lt;/p&gt;

&lt;p&gt;You can guess from timestamps, until you rebuild twice in one hour, build without committing, or commit without rebuilding. And you'll be guessing on a machine you've just rolled back because something is broken.&lt;/p&gt;

&lt;p&gt;It can be worse. Several &lt;code&gt;nixos-rebuild switch&lt;/code&gt; runs may appear to work, only for the next reboot to fail, because a switch replaces services and userspace while the new kernel and initrd wait for a boot. If you committed only the final configuration, the earlier state you now need may never have existed as a commit. Nix still has a generation you can boot. Git may have no matching source.&lt;/p&gt;

&lt;p&gt;How much this matters depends on the size of your configuration. Twenty lines can be retyped from memory on a bad evening. Mine stopped being one file a long time ago, and the state that built a given generation isn't something I could reconstruct by remembering it.&lt;/p&gt;

&lt;p&gt;NixOS already provides the option to record the connection. Getting it right takes a trustworthy revision, a way to find it again, and a decision about what happens when Git history changes. That last part is where I spent most of my time, and it ended with me deleting the blocking hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recording the revision
&lt;/h2&gt;

&lt;p&gt;In a NixOS module where the flake's &lt;code&gt;self&lt;/code&gt; is in scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;configurationRevision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;code&gt;nixos-version --configuration-revision&lt;/code&gt; gives you the commit hash, and if the commit still exists you can check it out and inspect the source that built the system. This is the approach in Eelco Dolstra's &lt;a href="https://www.tweag.io/blog/2020-07-31-nixos-flakes/" rel="noopener noreferrer"&gt;post on managing NixOS with flakes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Keep &lt;code&gt;flake.lock&lt;/code&gt; committed too. The revision and the lock file identify the configuration and its inputs. They don't guarantee that a future rebuild succeeds or produces the same result: inputs have to remain available, and overrides or impure dependencies can change the build without changing the recorded revision.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the dirty fallback gives up
&lt;/h2&gt;

&lt;p&gt;For a Git flake, &lt;code&gt;self.rev&lt;/code&gt; is absent when the tree is dirty, so the assignment above fails. A common fallback is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;configurationRevision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;rev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;dirtyRev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows generations without an exact source commit. &lt;code&gt;self.dirtyRev&lt;/code&gt; is the base hash with a &lt;code&gt;-dirty&lt;/code&gt; suffix, so you can tell that the build was dirty, but not what the uncommitted changes were. Checking out the base commit won't recover them.&lt;/p&gt;

&lt;p&gt;That's a reasonable policy for experiments. For generations I expect to recover through Git, I want a committed source state. That includes files Git doesn't track yet: flakes normally omit them, so commit everything you intend to build.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refusing dirty builds
&lt;/h2&gt;

&lt;p&gt;The first check lives in a wrapper around the rebuild command. Mine are &lt;code&gt;nxrb&lt;/code&gt; and &lt;code&gt;nxrs&lt;/code&gt;, and the check gives an early, readable error:&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;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/your/nix/config &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="nv"&gt;worktree_status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;git status &lt;span class="nt"&gt;--porcelain&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;v1 &lt;span class="nt"&gt;--untracked-files&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;normal&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$worktree_status&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ERROR: /path/to/your/nix/config has uncommitted or untracked files."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
  git status &lt;span class="nt"&gt;--short&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi
&lt;/span&gt;&lt;span class="nb"&gt;exec &lt;/span&gt;nixos-rebuild switch &lt;span class="nt"&gt;--flake&lt;/span&gt; /path/to/your/nix/config &lt;span class="nt"&gt;--sudo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simpler &lt;code&gt;git diff --quiet HEAD&lt;/code&gt; isn't enough: it ignores untracked files and misses a staged change whose working file has been changed back. &lt;code&gt;git status&lt;/code&gt; covers both the index and the worktree, and the wrapper deliberately rejects untracked files too.&lt;/p&gt;

&lt;p&gt;The wrapper only covers commands routed through it. Anything that rebuilds another way, down to a forgotten alias older than the wrapper, bypasses it. So the configuration also requires a revision, with an explicit escape hatch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;configurationRevision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;rev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kr"&gt;builtins&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;getEnv&lt;/span&gt; &lt;span class="s2"&gt;"NIXOS_ALLOW_DIRTY"&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
      &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;dirtyRev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;"dirty"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="kr"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Refusing to build without a clean Git revision."&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;throw&lt;/code&gt; stops evaluation with a message when the value is required, which improves on the error a bare &lt;code&gt;self.rev&lt;/code&gt; gives. It's not a security boundary: nothing stops someone changing the configuration or deploying an already built system.&lt;/p&gt;

&lt;p&gt;Under pure evaluation &lt;code&gt;builtins.getEnv&lt;/code&gt; returns an empty string, so the escape hatch needs both the variable and &lt;code&gt;--impure&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;dirty tree, normal eval          error: Refusing to build without a clean Git revision.
NIXOS_ALLOW_DIRTY=1, still pure  error: Refusing to build without a clean Git revision.
NIXOS_ALLOW_DIRTY=1 --impure     abc1234...-dirty
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An intentional dirty build also has to bypass the wrapper's check. It stays marked dirty, and its uncommitted changes need a separate snapshot if they're to be recovered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting the revision in the boot menu
&lt;/h2&gt;

&lt;p&gt;Putting the short revision in &lt;code&gt;system.nixos.label&lt;/code&gt; replaces the default label:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;nixos&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;shortRev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That loses the nixpkgs version everywhere the label is shown, including the boot menu and the version column of &lt;code&gt;nixos-rebuild list-generations&lt;/code&gt;. &lt;code&gt;system.nixos.tags&lt;/code&gt; keeps it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;nixos&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;shortRev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="nv"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;dirtyShortRev&lt;/span&gt; &lt;span class="nv"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;"dirty"&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 default label joins the sorted tags and the version with hyphens, giving something like &lt;code&gt;abc1234-26.05.20260903.a5cc6f2&lt;/code&gt;. See the &lt;a href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/misc/label.nix" rel="noopener noreferrer"&gt;NixOS label module&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the commit behind a generation
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;nixos-rebuild list-generations&lt;/code&gt; has a &lt;strong&gt;Configuration Revision&lt;/strong&gt; column. With the Kernel, Specialisation and Current columns omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generation  Build-date           NixOS version                   Configuration Revision
678         2026-09-05 20:26:00  71130bc-26.05.20260903.a5cc6f2  71130bc3004f5276e54253726c91403034cd6408
677         2026-09-05 19:22:30  d43d987-26.05.20260903.a5cc6f2  d43d987dd94b4706f4de7fcd2fe025de9a468081
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a retained generation, use its own version command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/nix/var/nix/profiles/system-677-link/sw/bin/nixos-version &lt;span class="nt"&gt;--json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--configuration-revision&lt;/code&gt; instead of &lt;code&gt;--json&lt;/code&gt; gives just the hash. It's the same command &lt;code&gt;nixos-rebuild list-generations&lt;/code&gt; runs to fill its column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping the commit alive
&lt;/h2&gt;

&lt;p&gt;Recording the revision says nothing about whether the commit stays available. An amend, rebase or &lt;code&gt;reset --hard&lt;/code&gt; can move the branch away from it, and once no ref or reflog reaches the old object, Git GC can remove it. The generation then records a hash that no longer resolves.&lt;/p&gt;

&lt;p&gt;Give each retained generation a ref:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;refs/generations/laptop/677 -&amp;gt; d43d987dd94b4706f4de7fcd2fe025de9a468081
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For an existing generation, run this in the configuration repository, substituting the host and generation:&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="nv"&gt;rev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;/nix/var/nix/profiles/system-677-link/sw/bin/nixos-version &lt;span class="nt"&gt;--configuration-revision&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
  git update-ref refs/generations/laptop/677 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$rev&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A ref keeps everything it reaches safe from GC: the commit, its ancestors, their trees and blobs. A surviving generation therefore protects older commits even after their own pins are gone.&lt;/p&gt;

&lt;p&gt;Running that command by hand after every rebuild won't last. This script does it for every generation on disk:&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;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-euo&lt;/span&gt; pipefail
&lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/your/nix/config
&lt;span class="nv"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;hostname&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;shopt&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; nullglob
&lt;span class="nv"&gt;links&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;/nix/var/nix/profiles/system-&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="nt"&gt;-link&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;shopt&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; nullglob
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="k"&gt;${#&lt;/span&gt;&lt;span class="nv"&gt;links&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"gen-pin: no generations found, leaving pins alone"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# Every generation on disk goes in, even one whose revision can't be read:&lt;/span&gt;
&lt;span class="c"&gt;# unreadable isn't absent, and its pin stays.&lt;/span&gt;
&lt;span class="nb"&gt;declare&lt;/span&gt; &lt;span class="nt"&gt;-A&lt;/span&gt; &lt;span class="nv"&gt;live&lt;/span&gt;&lt;span class="o"&gt;=()&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="nb"&gt;link &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;links&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nv"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;link&lt;/span&gt;&lt;span class="p"&gt;##*/system-&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
  &lt;span class="nv"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;%-link&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
  live[&lt;span class="nv"&gt;$gen&lt;/span&gt;&lt;span class="o"&gt;]=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$link&lt;/span&gt;&lt;span class="s2"&gt;/sw/bin/nixos-version"&lt;/span&gt; &lt;span class="nt"&gt;--configuration-revision&lt;/span&gt; 2&amp;gt;/dev/null&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; live[&lt;span class="nv"&gt;$gen&lt;/span&gt;&lt;span class="o"&gt;]=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="k"&gt;done

for &lt;/span&gt;gen &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="p"&gt;!live[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nv"&gt;rev&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;live&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$gen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$rev&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;dirty&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="k"&gt;esac&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;git cat-file &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$rev&lt;/span&gt;&lt;span class="s2"&gt;^{commit}"&lt;/span&gt; 2&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;git update-ref &lt;span class="s2"&gt;"refs/generations/&lt;/span&gt;&lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="nv"&gt;$gen&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$rev&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"gen-pin: generation &lt;/span&gt;&lt;span class="nv"&gt;$gen&lt;/span&gt;&lt;span class="s2"&gt; was built from &lt;/span&gt;&lt;span class="nv"&gt;$rev&lt;/span&gt;&lt;span class="s2"&gt;, which isn't here"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
  &lt;span class="k"&gt;fi
done

&lt;/span&gt;git &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nt"&gt;-each-ref&lt;/span&gt; &lt;span class="nt"&gt;--format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'%(refname)'&lt;/span&gt; &lt;span class="s2"&gt;"refs/generations/&lt;/span&gt;&lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="s2"&gt;/"&lt;/span&gt; |
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; ref&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;live&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;##*/&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]+x&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; git update-ref &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ref&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the rebuild wrapper, replace the &lt;code&gt;exec&lt;/code&gt; line with &lt;code&gt;nixos-rebuild switch --flake /path/to/your/nix/config --sudo "$@" &amp;amp;&amp;amp; gen-pin&lt;/code&gt;, so a failed rebuild still exits with an error. Run it after each garbage collection too, as yourself, not from the collection service that runs as root. Cleanup follows one rule: &lt;strong&gt;unreadable isn't absent&lt;/strong&gt;. If it finds no generations the script deletes nothing, and a generation whose revision can't be read keeps its pin.&lt;/p&gt;

&lt;p&gt;Running after the build leaves a new commit unpinned until the script next runs, normally a few minutes, and the reflog covers that gap: by default Git keeps a commit dropped from a branch for 30 days. A scheduled collection that doesn't run the script leaves stale pins, which only hold a commit until the next rebuild releases it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trying to block rewrites with a hook
&lt;/h2&gt;

&lt;p&gt;Preserving the commit wasn't enough for me. I wanted Git to refuse a rewrite that would drop it from the branch, so a stray amend would bounce off with a clear message.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://git-scm.com/docs/githooks#_reference_transaction" rel="noopener noreferrer"&gt;&lt;code&gt;reference-transaction&lt;/code&gt; hook&lt;/a&gt; can veto ref transactions: on Git 2.54, a non-zero exit in &lt;code&gt;preparing&lt;/code&gt; or &lt;code&gt;prepared&lt;/code&gt; aborts the transaction. I built a blocker on it and ran into four problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A tracked hook can disappear during reset.&lt;/strong&gt; I'd used &lt;code&gt;core.hooksPath = .githooks&lt;/code&gt; to keep the hook under version control. Resetting to a commit older than that directory removes the hook before the ref update reaches it. Installing the hook outside the worktree, with the path override removed, avoids that at the price of an installation step a clone doesn't carry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The ref name changes between hook states.&lt;/strong&gt; In my tests an amend reached &lt;code&gt;preparing&lt;/code&gt; with the input naming &lt;code&gt;HEAD&lt;/code&gt;, and only the later stages named &lt;code&gt;refs/heads/main&lt;/code&gt;. Filtering on &lt;code&gt;refs/heads/*&lt;/code&gt; silently skips the early event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;pack-refs&lt;/code&gt; looks like deletion.&lt;/strong&gt; With the files backend, packing refs removes their loose copies, and in my tests those events had the same hook input as a real deletion. Rejecting every apparent deletion of a pin broke routine maintenance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;reset --hard&lt;/code&gt; changes files before the veto.&lt;/strong&gt; This ended the idea. By the time the hook rejected the branch update, the index and worktree had already moved to the reset target. The branch stayed put, the files didn't, and they sat staged against the unchanged branch, so a later commit would record the rollback. In testing, a subsequent amend picked up the rolled-back content, and it took the reflog to recover it. Git has no &lt;code&gt;pre-reset&lt;/code&gt; hook that would make the refusal atomic, and &lt;code&gt;GIT_REFLOG_ACTION&lt;/code&gt; isn't a reliable way to tell which command is running.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A partial repair exists: &lt;code&gt;git read-tree -u --reset &amp;lt;old-commit&amp;gt;&lt;/code&gt; moves the index and worktree back without touching refs. But it runs after the files have already moved, so it can't bring back overwritten uncommitted work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can't block properly? Then don't
&lt;/h2&gt;

&lt;p&gt;The property I needed was that a built commit stays recoverable. I tested the pins in a disposable repository by moving the branch away, expiring the reflogs and forcing collection:&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;# Destructive recovery test: disposable repository only.&lt;/span&gt;
git reflog expire &lt;span class="nt"&gt;--expire&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;now &lt;span class="nt"&gt;--expire-unreachable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;now &lt;span class="nt"&gt;--all&lt;/span&gt;
git gc &lt;span class="nt"&gt;--prune&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;now &lt;span class="nt"&gt;--aggressive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pinned commit survived, and recovering a branch from it doesn't touch the worktree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch recovered-677 refs/generations/laptop/677
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The blocker wanted more: that the commit stays in the branch's ancestry. The hook can't deliver that without losing the worktree. The other place for the refusal is a separate bare repository that rejects non-fast-forward pushes, which leaves your files alone but never sees a purely local reset. Neither closes the requirement. I'd be glad to be shown an approach that does.&lt;/p&gt;

&lt;p&gt;So I kept the pins and removed the blocker. Rewrites go through, and the pinned source remains available. I'm one person with one configuration repository, I amend rarely and have never rebased it, and the worst case that survives a rewrite is one command to recreate a branch. Rare isn't never, and a guard that worked would be worth building. This one wouldn't close the case, so I'd pay for the machinery and keep the exposure anyway.&lt;/p&gt;

&lt;p&gt;Before automating a guard, ask whether it actually closes the case. Writing it is the cheap part. What you pay afterwards is everything it gets in the way of, plus a mechanism you have to remember a year later, weighed against what it really closes. Often enough a cheap guarantee, recoverable rather than prevented, is where the work should end.&lt;/p&gt;

&lt;p&gt;Your arithmetic may come out differently. In a shared repository someone else force-pushes to, or under a rule that history is append-only, the accident gets likelier, and the bare repository closes that case at the remote in a way no local hook can. Then it's worth building properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can still be lost
&lt;/h2&gt;

&lt;p&gt;Pins protect objects from Git GC while the refs exist in an intact repository. They don't protect against deleting the refs, losing the repository or corrupting its storage, and an ordinary clone doesn't copy the &lt;code&gt;refs/generations/*&lt;/code&gt; namespace. Nor does releasing a pin collect anything by itself: the commit stays as long as any other ref or reflog still reaches it, and how long generations themselves are kept is your own cleanup policy.&lt;/p&gt;

&lt;p&gt;For independent recovery, save the pinned history in another repository or a &lt;a href="https://git-scm.com/docs/git-bundle" rel="noopener noreferrer"&gt;Git bundle&lt;/a&gt;. If only the ref disappeared, it can be recreated from the recorded hash. If the commit objects disappeared, you need that archive. A hash and a bootable system can't reconstruct a deleted Git commit.&lt;/p&gt;

</description>
      <category>nixos</category>
      <category>git</category>
      <category>nix</category>
    </item>
  </channel>
</rss>
