Skip to content

Instantly share code, notes, and snippets.

@vizanto
Created November 20, 2013 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vizanto/7568026 to your computer and use it in GitHub Desktop.
Save vizanto/7568026 to your computer and use it in GitHub Desktop.
NixOS boot with (shared) `/nix` mounted through NFS. Using this hack to have `/nix` stored on ZFS in the SmartOS global zone, until SmartOS gets `virtfs` support.
{ config, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
/etc/nixos/hardware-configuration.nix
];
# Use the GRUB 2 boot loader.
boot.loader.grub.enable = true;
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/vda";
boot.kernelParams = ["boot.shell_on_fail"];
boot.initrd.supportedFilesystems = ["nfs" "nfsv3"];
boot.initrd.availableKernelModules = ["af_packet" "nfsv3"];
# af_packet: Required for DHCP
boot.initrd.postDeviceCommands = ''
ifconfig eth0 up
SCRIPT=`dirname $(which sh)`/dhcp-client-script
cat > $SCRIPT <<EOF
#!`which sh`
RESOLV_CONF=/resolv.conf
[ -n "\$broadcast" ] && BROADCAST="broadcast \$broadcast"
[ -n "\$subnet" ] && NETMASK="netmask \$subnet"
case "\$1" in
renew|bound)
ifconfig \$interface \$ip \$BROADCAST \$NETMASK up
hostname \$hostname
if [ -n "\$router" ] ; then
while route del default gw 0.0.0.0 dev \$interface ; do true; done
for i in \$router ; do route add default gw \$i dev \$interface; done
fi
echo -n > \$RESOLV_CONF
[ -n "\$domain" ] && echo search \$domain >> \$RESOLV_CONF
for i in \$dns ; do
echo nameserver \$i >> \$RESOLV_CONF
done
;;
esac
EOF
chmod +x $SCRIPT
udhcpc -n -f -q -s $SCRIPT
'';
boot.initrd.postMountCommands = ''
mv /resolv.conf /mnt-root/etc/resolv.conf.stage1
mkdir -p /mnt-root/nix/var/nix/profiles
mkdir -p /mnt-root/nix/vm-specific/`hostname`/var/nix/profiles
mount -o bind /mnt-root/nix/vm-specific/`hostname`/var/nix/profiles /mnt-root/nix/var/nix/profiles
'';
networking.hostName = ""; # DHCP please.
# Don't let DHCPcd manage eth0 as it's already configured in Stage 1
networking.dhcpcd.denyInterfaces = ["eth0"];
networking.localCommands = ''
if [ -f /etc/resolv.conf.stage1 ]; then
echo "# from Stage 1:" >> /etc/resolv.conf
cat /etc/resolv.conf.stage1 >> /etc/resolv.conf
rm /etc/resolv.conf.stage1
fi
'';
}
{ config, pkgs, ... }:
{
imports =
[ <nixos/modules/profiles/qemu-guest.nix>
];
boot.initrd.availableKernelModules = [ ];
boot.kernelModules = [ ];
boot.extraModulePackages = [ ];
fileSystems."/" =
{ device = "/dev/vda1";
fsType = "ext4";
options = "rw,data=ordered,relatime";
};
fileSystems."/nix" =
{ device = "10.20.0.254:/nix";
fsType = "nfs";
options = "nolock,rw,vers=3,rsize=131072,wsize=131072,namlen=255,hard,noacl,proto=tcp,timeo=11,retrans=3,sec=sys,mountaddr=10.20.0.254,mountvers=3,mountproto=tcp,local_lock=all,addr=10.20.0.254,noatime,nodiratime";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment