<?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>Vps on Carlos Vaz</title>
    <link>https://carlosvaz.com/tags/vps/</link>
    <description>Recent content in Vps 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 Jan 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://carlosvaz.com/tags/vps/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Installing NixOS with root on tmpfs and encrypted ZFS on a netcup VPS</title>
      <link>https://carlosvaz.com/posts/installing-nixos-with-root-on-tmpfs-and-encrypted-zfs-on-a-netcup-vps/</link>
      <pubDate>Tue, 10 Jan 2023 00:00:00 +0000</pubDate><author>carlos@carjorvaz.com (Carlos Vaz)</author>
      <guid>https://carlosvaz.com/posts/installing-nixos-with-root-on-tmpfs-and-encrypted-zfs-on-a-netcup-vps/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve recently ordered a netcup VPS.&#xA;Before, I had a Kimsufi server and although it had 2TB of storage for roughly 7€ per month, I found it too expensive and too slow for my needs.&lt;/p&gt;&#xA;&lt;p&gt;As I&amp;rsquo;ve now started hosting services at home, my VPS needs are only hosting a personal mailserver, my personal websites, a headscale server and to serve as a bastion server for some services I host at home that I want to be accessible from the outside world.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I&rsquo;ve recently ordered a netcup VPS.
Before, I had a Kimsufi server and although it had 2TB of storage for roughly 7€ per month, I found it too expensive and too slow for my needs.</p>
<p>As I&rsquo;ve now started hosting services at home, my VPS needs are only hosting a personal mailserver, my personal websites, a headscale server and to serve as a bastion server for some services I host at home that I want to be accessible from the outside world.</p>
<p>This means that I don&rsquo;t need anything too powerful and can go with something cheaper.
My final options were Hetzner Cloud CPX11, Scaleway Stardust, BuyVM Slice 1024 and the netcup VPS 200 G10s.</p>
<p>I ended up choosing the netcup VPS as it seemed to be the best value for the money.</p>
<p>If I weren&rsquo;t hosting email, I&rsquo;d strongly consider simply using Cloudflare Tunnel ou Tailscale Funnel for exposing some services to the public Internet and skip the VPS altogether.</p>
<p>The following post describes how I set up NixOS on this VPS.</p>
<h2 id="enabling-uefi-boot">Enabling UEFI boot</h2>
<p>The first step was to enable UEFI booting on the server.
Quite simple, just go to the VPS dashboard website &gt; Settings &gt; UEFI Settings &gt; Activate UEFI Boot.</p>
<h2 id="booting-the-nixos-iso">Booting the NixOS ISO</h2>
<p>Thankfully, netcup allows its users to boot their servers with their ISOs of choice.
If all you want is a standard NixOS setup, <a href="https://github.com/elitak/nixos-infect">nixos-infect</a> may be enough for your needs.
However, I want to install ZFS with encryption which requires formatting the existing partitions.
By being able to boot from the NixOS ISO, we skip the headache of installing NixOS through other exotic methods, such as installing from a rescue image or from running a kexec on the original deployed Linux distro (and that is a matter for another post).</p>
<p>This meant I had to download the NixOS ISO and upload it to their FTP server.
The credentials for uploading custom images can be found in Media &gt; DVD Drive &gt; Login data to FTP.</p>
<p>However, after running the usual SFTP command, I got the following error:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">❯ sftp -P &lt;sftp-port&gt; &lt;username&gt;@&lt;sftp-hostname&gt;
Unable to negotiate with &lt;sftp-hostname&gt; port &lt;sftp-port&gt;: no matching host key type found. Their offer: ssh-rsa,ssh-dss</code></pre><p>So I explicitly allowed <code>ssh-rsa</code> and it worked as expected:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">sftp -oHostKeyAlgorithms=+ssh-rsa -P &lt;sftp-port&gt; &lt;username&gt;@&lt;sftp-hostname&gt;
sftp&gt; put Downloads/nixos-minimal-22.11.1347.0bf3109eeb6-x86_64-linux.iso cdrom/</code></pre><p>Now, to boot from the uploaded ISO, we&rsquo;ll go to Own DVDs and click on attach DVD:</p>
<figure><img src="/netcup_dvd_boot.png">
</figure>

