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.storeContents = mkOption {
12 example = literalExample "[ pkgs.stdenv ]";
13 description = ''
14 This option lists additional derivations to be included in the
15 Nix store in the generated netboot image.
16 '';
17 };
18
19 };
20
21 config = {
22 # Don't build the GRUB menu builder script, since we don't need it
23 # here and it causes a cyclic dependency.
24 boot.loader.grub.enable = false;
25
26 # !!! Hack - attributes expected by other modules.
27 environment.systemPackages = [ pkgs.grub2_efi ]
28 ++ (if pkgs.stdenv.hostPlatform.system == "aarch64-linux"
29 then []
30 else [ pkgs.grub2 pkgs.syslinux ]);
31
32 fileSystems."/" =
33 { fsType = "tmpfs";
34 options = [ "mode=0755" ];
35 };
36
37 # In stage 1, mount a tmpfs on top of /nix/store (the squashfs
38 # image) to make this a live CD.
39 fileSystems."/nix/.ro-store" =
40 { fsType = "squashfs";
41 device = "../nix-store.squashfs";
42 options = [ "loop" ];
43 neededForBoot = true;
44 };
45
46 fileSystems."/nix/.rw-store" =
47 { fsType = "tmpfs";
48 options = [ "mode=0755" ];
49 neededForBoot = true;
50 };
51
52 fileSystems."/nix/store" =
53 { fsType = "overlay";
54 device = "overlay";
55 options = [
56 "lowerdir=/nix/.ro-store"
57 "upperdir=/nix/.rw-store/store"
58 "workdir=/nix/.rw-store/work"
59 ];
60 };
61
62 boot.initrd.availableKernelModules = [ "squashfs" "overlay" ];
63
64 boot.initrd.kernelModules = [ "loop" "overlay" ];
65
66 # Closures to be copied to the Nix store, namely the init
67 # script and the top-level system configuration directory.
68 netboot.storeContents =
69 [ config.system.build.toplevel ];
70
71 # Create the squashfs image that contains the Nix store.
72 system.build.squashfsStore = pkgs.callPackage ../../../lib/make-squashfs.nix {
73 storeContents = config.netboot.storeContents;
74 };
75
76
77 # Create the initrd
78 system.build.netbootRamdisk = pkgs.makeInitrd {
79 inherit (config.boot.initrd) compressor;
80 prepend = [ "${config.system.build.initialRamdisk}/initrd" ];
81
82 contents =
83 [ { object = config.system.build.squashfsStore;
84 symlink = "/nix-store.squashfs";
85 }
86 ];
87 };
88
89 system.build.netbootIpxeScript = pkgs.writeTextDir "netboot.ipxe" ''
90 #!ipxe
91 kernel ${pkgs.stdenv.hostPlatform.linux-kernel.target} init=${config.system.build.toplevel}/init initrd=initrd ${toString config.boot.kernelParams}
92 initrd initrd
93 boot
94 '';
95
96 boot.loader.timeout = 10;
97
98 boot.postBootCommands =
99 ''
100 # After booting, register the contents of the Nix store
101 # in the Nix database in the tmpfs.
102 ${config.nix.package}/bin/nix-store --load-db < /nix/store/nix-path-registration
103
104 # nixos-rebuild also requires a "system" profile and an
105 # /etc/NIXOS tag.
106 touch /etc/NIXOS
107 ${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
108 '';
109
110 };
111
112}