Skip to content

Instantly share code, notes, and snippets.

@andir
Last active May 2, 2020 14:19
Show Gist options
  • Save andir/adc04436acab619ce236e3b311dd62ec to your computer and use it in GitHub Desktop.
Save andir/adc04436acab619ce236e3b311dd62ec to your computer and use it in GitHub Desktop.
debian stretch
{ pkgs ? import <nixpkgs> {}}:
let
lib = pkgs.lib;
makeImageFromDebDist =
{ name, fullName, size ? 4096, urlPrefix
, packagesList ? "", packagesLists ? [packagesList]
, packages, extraPackages ? [], postInstall ? ""
, extraDebs ? []
, QEMU_OPTS ? "", memSize ? 512
, createRootFS }:
let
expr = pkgs.vmTools.debClosureGenerator {
inherit name packagesLists urlPrefix;
packages = packages ++ extraPackages;
};
in
(pkgs.vmTools.fillDiskWithDebs {
inherit name fullName size postInstall QEMU_OPTS memSize createRootFS;
debs = import expr {inherit (pkgs) fetchurl;} ++ extraDebs;
}) // {inherit expr;};
partitionDisk = disk: ''
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | ${pkgs.utillinux}/bin/fdisk ${disk}
o # clear the in memory partition table
n # new partition
p # primary partition
1 # partition number 1
# default - start at beginning of disk
# default, extend partition to end of disk
a # make a partition bootable
1 # bootable partition is partition 1 -- /dev/sda1
p # print the in-memory partition table
w # write the partition table
q # and we're done
EOF
${pkgs.e2fsprogs}/bin/mkfs.ext4 ${disk}1
mkdir /mnt
${pkgs.utillinux}/bin/mount -t ext4 ${disk}1 /mnt
touch /mnt/.debug
mkdir /mnt/proc /mnt/dev /mnt/sys
'';
withGrub = expr: args: let
baudRate = builtins.toString 115200;
grub-script = pkgs.writeScript "install-grub" ''
#!/bin/sh
set -ex
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
grub-install /dev/vda
echo "UUID=$1 / ext4 defaults 0 0" > /etc/fstab
sed -e 's/quiet//g' -i /etc/default/grub
cat - <<EOF >> /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="console=ttyS0,${baudRate}"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=${baudRate} --unit=0 --word=8 --parity=no --stop=1"
EOF
update-grub2
sed -e "s|root=/dev/vda1|root=UUID=$1|g" -i /boot/grub/grub.cfg
'';
g = {
postInstall = ''
${lib.optionalString (args?postInstall) args.postInstall}
P=$(${pkgs.utillinux}/bin/blkid --output export /dev/vda1)
echo -n $P
eval $(echo $P)
cp ${grub-script} /mnt/install-grub
chroot /mnt /install-grub $UUID
rm /mnt/install-grub
'';
};
in (expr (args // g));
withExtraPackages = pkgs: expr: args: let
packages = args.extraPackages or [];
in expr (args // { extraPackages = packages ++ pkgs; });
withPartition = expr: args: let
script = partitionDisk "/dev/vda";
newargs = args // { createRootFS = script; };
in expr (builtins.trace (builtins.toJSON newargs) newargs);
withScript = script: expr: args: let a.postInstall = ''
${lib.optionalString (args?postInstall) args.postInstall}
${script}
''; in expr (args // a);
withGrub64 = expr: withExtraPackages ["grub-pc" "linux-image-amd64"] (withGrub (withPartition expr));
withGrub32 = expr: withExtraPackages ["grub-pc" "linux-image-i386"] (withGrub (withPartition expr));
diskImageFuns = (lib.mapAttrs (name: as: as2: makeImageFromDebDist (as // as2)) pkgs.vmTools.debDistros);
basePackages = [
"systemd"
"systemd-sysv"
"apt"
"apt-transport-https"
"apt-utils"
"e2fsprogs"
"iproute2"
"nano"
"iputils-ping"
"psmisc"
"bash"
"bash-completion"
"isc-dhcp-client"
"tasksel"
"netscript-2.4"
"ca-certificates"
"curl"
];
withoutRootPassword = withScript "chroot /mnt /usr/bin/passwd -d root";
withDebianSources = release: withScript ''
echo "deb http://httpredir.debian.org/debian/ ${release} main" > /mnt/etc/apt/sources.list
echo "deb http://httpredir.debian.org/debian/ ${release}-updates main" >> /mnt/etc/apt/sources.list
echo "deb http://security.debian.org/ ${release}/updates main" >> /mnt/etc/apt/sources.list
'';
stretch.x86_64 = withDebianSources "stretch" (withoutRootPassword (withGrub64 (withExtraPackages basePackages diskImageFuns.debian9x86_64)));
myPackages = [
"vim"
];
in
{
stretch.x86_64 = {
vanilla = stretch.x86_64 {};
vim = stretch.x86_64 { extraPackages = myPackages; };
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment