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 = rec {
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 = "unionfs-fuse";
54 device = "unionfs";
55 options = [ "allow_other" "cow" "nonempty" "chroot=/mnt-root" "max_files=32768" "hide_meta_files" "dirs=/nix/.rw-store=rw:/nix/.ro-store=ro" ];
56 };
57
58 boot.initrd.availableKernelModules = [ "squashfs" ];
59
60 boot.initrd.kernelModules = [ "loop" ];
61
62 # Closures to be copied to the Nix store, namely the init
63 # script and the top-level system configuration directory.
64 netboot.storeContents =
65 [ config.system.build.toplevel ];
66
67 # Create the squashfs image that contains the Nix store.
68 system.build.squashfsStore = import ../../../lib/make-squashfs.nix {
69 inherit (pkgs) stdenv squashfsTools closureInfo;
70 storeContents = config.netboot.storeContents;
71 };
72
73
74 # Create the initrd
75 system.build.netbootRamdisk = pkgs.makeInitrd {
76 inherit (config.boot.initrd) compressor;
77 prepend = [ "${config.system.build.initialRamdisk}/initrd" ];
78
79 contents =
80 [ { object = config.system.build.squashfsStore;
81 symlink = "/nix-store.squashfs";
82 }
83 ];
84 };
85
86 system.build.netbootIpxeScript = pkgs.writeTextDir "netboot.ipxe" ''
87 #!ipxe
88 kernel ${pkgs.stdenv.hostPlatform.platform.kernelTarget} init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}
89 initrd initrd
90 boot
91 '';
92
93 boot.loader.timeout = 10;
94
95 boot.postBootCommands =
96 ''
97 # After booting, register the contents of the Nix store
98 # in the Nix database in the tmpfs.
99 ${config.nix.package}/bin/nix-store --load-db < /nix/store/nix-path-registration
100
101 # nixos-rebuild also requires a "system" profile and an
102 # /etc/NIXOS tag.
103 touch /etc/NIXOS
104 ${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
105 '';
106
107 };
108
109}