···11+#!/usr/bin/env bash
22+33+RED='\033[0;31m'
44+GREEN='\033[0;32m'
55+YELLOW='\033[1;33m'
66+NC='\033[0m' # No Color
77+88+# Determine the operating system
99+export OS=$(uname)
1010+1111+# Primary network interface
1212+if [[ "$OS" != "Darwin" ]]; then
1313+ export PRIMARY_IFACE=$(ip -o -4 route show to default | awk '{print $5}')
1414+ echo -e "${GREEN}Found primary network interface $PRIMARY_IFACE${NC}"
1515+fi
1616+1717+# Custom print function
1818+_print() {
1919+ if [[ "$OS" == "Darwin" ]]; then
2020+ echo -e "$1"
2121+ else
2222+ echo "$1"
2323+ fi
2424+}
2525+2626+# Custom prompt function
2727+_prompt() {
2828+ local message="$1"
2929+ local variable="$2"
3030+3131+ _print "$message"
3232+ read -r $variable
3333+}
3434+3535+insert_secrets_output() {
3636+ local pattern="outputs = { self, darwin, nix-homebrew, homebrew-bundle, homebrew-core, homebrew-cask, home-manager, nixpkgs, disko, agenix } @inputs:"
3737+ local insert_text="secrets "
3838+3939+ awk -v pat="$pattern" -v insert="$insert_text" '
4040+ $0 ~ pat {
4141+ sub(/} @inputs:/, ", " insert "} @inputs:"); # Replace the closing brace with the insert text followed by the brace
4242+ gsub(/ ,/, ","); # Correct any spaces before commas
4343+ print
4444+ next
4545+ }
4646+ { print }
4747+ ' flake.nix > flake.nix.tmp
4848+4949+ mv flake.nix.tmp flake.nix
5050+}
5151+5252+insert_secrets_input() {
5353+ # Define file path
5454+ FILE_PATH="flake.nix"
5555+5656+ # Backup the original file
5757+ cp "$FILE_PATH" "${FILE_PATH}.bak"
5858+5959+ # Temporary file for the text to insert
6060+ TEMP_FILE="temp_insert.txt"
6161+6262+ # Write the formatted text to the temporary file
6363+cat > "$TEMP_FILE" << 'EOF'
6464+ secrets = {
6565+ url = "git+ssh://git@github.com/%GITHUB_USER%/%GITHUB_SECRETS_REPO%.git";
6666+ flake = false;
6767+ };
6868+EOF
6969+7070+ # Check if the 'secrets' block already exists
7171+ if grep -q 'url = "git+ssh://git@github.com/%GITHUB_USER%/%GITHUB_SECRETS_REPO%.git"' "$FILE_PATH"; then
7272+ echo "The 'secrets' block already exists in the file."
7373+ rm "$TEMP_FILE"
7474+ rm "${FILE_PATH}.bak"
7575+ exit 0
7676+ fi
7777+7878+ # Find the start and end line numbers of the 'disko' block
7979+ START_LINE=$(grep -n 'disko = {' "$FILE_PATH" | head -n 1 | cut -d: -f1)
8080+ END_LINE=$(tail -n +$START_LINE "$FILE_PATH" | grep -n '};' | head -n 1 | cut -d: -f1)
8181+ END_LINE=$((START_LINE + END_LINE - 1))
8282+8383+ # Create a new file with the insertion
8484+ {
8585+ sed -n "1,${END_LINE}p" "$FILE_PATH"
8686+ cat "$TEMP_FILE"
8787+ sed -n "$((END_LINE + 1)),\$p" "$FILE_PATH"
8888+ } > "${FILE_PATH}.new"
8989+9090+ # Replace the original file with the new file
9191+ mv "${FILE_PATH}.new" "$FILE_PATH"
9292+9393+ # Clean up the temporary files
9494+ rm "$TEMP_FILE"
9595+ rm "${FILE_PATH}.bak"
9696+}
9797+9898+# Fetch username from the system
9999+export USERNAME=$(whoami)
100100+101101+# If the username is 'nixos' or 'root', ask the user for their username
102102+if [[ "$USERNAME" == "nixos" ]] || [[ "$USERNAME" == "root" ]]; then
103103+ _prompt "${YELLOW}You're running as $USERNAME. Please enter your desired username: ${NC}" USERNAME
104104+fi
105105+106106+# Check if git is available
107107+if command -v git >/dev/null 2>&1; then
108108+ # Fetch email and name from git config
109109+ export GIT_EMAIL=$(git config --get user.email)
110110+ export GIT_NAME=$(git config --get user.name)
111111+else
112112+ _print "${RED}Git is not available on this system.${NC}"
113113+fi
114114+115115+# If git email is not found or git is not available, ask the user
116116+if [[ -z "$GIT_EMAIL" ]]; then
117117+ _prompt "${YELLOW}Please enter your email: ${NC}" GIT_EMAIL
118118+fi
119119+120120+# If git name is not found or git is not available, ask the user
121121+if [[ -z "$GIT_NAME" ]]; then
122122+ _prompt "${YELLOW}Please enter your name: ${NC}" GIT_NAME
123123+fi
124124+125125+_prompt "${YELLOW}Please enter your Github username: ${NC}" GITHUB_USER
126126+_prompt "${YELLOW}Please enter your Github secrets repository name: ${NC}" GITHUB_SECRETS_REPO
127127+128128+export GITHUB_USER
129129+export GITHUB_SECRETS_REPO
130130+131131+select_boot_disk() {
132132+ local disks
133133+ local _boot_disk
134134+135135+ _print "${YELLOW}Available disks:${NC}"
136136+ disks=$(lsblk -nd --output NAME,SIZE | grep -v loop)
137137+ echo "$disks"
138138+139139+ # Warning message for data deletion
140140+ _print "${RED}WARNING: All data on the chosen disk will be erased during the installation!${NC}"
141141+ _prompt "${YELLOW}Please choose your boot disk (e.g., nvme0n1, sda): ${NC}" _boot_disk
142142+143143+ # Confirmation for disk selection to prevent accidental data loss
144144+ _print "${YELLOW}You have selected $_boot_disk as the boot disk. This will delete everything on this disk. Are you sure? (Y/N): ${NC}"
145145+ read -r confirmation
146146+ if [[ "$confirmation" =~ ^[Yy]$ ]]; then
147147+ export BOOT_DISK=$_boot_disk
148148+ else
149149+ _print "${RED}Disk selection cancelled by the user. Please run the script again to select the correct disk.${NC}"
150150+ exit 1
151151+ fi
152152+}
153153+154154+# Set hostname and find primary disk if this is NixOS
155155+if [[ "$OS" != "Darwin" ]]; then
156156+ _prompt "${YELLOW}Please enter a hostname for the system: ${NC}" HOST_NAME
157157+ export HOST_NAME
158158+ select_boot_disk
159159+fi
160160+161161+# Confirmation step
162162+confirm_details() {
163163+ _print "${GREEN}Username: $USERNAME"
164164+ _print "Email: $GIT_EMAIL"
165165+ _print "Name: $GIT_NAME${NC}"
166166+167167+ if([[ "$OS" != "Darwin" ]]); then
168168+ _print "${GREEN}Primary interface: $PRIMARY_IFACE"
169169+ _print "Boot disk: $BOOT_DISK"
170170+ _print "Hostname: $HOST_NAME${NC}"
171171+ fi
172172+173173+ _print "${GREEN}Secrets repository: $GITHUB_USER/$GITHUB_SECRETS_REPO${NC}"
174174+175175+ _prompt "${YELLOW}Is this correct? (Y/N): ${NC}" choice
176176+177177+ case "$choice" in
178178+ [Nn] ) _print "${RED}Exiting script.${NC}" && exit 1;;
179179+ [Yy] ) _print "${GREEN}Continuing...${NC}";;
180180+ * ) _print "${RED}Invalid option. Exiting script.${NC}" && exit 1;;
181181+ esac
182182+}
183183+184184+# Call the confirmation function
185185+confirm_details
186186+187187+# Function to replace tokens in each file
188188+replace_tokens() {
189189+ local file="$1"
190190+ if [[ $(basename $1) != "apply" ]]; then
191191+ if [[ "$OS" == "Darwin" ]]; then
192192+ # macOS
193193+ LC_ALL=C LANG=C sed -i '' -e "s/%USER%/$USERNAME/g" "$file"
194194+ LC_ALL=C LANG=C sed -i '' -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
195195+ LC_ALL=C LANG=C sed -i '' -e "s/%NAME%/$GIT_NAME/g" "$file"
196196+ LC_ALL=C LANG=C sed -i '' -e "s/%GITHUB_USER%/$GITHUB_USER/g" "$file"
197197+ LC_ALL=C LANG=C sed -i '' -e "s/%GITHUB_SECRETS_REPO%/$GITHUB_SECRETS_REPO/g" "$file"
198198+ else
199199+ # Linux or other
200200+ sed -i -e "s/%USER%/$USERNAME/g" "$file"
201201+ sed -i -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
202202+ sed -i -e "s/%NAME%/$GIT_NAME/g" "$file"
203203+ sed -i -e "s/%INTERFACE%/$PRIMARY_IFACE/g" "$file"
204204+ sed -i -e "s/%DISK%/$BOOT_DISK/g" "$file"
205205+ sed -i -e "s/%HOST%/$HOST_NAME/g" "$file"
206206+ sed -i -e "s/%GITHUB_USER%/$GITHUB_USER/g" "$file"
207207+ sed -i -e "s/%GITHUB_SECRETS_REPO%/$GITHUB_SECRETS_REPO/g" "$file"
208208+ fi
209209+ fi
210210+}
211211+212212+# Insert secrets repo into flake
213213+insert_secrets_input
214214+insert_secrets_output
215215+216216+# Traverse directories and call replace_tokens on each Nix file
217217+export -f replace_tokens
218218+find . -type f -exec bash -c 'replace_tokens "$0"' {} \;
219219+220220+echo "$USERNAME" > /tmp/username.txt
221221+_print "${GREEN}User $USERNAME information applied.${NC}"
···11+#!/usr/bin/env bash
22+33+RED='\033[0;31m'
44+GREEN='\033[0;32m'
55+YELLOW='\033[1;33m'
66+NC='\033[0m' # No Color
77+88+# Determine the operating system
99+export OS=$(uname)
1010+1111+# Primary network interface
1212+if [[ "$OS" != "Darwin" ]]; then
1313+ export PRIMARY_IFACE=$(ip -o -4 route show to default | awk '{print $5}')
1414+ echo -e "${GREEN}Found primary network interface $PRIMARY_IFACE${NC}"
1515+fi
1616+1717+# Custom print function
1818+_print() {
1919+ if [[ "$OS" == "Darwin" ]]; then
2020+ echo -e "$1"
2121+ else
2222+ echo "$1"
2323+ fi
2424+}
2525+2626+# Custom prompt function
2727+_prompt() {
2828+ local message="$1"
2929+ local variable="$2"
3030+3131+ _print "$message"
3232+ read -r $variable
3333+}
3434+3535+# Fetch username from the system
3636+export USERNAME=$(whoami)
3737+3838+# If the username is 'nixos' or 'root', ask the user for their username
3939+if [[ "$USERNAME" == "nixos" ]] || [[ "$USERNAME" == "root" ]]; then
4040+ _prompt "${YELLOW}You're running as $USERNAME. Please enter your desired username: ${NC}" USERNAME
4141+fi
4242+4343+# Check if git is available
4444+if command -v git >/dev/null 2>&1; then
4545+ # Fetch email and name from git config
4646+ export GIT_EMAIL=$(git config --get user.email)
4747+ export GIT_NAME=$(git config --get user.name)
4848+else
4949+ _print "${RED}Git is not available on this system.${NC}"
5050+fi
5151+5252+# If git email is not found or git is not available, ask the user
5353+if [[ -z "$GIT_EMAIL" ]]; then
5454+ _prompt "${YELLOW}Please enter your email: ${NC}" GIT_EMAIL
5555+fi
5656+5757+# If git name is not found or git is not available, ask the user
5858+if [[ -z "$GIT_NAME" ]]; then
5959+ _prompt "${YELLOW}Please enter your name: ${NC}" GIT_NAME
6060+fi
6161+6262+select_boot_disk() {
6363+ local disks
6464+ local _boot_disk
6565+6666+ _print "${YELLOW}Available disks:${NC}"
6767+ disks=$(lsblk -nd --output NAME,SIZE | grep -v loop)
6868+ echo "$disks"
6969+7070+ # Warning message for data deletion
7171+ _print "${RED}WARNING: All data on the chosen disk will be erased during the installation!${NC}"
7272+ _prompt "${YELLOW}Please choose your boot disk (e.g., nvme0n1, sda): ${NC}" _boot_disk
7373+7474+ # Confirmation for disk selection to prevent accidental data loss
7575+ _print "${YELLOW}You have selected $_boot_disk as the boot disk. This will delete everything on this disk. Are you sure? (Y/N): ${NC}"
7676+ read -r confirmation
7777+ if [[ "$confirmation" =~ ^[Yy]$ ]]; then
7878+ export BOOT_DISK=$_boot_disk
7979+ else
8080+ _print "${RED}Disk selection cancelled by the user. Please run the script again to select the correct disk.${NC}"
8181+ exit 1
8282+ fi
8383+}
8484+8585+# Set hostname and find primary disk if this is NixOS
8686+if [[ "$OS" != "Darwin" ]]; then
8787+ _prompt "${YELLOW}Please enter a hostname for the system: ${NC}" HOST_NAME
8888+ export HOST_NAME
8989+ select_boot_disk
9090+fi
9191+9292+# Confirmation step
9393+confirm_details() {
9494+ _print "${GREEN}Username: $USERNAME"
9595+ _print "Email: $GIT_EMAIL"
9696+ _print "Name: $GIT_NAME${NC}"
9797+9898+ if([[ "$OS" != "Darwin" ]]); then
9999+ _print "${GREEN}Primary interface: $PRIMARY_IFACE"
100100+ _print "Boot disk: $BOOT_DISK"
101101+ _print "Hostname: $HOST_NAME${NC}"
102102+ fi
103103+104104+ _prompt "${YELLOW}Is this correct? (Y/N): ${NC}" choice
105105+106106+ case "$choice" in
107107+ [Nn] ) _print "${RED}Exiting script.${NC}" && exit 1;;
108108+ [Yy] ) _print "${GREEN}Continuing...${NC}";;
109109+ * ) _print "${RED}Invalid option. Exiting script.${NC}" && exit 1;;
110110+ esac
111111+}
112112+113113+# Call the confirmation function
114114+confirm_details
115115+116116+# Function to replace tokens in each file
117117+replace_tokens() {
118118+ local file="$1"
119119+ if [[ $(basename $1) != "apply" ]]; then
120120+ if [[ "$OS" == "Darwin" ]]; then
121121+ # macOS
122122+ LC_ALL=C LANG=C sed -i '' -e "s/%USER%/$USERNAME/g" "$file"
123123+ LC_ALL=C LANG=C sed -i '' -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
124124+ LC_ALL=C LANG=C sed -i '' -e "s/%NAME%/$GIT_NAME/g" "$file"
125125+ else
126126+ # Linux or other
127127+ sed -i -e "s/%USER%/$USERNAME/g" "$file"
128128+ sed -i -e "s/%EMAIL%/$GIT_EMAIL/g" "$file"
129129+ sed -i -e "s/%NAME%/$GIT_NAME/g" "$file"
130130+ sed -i -e "s/%INTERFACE%/$PRIMARY_IFACE/g" "$file"
131131+ sed -i -e "s/%DISK%/$BOOT_DISK/g" "$file"
132132+ sed -i -e "s/%HOST%/$HOST_NAME/g" "$file"
133133+ fi
134134+ fi
135135+}
136136+137137+# Traverse directories and call replace_tokens on each Nix file
138138+export -f replace_tokens
139139+find . -type f -exec bash -c 'replace_tokens "$0"' {} \;
140140+141141+echo "$USERNAME" > /tmp/username.txt
142142+_print "${GREEN}User $USERNAME information applied.${NC}"
+28
apps/x86_64-linux/build-switch
···11+#!/bin/sh -e
22+33+RED='\033[1;31m'
44+GREEN='\033[1;32m'
55+YELLOW='\033[1;33m'
66+NC='\033[0m'
77+88+SYSTEM=$(uname -m)
99+1010+case "$SYSTEM" in
1111+ x86_64)
1212+ FLAKE_TARGET="x86_64-linux"
1313+ ;;
1414+ aarch64)
1515+ FLAKE_TARGET="aarch64-linux"
1616+ ;;
1717+ *)
1818+ echo -e "${RED}Unsupported architecture: $SYSTEM${NC}"
1919+ exit 1
2020+ ;;
2121+esac
2222+2323+echo -e "${YELLOW}Starting...${NC}"
2424+2525+# We pass SSH from user to root so root can download secrets from our private Github
2626+sudo SSH_AUTH_SOCK=$SSH_AUTH_SOCK /run/current-system/sw/bin/nixos-rebuild switch --flake .#$FLAKE_TARGET $@
2727+2828+echo -e "${GREEN}Switch to new generation complete!${NC}"
+33
apps/x86_64-linux/check-keys
···11+#!/usr/bin/env bash
22+set -e
33+44+RED='\033[0;31m'
55+GREEN='\033[0;32m'
66+NC='\033[0m'
77+88+# We're assuming this is being run as root in the NixOS installer
99+export SSH_DIR=/root/.ssh
1010+1111+check_keys() {
1212+ if [[ -f "${SSH_DIR}/id_ed25519" && -f "${SSH_DIR}/id_ed25519.pub" && -f "${SSH_DIR}/id_ed25519_agenix" && -f "${SSH_DIR}/id_ed25519_agenix.pub" ]]; then
1313+ echo -e "${GREEN}All SSH keys are present.${NC}"
1414+ else
1515+ echo -e "${RED}Some SSH keys are missing.${NC}"
1616+ if [[ ! -f "${SSH_DIR}/id_ed25519" ]]; then
1717+ echo -e "${RED}Missing: id_ed25519${NC}"
1818+ fi
1919+ if [[ ! -f "${SSH_DIR}/id_ed25519.pub" ]]; then
2020+ echo -e "${RED}Missing: id_ed25519.pub${NC}"
2121+ fi
2222+ if [[ ! -f "${SSH_DIR}/id_ed25519_agenix" ]]; then
2323+ echo -e "${RED}Missing: id_ed25519_agenix${NC}"
2424+ fi
2525+ if [[ ! -f "${SSH_DIR}/id_ed25519_agenix.pub" ]]; then
2626+ echo -e "${RED}Missing: id_ed25519_agenix.pub${NC}"
2727+ fi
2828+ echo -e "${GREEN}Run the createKeys script to generate the missing keys.${NC}"
2929+ exit 1
3030+ fi
3131+}
3232+3333+check_keys
+71
apps/x86_64-linux/copy-keys
···11+#!/usr/bin/env bash
22+set -e
33+44+unmount_usb() {
55+ if mountpoint -q /mnt/usb; then
66+ sudo umount /mnt/usb
77+ echo -e "\e[0;32mUSB drive unmounted.\e[0m"
88+ fi
99+}
1010+1111+mount_usb() {
1212+ if mountpoint -q /mnt/usb; then
1313+ echo -e "\e[0;32mUSB drive already mounted.\e[0m"
1414+ else
1515+ device_found=false
1616+ for dev in sda sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl; do
1717+ if sudo blkid /dev/$dev | grep -iq 'TYPE="vfat"'; then
1818+ device_found=true
1919+ mkdir -p /mnt/usb
2020+ sudo mount /dev/$dev /mnt/usb && { echo -e "\e[0;32mUSB drive mounted successfully on /dev/$dev.\e[0m"; break; } || echo -e "\e[0;31mFailed to mount /dev/$dev.\e[0m"
2121+ fi
2222+ done
2323+ if [ "$device_found" = false ]; then
2424+ echo -e "\e[0;31mNo USB devices found.\e[0m"
2525+ fi
2626+ fi
2727+}
2828+2929+setup_ssh_directory() {
3030+ export SSH_DIR=/root/.ssh
3131+ mkdir -p $SSH_DIR
3232+}
3333+3434+check_file_exists() {
3535+ if [[ ! -f $1 ]]; then
3636+ echo -e "\e[0;31mError: File $1 does not exist.\e[0m"
3737+ exit 1
3838+ fi
3939+}
4040+4141+copy_keys() {
4242+ check_file_exists "/mnt/usb/id_ed25519_agenix.pub"
4343+ check_file_exists "/mnt/usb/id_ed25519_agenix"
4444+ cp /mnt/usb/id_ed25519_agenix.pub $SSH_DIR
4545+ cp /mnt/usb/id_ed25519_agenix $SSH_DIR
4646+ chmod 600 $SSH_DIR/id_ed25519_{agenix,agenix.pub}
4747+ echo -e "\e[0;32mKeys copied successfully.\e[0m"
4848+}
4949+5050+set_keys() {
5151+ check_file_exists "/mnt/usb/id_ed25519_github.pub"
5252+ check_file_exists "/mnt/usb/id_ed25519_github"
5353+ cp /mnt/usb/id_ed25519_github.pub $SSH_DIR/id_ed25519.pub
5454+ cp /mnt/usb/id_ed25519_github $SSH_DIR/id_ed25519
5555+ chmod 600 $SSH_DIR/id_ed25519
5656+ chmod 644 $SSH_DIR/id_ed25519.pub
5757+}
5858+5959+change_ownership() {
6060+ chown nixos:wheel $SSH_DIR/id_ed25519{,.pub}
6161+ chown nixos:wheel $SSH_DIR/id_ed25519_{agenix,agenix.pub}
6262+}
6363+6464+trap unmount_usb EXIT
6565+6666+setup_ssh_directory
6767+mount_usb
6868+copy_keys
6969+set_keys
7070+change_ownership
7171+unmount_usb
+27
apps/x86_64-linux/create-keys
···11+#!/usr/bin/env bash
22+set -e
33+44+RED='\033[0;31m'
55+GREEN='\033[0;32m'
66+NC='\033[0m'
77+88+# We're assuming this is being run as root in the NixOS installer
99+export SSH_DIR=/root/.ssh
1010+1111+setup_ssh_directory() {
1212+ mkdir -p ${SSH_DIR}
1313+}
1414+1515+generate_keys() {
1616+ ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519" -N ""
1717+ ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519_agenix" -N ""
1818+ chmod 600 ${SSH_DIR}/id_ed25519{,_agenix}{,.pub}
1919+}
2020+2121+setup_ssh_directory
2222+generate_keys
2323+2424+echo -e "${GREEN}New SSH keys have been generated.${NC}"
2525+echo -e "${GREEN}1) Add the id_ed25519 key to Github.${NC}"
2626+cat "${SSH_DIR}/id_ed25519.pub"
2727+echo -e "${GREEN}2) Create a private nix-secrets repo in Github, even if it's empty.${NC}"