Skip to content

Commit

Permalink
Add disko
Browse files Browse the repository at this point in the history
  • Loading branch information
rake5k committed Jan 5, 2025
1 parent 5d39aea commit 52f9072
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 168 deletions.
21 changes: 21 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
inputs.systems.follows = "systems";
};

disko = {
url = "github:nix-community/disko";
inputs.nixpkgs.follows = "nixpkgs";
};

home-manager = {
url = "github:nix-community/home-manager/release-24.11";
inputs.nixpkgs.follows = "nixpkgs";
Expand Down
16 changes: 0 additions & 16 deletions hosts/nixos/hardware/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,11 @@
"sr_mod"
];
kernelModules = [ "dm-snapshot" ];
luks.devices.root = {
device = "/dev/sda2";
preLVM = true;
};
};
kernelModules = [ ];
extraModulePackages = [ ];
};

fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
fsType = "ext4";
};

fileSystems."/boot" = {
device = "/dev/disk/by-label/boot";
fsType = "vfat";
};

swapDevices = [ { device = "/dev/disk/by-label/swap"; } ];

nix.settings.max-jobs = lib.mkDefault 2;
powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
}
1 change: 1 addition & 0 deletions hosts/nixos/hardware/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
imports = [
# Include the results of the hardware scan.
./configuration.nix
./disk-config.nix
];

hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
Expand Down
32 changes: 32 additions & 0 deletions hosts/nixos/hardware/disk-config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
disko.devices = {
disk = {
nixos = {
device = "/dev/sda";
type = "disk";
content = {
type = "gpt";
partitions = {
ESP = {
type = "EF00";
size = "500M";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
size = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
};
};
};
};
};
};
}
168 changes: 16 additions & 152 deletions lib/apps/nixos-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,167 +12,31 @@ readonly FLAKE="${3}"
# Validate arguments

test "${HOSTNAME}" || {
# shellcheck disable=SC2016
echo '$HOSTNAME is not given!'
exit 1
# shellcheck disable=SC2016
echo '$HOSTNAME is not given!'
exit 1
}

NUM_SUPPORTED_DISKS=$(echo "${DISK}" | grep -P "^/dev/(sd[a-z]|nvme[0-9]n[1-9])$" -c || true)
readonly NUM_SUPPORTED_DISKS

(( NUM_SUPPORTED_DISKS > 0 )) || {
# shellcheck disable=SC2016
echo '$DISK is not of format "/dev/sda" or "/dev/nvme0n1"!'
exit 1
}

NUM_NVME_DISKS=$(echo "${DISK}" | grep "^/dev/nvme" -c || true)
readonly NUM_NVME_DISKS

is_nvme_disk() {
(( NUM_NVME_DISKS > 0 ))
}

get_partition() {
# shellcheck disable=SC2310
if is_nvme_disk; then
echo "${DISK}p${1}"
else
echo "${DISK}${1}"
fi
}

BOOT_PARTITION="$(get_partition 1)"
readonly BOOT_PARTITION
ROOT_PARTITION="$(get_partition 2)"
readonly ROOT_PARTITION


### Declare functions

readonly ROOT_CRYPT="root-crypt"
readonly BOOT_FS="BOOT"
readonly ROOT_FS="root"
readonly MOUNT_ROOT="/mnt"

partition() {
_log "[partition] Deleting partitions..."
dd if=/dev/zero of="${DISK}" bs=512 count=1 conv=notrunc status=progress

_log "[partition] Creating partition table..."
parted "${DISK}" mklabel gpt
parted "${DISK}" mkpart "boot" fat32 0% 1GiB
parted "${DISK}" set 1 esp on
parted "${DISK}" mkpart "root" ext4 1GiB 100%

_log "[partition] Result of partitioning:"
fdisk "${DISK}" -l
}

crypt_setup() {
_log "[crypt_setup] Encrypting LVM partition..."
cryptsetup luksFormat "${ROOT_PARTITION}"
cryptsetup luksOpen "${ROOT_PARTITION}" "${ROOT_CRYPT}"
}

