NixOS-PVE-Deployment-Util/setup.sh

124 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e
# ANSI color codes
BLUE='\033[0;34m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
2024-12-10 04:42:21 -08:00
# 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
2024-12-10 04:42:21 -08:00
# Setup repository
REPO_DIR="/root/.nixos-utils"
if [ -d "$REPO_DIR" ]; then
cd "$REPO_DIR"
git pull
else
git clone https://git.jeirslab.xyz/jeirmeister/NixOS-PVE-LXC.git "$REPO_DIR"
cd "$REPO_DIR"
fi
2024-12-10 04:42:21 -08:00
# 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}"
echo -e "\nConfiguring NixOS container $CTID..."
2024-12-10 04:42:21 -08:00
setup_lxc_terminal "$CTID"
mount_nixos_config "$CTID"
2024-12-10 04:42:21 -08:00
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}"