Hi,

The only way I have managed to get root fs on NFS to work in NixOS is by using the "nfsmount" program from klibc. The following patch works fine for me, but since I'm new to NixOS I'm open for any changes:


Index: modules/system/boot/stage-1.nix
===================================================================
--- modules/system/boot/stage-1.nix     (revision 28845)
+++ modules/system/boot/stage-1.nix     (working copy)
@@ -169,6 +169,10 @@
cp ${kernelPackages.splashutils}/${kernelPackages.splashutils.helperName} $out/bin/splash_helper
       ''}

+      # Copy nfsmount
+      # TODO: Maybe only do this if there is a nfs mount point defined?
+      cp -v ${pkgs.klibc}/lib/klibc/bin.static/nfsmount $out/bin
+
       ${config.boot.initrd.extraUtilsCommands}

       # Run patchelf to make the programs refer to the copied libraries.


Index: modules/system/boot/stage-1-init.sh
===================================================================
--- modules/system/boot/stage-1-init.sh (revision 28845)
+++ modules/system/boot/stage-1-init.sh (working copy)
@@ -218,16 +218,20 @@

     mkdir -p "/mnt-root$mountPoint" || true

-    # For CIFS mounts, retry a few times before giving up.
-    local n=0
-    while true; do
- if mount -t "$fsType" -o "$options" "$device" /mnt-root$mountPoint; then
-            break
-        fi
-        if [ "$fsType" != cifs -o "$n" -ge 10 ]; then fail; break; fi
-        echo "retrying..."
-        n=$((n + 1))
-    done
+    if [ "$fsType" = nfs ]; then
+      nfsmount "$device" /mnt-root$mountPoint
+    else
+      # For CIFS mounts, retry a few times before giving up.
+      local n=0
+      while true; do
+ if mount -t "$fsType" -o "$options" "$device" /mnt-root$mountPoint; then
+              break
+          fi
+          if [ "$fsType" != cifs -o "$n" -ge 10 ]; then fail; break; fi
+          echo "retrying..."
+          n=$((n + 1))
+      done
+    fi
 }


By applying this patch, the following root device specification in configuration.nix works perfectly for me:

  fileSystems = [
    { mountPoint = "/";
      fsType = "nfs";
      device = "192.168.42.1:/export/nixos_root";
    }
  ];


However, you of course also need to get the network up before mounting the root fs. I have solved this in the following way directly in configuration.nix, but I would really appreciate any suggestions on how to solve it more generally in the NixOS repository, so I could write a proper patch for it:

  boot.initrd.extraUtilsCommands =
    ''
      cp ${pkgs.iproute}/sbin/ip $out/bin
      cp ${pkgs.glibc}/lib/libresolv.so.* $out/lib
    '';

  boot.initrd.postDeviceCommands =
    ''
      ip link set eth0 up
      ip addr add 192.168.42.200/24 dev eth0
    '';

  # This is necessary, otherwise upstart will tear
  # down the network connection and the root fs will stop working.
  networking.useDHCP = false;


Best regards,
  Rickard Nilsson
_______________________________________________
nix-dev mailing list
[email protected]
https://mail.cs.uu.nl/mailman/listinfo/nix-dev

Reply via email to