nix config
1#!/usr/bin/env bash
2
3RED='\033[0;31m'
4GREEN='\033[0;32m'
5YELLOW='\033[1;33m'
6NC='\033[0m' # No Color
7
8# Determine the operating system
9export OS=$(uname)
10
11# Primary network interface
12if [[ "$OS" != "Darwin" ]]; then
13 export PRIMARY_IFACE=$(ip -o -4 route show to default | awk '{print $5}')
14 echo -e "${GREEN}Found primary network interface $PRIMARY_IFACE${NC}"
15fi
16
17# Custom print function
18_print() {
19 if [[ "$OS" == "Darwin" ]]; then
20 echo -e "$1"
21 else
22 echo "$1"
23 fi
24}
25
26# Custom prompt function
27_prompt() {
28 local message="$1"
29 local variable="$2"
30
31 _print "$message"
32 read -r $variable
33}
34
35# Fetch username from the system
36export USERNAME=$(whoami)
37
38# If the username is 'nixos' or 'root', ask the user for their username
39if [[ "$USERNAME" == "nixos" ]] || [[ "$USERNAME" == "root" ]]; then
40 _prompt "${YELLOW}You're running as $USERNAME. Please enter your desired username: ${NC}" USERNAME
41fi
42
43# Check if git is available
44if command -v git >/dev/null 2>&1; then
45 # Fetch email and name from git config
46 export GIT_EMAIL=$(git config --get user.email)
47 export GIT_NAME=$(git config --get user.name)
48else
49 _print "${RED}Git is not available on this system.${NC}"
50fi
51
52# If git email is not found or git is not available, ask the user
53if [[ -z "$GIT_EMAIL" ]]; then
54 _prompt "${YELLOW}Please enter your email: ${NC}" GIT_EMAIL
55fi
56
57# If git name is not found or git is not available, ask the user
58if [[ -z "$GIT_NAME" ]]; then
59 _prompt "${YELLOW}Please enter your name: ${NC}" GIT_NAME
60fi
61
62select_boot_disk() {
63 local disks
64 local _boot_disk
65
66 _print "${YELLOW}Available disks:${NC}"
67 disks=$(lsblk -nd --output NAME,SIZE | grep -v loop)
68 echo "$disks"
69
70 # Warning message for data deletion
71 _print "${RED}WARNING: All data on the chosen disk will be erased during the installation!${NC}"
72 _prompt "${YELLOW}Please choose your boot disk (e.g., nvme0n1, sda): ${NC}" _boot_disk
73
74 # Confirmation for disk selection to prevent accidental data loss
75 _print "${YELLOW}You have selected $_boot_disk as the boot disk. This will delete everything on this disk. Are you sure? (Y/N): ${NC}"
76 read -r confirmation
77 if [[ "$confirmation" =~ ^[Yy]$ ]]; then
78 export BOOT_DISK=$_boot_disk
79 else
80 _print "${RED}Disk selection cancelled by the user. Please run the script again to select the correct disk.${NC}"
81 exit 1
82 fi
83}
84
85# Set hostname and find primary disk if this is NixOS
86if [[ "$OS" != "Darwin" ]]; then
87 _prompt "${YELLOW}Please enter a hostname for the system: ${NC}" HOST_NAME
88 export HOST_NAME
89 select_boot_disk
90fi
91
92# Confirmation step
93confirm_details() {
94 _print "${GREEN}Username: $USERNAME"
95 _print "Email: $GIT_EMAIL"
96 _print "Name: $GIT_NAME${NC}"
97
98 if([[ "$OS" != "Darwin" ]]); then
99 _print "${GREEN}Primary interface: $PRIMARY_IFACE"
100 _print "Boot disk: $BOOT_DISK"
101 _print "Hostname: $HOST_NAME${NC}"
102 fi
103
104 _prompt "${YELLOW}Is this correct? (Y/N): ${NC}" choice
105
106 case "$choice" in
107 [Nn] ) _print "${RED}Exiting script.${NC}" && exit 1;;
108 [Yy] ) _print "${GREEN}Continuing...${NC}";;
109 * ) _print "${RED}Invalid option. Exiting script.${NC}" && exit 1;;
110 esac
111}
112
113# Call the confirmation function
114confirm_details
115
116# Function to replace tokens in each file
117replace_tokens() {
118 local file="$1"
119 if [[ $(basename $1) != "apply" ]]; then
120 if [[ "$OS" == "Darwin" ]]; then
121 # macOS
122 LC_ALL=C LANG=C sed -i '' -e "s/%USER%/$USERNAME/g" "$file"
123 LC_ALL=C LANG=C sed -i '' -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
124 LC_ALL=C LANG=C sed -i '' -e "s/%NAME%/$GIT_NAME/g" "$file"
125 else
126 # Linux or other
127 sed -i -e "s/%USER%/$USERNAME/g" "$file"
128 sed -i -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
129 sed -i -e "s/%NAME%/$GIT_NAME/g" "$file"
130 sed -i -e "s/%INTERFACE%/$PRIMARY_IFACE/g" "$file"
131 sed -i -e "s/%DISK%/$BOOT_DISK/g" "$file"
132 sed -i -e "s/%HOST%/$HOST_NAME/g" "$file"
133 fi
134 fi
135}
136
137# Traverse directories and call replace_tokens on each Nix file
138export -f replace_tokens
139find . -type f -exec bash -c 'replace_tokens "$0"' {} \;
140
141echo "$USERNAME" > /tmp/username.txt
142_print "${GREEN}User $USERNAME information applied.${NC}"