This repository has no description
3.7 kB
149 lines
1#!/usr/bin/env bash
2set -euo pipefail
3set -x
4
5# Default values
6OUTPUT="installer.iso"
7ARCH="x86_64-linux" # Default to x86_64-linux for cross-compilation
8CONFIG_DIR=""
9FLAKE_URL=""
10
11usage() {
12 cat <<EOF
13Usage: $0 [OPTIONS]
14
15Build a NixOS installer ISO with custom configuration.
16
17OPTIONS:
18 -o, --output FILE Output ISO file (default: installer.iso)
19 -a, --arch ARCH Architecture: x86_64-linux or aarch64-linux (default: x86_64-linux)
20 -c, --config DIR Configuration directory containing installer-config.json and files/
21 -f, --flake URL Flake URL to install automatically (creates minimal config)
22 -h, --help Show this help message
23
24EXAMPLES:
25 $0 -o my-installer.iso -f github:user/nixos-config
26 $0 -o my-installer.iso -c ./installer-config
27
28Configuration Directory Structure:
29installer-config/
30├── installer-config.json # Configuration file
31└── files/ # Files to copy to target system
32 ├── etc/
33 │ └── resolv.conf # Gets copied to /etc/resolv.conf
34 └── root/
35 └── .ssh/
36 └── authorized_keys # Gets copied to /root/.ssh/authorized_keys
37
38JSON Configuration Format:
39{
40 "flakeUrl": "github:user/nixos-config",
41 "fileMetadata": {
42 "/etc/resolv.conf": {
43 "owner": "root:root",
44 "permissions": "644"
45 },
46 "/root/.ssh/authorized_keys": {
47 "owner": "root:root",
48 "permissions": "600"
49 }
50 }
51}
52
53Default file metadata: owner=root:root, permissions=400
54EOF
55}
56
57# Parse arguments
58while [[ $# -gt 0 ]]; do
59 case $1 in
60 -o|--output)
61 OUTPUT="$2"
62 shift 2
63 ;;
64 -a|--arch)
65 ARCH="$2"
66 shift 2
67 ;;
68 -c|--config)
69 CONFIG_DIR="$2"
70 shift 2
71 ;;
72 -f|--flake)
73 FLAKE_URL="$2"
74 shift 2
75 ;;
76 -h|--help)
77 usage
78 exit 0
79 ;;
80 *)
81 echo "Unknown option: $1"
82 usage
83 exit 1
84 ;;
85 esac
86done
87
88# Build the base ISO
89echo "Building base ISO for $ARCH..."
90BASE_ISO=$(nix build -L --no-link --print-out-paths "$FLAKE_ROOT#nixosConfigurations.installer-$ARCH.config.system.build.images.iso-installer")
91
92# Determine what to append to the ISO
93if [ -n "$FLAKE_URL" ]; then
94 # Create minimal config for flake URL
95 echo "Creating minimal configuration for flake URL: $FLAKE_URL"
96 TEMP_CONFIG_DIR=$(mktemp -d)
97 trap 'rm -rf "$TEMP_CONFIG_DIR"' EXIT
98
99 cat > "$TEMP_CONFIG_DIR/installer-config.json" <<EOF
100{
101 "flakeUrl": "$FLAKE_URL"
102}
103EOF
104
105 echo "Appending flake configuration to ISO..."
106 xorriso \
107 -indev "$BASE_ISO"/iso/*.iso \
108 -outdev "$OUTPUT" \
109 -boot_image any replay \
110 -map "$TEMP_CONFIG_DIR/installer-config.json" /installer-config.json \
111 -commit_eject all
112
113elif [ -n "$CONFIG_DIR" ]; then
114 # Use provided configuration directory
115 echo "Appending configuration from $CONFIG_DIR to ISO..."
116
117 if [ ! -d "$CONFIG_DIR" ]; then
118 echo "Error: Configuration directory '$CONFIG_DIR' not found"
119 exit 1
120 fi
121
122 # Build xorriso command with all necessary mappings
123 XORRISO_ARGS=(
124 -indev "$BASE_ISO"/iso/*.iso
125 -outdev "$OUTPUT"
126 -boot_image any replay
127 )
128
129 # Add installer-config.json if it exists
130 if [ -f "$CONFIG_DIR/installer-config.json" ]; then
131 XORRISO_ARGS+=(-map "$CONFIG_DIR/installer-config.json" /installer-config.json)
132 fi
133
134 # Add files directory if it exists
135 if [ -d "$CONFIG_DIR/files" ]; then
136 XORRISO_ARGS+=(-map "$CONFIG_DIR/files" /files)
137 fi
138
139 XORRISO_ARGS+=(-commit_eject all)
140
141 xorriso "${XORRISO_ARGS[@]}"
142
143else
144 # No configuration specified, just copy the base ISO
145 echo "No configuration specified, copying base ISO..."
146 cp "$BASE_ISO"/iso/*.iso "$OUTPUT"
147fi
148
149echo "ISO created: $OUTPUT"