-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.nix
71 lines (66 loc) · 1.94 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
pkgs ? import <nixpkgs> { },
}:
pkgs.mkShell {
name = "nixos-server";
packages = [
pkgs.qemu
pkgs.docker
(pkgs.writeShellScriptBin "buildIso" ''
#!/bin/bash
set -euo pipefail
mkdir -p output
cp $(nix-build default.nix -A buildIso --argstr partition "''${1:-default60G}" --argstr cloud "''${2:-false}")/iso/* output/
'')
(pkgs.writeShellScriptBin "buildQcow2" ''
#!/bin/bash
set -euo pipefail
mkdir -p output
chmod +w output -R
cp $(nix-build default.nix -A buildQcow2 --argstr profile $1)/nixos.qcow2 output/$1.qcow2
'')
(pkgs.writeShellScriptBin "buildOciQcow2" ''
#!/bin/bash
set -euo pipefail
mkdir -p output
chmod +w output -R
$(nix-build default.nix -A ociQcow2 --argstr profile $1) > output/$1-qcow2-oci.tar
'')
(pkgs.writeShellScriptBin "runIso" ''
#!/bin/bash
set -euo pipefail
# Step 1: Build the NixOS ISO
if ! nix-build default.nix --argstr partition "''${1:-default}" --argstr cloud "''${2:-false}"; then
echo "nix-build failed!"
exit 1
fi
# Ensure the ISO path exists
ISO_PATH=$(find result/iso -name "*.iso" | head -n 1)
if [[ -z "$ISO_PATH" ]]; then
echo "ISO not found after build!"
exit 1
fi
# Step 2: Create a 50G raw disk image
if ! qemu-img create -f raw disk.img 50G; then
echo "Failed to create disk image!"
exit 1
fi
# Step 3: Launch QEMU with the specified configuration
qemu-system-x86_64 \
-enable-kvm \
-m 4096 \
-cpu host \
-cdrom "$ISO_PATH" \
-boot once=d \
-drive file=disk.img,format=raw \
-nic user,model=virtio,hostfwd=tcp::2222-:22 || {
echo "QEMU launch failed!"
rm -f disk.img
exit 1
}
# Step 4: Clean up the disk image
echo "QEMU exited. Cleaning up."
rm -f disk.img
'')
];
}