diff --git a/configuration.nix b/configuration.nix index 9182872..08678b9 100644 --- a/configuration.nix +++ b/configuration.nix @@ -60,6 +60,7 @@ in { # Networking networking = { + hostName = jsonConfig.hostname; firewall = { enable = true; # Only allow necessary ports diff --git a/flake.nix b/flake.nix index 041ea94..0ad824b 100644 --- a/flake.nix +++ b/flake.nix @@ -42,8 +42,8 @@ inherit self; }; }; - deploy.nodes.server = { - hostname = "self-host-playbook"; + deploy.nodes.${jsonConfig.hostname} = { + hostname = jsonConfig.hostname; profiles.system = { sshUser = jsonConfig.username; user = "root"; diff --git a/install.sh b/install.sh index a265126..8f5ee02 100755 --- a/install.sh +++ b/install.sh @@ -279,10 +279,18 @@ echo "" echo "📝 Please provide the following information:" echo "-------------------------------------------" read -p "1. Enter target server IP address: " IP_ADDRESS -read -p "2. Enter desired username for server access: " USERNAME -read -s -p "3. Enter desired password: " PASSWORD +read -p "2. Enter hostname for the server: " HOSTNAME + +# Validate hostname format +while ! [[ $HOSTNAME =~ ^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$ ]]; do + echo "❌ Invalid hostname format. Please use a valid hostname (e.g., my-server.example.com)" + read -p "Enter hostname for the server: " HOSTNAME +done + +read -p "3. Enter desired username for server access: " USERNAME +read -s -p "4. Enter desired password: " PASSWORD echo -echo "4. Enter domain names for services (must point to $IP_ADDRESS):" +echo "5. Enter domain names for services (must point to $IP_ADDRESS):" read -p " - Domain for Portainer: " PORTAINER_DOMAIN read -p " - Domain for n8n: " N8N_DOMAIN read -p " - Domain for Baserow: " BASEROW_DOMAIN @@ -296,10 +304,10 @@ read -p "Enter your choice (1-2): " KEY_CHOICE case $KEY_CHOICE in 1) - INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server root@$IP_ADDRESS" + INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#$HOSTNAME root@$IP_ADDRESS" ;; 2) - INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server -i $SSH_KEY_PATH root@$IP_ADDRESS" + INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#$HOSTNAME -i $SSH_KEY_PATH root@$IP_ADDRESS" ;; *) echo "❌ Invalid choice" diff --git a/update.sh b/update.sh index ee447e1..6a86d9d 100644 --- a/update.sh +++ b/update.sh @@ -153,24 +153,25 @@ setup_ssh_config() { chmod 600 "$ssh_config_file" } -update_config_json() { - local ip_address=$1 +update_config_value() { + local key=$1 + local value=$2 local config_file="config.json" # Read existing config local config config=$(cat "$config_file") - # Update or add ipAddress field - if jq -e '.ipAddress' "$config_file" >/dev/null 2>&1; then - config=$(echo "$config" | jq --arg ip "$ip_address" '.ipAddress = $ip') + # Update or add the field + if jq -e ".$key" "$config_file" >/dev/null 2>&1; then + config=$(echo "$config" | jq --arg key "$key" --arg value "$value" '.[$key] = $value') else - config=$(echo "$config" | jq --arg ip "$ip_address" '. + {ipAddress: $ip}') + config=$(echo "$config" | jq --arg key "$key" --arg value "$value" '. + {($key): $value}') fi # Write back to file echo "$config" | jq '.' > "$config_file" - echo "✅ Updated IP address in config.json" + echo "✅ Updated $key in config.json" } install_deploy_rs() { @@ -204,6 +205,7 @@ fi USERNAME=$(jq -r '.username' config.json) IP_ADDRESS=$(jq -r '.ipAddress // empty' config.json) +HOSTNAME=$(jq -r '.hostname // empty' config.json) if [ -z "$USERNAME" ]; then echo "❌ Error: Could not read username from config.json" @@ -222,7 +224,22 @@ if [ -z "$IP_ADDRESS" ]; then fi # Update config.json with the new IP address - update_config_json "$IP_ADDRESS" + update_config_value "ipAddress" "$IP_ADDRESS" +fi + +# If hostname is not in config.json, prompt for it +if [ -z "$HOSTNAME" ]; then + echo "â„šī¸ No hostname found in config.json" + read -p "Enter the hostname for your server: " HOSTNAME + + # Validate hostname format (basic validation) + if ! [[ $HOSTNAME =~ ^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$ ]]; then + echo "❌ Error: Invalid hostname format" + exit 1 + fi + + # Update config.json with the new hostname + update_config_value "hostname" "$HOSTNAME" fi VERSION_FILE="version.json" @@ -267,7 +284,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then echo install_deploy_rs echo "🚀 Applying the update to your system..." - deploy .#server + deploy .#$HOSTNAME echo echo "If you encounter any issues, your backup is available in $backup_dir" else