Skip to content

Instantly share code, notes, and snippets.

@cstrahan
Last active December 13, 2017 07:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cstrahan/15668500a2b78d3c11cb to your computer and use it in GitHub Desktop.
Save cstrahan/15668500a2b78d3c11cb to your computer and use it in GitHub Desktop.
Install NixOS on Hetzner(PX60-SSD)
set -x
wget http://archive.zfsonlinux.org/debian/pool/main/z/zfsonlinux/zfsonlinux_2%7Ewheezy_all.deb
dpkg -i zfsonlinux_2~wheezy_all.deb
apt-get update
apt-get install -y debian-zfs
DISK_SIZE=$(parted /dev/sda unit MB print | grep '^Disk' | sed -r 's/.* ([0-9]+)MB.*/\1/')
# Partitions
partition_drive() {
local drive=$1
local index=$2
# bios+grub
parted -s ${drive} "mklabel gpt"
parted -a optimal -s ${drive} "mkpart biosboot${index} ext4 1m 2m"
parted -s ${drive} "set 1 bios_grub on"
mkfs.ext4 -m 0 ${drive}1
# /boot
parted -a optimal -s ${drive} "mkpart boot${index} ext4 2m 256m"
parted -s ${drive} "set 2 raid on"
# /
parted -a optimal -s ${drive} "mkpart root${index} zfs 256m $(($DISK_SIZE - 10000))"
# swap
parted -a optimal -s ${drive} "mkpart swap${index} ext4 $(($DISK_SIZE - 10000)) 100%"
mkswap -L swap${index} ${drive}4
swapon ${drive}4
}
partition_drive /dev/sda 0
partition_drive /dev/sdb 1
# Create the filesystems
zpool create -f -o ashift=12 rpool mirror /dev/sda3 /dev/sdb3
zfs create -o mountpoint=none -o checksum=fletcher4 -o atime=off rpool/ROOT
zfs create -o mountpoint=legacy rpool/ROOT/nixos
# Mount the filesystems manually
mkdir /mnt
mount -t zfs rpool/ROOT/nixos /mnt
# Mount boot
mkdir /mnt/boot
mount /dev/sda2 /mnt/boot
# Install nix
mkdir -m 0755 /nix && chown root /nix
bash <(curl https://nixos.org/nix/install)
source /root/.nix-profile/etc/profile.d/nix.sh
echo "{ ... }: { }" > $HOME/configuration.nix
export NIX_PATH=nixos-config=$HOME/configuration.nix:nixos=/root/.nix-defexpr/channels/nixos/nixos:$NIX_PATH
nix-channel --remove nixpkgs
nix-channel --add http://nixos.org/channels/nixos-14.04 nixos
nix-channel --update
nix-env -f "<nixos>" -i -A config.system.build.nixos-install -A config.system.build.nixos-option -A config.system.build.nixos-generate-config
# Generate the NixOS configuration, as per the NixOS manual
nixos-generate-config --root /mnt
cat <<EOF > /mnt/etc/nixos/configuration.nix
{ config, pkgs, ... }:
{
imports = [
./hardware-configuration.nix
];
time.timeZone = "UTC";
networking.firewall.enable = false;
boot.supportedFilesystems = [ "zfs" ];
boot.loader.grub.enable = true;
boot.loader.grub.version = 2;
boot.loader.grub.devices = [ "/dev/sda" "/dev/sdb" ];
services.openssh.enable = true;
services.openssh.passwordAuthentication = false;
security.sudo = {
enable = true;
wheelNeedsPassword = false;
};
users = {
mutableUsers = false;
extraUsers = [
{
uid = 1000;
name = "cstrahan";
group = "users";
extraGroups = [ "wheel" ];
password = "CHANGE ME";
createHome = true;
home = "/home/cstrahan";
useDefaultShell = true;
openssh.authorizedKeys.keys = [
"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAz2TFqy5iiwZCivDbVPhZP++Few+VncNY3o9X6N+hnn+L7065YvN0Tgruq0X+MsIv77Jphx7fv/a0ycEQNBKHygR+z044/zNzZh/w3CKZJLOun/ahF4M+a4ORGTKXKSgGP2UlLZjpfo5BxqzmUB0N1u7Hfi1Y1Umtjgq/ruknOrU2pZNj9LM4p4uf9jF2eTGJsDTTF6KFtAQPX+BISbGWnPKkV7zVWemgiGdTnQCayUCPUsxtdFtK0jY8nUIW7IGs+tk3sb0R3WL9BkuAap6j/j/2xCa5qppnZixfks7hJ7A9Q4M3Uyvrtx3thRHeM2k5hKkI2wc9CWWFmYpPxQDVuQ== charles.c.strahan@gmail.com"
];
}
];
};
nix = {
useChroot = true;
extraOptions = ''
gc-keep-outputs = true
'';
};
}
EOF
# Use <nixos> and <nixpkgs> from the nixos channel
NIX_PATH=nixpkgs=/root/.nix-defexpr/channels/nixos/nixpkgs:nixos=/root/.nix-defexpr/channels/nixos/nixos
nixos-install
umount /mnt/boot
mdadm --stop /dev/md0
mdadm --zero-superblock /dev/sda2
mdadm --zero-superblock /dev/sdb2
umount /mnt
zpool destroy -f rpool
swapoff /dev/sda4
swapoff /dev/sdb4
parted -s /dev/sda "mklabel gpt"
parted -s /dev/sdb "mklabel gpt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment