about summary refs log tree commit diff stats
path: root/bootstrap/99_ensure_config_variables
blob: 6758b221490c47112631255dd4339d894fa63b37 (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
#! /usr/bin/env dash

# shellcheck source=/dev/null
#SHELL_LIBRARY_VERSION="1.1.4" . %SHELL_LIBRARY_PATH
SHELL_LIBRARY_VERSION="1.1.4" . /nix/store/m5vww39n11768whvhww00zrgkd3ry0q9-shell_library-v1.1.4/include/lib.sh

if ! [ "$NIX_HOST" ]; then
    msg "Select a host:"
    _hosts=$(mktmp)
    curl https://codeberg.org/soispha/nixos-config/raw/branch/prime/flake/nixosConfigurations/default.nix 2> /dev/null | awk -F '"' '/hosts = /{for (i=2;i<NF;i+=2) {print $i}}' > "$_hosts"

    while ! grep "$host" "$_hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; do
        i=1
        while read -r host_read; do
            printf "%4s) %s \n" "$i" "$host_read"
            i=$((i + 1))
        done < "$_hosts"
        printf "%4s) Exit\n" "q"
        readp "Enter a option: " host

        [ "$host" = "q" ] && exit 1
        host=$(awk -v i="$host" '{if (NR==i) {print $0}}' "$_hosts")
        if ! grep "$host" "$_hosts" > /dev/null || [ "$(printf "%s" "$host" | wc -c)" -eq 0 ]; then
            warning "No host selected. Select a host to continue.\n"
        fi
    done

    export NIX_HOST="$host"
fi
if ! [ "$NIX_DISK" ] && [ "$NIX_INSTALL" ]; then
    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:"

    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

    warn "All data on disk $(jq '.item' "$_disk") will be deleted."
    readp "Continue with this disk? [N/y]: " result
    case "$result" in
        [Yy])
            msg "Great, continuing..."
            _disk="$(jq '.item' "$_disk" | tr -d "\"")"
            export NIX_DISK="$_disk"
            ;;
        *)
            msg "Sure, keep your data"
            exit 1
            ;;
    esac
fi

if ! [ "$ACTIVATE_TO_SETUP" ] && ! [ "$INSTALL" ]; then
    readp "Do you want to continue with nixos-install after activation? [N/y]: " result
    case $result in
        [Yy])
            export ACTIVATE_TO_SETUP=yes;
            ;;
        *)
            export ACTIVATE_TO_SETUP=no;
            ;;
    esac
fi

# vim: ft=sh