2016-04-08 13:53:27 +00:00
|
|
|
# This module creates netboot media containing the given NixOS
|
|
|
|
# configuration.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
netboot.storeContents = mkOption {
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression "[ pkgs.stdenv ]";
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-04-08 13:53:27 +00:00
|
|
|
This option lists additional derivations to be included in the
|
|
|
|
Nix store in the generated netboot image.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
config = {
|
2016-04-08 13:53:27 +00:00
|
|
|
# Don't build the GRUB menu builder script, since we don't need it
|
|
|
|
# here and it causes a cyclic dependency.
|
|
|
|
boot.loader.grub.enable = false;
|
|
|
|
|
|
|
|
# !!! Hack - attributes expected by other modules.
|
2017-04-14 21:59:54 +00:00
|
|
|
environment.systemPackages = [ pkgs.grub2_efi ]
|
2018-08-20 19:11:29 +00:00
|
|
|
++ (if pkgs.stdenv.hostPlatform.system == "aarch64-linux"
|
2017-04-14 21:59:54 +00:00
|
|
|
then []
|
|
|
|
else [ pkgs.grub2 pkgs.syslinux ]);
|
2016-04-08 13:53:27 +00:00
|
|
|
|
2021-08-03 22:12:03 +00:00
|
|
|
fileSystems."/" = mkImageMediaOverride
|
2021-08-03 19:36:07 +00:00
|
|
|
{ fsType = "tmpfs";
|
2016-04-08 13:53:27 +00:00
|
|
|
options = [ "mode=0755" ];
|
|
|
|
};
|
|
|
|
|
2016-11-29 14:31:07 +00:00
|
|
|
# In stage 1, mount a tmpfs on top of /nix/store (the squashfs
|
|
|
|
# image) to make this a live CD.
|
2021-08-03 22:12:03 +00:00
|
|
|
fileSystems."/nix/.ro-store" = mkImageMediaOverride
|
2016-11-29 14:31:07 +00:00
|
|
|
{ fsType = "squashfs";
|
|
|
|
device = "../nix-store.squashfs";
|
|
|
|
options = [ "loop" ];
|
|
|
|
neededForBoot = true;
|
|
|
|
};
|
|
|
|
|
2021-08-03 22:12:03 +00:00
|
|
|
fileSystems."/nix/.rw-store" = mkImageMediaOverride
|
2016-11-29 14:31:07 +00:00
|
|
|
{ fsType = "tmpfs";
|
|
|
|
options = [ "mode=0755" ];
|
|
|
|
neededForBoot = true;
|
|
|
|
};
|
|
|
|
|
2021-08-03 22:12:03 +00:00
|
|
|
fileSystems."/nix/store" = mkImageMediaOverride
|
2020-02-05 18:27:16 +00:00
|
|
|
{ fsType = "overlay";
|
|
|
|
device = "overlay";
|
|
|
|
options = [
|
|
|
|
"lowerdir=/nix/.ro-store"
|
|
|
|
"upperdir=/nix/.rw-store/store"
|
|
|
|
"workdir=/nix/.rw-store/work"
|
|
|
|
];
|
2021-06-08 16:52:02 +00:00
|
|
|
|
|
|
|
depends = [
|
|
|
|
"/nix/.ro-store"
|
|
|
|
"/nix/.rw-store/store"
|
|
|
|
"/nix/.rw-store/work"
|
|
|
|
];
|
2016-11-29 14:31:07 +00:00
|
|
|
};
|
|
|
|
|
2020-02-05 18:27:16 +00:00
|
|
|
boot.initrd.availableKernelModules = [ "squashfs" "overlay" ];
|
2016-04-08 13:53:27 +00:00
|
|
|
|
2020-02-05 18:27:16 +00:00
|
|
|
boot.initrd.kernelModules = [ "loop" "overlay" ];
|
2016-04-08 13:53:27 +00:00
|
|
|
|
|
|
|
# Closures to be copied to the Nix store, namely the init
|
|
|
|
# script and the top-level system configuration directory.
|
2016-11-29 14:31:07 +00:00
|
|
|
netboot.storeContents =
|
2016-04-08 13:53:27 +00:00
|
|
|
[ config.system.build.toplevel ];
|
|
|
|
|
|
|
|
# Create the squashfs image that contains the Nix store.
|
2019-04-18 01:11:04 +00:00
|
|
|
system.build.squashfsStore = pkgs.callPackage ../../../lib/make-squashfs.nix {
|
2016-04-08 13:53:27 +00:00
|
|
|
storeContents = config.netboot.storeContents;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
# Create the initrd
|
2022-06-07 12:05:59 +00:00
|
|
|
system.build.netbootRamdisk = pkgs.makeInitrdNG {
|
2016-04-08 13:53:27 +00:00
|
|
|
inherit (config.boot.initrd) compressor;
|
|
|
|
prepend = [ "${config.system.build.initialRamdisk}/initrd" ];
|
|
|
|
|
|
|
|
contents =
|
|
|
|
[ { object = config.system.build.squashfsStore;
|
|
|
|
symlink = "/nix-store.squashfs";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2017-04-14 21:59:54 +00:00
|
|
|
system.build.netbootIpxeScript = pkgs.writeTextDir "netboot.ipxe" ''
|
|
|
|
#!ipxe
|
2021-12-12 02:34:55 +00:00
|
|
|
# Use the cmdline variable to allow the user to specify custom kernel params
|
|
|
|
# when chainloading this script from other iPXE scripts like netboot.xyz
|
|
|
|
kernel ${pkgs.stdenv.hostPlatform.linux-kernel.target} init=${config.system.build.toplevel}/init initrd=initrd ${toString config.boot.kernelParams} ''${cmdline}
|
2017-04-14 21:59:54 +00:00
|
|
|
initrd initrd
|
|
|
|
boot
|
|
|
|
'';
|
2016-04-08 13:53:27 +00:00
|
|
|
|
2022-06-09 17:59:03 +00:00
|
|
|
# A script invoking kexec on ./bzImage and ./initrd.gz.
|
|
|
|
# Usually used through system.build.kexecTree, but exposed here for composability.
|
|
|
|
system.build.kexecScript = pkgs.writeScript "kexec-boot" ''
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
if ! kexec -v >/dev/null 2>&1; then
|
|
|
|
echo "kexec not found: please install kexec-tools" 2>&1
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "''${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
kexec --load ''${SCRIPT_DIR}/bzImage \
|
|
|
|
--initrd=''${SCRIPT_DIR}/initrd.gz \
|
|
|
|
--command-line "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}"
|
|
|
|
kexec -e
|
|
|
|
'';
|
|
|
|
|
|
|
|
# A tree containing initrd.gz, bzImage and a kexec-boot script.
|
|
|
|
system.build.kexecTree = pkgs.linkFarm "kexec-tree" [
|
|
|
|
{
|
|
|
|
name = "initrd.gz";
|
|
|
|
path = "${config.system.build.netbootRamdisk}/initrd";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "bzImage";
|
|
|
|
path = "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "kexec-boot";
|
|
|
|
path = config.system.build.kexecScript;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2016-04-08 13:53:27 +00:00
|
|
|
boot.loader.timeout = 10;
|
|
|
|
|
|
|
|
boot.postBootCommands =
|
|
|
|
''
|
|
|
|
# After booting, register the contents of the Nix store
|
|
|
|
# in the Nix database in the tmpfs.
|
|
|
|
${config.nix.package}/bin/nix-store --load-db < /nix/store/nix-path-registration
|
|
|
|
|
|
|
|
# nixos-rebuild also requires a "system" profile and an
|
|
|
|
# /etc/NIXOS tag.
|
|
|
|
touch /etc/NIXOS
|
|
|
|
${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|