Compare commits
2 Commits
2f0ef69d01
...
c3157a194f
Author | SHA1 | Date | |
---|---|---|---|
c3157a194f | |||
|
0865902a6f |
@ -1,16 +0,0 @@
|
||||
# Details: https://github.com/kirill-markin/repo-to-text
|
||||
# Syntax: gitignore rules
|
||||
|
||||
# Ignore files and directories for all sections from gitignore file
|
||||
# Default: True
|
||||
gitignore-import-and-ignore: True
|
||||
|
||||
# Ignore files and directories for tree
|
||||
# and "Contents of ..." sections
|
||||
ignore-tree-and-content:
|
||||
- ".repo-to-text-settings.yaml"
|
||||
|
||||
# Ignore files and directories for "Contents of ..." section
|
||||
ignore-content:
|
||||
- "README.md"
|
||||
- "LICENSE"
|
168
scripts/nixos-pve-config
Normal file → Executable file
168
scripts/nixos-pve-config
Normal file → Executable file
@ -7,8 +7,15 @@ GREEN='\033[0;32m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m'
|
||||
|
||||
# Function to list and select NixOS containers
|
||||
list_nixos_containers() {
|
||||
# Check for root
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo -e "${RED}This script must be run as root${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
REPO_DIR="/root/.nixos-utils"
|
||||
|
||||
# List and select NixOS containers
|
||||
declare -a valid_ctids
|
||||
echo -e "${BLUE}Available NixOS Containers:${NC}"
|
||||
printf "%-8s %s\n" "VMID" "Name"
|
||||
@ -25,56 +32,98 @@ list_nixos_containers() {
|
||||
|
||||
if [ ${#valid_ctids[@]} -eq 0 ]; then
|
||||
echo -e "${RED}No NixOS containers found!${NC}" >&2
|
||||
return 1
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Container selection
|
||||
while true; do
|
||||
echo -e "\nEnter the CTID of the container you want to configure:"
|
||||
read -r selected_ctid
|
||||
read -r CTID
|
||||
|
||||
valid_selection=0
|
||||
for ctid in "${valid_ctids[@]}"; do
|
||||
if [ "$ctid" = "$selected_ctid" ]; then
|
||||
echo "$selected_ctid"
|
||||
return 0
|
||||
if [ "$ctid" = "$CTID" ]; then
|
||||
valid_selection=1
|
||||
break
|
||||
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"
|
||||
if [ $valid_selection -eq 1 ]; then
|
||||
break
|
||||
else
|
||||
sed -i "s|^${search}.*|${replace}|" "$file"
|
||||
echo -e "${RED}Error: Invalid CTID selected! Please try again.${NC}" >&2
|
||||
fi
|
||||
fi
|
||||
}
|
||||
done
|
||||
|
||||
# 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"
|
||||
echo -e "${GREEN}Selected container: $CTID${NC}"
|
||||
|
||||
# Password setup prompt
|
||||
echo -e "\nWould you like to set a password for the 'nixos' user? (y/n)"
|
||||
read -r set_password
|
||||
|
||||
if [[ "$set_password" =~ ^[Yy]$ ]]; then
|
||||
while true; do
|
||||
echo -e "\nEnter new password for 'nixos' user:"
|
||||
read -s password
|
||||
echo -e "\nConfirm password:"
|
||||
read -s password_confirm
|
||||
|
||||
if [ "$password" = "$password_confirm" ]; then
|
||||
echo -e "\n${GREEN}Password confirmed${NC}"
|
||||
break
|
||||
else
|
||||
echo -e "\n${RED}Passwords do not match. Please try again.${NC}"
|
||||
echo -e "Press Enter to continue or Ctrl+C to exit"
|
||||
read -r
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo -e "\nConfiguring NixOS container $CTID..."
|
||||
|
||||
# Setup LXC terminal configuration
|
||||
PVE_CONFIG="/etc/pve/lxc/${CTID}.conf"
|
||||
LXC_CONFIG="/var/lib/lxc/${CTID}/config"
|
||||
|
||||
# Update PVE config
|
||||
if [ -f "$PVE_CONFIG" ]; then
|
||||
if ! grep -q "^lxc.init_cmd:" "$PVE_CONFIG"; then
|
||||
echo "lxc.init_cmd: /run/current-system/sw/bin/bash" >> "$PVE_CONFIG"
|
||||
else
|
||||
sed -i "s|^lxc.init_cmd:.*|lxc.init_cmd: /run/current-system/sw/bin/bash|" "$PVE_CONFIG"
|
||||
fi
|
||||
|
||||
if ! grep -q "^cmode:" "$PVE_CONFIG"; then
|
||||
echo "cmode: shell" >> "$PVE_CONFIG"
|
||||
else
|
||||
sed -i "s|^cmode:.*|cmode: shell|" "$PVE_CONFIG"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Update LXC config
|
||||
if [ -f "$LXC_CONFIG" ]; then
|
||||
if ! grep -q "^lxc.init.cmd" "$LXC_CONFIG"; then
|
||||
echo "lxc.init.cmd = /run/current-system/sw/bin/bash" >> "$LXC_CONFIG"
|
||||
else
|
||||
sed -i "s|^lxc.init.cmd.*|lxc.init.cmd = /run/current-system/sw/bin/bash|" "$LXC_CONFIG"
|
||||
fi
|
||||
|
||||
if ! grep -q "^lxc.environment = TERM" "$LXC_CONFIG"; then
|
||||
echo "lxc.environment = TERM=linux" >> "$LXC_CONFIG"
|
||||
else
|
||||
sed -i "s|^lxc.environment = TERM.*|lxc.environment = TERM=linux|" "$LXC_CONFIG"
|
||||
fi
|
||||
|
||||
if ! grep -q "^lxc.environment = PATH" "$LXC_CONFIG"; then
|
||||
echo "lxc.environment = PATH=/run/current-system/sw/bin" >> "$LXC_CONFIG"
|
||||
else
|
||||
sed -i "s|^lxc.environment = PATH.*|lxc.environment = PATH=/run/current-system/sw/bin|" "$LXC_CONFIG"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Mount and copy configs
|
||||
MOUNT_POINT="/root/nixos-config-${CTID}"
|
||||
CONFIG_FILE="/etc/pve/lxc/${CTID}.conf"
|
||||
|
||||
ROOTFS_LINE=$(grep "^rootfs:" "$CONFIG_FILE")
|
||||
VOLUME_NAME=$(echo "$ROOTFS_LINE" | sed 's/rootfs: local-lvm:\([^,]*\).*/\1/')
|
||||
@ -84,39 +133,16 @@ mount_nixos_config() {
|
||||
mount "$DEVICE_PATH" "$MOUNT_POINT"
|
||||
mkdir -p "${MOUNT_POINT}/etc/nixos"
|
||||
cp -r "${REPO_DIR}/nix-config/"* "${MOUNT_POINT}/etc/nixos/"
|
||||
|
||||
# Set password if requested
|
||||
if [[ "$set_password" =~ ^[Yy]$ ]]; then
|
||||
pct exec ${CTID} -- echo "nixos:${password}" | chpasswd
|
||||
fi
|
||||
|
||||
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"
|
||||
|
||||
# Rebuild NixOS configuration
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user