at 24.11-pre 5.2 kB view raw
1# This module creates netboot media containing the given NixOS 2# configuration. 3 4{ config, lib, pkgs, ... }: 5 6with lib; 7 8{ 9 options = { 10 11 netboot.squashfsCompression = mkOption { 12 default = with pkgs.stdenv.hostPlatform; "xz -Xdict-size 100% " 13 + lib.optionalString isx86 "-Xbcj x86" 14 # Untested but should also reduce size for these platforms 15 + lib.optionalString isAarch "-Xbcj arm" 16 + lib.optionalString (isPower && is32bit && isBigEndian) "-Xbcj powerpc" 17 + lib.optionalString (isSparc) "-Xbcj sparc"; 18 description = '' 19 Compression settings to use for the squashfs nix store. 20 ''; 21 example = "zstd -Xcompression-level 6"; 22 type = types.str; 23 }; 24 25 netboot.storeContents = mkOption { 26 example = literalExpression "[ pkgs.stdenv ]"; 27 description = '' 28 This option lists additional derivations to be included in the 29 Nix store in the generated netboot image. 30 ''; 31 }; 32 33 }; 34 35 config = { 36 # Don't build the GRUB menu builder script, since we don't need it 37 # here and it causes a cyclic dependency. 38 boot.loader.grub.enable = false; 39 40 # !!! Hack - attributes expected by other modules. 41 environment.systemPackages = [ pkgs.grub2_efi ] 42 ++ (lib.optionals (pkgs.stdenv.hostPlatform.system != "aarch64-linux") [pkgs.grub2 pkgs.syslinux]); 43 44 fileSystems."/" = mkImageMediaOverride 45 { fsType = "tmpfs"; 46 options = [ "mode=0755" ]; 47 }; 48 49 # In stage 1, mount a tmpfs on top of /nix/store (the squashfs 50 # image) to make this a live CD. 51 fileSystems."/nix/.ro-store" = mkImageMediaOverride 52 { fsType = "squashfs"; 53 device = "../nix-store.squashfs"; 54 options = [ "loop" ]; 55 neededForBoot = true; 56 }; 57 58 fileSystems."/nix/.rw-store" = mkImageMediaOverride 59 { fsType = "tmpfs"; 60 options = [ "mode=0755" ]; 61 neededForBoot = true; 62 }; 63 64 fileSystems."/nix/store" = mkImageMediaOverride 65 { overlay = { 66 lowerdir = [ "/nix/.ro-store" ]; 67 upperdir = "/nix/.rw-store/store"; 68 workdir = "/nix/.rw-store/work"; 69 }; 70 neededForBoot = true; 71 }; 72 73 boot.initrd.availableKernelModules = [ "squashfs" "overlay" ]; 74 75 boot.initrd.kernelModules = [ "loop" "overlay" ]; 76 77 # Closures to be copied to the Nix store, namely the init 78 # script and the top-level system configuration directory. 79 netboot.storeContents = 80 [ config.system.build.toplevel ]; 81 82 # Create the squashfs image that contains the Nix store. 83 system.build.squashfsStore = pkgs.callPackage ../../../lib/make-squashfs.nix { 84 storeContents = config.netboot.storeContents; 85 comp = config.netboot.squashfsCompression; 86 }; 87 88 89 # Create the initrd 90 system.build.netbootRamdisk = pkgs.makeInitrdNG { 91 inherit (config.boot.initrd) compressor; 92 prepend = [ "${config.system.build.initialRamdisk}/initrd" ]; 93 94 contents = 95 [ { object = config.system.build.squashfsStore; 96 symlink = "/nix-store.squashfs"; 97 } 98 ]; 99 }; 100 101 system.build.netbootIpxeScript = pkgs.writeTextDir "netboot.ipxe" '' 102 #!ipxe 103 # Use the cmdline variable to allow the user to specify custom kernel params 104 # when chainloading this script from other iPXE scripts like netboot.xyz 105 kernel ${pkgs.stdenv.hostPlatform.linux-kernel.target} init=${config.system.build.toplevel}/init initrd=initrd ${toString config.boot.kernelParams} ''${cmdline} 106 initrd initrd 107 boot 108 ''; 109 110 # A script invoking kexec on ./bzImage and ./initrd.gz. 111 # Usually used through system.build.kexecTree, but exposed here for composability. 112 system.build.kexecScript = pkgs.writeScript "kexec-boot" '' 113 #!/usr/bin/env bash 114 if ! kexec -v >/dev/null 2>&1; then 115 echo "kexec not found: please install kexec-tools" 2>&1 116 exit 1 117 fi 118 SCRIPT_DIR=$( cd -- "$( dirname -- "''${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 119 kexec --load ''${SCRIPT_DIR}/bzImage \ 120 --initrd=''${SCRIPT_DIR}/initrd.gz \ 121 --command-line "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}" 122 kexec -e 123 ''; 124 125 # A tree containing initrd.gz, bzImage and a kexec-boot script. 126 system.build.kexecTree = pkgs.linkFarm "kexec-tree" [ 127 { 128 name = "initrd.gz"; 129 path = "${config.system.build.netbootRamdisk}/initrd"; 130 } 131 { 132 name = "bzImage"; 133 path = "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}"; 134 } 135 { 136 name = "kexec-boot"; 137 path = config.system.build.kexecScript; 138 } 139 ]; 140 141 boot.loader.timeout = 10; 142 143 boot.postBootCommands = 144 '' 145 # After booting, register the contents of the Nix store 146 # in the Nix database in the tmpfs. 147 ${config.nix.package}/bin/nix-store --load-db < /nix/store/nix-path-registration 148 149 # nixos-rebuild also requires a "system" profile and an 150 # /etc/NIXOS tag. 151 touch /etc/NIXOS 152 ${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system 153 ''; 154 155 }; 156 157}