diff --git a/setup.sh b/setup.sh index e5168fd..001fdce 100755 --- a/setup.sh +++ b/setup.sh @@ -7,59 +7,118 @@ GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' -# Check if running as root +# Function to list and select NixOS containers +list_nixos_containers() { + declare -a valid_ctids + echo -e "${BLUE}Available NixOS Containers:${NC}" + printf "%-8s %s\n" "VMID" "Name" + echo "----------------" + + for conf in /etc/pve/lxc/*.conf; do + if grep -q "^ostype: nixos" "$conf"; then + vmid=$(basename "$conf" .conf) + name=$(grep "^hostname:" "$conf" | cut -d' ' -f2) + printf "%-8s %s\n" "$vmid" "$name" + valid_ctids+=("$vmid") + fi + done + + if [ ${#valid_ctids[@]} -eq 0 ]; then + echo -e "${RED}No NixOS containers found!${NC}" >&2 + return 1 + fi + + echo -e "\nEnter the CTID of the container you want to configure:" + read -r selected_ctid + + for ctid in "${valid_ctids[@]}"; do + if [ "$ctid" = "$selected_ctid" ]; then + echo "$selected_ctid" + return 0 + fi + done + + echo -e "${RED}Error: Invalid CTID selected!${NC}" >&2 + return 1 +} + +# Function to setup LXC terminal +setup_lxc_terminal() { + local CTID="$1" + local PVE_CONFIG="/etc/pve/lxc/${CTID}.conf" + local LXC_CONFIG="/var/lib/lxc/${CTID}/config" + + update_config_line "$PVE_CONFIG" "lxc.init_cmd:" "lxc.init_cmd: /run/current-system/sw/bin/bash" + update_config_line "$PVE_CONFIG" "cmode:" "cmode: shell" + update_config_line "$LXC_CONFIG" "lxc.init.cmd" "lxc.init.cmd = /run/current-system/sw/bin/bash" + update_config_line "$LXC_CONFIG" "lxc.environment = TERM" "lxc.environment = TERM=linux" + update_config_line "$LXC_CONFIG" "lxc.environment = PATH" "lxc.environment = PATH=/run/current-system/sw/bin" +} + +# Function to update config lines +update_config_line() { + local file="$1" + local search="$2" + local replace="$3" + + if [ -f "$file" ]; then + if ! grep -q "^${search}" "$file"; then + echo "${replace}" >> "$file" + else + sed -i "s|^${search}.*|${replace}|" "$file" + fi + fi +} + +# Function to mount and copy configs +mount_nixos_config() { + local CTID="$1" + local MOUNT_POINT="/root/nixos-config-${CTID}" + local CONFIG_FILE="/etc/pve/lxc/${CTID}.conf" + + ROOTFS_LINE=$(grep "^rootfs:" "$CONFIG_FILE") + VOLUME_NAME=$(echo "$ROOTFS_LINE" | sed 's/rootfs: local-lvm:\([^,]*\).*/\1/') + DEVICE_PATH="/dev/pve/${VOLUME_NAME}" + + mkdir -p "$MOUNT_POINT" + mount "$DEVICE_PATH" "$MOUNT_POINT" + mkdir -p "${MOUNT_POINT}/etc/nixos" + cp -r "${REPO_DIR}/nix-config/"* "${MOUNT_POINT}/etc/nixos/" + umount "$MOUNT_POINT" + rmdir "$MOUNT_POINT" +} + +# Main execution if [ "$(id -u)" -ne 0 ]; then echo -e "${RED}This script must be run as root${NC}" exit 1 fi -# Check and install git if needed -if ! command -v git >/dev/null 2>&1; then - echo "Installing git..." - nix-env -i git -fi - +# Setup repository REPO_DIR="/root/.nixos-utils" - -# Clone or update repository if [ -d "$REPO_DIR" ]; then - echo "Updating existing repository..." cd "$REPO_DIR" git pull else - echo "Cloning repository..." git clone https://git.jeirslab.xyz/jeirmeister/NixOS-PVE-LXC.git "$REPO_DIR" cd "$REPO_DIR" fi -# Make scripts executable -echo "Making scripts executable..." -find ./scripts -name "*.sh" -execdir chmod u+x {} + - -# Get CTID from list-lxcs.sh -echo -e "${BLUE}Detecting NixOS containers...${NC}" -CTID=$(./scripts/list-lxcs.sh) - +# Select and configure container +CTID=$(list_nixos_containers) if [ -z "$CTID" ]; then echo -e "${RED}No container ID was selected${NC}" exit 1 fi echo -e "${GREEN}Selected container: $CTID${NC}" - -# Execute configuration steps in sequence echo -e "\nConfiguring NixOS container $CTID..." -echo "Step 1: Setting up LXC terminal..." -./scripts/setup-lxc-terminal.sh "$CTID" +setup_lxc_terminal "$CTID" +mount_nixos_config "$CTID" -echo "Step 2: Mounting and copying NixOS configuration..." -./scripts/mount-nixos-config.sh "$CTID" - -echo "Step 3: Rebuilding NixOS configuration..." -./scripts/rebuild-nixos.sh "$CTID" - -echo "Step 4: Running post-deployment tasks..." -./scripts/post-deploy.sh +echo "Rebuilding NixOS configuration..." +pct exec ${CTID} -- nix-channel --update +pct exec ${CTID} -- nixos-rebuild switch -I nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos -I nixos-config=/etc/nixos/configuration.nix echo -e "${GREEN}NixOS container $CTID has been configured successfully!${NC}" \ No newline at end of file