<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Uefi on Carlos Vaz</title>
    <link>https://carlosvaz.com/tags/uefi/</link>
    <description>Recent content in Uefi on Carlos Vaz</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-US</language>
    <managingEditor>carlos@carjorvaz.com (Carlos Vaz)</managingEditor>
    <webMaster>carlos@carjorvaz.com (Carlos Vaz)</webMaster>
    <lastBuildDate>Tue, 10 May 2022 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://carlosvaz.com/tags/uefi/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Installing NixOS on the Raspberry Pi 4 with UEFI and ZFS</title>
      <link>https://carlosvaz.com/posts/nixos-on-raspberry-pi-4-with-uefi-and-zfs/</link>
      <pubDate>Tue, 10 May 2022 00:00:00 +0000</pubDate><author>carlos@carjorvaz.com (Carlos Vaz)</author>
      <guid>https://carlosvaz.com/posts/nixos-on-raspberry-pi-4-with-uefi-and-zfs/</guid>
      <description>&lt;p&gt;After spending some time with &lt;a href=&#34;https://osmc.tv/&#34;&gt;OSMC&lt;/a&gt; on my Raspberry Pi 4, I&amp;rsquo;ve been meaning to install NixOS on it as it has been my distribution of choice on all my other systems.&lt;/p&gt;&#xA;&lt;p&gt;Installing &amp;ldquo;vanilla&amp;rdquo; NixOS on the Pi isn&amp;rsquo;t too difficult, just follow the &lt;a href=&#34;https://nixos.wiki/wiki/NixOS_on_ARM#SD_card_images_.28SBCs_and_similar_platforms.29&#34;&gt;installation steps on the wiki&lt;/a&gt;: download the SD card image, decompress it, flash the image to the SD card and you&amp;rsquo;re good to go.&lt;/p&gt;&#xA;&lt;p&gt;However, it gets more complicated when you try to go off the beaten path, such as when trying to do full-disk encryption or using filesystems other than ext4.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>After spending some time with <a href="https://osmc.tv/">OSMC</a> on my Raspberry Pi 4, I&rsquo;ve been meaning to install NixOS on it as it has been my distribution of choice on all my other systems.</p>
