Netboot

From NixOS Wiki
Revision as of 22:10, 7 December 2019 by imported>Erikarvstedt (Add section 'Building and serving a netboot image')

Building and serving a netboot image

Prerequisites

Enable Docker on the host system: virtualisation.docker.enable = true

Example

This example uses pixicore for hosting, which works in an ordinary network environment with an existing DHCP server.

#!/usr/bin/env bash -e

nix-build --out-link /tmp/netboot - <<'EOF'
let
  nixpkgs = <nixpkgs>;
  pkgs = import nixpkgs {};

  configuration = { config, pkgs, lib, ... }: with lib; {
    imports = [
        "${nixpkgs}/nixos/modules/installer/netboot/netboot-minimal.nix"
    ];
    ## Some useful options for setting up a new system
    services.mingetty.autologinUser = mkForce "root";
    # Enable sshd which gets disabled by netboot-minimal.nix
    systemd.services.sshd.wantedBy = mkOverride 0 [ "multi-user.target" ];
    # users.users.root.openssh.authorizedKeys.keys = [ ... ];
    # i18n.consoleKeyMap = "de";
  };

  nixos = import "${nixpkgs}/nixos" {
    inherit configuration;
    # system = ...;
  };
in
  pkgs.symlinkJoin {
    name = "netboot";
    paths = with nixos.config.system.build; [
      netbootRamdisk
      kernel
      netbootIpxeScript
    ];
    preferLocalBuild = true;
  }
EOF

n=$(realpath /tmp/netboot)
init=$(grep -ohP 'init=\S+' $n/netboot.ipxe)

# Start the PXE server.
# These ports need to be open in your firewall:
# UDP: 67, 69
# TCP: 64172
docker run --rm \
       -v /etc/ssl/certs:/etc/ssl/certs:ro \
       -v /nix/store:/nix/store:ro \
       --net=host \
       pixiecore/pixiecore:master \
       boot $n/bzImage $n/initrd \
       --cmdline "$init loglevel=4" \
       -d --dhcp-no-bind --port 64172 --status-port 64172

See the official NixOS Manual for more information about PXE booting.

Native netboot hosting with NixOS

Pixiecore is still in the process of being added to NixOS. Meanwhile, you can use the netboot_server module from clever.

netboot.xyz

There is now official netboot.xyz support. Just select NixOS from Linux installs and you should be ready to go.

Note: Your iPXE must be recent enough to support https:// links