2020-09-08 03:08:20 +04:00
#! /usr/bin/env nix-shell
2022-01-30 00:09:53 +00:00
#! nix-shell -i bash -p perl -p gptfdisk -p parted -p git
2020-09-08 03:08:20 +04:00
set -e
CONFIG_FOLDER = " $( dirname " $( pwd ) " ) "
2022-01-30 00:09:53 +00:00
DEVICE_NAME = Packard-Server
MAX_JOBS = 4
SWAP_SIZE = 12GiB
#NIXOS_COMMIT="364b5555ee04bf61ee0075a3adab4c9351a8d38c"
USE_ECNRYPTION = true
2020-09-08 03:08:20 +04:00
clean_stdin( ) {
while read -r -t 0; do read -r; done
}
pprint ( ) {
local cyan = "\e[96m"
local default = "\e[39m"
local timestamp
timestamp = $( date +%FT%T.%3NZ)
echo -e " ${ cyan } ${ timestamp } $1 ${ default } " 1>& 2
}
# Create new partitions
create_new_part_table( ) {
select ENTRY in $( ls /dev/disk/by-id/) ;
do
DISK = " /dev/disk/by-id/ $ENTRY "
echo " Installing system on $ENTRY "
break
done
read -p " > Do you want to wipe all data on $ENTRY ? " -n 1 -r
echo
if [ [ " $REPLY " = ~ ^[ Yy] $ ] ]
then
wipefs -af " $DISK "
sgdisk -Zo " $DISK "
fi
pprint "Creating boot (EFI) partition"
sgdisk -n 1:1MiB:+256MiB -t 1:EF00 " $DISK "
BOOT = " $DISK -part1 "
pprint "Creating SWAP partition"
2020-11-17 01:59:51 +04:00
sgdisk -n 2::+$SWAP_SIZE -t 2:8200 " $DISK "
2020-09-08 03:08:20 +04:00
SWAP = " $DISK -part2 "
2020-11-17 01:59:51 +04:00
if [ [ " $USE_ECNRYPTION " = true ] ]
then
pprint "Creating LUKS partition"
sgdisk -n 3 -t 3:8309 " $DISK "
else
pprint "Creating ROOT partition"
sgdisk -n 3 -t 3:8300 " $DISK "
fi
2020-09-08 03:08:20 +04:00
LINUX = " $DISK -part3 "
partprobe " $DISK "
sleep 1
pprint " Format BOOT partition $BOOT "
mkfs.vfat " $BOOT "
}
# Using existed partitions
use_old_part_table( ) {
lsblk -o name,type,size,mountpoint | grep part
pprint "Select BOOT partition (must already be formatted in vfat!)"
select ENTRY in $( lsblk -o path,size,type | grep part | awk '{print $1}' ) ;
do
BOOT = " $ENTRY "
echo " You select $BOOT as BOOT "
break
done
2020-11-17 01:59:51 +04:00
if [ [ " $USE_ECNRYPTION " = true ] ]
then
pprint "Select the partition on which LUKS will be created"
else
pprint "Select the partition on which ROOT will be created"
fi
2020-09-08 03:08:20 +04:00
select ENTRY in $( lsblk -o path,size,type | grep part | awk '{print $1}' ) ;
do
LINUX = " $ENTRY "
echo " Installing system on $LINUX "
break
done
pprint "Select the partition on which SWAP will be created"
select ENTRY in $( lsblk -o path,size,type | grep part | awk '{print $1}' && echo NONE) ;
do
SWAP = " $ENTRY "
echo " You select $SWAP as SWAP "
break
done
clean_stdin
read -p " > Do you want to format BOOT partition in $BOOT ? " -n 1 -r
echo
if [ [ " $REPLY " = ~ ^[ Yy] $ ] ]
then
mkfs.vfat " $BOOT "
fi
}
### INSTALLATION BEGIN ###
2020-11-17 01:59:51 +04:00
read -p "> Do you want to encrypt your disk with LUKS?" -n 1 -r
echo
if [ [ " $REPLY " = ~ ^[ Yy] $ ] ]
then
USE_ECNRYPTION = true
else
USE_ECNRYPTION = false
fi
2020-09-08 03:08:20 +04:00
read -p "> Do you want to partition the disk (new gpt table)?" -n 1 -r
echo
if [ [ " $REPLY " = ~ ^[ Yy] $ ] ]
then
create_new_part_table
else
use_old_part_table
fi
2020-11-17 01:59:51 +04:00
if [ [ " $USE_ECNRYPTION " = true ] ]
then
pprint " Creating LUKS container on $LINUX "
clean_stdin
cryptsetup --type luks2 --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-random luksFormat " $LINUX "
2020-09-08 03:08:20 +04:00
2020-11-17 01:59:51 +04:00
pprint " Open LUKS container on $LINUX "
LUKS_DEVICE_NAME = cryptroot
clean_stdin
cryptsetup luksOpen " $LINUX " " $LUKS_DEVICE_NAME "
2020-09-08 03:08:20 +04:00
2020-11-17 01:59:51 +04:00
LUKS_DISK = " /dev/mapper/ $LUKS_DEVICE_NAME "
2020-09-08 03:08:20 +04:00
2021-06-16 05:30:04 +03:00
pprint " Create BTRFS partition on $LUKS_DISK "
BTRFS = " ${ LUKS_DISK } "
2020-11-17 01:59:51 +04:00
else
LINUX_PARTUUID = $( blkid --match-tag PARTUUID --output value " $LINUX " )
2021-06-16 05:30:04 +03:00
BTRFS = " /dev/disk/by-partuuid/ $LINUX_PARTUUID "
2020-11-17 01:59:51 +04:00
fi
2020-09-08 03:08:20 +04:00
if [ [ " $SWAP " != "NONE" ] ] ; then
pprint " Create SWAP partition on $SWAP "
mkswap $SWAP
fi
2021-06-16 05:30:04 +03:00
pprint " Create BTRFS partition on $BTRFS "
mkfs.btrfs -L root -f " $BTRFS "
2020-09-08 03:08:20 +04:00
2021-06-16 05:30:04 +03:00
pprint "Mount BTRFS partition"
mkdir -p /mnt
mount -t btrfs " $BTRFS " /mnt
2020-09-08 03:08:20 +04:00
2021-10-24 23:28:29 +03:00
pprint "Create and mount BTRFS subvolumes" is forbidden in restricted mode
2021-06-16 05:30:04 +03:00
btrfs subvolume create /mnt/nixos
btrfs subvolume create /mnt/nix
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/var
btrfs subvolume create /mnt/bittorrent
2021-09-15 15:36:44 +03:00
btrfs subvolume create /mnt/libvirt
2020-09-08 03:08:20 +04:00
2021-06-16 05:30:04 +03:00
umount /mnt
2020-09-08 03:08:20 +04:00
2021-06-16 05:30:04 +03:00
mount -t btrfs -o subvol = nixos,compress-force= zstd,noatime,autodefrag,ssd " $BTRFS " /mnt
mkdir -p /mnt/nix
mount -t btrfs -o subvol = nix,compress-force= zstd,noatime,autodefrag,ssd " $BTRFS " /mnt/nix
mkdir -p /mnt/home
mount -t btrfs -o subvol = home,compress-force= zstd,noatime,autodefrag,ssd " $BTRFS " /mnt/home
mkdir -p /mnt/var
mount -t btrfs -o subvol = var,compress-force= zstd,noatime,autodefrag,ssd " $BTRFS " /mnt/var
mkdir -p /mnt/media/bittorrent
2021-10-24 23:28:29 +03:00
chown 1000:100 /mnt/media/bittorrent
mount -t btrfs -o subvol = bittorrent,nodatacow,ssd,uid= 1000,gid= 100 " $BTRFS " /mnt/media/bittorrent
2021-09-15 15:36:44 +03:00
mkdir -p /mnt/media/libvirt
2021-10-24 23:28:29 +03:00
chown 1000:100 /mnt/media/libvirt
mount -t btrfs -o subvol = libvirt,nodatacow,ssd,uid= 1000,gid= 100 " $BTRFS " /mnt/media/libvirt
2020-09-08 03:08:20 +04:00
mkdir /mnt/boot
mount " $BOOT " /mnt/boot
pprint "Generate NixOS configuration"
nixos-generate-config --root /mnt
HOSTID = $( head -c8 /etc/machine-id)
LINUX_DISK_UUID = $( blkid --match-tag PARTUUID --output value " $LINUX " )
if [ [ " $SWAP " != "NONE" ] ] ; then
SWAP_UUID = $( blkid --match-tag PARTUUID --output value " $SWAP " )
fi
HARDWARE_CONFIG = $( mktemp)
cat <<CONFIG > "$HARDWARE_CONFIG "
networking.hostId = " $HOSTID " ;
2021-06-16 05:30:04 +03:00
boot.initrd.supportedFilesystems = [ "btrfs" ] ;
boot.supportedFilesystems = [ "btrfs" ] ;
2020-09-08 03:08:20 +04:00
CONFIG
2021-06-16 05:30:04 +03:00
pprint "Append BTRFS configuration to hardware-configuration.nix"
2020-09-08 03:08:20 +04:00
sed -i " \$e cat $HARDWARE_CONFIG " /mnt/etc/nixos/hardware-configuration.nix
if [ [ " $SWAP " != "NONE" ] ] ; then
perl -0777 -pi -e " s#swapDevices.+#swapDevices = [\n {\n device = \"/dev/disk/by-partuuid/ $SWAP_UUID \";\n randomEncryption.enable = true;\n }\n ];# " /mnt/etc/nixos/hardware-configuration.nix
fi
2022-01-30 00:09:53 +00:00
sed -i "s#\"subvol=nixos\"#\"subvol=nixos\" \"compress-force=zstd\" \"noatime\" \"autodefrag\"#" /mnt/etc/nixos/hardware-configuration.nix
sed -i "s#\"subvol=home\"#\"subvol=home\" \"compress-force=zstd\" \"noatime\" \"autodefrag\"#" /mnt/etc/nixos/hardware-configuration.nix
sed -i "s#\"subvol=nix\"#\"subvol=nix\" \"compress-force=zstd\" \"noatime\" \"autodefrag\"#" /mnt/etc/nixos/hardware-configuration.nix
sed -i "s#\"subvol=var\"#\"subvol=var\" \"compress-force=zstd\" \"noatime\" \"autodefrag\"#" /mnt/etc/nixos/hardware-configuration.nix
sed -i "s# \"subvol=bittorrent\" #\n \"subvol=bittorrent\" \"nodatacow\"\n \"uid=\${toString config.users.users.alukard.uid}\"\n \"gid=\${toString config.users.groups.users.gid}\"\n #" /mnt/etc/nixos/hardware-configuration.nix
sed -i "s# \"subvol=libvirt\" #\n \"subvol=libvirt\" \"nodatacow\"\n \"uid=\${toString config.users.users.alukard.uid}\"\n \"gid=\${toString config.users.groups.users.gid}\"\n #" /mnt/etc/nixos/hardware-configuration.nix
2021-06-16 05:30:04 +03:00
2021-02-07 02:38:11 +03:00
cp /mnt/etc/nixos/hardware-configuration.nix $CONFIG_FOLDER /machines/$DEVICE_NAME /hardware-configuration.nix
2022-01-30 00:09:53 +00:00
chown 1000:users ../machines/$DEVICE_NAME /hardware-configuration.nix
2020-09-08 03:08:20 +04:00
# Change <not-detected> for flakes
2021-06-16 05:30:04 +03:00
sed -i "s#<nixpkgs/nixos/modules/installer/scan/not-detected.nix>#\"\${inputs.nixpkgs}/nixos/modules/installer/scan/not-detected.nix\"#" $CONFIG_FOLDER /machines/$DEVICE_NAME /hardware-configuration.nix
2022-01-30 00:09:53 +00:00
git add -A
2020-09-08 03:08:20 +04:00
clean_stdin
read -p "> Do you want to execute nixos-install command?" -n 1 -r
echo
if [ [ " $REPLY " = ~ ^[ Yy] $ ] ]
then
2021-10-24 23:28:29 +03:00
nixos-install --flake " ../# $DEVICE_NAME " --max-jobs $MAX_JOBS --no-root-passwd --impure
2020-09-08 03:08:20 +04:00
fi
2021-02-07 02:38:11 +03:00
pprint "Copy config to destination system"
mkdir -p /mnt/home/alukard/nixos-config
2021-06-27 20:44:49 +03:00
cp -aT $CONFIG_FOLDER /mnt/home/alukard/nixos-config