From 5fd43a04ff62bdcecd73d6d2ca7e9d67fdd60a6c Mon Sep 17 00:00:00 2001 From: root Date: Tue, 10 Dec 2024 05:08:06 -0800 Subject: [PATCH] (fix) deleted unnescessary scripts, converted installation to binary method --- README.md | 23 ++++--- scripts/list-lxcs.sh | 49 -------------- scripts/mount-nixos-config.sh | 39 ----------- scripts/nixos-pve-config | 124 ++++++++++++++++++++++++++++++++++ scripts/rebuild-nixos.sh | 17 ----- scripts/setup-lxc-terminal.sh | 37 ---------- setup.sh | 116 +++---------------------------- 7 files changed, 146 insertions(+), 259 deletions(-) delete mode 100644 scripts/list-lxcs.sh delete mode 100644 scripts/mount-nixos-config.sh create mode 100644 scripts/nixos-pve-config delete mode 100644 scripts/rebuild-nixos.sh delete mode 100644 scripts/setup-lxc-terminal.sh diff --git a/README.md b/README.md index 4eb44db..9ea7b52 100644 --- a/README.md +++ b/README.md @@ -16,18 +16,19 @@ A comprehensive toolkit for managing NixOS containers in Proxmox VE. This reposi ## Quick Installation ```bash -curl -sSf https://git.jeirslab.xyz/jeirmeister/NixOS-PVE-Deployment-Util/raw/branch/main/setup.sh | bash +curl -sSf https://git.jeirslab.xyz/jeirmeister/NixOS-PVE-LXC/raw/branch/main/setup.sh | bash ``` ## What It Does The installation script will: 1. Install required dependencies (git) -2. Clone/update the configuration repository -3. Detect available NixOS containers -4. Apply optimized configuration settings -5. Configure proper LXC terminal access -6. Set up system maintenance tasks +2. Clone/update the configuration repository to `/root/.nixos-utils` +3. Install the configuration utility system-wide +4. Detect and list available NixOS containers +5. Apply optimized configuration settings +6. Configure proper LXC terminal access +7. Set up system maintenance tasks ## Manual Installation @@ -49,10 +50,8 @@ cd NixOS-PVE-LXC │ ├── network.nix # Network configuration │ └── users.nix # User and SSH settings └── scripts/ # Utility scripts - ├── list-lxcs.sh # Container detection - ├── mount-nixos-config.sh # Configuration deployment - ├── rebuild-nixos.sh # System rebuild - └── setup-lxc-terminal.sh # Terminal setup + ├── get-latest.sh # Download latest NixOS builds + └── post-deploy.sh # Post-deployment tasks ``` ## Features in Detail @@ -84,6 +83,10 @@ cd NixOS-PVE-LXC Contributions are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests. +## License + +MIT License - See LICENSE.md for details + ## Note Always review scripts before running them with curl. You can inspect all source code at the repository. \ No newline at end of file diff --git a/scripts/list-lxcs.sh b/scripts/list-lxcs.sh deleted file mode 100644 index 5bd0616..0000000 --- a/scripts/list-lxcs.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash - -# ANSI color codes -BLUE='\033[0;34m' -RED='\033[0;31m' -NC='\033[0m' - -# Array to store valid CTIDs -declare -a valid_ctids - -echo -e "${BLUE}Available NixOS Containers:${NC}" -printf "%-8s %s\n" "VMID" "Name" -echo "----------------" - -# Populate the list and store valid CTIDs -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 - exit 1 -fi - -# Ask for user input -echo -e "\nEnter the CTID of the container you want to configure:" -read -r selected_ctid - -# Validate input -valid=0 -for ctid in "${valid_ctids[@]}"; do - if [ "$ctid" = "$selected_ctid" ]; then - valid=1 - break - fi -done - -if [ $valid -eq 0 ]; then - echo -e "${RED}Error: Invalid CTID selected!${NC}" >&2 - exit 1 -fi - -# Output the selected CTID for use in other scripts -echo "$selected_ctid" \ No newline at end of file diff --git a/scripts/mount-nixos-config.sh b/scripts/mount-nixos-config.sh deleted file mode 100644 index eba429e..0000000 --- a/scripts/mount-nixos-config.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -set -e - -if [ -z "$1" ]; then - echo "Usage: $0 " - exit 1 -fi - -CTID="$1" -CONFIG_FILE="/etc/pve/lxc/${CTID}.conf" -MOUNT_POINT="/root/nixos-config-${CTID}" -NIX_CONFIG_DIR="./nix-config" - -if [ ! -d "$NIX_CONFIG_DIR" ]; then - echo "Error: nix-config directory not found" - exit 1 -fi - -# Parse container config and mount -echo "Parsing container configuration..." -ROOTFS_LINE=$(grep "^rootfs:" "$CONFIG_FILE") -VOLUME_NAME=$(echo "$ROOTFS_LINE" | sed 's/rootfs: local-lvm:\([^,]*\).*/\1/') -DEVICE_PATH="/dev/pve/${VOLUME_NAME}" - -echo "Creating mount point..." -mkdir -p "$MOUNT_POINT" - -echo "Mounting container filesystem..." -mount "$DEVICE_PATH" "$MOUNT_POINT" - -echo "Copying NixOS configuration..." -mkdir -p "${MOUNT_POINT}/etc/nixos" -cp -r ${NIX_CONFIG_DIR}/* "${MOUNT_POINT}/etc/nixos/" - -echo "Unmounting container filesystem..." -umount "$MOUNT_POINT" -rmdir "$MOUNT_POINT" - -echo "Configuration files copied successfully" \ No newline at end of file diff --git a/scripts/nixos-pve-config b/scripts/nixos-pve-config new file mode 100644 index 0000000..001fdce --- /dev/null +++ b/scripts/nixos-pve-config @@ -0,0 +1,124 @@ +#!/bin/bash +set -e + +# ANSI color codes +BLUE='\033[0;34m' +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' + +# 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 + +# 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 + +# 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..." + +setup_lxc_terminal "$CTID" +mount_nixos_config "$CTID" + +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 diff --git a/scripts/rebuild-nixos.sh b/scripts/rebuild-nixos.sh deleted file mode 100644 index 18ffa9f..0000000 --- a/scripts/rebuild-nixos.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -set -e - -if [ -z "$1" ]; then - echo "Usage: $0 " - exit 1 -fi - -CTID="$1" - -echo "Updating channels..." -pct exec ${CTID} -- nix-channel --update - -echo "Rebuilding NixOS configuration..." -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 "NixOS rebuild completed successfully" \ No newline at end of file diff --git a/scripts/setup-lxc-terminal.sh b/scripts/setup-lxc-terminal.sh deleted file mode 100644 index 7a0ec90..0000000 --- a/scripts/setup-lxc-terminal.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash -set -e - -if [ -z "$1" ]; then - echo "Usage: $0 " - exit 1 -fi - -CTID="$1" - -# Function to add or update line in config file -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 -} - -# Update Proxmox LXC config -PVE_CONFIG="/etc/pve/lxc/${CTID}.conf" -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 LXC system config -LXC_CONFIG="/var/lib/lxc/${CTID}/config" -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" - -echo "LXC terminal configuration updated successfully" \ No newline at end of file diff --git a/setup.sh b/setup.sh index 001fdce..011a172 100755 --- a/setup.sh +++ b/setup.sh @@ -1,124 +1,26 @@ #!/bin/bash set -e -# ANSI color codes -BLUE='\033[0;34m' -GREEN='\033[0;32m' -RED='\033[0;31m' -NC='\033[0m' - -# 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 +# Check if running as root if [ "$(id -u)" -ne 0 ]; then echo -e "${RED}This script must be run as root${NC}" exit 1 fi -# Setup repository +# Install mode +INSTALL_DIR="/usr/local/bin" REPO_DIR="/root/.nixos-utils" + +# Clone/update repository 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 -# Select and configure container -CTID=$(list_nixos_containers) -if [ -z "$CTID" ]; then - echo -e "${RED}No container ID was selected${NC}" - exit 1 -fi +# Install the configuration script +ln -sf "$REPO_DIR/scripts/nixos-pve-config" "${INSTALL_DIR}/nixos-pve-config" +chmod +x "${INSTALL_DIR}/nixos-pve-config" -echo -e "${GREEN}Selected container: $CTID${NC}" -echo -e "\nConfiguring NixOS container $CTID..." - -setup_lxc_terminal "$CTID" -mount_nixos_config "$CTID" - -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 +echo "Installation complete. Run 'nixos-pve-config' to configure containers." \ No newline at end of file