198 lines
5.2 KiB
Nix
198 lines
5.2 KiB
Nix
# vm.nix
|
|
let
|
|
pkgs = import <nixpkgs> {};
|
|
in
|
|
|
|
let
|
|
# 1. SOURCE YOUR DEBIAN IMAGE
|
|
debianImage = pkgs.fetchurl {
|
|
name = "debian-13-genericcloud-amd64.qcow2";
|
|
url = "https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2";
|
|
hash = "sha256-V9a6DUghJB5j4Vz9Y5aikZ8syhx5Zn2593m8/8xNwIU=";
|
|
};
|
|
|
|
# 2. VM DATA DIRECTORY
|
|
vmDataDir = "/home/${builtins.getEnv "USER"}/.local/share/debian-vm";
|
|
|
|
# 3. CREATE ALL FILES IN A SCRIPT
|
|
setupScript = pkgs.writeShellScriptBin "setup-vm-files" ''
|
|
set -e
|
|
|
|
# Create VM directory
|
|
mkdir -p "${vmDataDir}"
|
|
|
|
# Create persistent disk if it doesn't exist (with 20GB initial size)
|
|
if [ ! -f "${vmDataDir}/debian-persistent.qcow2" ]; then
|
|
echo "Creating persistent disk from base image..."
|
|
${pkgs.qemu}/bin/qemu-img create -f qcow2 \
|
|
-b "${debianImage}" \
|
|
-F qcow2 \
|
|
"${vmDataDir}/debian-persistent.qcow2" \
|
|
20G
|
|
fi
|
|
|
|
# Create cloud-init configuration
|
|
cat > "${vmDataDir}/user-data" << 'EOF'
|
|
#cloud-config
|
|
# User configuration
|
|
users:
|
|
- name: debian
|
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
|
groups: sudo, users, adm
|
|
shell: /bin/bash
|
|
lock_passwd: false
|
|
passwd: nixos
|
|
system: false
|
|
create_groups: true
|
|
|
|
# Enable password authentication
|
|
ssh_pwauth: true
|
|
chpasswd:
|
|
expire: false
|
|
list: |
|
|
debian:nixos
|
|
root:nixos
|
|
|
|
# Update system
|
|
package_update: true
|
|
package_upgrade: true
|
|
|
|
# Install packages
|
|
packages:
|
|
- git
|
|
- curl
|
|
- wget
|
|
|
|
# Run commands - FIXED: Moved all setup to runcmd instead of write_files
|
|
runcmd:
|
|
# Configure sudo
|
|
- [sed, -i, '/^%sudo/s/ALL$/NOPASSWD:ALL/', /etc/sudoers]
|
|
|
|
# Create log directory
|
|
- [mkdir, -p, /var/log]
|
|
|
|
# Update and install packages
|
|
- [apt-get, update]
|
|
- [apt-get, install, -y, git, curl, wget]
|
|
|
|
# Set up user environment
|
|
- [chown, -R, debian:debian, /home/debian]
|
|
- [chmod, 700, /home/debian]
|
|
|
|
# Execute setup as debian user - FIXED: All commands in one script
|
|
- |
|
|
su - debian -c '
|
|
set -e
|
|
echo "Starting first-boot setup as debian user..."
|
|
|
|
# Clone repository
|
|
cd /home/debian
|
|
echo "Cloning GRCon23Tutorial repository..."
|
|
git clone https://github.com/ARDC-TOBB-ETU/GRCon23Tutorial --depth=1
|
|
|
|
# Run installation
|
|
cd GRCon23Tutorial
|
|
echo "Running installation script..."
|
|
chmod +x install.sh
|
|
./install.sh
|
|
|
|
# Source conda and start Jupyter Lab
|
|
echo "Setting up conda environment and starting Jupyter Lab..."
|
|
source "/home/debian/conda/etc/profile.d/conda.sh"
|
|
conda activate GRCon23
|
|
jupyter-lab --ip=0.0.0.0 --port=8888 --no-browser &
|
|
JUPYTER_PID=$!
|
|
echo $JUPYTER_PID > /tmp/jupyter.pid
|
|
echo "Jupyter Lab started with PID: $JUPYTER_PID"
|
|
|
|
echo "First-boot setup completed!"
|
|
echo "Jupyter Lab running on port 8888"
|
|
'
|
|
|
|
# Create marker file
|
|
- [touch, /etc/vm-initialized]
|
|
- [echo, "VM setup completed at $(date)", ">>", /var/log/vm-init.log]
|
|
- [echo, "Jupyter Lab should be running on port 8888", ">>", /var/log/vm-init.log]
|
|
|
|
# Final message
|
|
final_message: |
|
|
VM initialization complete!
|
|
|
|
Setup completed:
|
|
1. Git cloned GRCon23Tutorial repository
|
|
2. Ran ./install.sh
|
|
3. Started Jupyter Lab on port 8888
|
|
|
|
Access:
|
|
SSH: ssh debian@localhost -p 2222
|
|
Password: nixos
|
|
Jupyter Lab: http://localhost:8888 (forwarded from VM port 8888)
|
|
|
|
Check status in VM:
|
|
sudo journalctl -u cloud-init
|
|
cat /var/log/vm-init.log
|
|
EOF
|
|
|
|
# Create cloud-init ISO
|
|
${pkgs.cloud-utils}/bin/cloud-localds \
|
|
"${vmDataDir}/cloud-init.iso" \
|
|
"${vmDataDir}/user-data"
|
|
|
|
echo "VM files created in ${vmDataDir}"
|
|
echo "First boot will:"
|
|
echo "1. Clone GRCon23Tutorial repository"
|
|
echo "2. Run ./install.sh"
|
|
echo "3. Start Jupyter Lab on port 8888"
|
|
echo ""
|
|
echo "Access Jupyter at: http://localhost:8888"
|
|
'';
|
|
|
|
# 4. LAUNCH SCRIPT WITH PORT FORWARDING
|
|
runScript = pkgs.writeShellScriptBin "run-debian-vm" ''
|
|
set -e
|
|
|
|
# Run setup first
|
|
${setupScript}/bin/setup-vm-files
|
|
|
|
# Check if VM is already running
|
|
if pgrep -f "debian-persistent.qcow2" > /dev/null; then
|
|
echo "VM appears to be already running. Stopping it first..."
|
|
pkill -f "debian-persistent.qcow2"
|
|
sleep 2
|
|
fi
|
|
|
|
echo "Starting Debian VM..."
|
|
echo ""
|
|
echo "First boot will:"
|
|
echo "1. Clone GRCon23Tutorial repository"
|
|
echo "2. Run ./install.sh"
|
|
echo "3. Start Jupyter Lab on port 8888"
|
|
echo ""
|
|
echo "Access after boot:"
|
|
echo " SSH: ssh debian@localhost -p 2222"
|
|
echo " Jupyter: http://localhost:8888"
|
|
echo " Password for 'debian' user: nixos"
|
|
echo ""
|
|
echo "Press Ctrl+Alt+G to release mouse from VM window"
|
|
|
|
# Launch QEMU with port 8888 forwarded
|
|
${pkgs.qemu_kvm}/bin/qemu-kvm \
|
|
-name "Debian-GRCon-VM" \
|
|
-machine accel=kvm \
|
|
-cpu host \
|
|
-smp 2 \
|
|
-m 2048 \
|
|
-drive file="${vmDataDir}/debian-persistent.qcow2",format=qcow2,if=virtio \
|
|
-drive file="${vmDataDir}/cloud-init.iso",format=raw,if=virtio \
|
|
-netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::8888-:8888 \
|
|
-device virtio-net-pci,netdev=n1 \
|
|
-display gtk \
|
|
-vga virtio \
|
|
"$@"
|
|
'';
|
|
|
|
in
|
|
{
|
|
system.build.customVM = runScript;
|
|
}
|