about summary refs log tree commit diff stats
path: root/bootstrap/install/install.sh
blob: c2a3814cbd47928441a781d9c3cfefe6e4f37cbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#! /usr/bin/env dash
# shellcheck disable=SC2086
# shellcheck source=/dev/null
. %SHELL_LIBRARY_PATH
bsc() {
    msg2 "$(btrfs subvolume create "$1" || die "Creating of subvol $1 failed")";
}

mne() {
    mount --mkdir --options compress-force=zstd:15,subvol="$1" $DISK_ROOT "$2" || die "Mounting of $1 failed";
}


available_disks="$(mktmp)";
lsblk -J | jq '.[] | map(.name) | [foreach .[] as $item ({item: null, index: -1}; {$item, index: (.index + 1)})]' > $available_disks;


[ "$(jq '.[]' "$available_disks" | wc -l)" -eq 0 ] && die "No disks found"
msg "Select a disk to format:\n"

i=9999; # nobody will have so many disks attached

while ! jq -e --argjson i "$i" '.[$i]' "$available_disks" > /dev/null 2>&1; do
    for disk in $(jq -c '.[]' "$available_disks" ); do
        printf "%4s) %s \n" "$(echo "$disk" | jq '.index')" "$(echo "$disk" | jq '.item' | tr -d "\"")";
    done
    printf "%4s) Exit\n" "q"
    readp  "Enter a option: " disk

    if [ $disk = "q" ];then
        exit 1
    else
        i="$disk"
    fi
    if jq -e --argjson i "$i" 'nth($i)' "$available_disks"  > /dev/null 2>&1; then
        disk=$(mktmp);
        jq -e --argjson i "$i" 'nth($i)' "$available_disks" > "$disk";
    else
        warning "No disk selected. Select a disk to continue.\n"
    fi
done

readp "Do you really want to delete all data on disk $(jq '.item' "$disk")? [N/y]: " result
case $result in
    [Yy])
        msg "Great, deleting everything..."
        disk="$(jq '.item' "$disk" | tr -d "\"")"
        ;;
    *)
        msg "Sure, keep your data"
        exit 1
        ;;
esac

sgdisk -Z "/dev/${disk}" > /dev/null|| die "Zapping failed"

sgdisk -n 1:0:+550M -n 2:0:"$ENDSECTOR" -t 1:ef00 -t 2:8300 "/dev/${disk}" > /dev/null|| die "Partitioning failed"

case "$disk" in
    "nvme"*)
        export DISK_EFI="/dev/${disk}"p1
        export DISK_ROOT="/dev/${disk}"p2
        ;;
    "sd"* |"vd"*)
        export DISK_EFI="/dev/${disk}"1
        export DISK_ROOT="/dev/${disk}"2
        ;;
    *)
        die "The disk type: ${disk} is not yet supported!"
        ;;
esac

msg "Started Formatting..."
mkfs.fat -F32 "$DISK_EFI" > /dev/null || die "Formatting(fat32) failed"
mkfs.btrfs -f "$DISK_ROOT" > /dev/null || die "Formatting(btrfs) failed"


msg "Mounting..."
mount -t btrfs $DISK_ROOT /mnt
cd /mnt || die "(Bug): no /mnt"
bsc nix-store
bsc persistent-storage
bsc persistent-storage/nixos-config
bsc persistent-storage/.snapshots
bsc swap
cd /
umount -R /mnt
mount -t tmpfs none /mnt
mount --mkdir "$DISK_EFI" /mnt/boot

mne nix-store /mnt/nix
mne persistent-storage /mnt/srv
mne swap /mnt/swap

mount --mkdir --options bind /mnt/srv/nixos-config /mnt/etc/nixos

msg "Finished mounting and generating btrfs subvolumes"

msg "Creating swapfile..."
msg2 "$(btrfs filesystem mkswapfile --size "$(free -m | awk '{if (NR==2) {printf "%sm\n", $2 + 500}}')" /mnt/swap/swapfile)";
msg "Finished creating swapfile!"
msg "Important information:"
msg2 "Swapfile UUID is: $(findmnt -no UUID -T /mnt/swap/swapfile)";
msg2 "Swapfile resume offset is: $(btrfs inspect-internal map-swapfile -r /mnt/swap/swapfile)";
msg2 "Root-disk UUID is: $(lsblk -no UUID -T $DISK_ROOT)";
msg2 "EFI-disk UUID is: $(lsblk -no UUID -T $DISK_EFI)";
warning "This information needs to be entered in the host configuration BEFORE rebuilding it. Otherwise the system won't boot!"

info_applied=false;
while [ "$info_applied" = false ];do
    readp "Have you updated your chosen host with this information? [N/y]: " result
    case $result in
        [Yy])
            info_applied=true;
            ;;
        *)
            warning "You won't be able to boot, if you don't update it!"
            ;;
    esac
done

msg "Checking for incompatibilities..."
ssd_or_hdd=$(cat /sys/block/$disk/queue/rotational);
case "$ssd_or_hdd" in
    0)
        msg2 "You seem to use a ssd."
        trim_support=$(lsblk --bytes --json --discard | jq --arg name "$disk" '.blockdevices | .[] | select(.name == $name) | (.["disc-gran"] + .["disc-max"]) != 0');
        case $trim_support in
            "true")
                msg2 "Yay, your ssd supports trim, go on and activate it";
                ;;
            "false")
                msg2 "Nay, your ssd doesn't support trim, go on";
                ;;
            *)
                warning "Your ssd doesn't seem to exists, if this bothers you please open an issue.'";
                ;;
        esac
        ;;
    1)
        msg2 "You seem to use a hdd there is nothing you have to do";
        ;;
    *)
        warning "There is no indicator, which shows, which drive your are using.\n This means, that you have to check yourself, which optimizations you should activate."
        ;;
esac


readp "Do you want to continue with nixos-install? [N/y]: " result
case $result in
    [Yy])
        nix run "git+https://codeberg.org/ene/nixos-config#setup" --experimental-features 'nix-command flakes';
        ;;
    *)
        msg "Sure, do it yourself"
        exit 1
        ;;
esac