#!/usr/bin/env bash set -euo pipefail gum style \ --foreground 212 \ --bold \ --border normal \ --align center \ --width 50 \ --margin "1 2" \ "🔧 Add Custom Service" SERVICE_NAME="$(gum input --placeholder "Enter service name" --prompt "Service Name: ")" [[ -z "${SERVICE_NAME}" ]] && { gum style --foreground 1 "⚠️ Service name cannot be empty"; exit 1; } SUBDOMAIN="$(gum input --placeholder "Enter subdomain" --prompt "Subdomain: ")" [[ -z "${SUBDOMAIN}" ]] && { gum style --foreground 1 "⚠️ Subdomain cannot be empty"; exit 1; } PORT="$(gum input --placeholder "Enter port number" --prompt "Port: ")" [[ ! "${PORT}" =~ ^[0-9]+$ ]] && { gum style --foreground 1 "⚠️ Port must be a number"; exit 1; } gum confirm "Is ${SUBDOMAIN} the correct domain?" || { gum style --foreground 1 "❌ Operation cancelled"; exit 0; } CONFIG_DIR="/etc/nixos/current-systemconfig" SERVICES_DIR="${CONFIG_DIR}/custom-services" # Create directories if they don't exist mkdir -p "${SERVICES_DIR}" FILE_PATH="${SERVICES_DIR}/${SERVICE_NAME}.nix" cat > "${FILE_PATH}" << EOF { services.caddy.virtualHosts.${SUBDOMAIN} = { extraConfig = '' reverse_proxy localhost:${PORT} header { Strict-Transport-Security "max-age=31536000; includeSubDomains" X-Content-Type-Options "nosniff" X-Frame-Options "DENY" Referrer-Policy "strict-origin-when-cross-origin" } ''; }; } EOF if [[ $? -eq 0 ]]; then gum style --foreground 212 "✅ Service file created successfully at ${FILE_PATH}" # Initialize Git repo if it doesn't exist if [[ ! -d "${CONFIG_DIR}/.git" ]]; then cd "${CONFIG_DIR}" gum spin --spinner dot --title "Initializing Git repository..." -- git init fi gum spin --spinner dot --title "Adding all files..." -- git add . gum spin --spinner dot --title "Creating initial commit..." -- git commit -m "Initial commit" # NixOS rebuild cd "${CONFIG_DIR}" gum spin --spinner dot --title "Rebuilding NixOS..." -- \ nixos-rebuild switch --flake .#nixos gum style --foreground 212 "✅ Service deployed successfully!" else gum style --foreground 1 "❌ Failed to create service file" exit 1 fi