create_filesystems() {
local root_partition="${1}"
_log "[create_filesystems] Creating filesystems..."
mkfs.vfat -n "${BOOT_FS}" "${BOOT_PARTITION}"
mkfs.btrfs -f -L "${ROOT_FS}" "${root_partition}"

sleep 2

_log "[create_filesystems] Creating sub volumes"
mount "/dev/disk/by-label/${ROOT_FS}" "${MOUNT_ROOT}"
btrfs subvolume create "${MOUNT_ROOT}/@"
btrfs subvolume create "${MOUNT_ROOT}/@home"
btrfs subvolume create "${MOUNT_ROOT}/@nix"
btrfs subvolume create "${MOUNT_ROOT}/@swap"
umount "${MOUNT_ROOT}"

_log "[create_filesystems] Result of filesystems creation:"
lsblk -f "${DISK}"
((NUM_SUPPORTED_DISKS > 0)) || {
# shellcheck disable=SC2016
echo '$DISK is not of format "/dev/sda" or "/dev/nvme0n1"!'
exit 1
}

decrypt_volumes() {
_log "[decrypt_volumes] Decrypting volumes..."
cryptsetup luksOpen "${ROOT_PARTITION}" "${ROOT_CRYPT}"

_log "[decrypt_volumes] Volumes decrypted:"
lsblk -f "${DISK}"
}

mount_filesystems() {
_log "[mount_filesystems] Checking if we need to decrypt any disk..."
ROOT_PARTITION_TYPE=$(blkid -s "TYPE" -o "value" "${ROOT_PARTITION}")
readonly ROOT_PARTITION_TYPE
_log "[mount_filesystems] Root partition type is: ${ROOT_PARTITION_TYPE}"
if [[ "${ROOT_PARTITION_TYPE}" == "crypto_LUKS" ]]; then
CRYPT_VOL_STATUS="$(cryptsetup -q status "${ROOT_CRYPT}" || true)"
readonly CRYPT_VOL_STATUS
_log "[mount_filesystems] Volume encryption status is: ${CRYPT_VOL_STATUS}"
CRYPT_VOL_NUM_ACTIVE=$(echo "${CRYPT_VOL_STATUS}" | grep "^/dev/mapper/${ROOT_CRYPT} is active" -c || true)
readonly CRYPT_VOL_NUM_ACTIVE
if (( CRYPT_VOL_NUM_ACTIVE < 1 )); then
_log "[mount_filesystems] Volume is not active yet, we need to decrypt it."
decrypt_volumes
fi
fi

sleep 2

_log "[mount_filesystems] Mounting file systems..."

grep "${ROOT_PARTITION} ${MOUNT_ROOT} btrfs" "/proc/mounts" \
|| mount -o noatime,compress=lzo,subvol=@ "/dev/disk/by-label/${ROOT_FS}" "${MOUNT_ROOT}"

# shellcheck disable=SC2248
mkdir -p ${MOUNT_ROOT}/{home,nix,swap}
grep "${ROOT_PARTITION} ${MOUNT_ROOT}/home btrfs" "/proc/mounts" \
|| mount -o noatime,compress=lzo,subvol=@home "/dev/disk/by-label/${ROOT_FS}" "${MOUNT_ROOT}/home"
grep "${ROOT_PARTITION} ${MOUNT_ROOT}/nix btrfs" "/proc/mounts" \
|| mount -o noatime,compress=zstd,subvol=@nix "/dev/disk/by-label/${ROOT_FS}" "${MOUNT_ROOT}/nix"
grep "${ROOT_PARTITION} ${MOUNT_ROOT}/swap btrfs" "/proc/mounts" \
|| mount -o subvol=@swap "/dev/disk/by-label/${ROOT_FS}" "${MOUNT_ROOT}/swap"

local mount_boot="${MOUNT_ROOT}/boot"
mkdir -p "${mount_boot}"
grep "${BOOT_PARTITION} ${mount_boot} vfat" "/proc/mounts" \
|| mount "${BOOT_PARTITION}" "${mount_boot}"

_log "[mount_filesystems] File systems mounted:"
findmnt --real
}

install() {
_log "[install] Installing NixOS..."
nixos-install --root "${MOUNT_ROOT}" --flake "${FLAKE}#${HOSTNAME}" --impure
_log "[install] Installing NixOS... finished!"

_log "[install] Installation finished, please reboot and remove installation media..."
}


### Pull the trigger

# shellcheck disable=SC2310
if _read_boolean "Do you want to DELETE ALL PARTITIONS?" N; then
partition

# shellcheck disable=SC2310
if _read_boolean "Do you want to ENCRYPT THE DISK?" N; then
crypt_setup
create_filesystems "/dev/mapper/${ROOT_CRYPT}"
else
create_filesystems "${ROOT_PARTITION}"
fi
if _read_boolean "Do you want to DELETE ALL PARTITIONS and INSTALL NixOS now?" N; then
serial=$(udevadm info --query=property --property=ID_SERIAL_SHORT --value -n "${DISK}")
symlinks=$(udevadm info --query=property --property=DEVLINKS --value -n "${DISK}")
device=$(echo "${symlinks}" | awk '{for(i=1;i<=NF;i++) if($i ~ /\/dev\/disk\/by-id\/.*'"${serial}"'$/) {print $i; exit}}')
[[ -L "${device}" ]] || {
echo "Could not find a suitable symlink by id for ${DISK}!"
exit 1
}

nix run 'github:nix-community/disko/latest#disko-install' -- --flake "${FLAKE}#${HOSTNAME}" --impure --write-efi-boot-entries --disk "root" "${device}"
fi

# shellcheck disable=SC2310
if _read_boolean "Do you want to INSTALL NixOS now?" N; then
mount_filesystems
install
fi

3 changes: 3 additions & 0 deletions lib/builders/mkNixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ inputs.nixpkgs.lib.nixosSystem {
};
}

# Disko
inputs.disko.nixosModules.disko

# Home-Manager
inputs.home-manager.nixosModules.home-manager
./modules/home-manager
Expand Down

0 comments on commit 52f9072

Please sign in to comment.