<p>Installing &ldquo;vanilla&rdquo; NixOS on the Pi isn&rsquo;t too difficult, just follow the <a href="https://nixos.wiki/wiki/NixOS_on_ARM#SD_card_images_.28SBCs_and_similar_platforms.29">installation steps on the wiki</a>: download the SD card image, decompress it, flash the image to the SD card and you&rsquo;re good to go.</p>
<p>However, it gets more complicated when you try to go off the beaten path, such as when trying to do full-disk encryption or using filesystems other than ext4.</p>
<p>After some reading and some trial and error, here are the steps I took to get NixOS with ZFS on the Pi, with UEFI booting.</p>
<h2 id="requirements">Requirements</h2>
<p>Make sure you&rsquo;ve enabled support for USB booting on your Raspberry Pi. If yours is considerably older, you might need to update its EEPROM before you can enable support for USB booting. For more details, check the <a href="https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#usb-mass-storage-boot">documentation</a>.</p>
<h2 id="installation-steps">Installation steps</h2>
<p>I started by booting the Pi off of a USB stick with the <a href="https://hydra.nixos.org/job/nixos/release-21.11/nixos.sd_image.aarch64-linux">generic SD card AArch64 image</a> but it would work just as well by initially booting off of an SD card.</p>
<p>As my installation target was a SATA SSD, I now connected it to one of the USB 3.0 ports on the Pi. I find using the Raspberry Pi with an SSD makes it feel more like a real computer, as the SSD is faster, has more storage capacity and will most certainly be more durable than a flimsy SD card, especially now that I&rsquo;ll also be using the Pi as a home server.</p>
<h3 id="partitioning">Partitioning</h3>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>wipefs -a /dev/sda
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>parted -a optimal /dev/sda -- mklabel gpt
</span></span><span style="display:flex;"><span>parted -a optimal /dev/sda -- mkpart ESP fat32 1MiB 513MiB
</span></span><span style="display:flex;"><span>parted -a optimal /dev/sda -- set <span style="color:#ae81ff">1</span> esp on
</span></span><span style="display:flex;"><span>parted -a optimal /dev/sda -- mkpart primary linux-swap 513MiB 8705MiB
</span></span><span style="display:flex;"><span>parted -a optimal /dev/sda -- mkpart primary 8705MiB 100%
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mkfs.fat -F <span style="color:#ae81ff">32</span> -n boot /dev/sda1
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mkswap -L swap /dev/sda2
</span></span><span style="display:flex;"><span>swapon /dev/sda2
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>zpool create -O mountpoint<span style="color:#f92672">=</span>none -O atime<span style="color:#f92672">=</span>off -o ashift<span style="color:#f92672">=</span><span style="color:#ae81ff">12</span> -O acltype<span style="color:#f92672">=</span>posixacl -O xattr<span style="color:#f92672">=</span>sa -O compression<span style="color:#f92672">=</span>zstd -O dnodesize<span style="color:#f92672">=</span>auto -O normalization<span style="color:#f92672">=</span>formD zroot /dev/sda3
</span></span><span style="display:flex;"><span>zfs create -o refreservation<span style="color:#f92672">=</span>1G -o mountpoint<span style="color:#f92672">=</span>none zroot/reserved
</span></span><span style="display:flex;"><span>zfs create zroot/root
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mount -t zfs -o zfsutil zroot/root /mnt
</span></span><span style="display:flex;"><span>mkdir -p /mnt/boot
</span></span><span style="display:flex;"><span>mount /dev/sda1 /mnt/boot</span></span></code></pre></div><h3 id="uefi-setup">UEFI setup</h3>
<p>We&rsquo;ll now setup UEFI on the Pi. We do this by placing the files from the <a href="https://github.com/pftf/RPi4">Raspberry Pi 4 UEFI Firmware Images</a> on the boot partition:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix-shell -p wget unzip
</span></span><span style="display:flex;"><span>cd /mnt/boot
</span></span><span style="display:flex;"><span>wget https://github.com/pftf/RPi4/releases/download/v1.33/RPi4_UEFI_Firmware_v1.33.zip
</span></span><span style="display:flex;"><span>unzip RPi4_UEFI_Firmware_v1.33.zip
</span></span><span style="display:flex;"><span>rm README.md
</span></span><span style="display:flex;"><span>rm RPi4_UEFI_Firmware_v1.33.zip</span></span></code></pre></div><p><em>2022/11/04 Update:</em> As a reader pointed out, you can also do this step by just copying the files on the installation media (assuming <code>/dev/sda</code> is your installation disk):</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>mkdir /firmware
</span></span><span style="display:flex;"><span>mount /dev/sda1 /firmware
</span></span><span style="display:flex;"><span>cp /firmware/* /mnt/boot</span></span></code></pre></div><h3 id="nixos-installation">NixOS installation</h3>
<p>As per usual, we let NixOS generate a config for us:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nixos-generate-config --root /mnt</span></span></code></pre></div><p>As we&rsquo;re using ZFS, we&rsquo;ll need to enable ZFS support:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>boot<span style="color:#f92672">.</span>supportedFilesystems <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;zfs&#34;</span> ];
</span></span><span style="display:flex;"><span>networking<span style="color:#f92672">.</span>hostId <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&lt;8 random numbers&gt;&#34;</span>;</span></span></code></pre></div><p>And also specify the <code>zfsutil</code> mount option for each of our datasets (in the <code>/mnt/etc/nixos/hardware-configuration.nix</code> file):</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>fileSystems<span style="color:#f92672">.</span><span style="color:#e6db74">&#34;/&#34;</span> <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  device <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;zroot/root&#34;</span>;
</span></span><span style="display:flex;"><span>  fsType <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;zfs&#34;</span>;
</span></span><span style="display:flex;"><span>  options <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;zfsutil&#34;</span> ];
</span></span><span style="display:flex;"><span>};</span></span></code></pre></div><p>Now, as we&rsquo;re booting with UEFI, we&rsquo;ll delete the following line which is generated by default:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>boot<span style="color:#f92672">.</span>loader<span style="color:#f92672">.</span>generic-extlinux-compatible<span style="color:#f92672">.</span>enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;</span></span></code></pre></div><p>&hellip;and we&rsquo;ll install a UEFI bootloader:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>boot<span style="color:#f92672">.</span>loader<span style="color:#f92672">.</span>efi<span style="color:#f92672">.</span>canTouchEfiVariables <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>boot<span style="color:#f92672">.</span>loader<span style="color:#f92672">.</span>systemd-boot<span style="color:#f92672">.</span>enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;</span></span></code></pre></div><p>Finally, we&rsquo;ll install NixOS:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>nixos-install</span></span></code></pre></div><h3 id="closing-remarks">Closing remarks</h3>
<p>To my surprise, I found that by using UEFI I could more easily achieve non-standard setups with NixOS on the Pi. I had some attempts at trying to install NixOS with ZFS with the usual booting setup on the Pi but I could never seem to boot it after the installation.</p>
<p>I should also note that setting up encryption and remote unlocking is not very difficult on this setup, as it only requires a little bit more of ZFS configuration.</p>
<p>After setting this up, a friend of mine pointed me to the following links:</p>
<ul>
<li><a href="https://discourse.nixos.org/t/planning-for-a-better-nixos-on-arm/15346">Planning for a better NixOS on ARM</a></li>
<li><a href="https://nixos.wiki/wiki/NixOS_on_ARM/UEFI">NixOS on ARM/UEFI</a></li>
</ul>
<p>It turns out that I accidentally ended up building an ARM system which is aligned with the NixOS ARM lead&rsquo;s view for NixOS ARM systems in the future, which I find amusing.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://mgdm.net/weblog/nixos-on-raspberry-pi-4">https://mgdm.net/weblog/nixos-on-raspberry-pi-4</a></li>
<li><a href="https://nixos.wiki/wiki/NixOS_on_ARM">https://nixos.wiki/wiki/NixOS_on_ARM</a></li>
<li><a href="https://nixos.wiki/wiki/NixOS_on_ARM/Raspberry_Pi_4">https://nixos.wiki/wiki/NixOS_on_ARM/Raspberry_Pi_4</a></li>
<li><a href="https://elis.nu/blog/2019/08/encrypted-zfs-mirror-with-mirrored-boot-on-nixos/">https://elis.nu/blog/2019/08/encrypted-zfs-mirror-with-mirrored-boot-on-nixos/</a></li>
<li><a href="https://nixos.wiki/wiki/ZFS">https://nixos.wiki/wiki/ZFS</a></li>
<li><a href="https://nixos.org/manual/nixos/stable/index.html#sec-installation">https://nixos.org/manual/nixos/stable/index.html#sec-installation</a></li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
