Compare commits
8 Commits
f41b7159d3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e5ae18155a | |||
| c330054da6 | |||
| 8315388aee | |||
| 879194c05c | |||
| 198149fca0 | |||
| 455e54f81b | |||
| 31e48ff3fc | |||
| 7702d4914d |
11
README.md
11
README.md
@@ -3,6 +3,10 @@ Nix VM with Debian + GUI
|
||||
Username: `debian`
|
||||
Password: `nixos`
|
||||
|
||||
0. Clean the VM:
|
||||
`nh clean all`
|
||||
`rm ~/.local/share/debian-vm/debian-persistent.qcow2`
|
||||
|
||||
1. Build the VM:
|
||||
```bash
|
||||
nix-build vm.nix -A system.build.customVM
|
||||
@@ -11,3 +15,10 @@ nix-build vm.nix -A system.build.customVM
|
||||
```bash
|
||||
./result/bin/run-debian-vm
|
||||
```
|
||||
3. Watch it unravel:
|
||||
```bash
|
||||
ssh debian@localhost -p 2222
|
||||
# Inside VM after SSH
|
||||
watch -n 1 "cat /var/log/cloud-init-output.log | tail"
|
||||
```
|
||||
4. After a while, the Jupyter URL will spawn
|
||||
|
||||
157
vm.nix
157
vm.nix
@@ -8,7 +8,7 @@ let
|
||||
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="; # REPLACE THIS
|
||||
hash = "sha256-V9a6DUghJB5j4Vz9Y5aikZ8syhx5Zn2593m8/8xNwIU=";
|
||||
};
|
||||
|
||||
# 2. VM DATA DIRECTORY
|
||||
@@ -21,32 +21,117 @@ let
|
||||
# Create VM directory
|
||||
mkdir -p "${vmDataDir}"
|
||||
|
||||
# Create persistent disk if it doesn't exist
|
||||
# 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"
|
||||
"${vmDataDir}/debian-persistent.qcow2" \
|
||||
20G
|
||||
fi
|
||||
|
||||
# Create cloud-init configuration
|
||||
cat > "${vmDataDir}/user-data" << 'EOF'
|
||||
#cloud-config
|
||||
password: nixos
|
||||
chpasswd: { expire: False }
|
||||
ssh_pwauth: True
|
||||
runcmd:
|
||||
- |
|
||||
if [ ! -f /etc/vm-initialized ]; then
|
||||
cd /home/debian
|
||||
sudo -u debian git clone https://github.com/someone/something
|
||||
cd something
|
||||
chmod +x install.sh
|
||||
sudo -u debian ./install.sh
|
||||
touch /etc/vm-initialized
|
||||
fi
|
||||
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 \
|
||||
@@ -54,24 +139,52 @@ let
|
||||
"${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
|
||||
# 4. LAUNCH SCRIPT WITH PORT FORWARDING
|
||||
runScript = pkgs.writeShellScriptBin "run-debian-vm" ''
|
||||
set -e
|
||||
|
||||
# Run setup first
|
||||
${setupScript}/bin/setup-vm-files
|
||||
|
||||
# Launch QEMU with KVM acceleration
|
||||
# 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-Persistent-VM" \
|
||||
-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 \
|
||||
-netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::8888-:8888 \
|
||||
-device virtio-net-pci,netdev=n1 \
|
||||
-display gtk \
|
||||
-vga virtio \
|
||||
|
||||
Reference in New Issue
Block a user