2009-06-05 14:35:27 +01:00
|
|
|
# This module creates a bootable ISO image containing the given NixOS
|
|
|
|
# configuration. The derivation for the ISO image will be placed in
|
|
|
|
# config.system.build.isoImage.
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-11-14 16:12:02 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-06-05 14:35:27 +01:00
|
|
|
|
|
|
|
let
|
2009-06-09 13:01:31 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# The Grub image.
|
|
|
|
grubImage = pkgs.runCommand "grub_eltorito" {}
|
|
|
|
''
|
|
|
|
${pkgs.grub2}/bin/grub-mkimage -O i386-pc -o tmp biosdisk iso9660 help linux linux16 chain png jpeg echo gfxmenu reboot
|
|
|
|
cat ${pkgs.grub2}/lib/grub/*/cdboot.img tmp > $out
|
|
|
|
''; # */
|
|
|
|
|
|
|
|
|
|
|
|
# The configuration file for Grub.
|
|
|
|
grubCfg =
|
|
|
|
''
|
|
|
|
set default=${builtins.toString config.boot.loader.grub.default}
|
|
|
|
set timeout=${builtins.toString config.boot.loader.grub.timeout}
|
|
|
|
|
|
|
|
if loadfont /boot/grub/unicode.pf2; then
|
|
|
|
set gfxmode=640x480
|
|
|
|
insmod gfxterm
|
|
|
|
insmod vbe
|
|
|
|
terminal_output gfxterm
|
|
|
|
|
|
|
|
insmod png
|
|
|
|
if background_image /boot/grub/splash.png; then
|
|
|
|
set color_normal=white/black
|
|
|
|
set color_highlight=black/white
|
|
|
|
else
|
|
|
|
set menu_color_normal=cyan/blue
|
|
|
|
set menu_color_highlight=white/blue
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
${config.boot.loader.grub.extraEntries}
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
|
|
# The efi boot image
|
2013-11-14 23:18:27 +00:00
|
|
|
efiDir = pkgs.runCommand "efi-directory" {} ''
|
|
|
|
mkdir -p $out/efi/boot
|
|
|
|
cp -v ${pkgs.gummiboot}/lib/gummiboot/gummiboot${targetArch}.efi $out/efi/boot/boot${targetArch}.efi
|
|
|
|
mkdir -p $out/loader/entries
|
|
|
|
echo "title NixOS LiveCD" > $out/loader/entries/nixos-livecd.conf
|
|
|
|
echo "linux /boot/bzImage" >> $out/loader/entries/nixos-livecd.conf
|
|
|
|
echo "initrd /boot/initrd" >> $out/loader/entries/nixos-livecd.conf
|
|
|
|
echo "options init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}" >> $out/loader/entries/nixos-livecd.conf
|
|
|
|
echo "default nixos-livecd" > $out/loader/loader.conf
|
|
|
|
echo "timeout 5" >> $out/loader/loader.conf
|
|
|
|
'';
|
|
|
|
|
2013-10-09 16:44:50 +01:00
|
|
|
efiImg = pkgs.runCommand "efi-image_eltorito" { buildInputs = [ pkgs.mtools ]; }
|
2013-09-04 12:05:09 +01:00
|
|
|
''
|
|
|
|
#Let's hope 10M is enough
|
|
|
|
dd bs=2048 count=5120 if=/dev/zero of="$out"
|
|
|
|
${pkgs.dosfstools}/sbin/mkfs.vfat "$out"
|
2013-11-14 23:18:27 +00:00
|
|
|
mcopy -svi "$out" ${efiDir}/* ::
|
|
|
|
mmd -i "$out" boot
|
2013-10-09 16:44:50 +01:00
|
|
|
mcopy -v -i "$out" \
|
2013-11-14 23:18:27 +00:00
|
|
|
${config.boot.kernelPackages.kernel}/bzImage ::boot/bzImage
|
2013-10-09 16:44:50 +01:00
|
|
|
mcopy -v -i "$out" \
|
2013-11-14 23:18:27 +00:00
|
|
|
${config.system.build.initialRamdisk}/initrd ::boot/initrd
|
2013-09-04 12:05:09 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
targetArch = if pkgs.stdenv.isi686 then
|
|
|
|
"ia32"
|
|
|
|
else if pkgs.stdenv.isx86_64 then
|
|
|
|
"x64"
|
|
|
|
else
|
|
|
|
throw "Unsupported architecture";
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2009-06-09 13:01:31 +01:00
|
|
|
options = {
|
|
|
|
|
2009-11-14 16:12:02 +00:00
|
|
|
isoImage.isoName = mkOption {
|
2011-04-20 11:48:52 +01:00
|
|
|
default = "${config.isoImage.isoName}.iso";
|
2009-06-09 16:23:03 +01:00
|
|
|
description = ''
|
|
|
|
Name of the generated ISO image file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-04-20 11:48:52 +01:00
|
|
|
isoImage.isoBaseName = mkOption {
|
|
|
|
default = "nixos";
|
|
|
|
description = ''
|
|
|
|
Prefix of the name of the generated ISO image file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-14 16:12:02 +00:00
|
|
|
isoImage.compressImage = mkOption {
|
2009-06-09 16:23:03 +01:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether the ISO image should be compressed using
|
|
|
|
<command>bzip2</command>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-14 16:12:02 +00:00
|
|
|
isoImage.volumeID = mkOption {
|
2009-06-09 16:23:03 +01:00
|
|
|
default = "NIXOS_BOOT_CD";
|
|
|
|
description = ''
|
|
|
|
Specifies the label or volume ID of the generated ISO image.
|
|
|
|
Note that the label is used by stage 1 of the boot process to
|
|
|
|
mount the CD, so it should be reasonably distinctive.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-14 16:12:02 +00:00
|
|
|
isoImage.contents = mkOption {
|
2009-06-09 13:01:31 +01:00
|
|
|
example =
|
|
|
|
[ { source = pkgs.memtest86 + "/memtest.bin";
|
|
|
|
target = "boot/memtest.bin";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
description = ''
|
2009-06-09 14:27:50 +01:00
|
|
|
This option lists files to be copied to fixed locations in the
|
|
|
|
generated ISO image.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-14 16:12:02 +00:00
|
|
|
isoImage.storeContents = mkOption {
|
2009-06-10 17:29:48 +01:00
|
|
|
example = [pkgs.stdenv];
|
2009-06-09 14:27:50 +01:00
|
|
|
description = ''
|
|
|
|
This option lists additional derivations to be included in the
|
|
|
|
Nix store in the generated ISO image.
|
2009-06-09 13:01:31 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2010-08-29 13:11:31 +01:00
|
|
|
isoImage.includeSystemBuildDependencies = mkOption {
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = ''
|
|
|
|
Set this option to include all the needed sources etc in the
|
|
|
|
image. It significantly increases image size. Use that when
|
|
|
|
you want to be able to keep all the sources needed to build your
|
|
|
|
system or when you are going to install the system on a computer
|
|
|
|
with slow on non-existent network connection.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-03-16 05:37:24 +00:00
|
|
|
isoImage.makeEfiBootable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2012-03-16 11:31:33 +00:00
|
|
|
Whether the ISO image should be an efi-bootable volume.
|
2012-03-16 05:37:24 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-09 13:01:31 +01:00
|
|
|
};
|
|
|
|
|
2009-06-05 14:35:27 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
config = {
|
2009-11-10 21:42:38 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
boot.loader.grub.version = 2;
|
2009-11-10 21:42:38 +00:00
|
|
|
|
2013-09-04 12:05:09 +01: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;
|
2009-11-13 16:45:41 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# !!! Hack - attributes expected by other modules.
|
|
|
|
system.boot.loader.kernelFile = "bzImage";
|
|
|
|
environment.systemPackages = [ pkgs.grub2 ];
|
2009-11-13 16:45:41 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# In stage 1 of the boot, mount the CD as the root FS by label so
|
|
|
|
# that we don't need to know its device. We pass the label of the
|
|
|
|
# root filesystem on the kernel command line, rather than in
|
|
|
|
# `fileSystems' below. This allows CD-to-USB converters such as
|
|
|
|
# UNetbootin to rewrite the kernel command line to pass the label or
|
|
|
|
# UUID of the USB stick. It would be nicer to write
|
|
|
|
# `root=/dev/disk/by-label/...' here, but UNetbootin doesn't
|
|
|
|
# recognise that.
|
|
|
|
boot.kernelParams = [ "root=LABEL=${config.isoImage.volumeID}" ];
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# Note that /dev/root is a symlink to the actual root device
|
|
|
|
# specified on the kernel command line, created in the stage 1 init
|
|
|
|
# script.
|
|
|
|
fileSystems."/".device = "/dev/root";
|
2009-11-13 16:45:41 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
fileSystems."/nix/store" =
|
|
|
|
{ fsType = "squashfs";
|
|
|
|
device = "/nix-store.squashfs";
|
|
|
|
options = "loop";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
boot.initrd.availableKernelModules = [ "squashfs" "iso9660" ];
|
2012-03-16 05:37:24 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
boot.initrd.kernelModules = [ "loop" ];
|
2012-03-16 05:37:24 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# In stage 1, mount a tmpfs on top of / (the ISO image) and
|
|
|
|
# /nix/store (the squashfs image) to make this a live CD.
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
''
|
|
|
|
mkdir -p /unionfs-chroot/ro-root
|
|
|
|
mount --rbind $targetRoot /unionfs-chroot/ro-root
|
2009-06-05 14:35:27 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
mkdir /unionfs-chroot/rw-root
|
|
|
|
mount -t tmpfs -o "mode=755" none /unionfs-chroot/rw-root
|
|
|
|
mkdir /mnt-root-union
|
|
|
|
unionfs -o allow_other,cow,chroot=/unionfs-chroot,max_files=32768 /rw-root=RW:/ro-root=RO /mnt-root-union
|
|
|
|
oldTargetRoot=$targetRoot
|
|
|
|
targetRoot=/mnt-root-union
|
2009-06-05 14:35:27 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
mkdir /unionfs-chroot/rw-store
|
|
|
|
mount -t tmpfs -o "mode=755" none /unionfs-chroot/rw-store
|
|
|
|
mkdir -p $oldTargetRoot/nix/store
|
|
|
|
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-store=RW:/ro-root/nix/store=RO /mnt-root-union/nix/store
|
|
|
|
'';
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# Closures to be copied to the Nix store on the CD, namely the init
|
|
|
|
# script and the top-level system configuration directory.
|
|
|
|
isoImage.storeContents =
|
|
|
|
[ config.system.build.toplevel ] ++
|
|
|
|
optional config.isoImage.includeSystemBuildDependencies
|
|
|
|
config.system.build.toplevel.drvPath;
|
|
|
|
|
|
|
|
# Create the squashfs image that contains the Nix store.
|
|
|
|
system.build.squashfsStore = import ../../../lib/make-squashfs.nix {
|
|
|
|
inherit (pkgs) stdenv squashfsTools perl pathsFromGraph;
|
|
|
|
storeContents = config.isoImage.storeContents;
|
|
|
|
};
|
2010-01-09 15:13:06 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# Individual files to be included on the CD, outside of the Nix
|
|
|
|
# store on the CD.
|
|
|
|
isoImage.contents =
|
|
|
|
[ { source = grubImage;
|
|
|
|
target = "/boot/grub/grub_eltorito";
|
|
|
|
}
|
2013-10-02 11:29:07 +01:00
|
|
|
{ source = pkgs.substituteAll {
|
|
|
|
name = "grub.cfg";
|
|
|
|
src = pkgs.writeText "grub.cfg-in" grubCfg;
|
|
|
|
bootRoot = "/boot";
|
|
|
|
};
|
2013-09-04 12:05:09 +01:00
|
|
|
target = "/boot/grub/grub.cfg";
|
|
|
|
}
|
|
|
|
{ source = config.boot.kernelPackages.kernel + "/bzImage";
|
|
|
|
target = "/boot/bzImage";
|
|
|
|
}
|
|
|
|
{ source = config.system.build.initialRamdisk + "/initrd";
|
|
|
|
target = "/boot/initrd";
|
|
|
|
}
|
|
|
|
{ source = "${pkgs.grub2}/share/grub/unicode.pf2";
|
|
|
|
target = "/boot/grub/unicode.pf2";
|
|
|
|
}
|
|
|
|
{ source = config.boot.loader.grub.splashImage;
|
|
|
|
target = "/boot/grub/splash.png";
|
|
|
|
}
|
|
|
|
{ source = config.system.build.squashfsStore;
|
|
|
|
target = "/nix-store.squashfs";
|
|
|
|
}
|
|
|
|
{ # Quick hack: need a mount point for the store.
|
2013-10-02 11:29:07 +01:00
|
|
|
source = pkgs.runCommand "empty" {} "mkdir -p $out";
|
2013-09-04 12:05:09 +01:00
|
|
|
target = "/nix/store";
|
|
|
|
}
|
2013-10-02 11:29:07 +01:00
|
|
|
] ++ optionals config.isoImage.makeEfiBootable [
|
2013-09-04 12:05:09 +01:00
|
|
|
{ source = efiImg;
|
|
|
|
target = "/boot/efi.img";
|
|
|
|
}
|
2013-11-14 23:18:27 +00:00
|
|
|
{ source = "${efiDir}/efi";
|
|
|
|
target = "/efi";
|
|
|
|
}
|
|
|
|
{ source = "${efiDir}/loader";
|
|
|
|
target = "/loader";
|
|
|
|
}
|
2013-10-02 11:29:07 +01:00
|
|
|
] ++ mapAttrsToList (n: v: { source = v; target = "/boot/${n}"; }) config.boot.loader.grub.extraFiles;
|
2013-09-04 12:05:09 +01:00
|
|
|
|
|
|
|
# The Grub menu.
|
|
|
|
boot.loader.grub.extraEntries =
|
|
|
|
''
|
2013-10-02 11:29:07 +01:00
|
|
|
menuentry "NixOS ${config.system.nixosVersion} Installer" {
|
2013-09-04 12:05:09 +01:00
|
|
|
linux /boot/bzImage init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}
|
|
|
|
initrd /boot/initrd
|
|
|
|
}
|
|
|
|
|
|
|
|
menuentry "Boot from hard disk" {
|
|
|
|
set root=(hd0)
|
|
|
|
chainloader +1
|
|
|
|
}
|
|
|
|
'';
|
2012-03-18 18:55:47 +00:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
boot.loader.grub.timeout = 10;
|
2009-06-05 14:35:27 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# Create the ISO image.
|
|
|
|
system.build.isoImage = import ../../../lib/make-iso9660-image.nix ({
|
|
|
|
inherit (pkgs) stdenv perl cdrkit pathsFromGraph;
|
2009-06-10 17:29:48 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
inherit (config.isoImage) isoName compressImage volumeID contents;
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
bootable = true;
|
|
|
|
bootImage = "/boot/grub/grub_eltorito";
|
2013-10-02 11:29:07 +01:00
|
|
|
} // optionalAttrs config.isoImage.makeEfiBootable {
|
2013-09-04 12:05:09 +01:00
|
|
|
efiBootable = true;
|
|
|
|
efiBootImage = "boot/efi.img";
|
|
|
|
});
|
2009-06-09 13:01:31 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
boot.postBootCommands =
|
|
|
|
''
|
|
|
|
# After booting, register the contents of the Nix store on the
|
|
|
|
# CD in the Nix database in the tmpfs.
|
2013-10-28 15:28:04 +00:00
|
|
|
${config.nix.package}/bin/nix-store --load-db < /nix/store/nix-path-registration
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# nixos-rebuild also requires a "system" profile and an
|
|
|
|
# /etc/NIXOS tag.
|
|
|
|
touch /etc/NIXOS
|
2013-10-28 15:28:04 +00:00
|
|
|
${config.nix.package}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
2013-09-04 12:05:09 +01:00
|
|
|
'';
|
2009-06-05 14:35:27 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
# Add vfat support to the initrd to enable people to copy the
|
|
|
|
# contents of the CD to a bootable USB stick. Need unionfs-fuse for union mounts
|
|
|
|
boot.initrd.supportedFilesystems = [ "vfat" "unionfs-fuse" ];
|
2009-06-05 17:02:58 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
};
|
2012-03-09 16:17:37 +00:00
|
|
|
|
2009-06-05 14:35:27 +01:00
|
|
|
}
|