<p>And reboot the server.</p>
<h2 id="installing-nixos">Installing NixOS</h2>
<h3 id="networking-the-live-iso">Networking the live ISO</h3>
<p>After booting to NixOS on the server, I found it had no network connectivity.
So I connected through VNC through the control panel and am now in the NixOS live ISO.</p>
<p>We&rsquo;ll start by manually setting up the network, according to the Network section of the control panel:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">ip addr add &lt;server-ip&gt;/&lt;prefix-length&gt; dev &lt;ethernet-network-interface&gt;
ip route add default via &lt;gateway-ip&gt; dev &lt;ethernet-network-interface&gt;
echo &#34;nameserver 1.1.1.1&#34; &gt; /etc/resolv.conf</code></pre><p>We can now change the password for the root and finish the installation through SSH or just continue using the VNC window.</p>
<h3 id="partitioning">Partitioning</h3>
<p>Now, we&rsquo;ll wipe the drive and create its partitions:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">wipefs -a /dev/sda

parted -a optimal /dev/sda
(parted) unit mib
(parted) mklabel gpt
(parted) mkpart ESP fat32 1 513
(parted) set 1 boot on
(parted) mkpart primary 513 100%
(parted) quit</code></pre><p>We&rsquo;ll now format the <code>/boot</code> partition:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">mkfs.fat -F 32 -n boot /dev/sda1</code></pre><p>Finally, we&rsquo;ll create the ZFS pool for the persistent datasets:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">ls /dev/disk/by-id/* # Find the correct disk.
ls /dev/disk/by-path/* # ... or if the VM is using virtio

zpool create \
    -o ashift=12 \
    -o autotrim=on \
    -O acltype=posixacl \
    -O atime=off \
    -O canmount=off \
    -O compression=zstd \
    -O dnodesize=auto \
    -O normalization=formD \
    -O xattr=sa \
    -O mountpoint=none \
    -O encryption=on \
    -O keylocation=prompt \
    -O keyformat=passphrase \
    rpool /dev/disk/by-id/&lt;disk&gt;

zfs create -p -o refreservation=1G -o mountpoint=none rpool/local/reserved
zfs create -p rpool/local/nix
zfs create -p rpool/safe/persist</code></pre><p>We&rsquo;ll now start to mount our partitions and datasets so we can finally install NixOS:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">mount -t tmpfs none /mnt
mkdir -p /mnt/{boot,nix,persist}

mount /dev/sda1 /mnt/boot
mount -t zfs -o zfsutil rpool/local/nix /mnt/nix
mount -t zfs -o zfsutil rpool/safe/persist /mnt/persist</code></pre><h2 id="configuring-nixos">Configuring NixOS</h2>
<p>Let&rsquo;s start by generating a configuration:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">nixos-generate-config --root /mnt</code></pre><p>And here follows some specific configuration, in addition to the one we&rsquo;re used to.</p>
<h3 id="enable-zfs-support">Enable ZFS support:</h3>
<p><code>/etc/nixos/configuration.nix</code></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>boot<span style="color:#f92672">.</span>kernelPackages <span style="color:#f92672">=</span> config<span style="color:#f92672">.</span>boot<span style="color:#f92672">.</span>zfs<span style="color:#f92672">.</span>package<span style="color:#f92672">.</span>latestCompatibleLinuxPackages;
</span></span><span style="display:flex;"><span>networking<span style="color:#f92672">.</span>hostId <span style="color:#f92672">=</span> <span style="color:#e6db74">&lt;host-id&gt;</span>; <span style="color:#75715e"># For example: head -c 8 /etc/machine-id</span></span></span></code></pre></div><h3 id="set-up-the-root-user">Set up the root user</h3>
<p><code>/etc/nixos/configuration.nix</code></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><span style="color:#75715e"># To generate a hash to put in initialHashedPassword</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># you can do this:</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># $ nix-shell --run &#39;mkpasswd -m SHA-512 -s&#39; -p mkpasswd</span>
</span></span><span style="display:flex;"><span>users<span style="color:#f92672">.</span>users<span style="color:#f92672">.</span>root<span style="color:#f92672">.</span>initialPassword <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;hunter2&#34;</span>;
</span></span><span style="display:flex;"><span>users<span style="color:#f92672">.</span>mutableUsers <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;</span></span></code></pre></div><p>You can also just skip this part and set up remote access by SSH. Be careful not to get locked out.</p>
<h3 id="partition-and-dataset-configuration">Partition and dataset configuration</h3>
<p><code>/etc/nixos/hardware-configuration.nix</code></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;none&#34;</span>;
</span></span><span style="display:flex;"><span>    fsType <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;tmpfs&#34;</span>;
</span></span><span style="display:flex;"><span>    options <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;defaults&#34;</span> <span style="color:#e6db74">&#34;size=2G&#34;</span> <span style="color:#e6db74">&#34;mode=755&#34;</span> ];
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>fileSystems<span style="color:#f92672">.</span><span style="color:#e6db74">&#34;/boot&#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;/dev/disk/by-uuid/&lt;boot_partition_uuid&gt;&#34;</span>;
</span></span><span style="display:flex;"><span>    fsType <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;vfat&#34;</span>;
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>fileSystems<span style="color:#f92672">.</span><span style="color:#e6db74">&#34;/nix&#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;rpool/local/nix&#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><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>fileSystems<span style="color:#f92672">.</span><span style="color:#e6db74">&#34;/persist&#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;rpool/safe/persist&#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>    neededForBoot <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>};</span></span></code></pre></div><h3 id="swap">Swap</h3>
<p>Enabling swap is usually a <a href="https://chrisdown.name/2018/01/02/in-defence-of-swap.html">good idea</a>.
But we&rsquo;d also like to avoid having swap on disk, as it&rsquo;s really slow.
So we&rsquo;ll follow <a href="https://old.reddit.com/r/Fedora/comments/r4a4so/interesting_fedora_does_not_support_hibernation/hmfc763/">Fedora&rsquo;s lead</a> and set up swap on ZRAM:
<code>/etc/nixos/configuration.nix</code></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>zramSwap<span style="color:#f92672">.</span>enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;</span></span></code></pre></div><h3 id="remote-access-through-ssh">Remote access through SSH</h3>
<p>To enable remote SSH access with public key authentication, we&rsquo;ll add the following:</p>
<p><code>/etc/nixos/configuration.nix</code></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>services<span style="color:#f92672">.</span>openssh <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>  openFirewall <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>  passwordAuthentication <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>  kbdInteractiveAuthentication <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>  hostKeys <span style="color:#f92672">=</span> [
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>      bits <span style="color:#f92672">=</span> <span style="color:#ae81ff">4096</span>;
</span></span><span style="display:flex;"><span>      path <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;/persist/etc/ssh/ssh_host_rsa_key&#34;</span>;
</span></span><span style="display:flex;"><span>      type <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;rsa&#34;</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>      path <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;/persist/etc/ssh/ssh_host_ed25519_key&#34;</span>;
</span></span><span style="display:flex;"><span>      type <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;ed25519&#34;</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  ];
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>users<span style="color:#f92672">.</span>users<span style="color:#f92672">.</span>root<span style="color:#f92672">.</span>openssh<span style="color:#f92672">.</span>authorizedKeys<span style="color:#f92672">.</span>keys <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;ssh_public_key&#34;</span> ];</span></span></code></pre></div><h3 id="static-ip-configuration">Static IP configuration</h3>
<p>Because netcup doesn&rsquo;t provide DHCP, we&rsquo;ll need to manually set up networking:</p>
<p><code>/etc/nixos/configuration.nix</code></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>networking <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  networkmanager<span style="color:#f92672">.</span>enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>  useDHCP <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  interfaces<span style="color:#f92672">.</span>ens3 <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>    useDHCP <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    ipv4<span style="color:#f92672">.</span>addresses <span style="color:#f92672">=</span> [{
</span></span><span style="display:flex;"><span>      address <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&lt;ipv4_address&gt;&#34;</span>;
</span></span><span style="display:flex;"><span>      prefixLength <span style="color:#f92672">=</span> <span style="color:#e6db74">&lt;ipv4_prefix_length&gt;</span>;
</span></span><span style="display:flex;"><span>    }];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    ipv6<span style="color:#f92672">.</span>addresses <span style="color:#f92672">=</span> [{
</span></span><span style="display:flex;"><span>      address <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&lt;ipv6_address&gt;&#34;</span>;
</span></span><span style="display:flex;"><span>      prefixLength <span style="color:#f92672">=</span> <span style="color:#e6db74">&lt;ipv6_prefix_length&gt;</span>;
</span></span><span style="display:flex;"><span>    }];
</span></span><span style="display:flex;"><span>  };
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  defaultGateway <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&lt;ipv4_gateway&gt;&#34;</span>;
</span></span><span style="display:flex;"><span>};</span></span></code></pre></div><h3 id="persistence">Persistence</h3>
<p>Because we have root on tmpfs, we&rsquo;ll need to persist specific files and folders we wish to keep. There are many ways to do this but I found using the <a href="https://github.com/nix-community/impermanence">impermanence</a> module quite straightforward:</p>
<p><code>/etc/nixos/configuration.nix</code></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>environment<span style="color:#f92672">.</span>persistence<span style="color:#f92672">.</span><span style="color:#e6db74">&#34;/persist&#34;</span> <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  hideMounts <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>  files <span style="color:#f92672">=</span> [
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;/etc/machine-id&#34;</span>
</span></span><span style="display:flex;"><span>  ];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  directories <span style="color:#f92672">=</span> [
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;/var/log&#34;</span>
</span></span><span style="display:flex;"><span>  ];
</span></span><span style="display:flex;"><span>};</span></span></code></pre></div><h3 id="remotely-decrypting-zfs">Remotely decrypting ZFS</h3>
<p>We&rsquo;ll now setup remote unlocking of the ZFS pool so we don&rsquo;t need to VNC into the machine every time we reboot.</p>
<p>For this, we&rsquo;ll need to enable networking in the initrd, which envolves enabling the correct kernel module.
Not only that, if your provider doesn&rsquo;t provide DHCP, you&rsquo;ll also need to manually set up the static IPv4 address.</p>
<p><code>/etc/nixos/configuration.nix</code></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> {
</span></span><span style="display:flex;"><span>  <span style="color:#75715e"># Set up static IPv4 address in the initrd.</span>
</span></span><span style="display:flex;"><span>  kernelParams <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;ip=&lt;ipv4_address&gt;::&lt;ipv4_gateway&gt;:&lt;ipv4_netmask&gt;::&lt;interface&gt;:none&#34;</span> ];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  initrd <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># Switch this to your ethernet&#39;s kernel module.</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># You can check what module you&#39;re currently using by running: lspci -v</span>
</span></span><span style="display:flex;"><span>    kernelModules <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;virtio_pci&#34;</span> ];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    network <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># This will use udhcp to get an ip address.</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># Make sure you have added the kernel module for your network driver to `boot.initrd.availableKernelModules`,</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># so your initrd can load it!</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># Static ip addresses might be configured using the ip argument in kernel command line:</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt</span>
</span></span><span style="display:flex;"><span>      enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>      ssh <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>        enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># To prevent ssh clients from freaking out because a different host key is used,</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># a different port for ssh is useful (assuming the same host has also a regular sshd running)</span>
</span></span><span style="display:flex;"><span>        port <span style="color:#f92672">=</span> <span style="color:#ae81ff">2222</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># hostKeys paths must be unquoted strings, otherwise you&#39;ll run into issues with boot.initrd.secrets</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># the keys are copied to initrd from the path specified; multiple keys can be set</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># you can generate any number of host keys using</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># `ssh-keygen -t ed25519 -N &#34;&#34; -f /path/to/ssh_host_ed25519_key`</span>
</span></span><span style="display:flex;"><span>        hostKeys <span style="color:#f92672">=</span> [ <span style="color:#e6db74">/path/to/ssh_host_ed25519_key_initrd</span> ];
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># public ssh key used for login</span>
</span></span><span style="display:flex;"><span>        authorizedKeys <span style="color:#f92672">=</span> [ <span style="color:#e6db74">&#34;&lt;ssh_public_key&gt;&#34;</span> ];
</span></span><span style="display:flex;"><span>      };
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># this will automatically load the zfs password prompt on login</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># and kill the other prompt so boot can continue</span>
</span></span><span style="display:flex;"><span>      postCommands <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        cat &lt;&lt;EOF &gt; /root/.profile
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        if pgrep -x &#34;zfs&#34; &gt; /dev/null
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        then
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">          zfs load-key -a
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">          killall zfs
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        else
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">          echo &#34;zfs not running -- maybe the pool is taking some time to load for some unforseen reason.&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        fi
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">        EOF
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      &#39;&#39;</span>;
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>  };
</span></span><span style="display:flex;"><span>};</span></span></code></pre></div><p>Then, after NixOS is installed, to decrypt the pool:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">ssh -4 -p 2222 root@&lt;ipv4_address&gt;</code></pre><h2 id="installing-nixos">Installing NixOS</h2>
<p>We&rsquo;re finally ready to install NixOS:</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">nixos-install --no-root-passwd --root /mnt
umount -Rl /mnt
zpool export -a</code></pre><p>And don&rsquo;t forget to detach the DVD before rebooting.</p>





<pre tabindex="0"><code class="language-nil" data-lang="nil">reboot</code></pre><h2 id="closing-remarks">Closing remarks</h2>
<p>Although this initial setup took some work, I&rsquo;m happy with the final result.</p>
<p>By <a href="https://grahamc.com/blog/erase-your-darlings">erasing our darlings</a>, we keep our system clean and fresh.
It&rsquo;s also reassuring to know that everything that&rsquo;s running is explicitly set in the NixOS configuration and that we can just move it to some other host.
This also makes backups much easier, as I know exactly what needs to be kept.</p>
<p>I intend to have all my future servers set up like this.
Hopefully, following this post will make it easier.</p>
<h2 id="references">References</h2>
<ul>
<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://elis.nu/blog/2020/05/nixos-tmpfs-as-root">https://elis.nu/blog/2020/05/nixos-tmpfs-as-root</a></li>
<li><a href="https://nixos.wiki/wiki/ZFS">https://nixos.wiki/wiki/ZFS</a></li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
