forked from
lass.pds.lassul.us/superconfig
This repository has no description
6.7 kB
185 lines
1{ self, ... }:
2let
3 # Shared installer configuration module
4 installerModule =
5 { modulesPath, pkgs, ... }:
6 {
7 imports = [
8 (modulesPath + "/image/images.nix")
9 ];
10
11 # Basic installer configuration
12 networking.hostName = "installer";
13
14 # Enable emergency access in initrd for debugging
15 boot.initrd.systemd.emergencyAccess = true;
16
17 # Enable serial console for VM usage
18 boot.kernelParams = [
19 "console=ttyS0,115200n8"
20 "console=tty0"
21 ];
22
23 # Configure systemd for serial console
24 systemd.services."serial-getty@ttyS0" = {
25 enable = true;
26 wantedBy = [ "getty.target" ];
27 serviceConfig.Restart = "always";
28 };
29
30 # Process installer configuration from ISO during system activation
31 system.activationScripts.installer-config = {
32 text = ''
33 echo "Processing installer configuration from ISO..."
34
35 # Find the ISO device (usually sr0, but could be others)
36 ISO_DEVICE=""
37 for dev in /dev/sr*; do
38 if [ -b "$dev" ]; then
39 ISO_DEVICE="$dev"
40 break
41 fi
42 done
43
44 if [ -z "$ISO_DEVICE" ]; then
45 echo "No ISO device found, checking loop devices..."
46 for dev in /dev/loop*; do
47 if [ -b "$dev" ] && ${pkgs.util-linux}/bin/lsblk -no FSTYPE "$dev" 2>/dev/null | ${pkgs.gnugrep}/bin/grep -q iso9660; then
48 ISO_DEVICE="$dev"
49 break
50 fi
51 done
52 fi
53
54 if [ -n "$ISO_DEVICE" ]; then
55 echo "Found ISO device: $ISO_DEVICE"
56 mkdir -p /mnt/iso
57 if ${pkgs.util-linux}/bin/mount -t iso9660 -o ro "$ISO_DEVICE" /mnt/iso; then
58 echo "Successfully mounted ISO"
59
60 # Check for installer configuration
61 if [ -f /mnt/iso/installer-config.json ]; then
62 echo "Found installer configuration"
63 CONFIG_FILE="/mnt/iso/installer-config.json"
64
65 # Process flake URL if present
66 FLAKE_URL=$(${pkgs.jq}/bin/jq -r '.flakeUrl // empty' "$CONFIG_FILE" || true)
67 if [ -n "$FLAKE_URL" ]; then
68 echo "Flake URL configured: $FLAKE_URL"
69 # Store for later installation
70 mkdir -p /var/lib
71 echo "$FLAKE_URL" > /var/lib/installer-flake-url
72 fi
73
74 # Process files from file structure
75 if [ -d /mnt/iso/files ]; then
76 echo "Processing files from file structure..."
77
78 # Copy all files from the files directory, preserving structure
79 ${pkgs.findutils}/bin/find /mnt/iso/files -type f | while read -r source_file; do
80 # Remove /mnt/iso/files prefix to get target path
81 target_path="''${source_file#/mnt/iso/files}"
82 echo "Copying file: $source_file -> $target_path"
83
84 # Create target directory
85 mkdir -p "$(dirname "$target_path")"
86
87 # Copy the file
88 ${pkgs.coreutils}/bin/cp "$source_file" "$target_path"
89
90 # Apply metadata from JSON if present
91 if ${pkgs.jq}/bin/jq -e ".fileMetadata[\"$target_path\"]" "$CONFIG_FILE" >/dev/null 2>&1; then
92 # Extract metadata for this file
93 OWNER=$(${pkgs.jq}/bin/jq -r ".fileMetadata[\"$target_path\"].owner // \"root:root\"" "$CONFIG_FILE")
94 PERMS=$(${pkgs.jq}/bin/jq -r ".fileMetadata[\"$target_path\"].permissions // \"400\"" "$CONFIG_FILE")
95
96 echo "Setting owner: $OWNER, permissions: $PERMS for $target_path"
97 ${pkgs.coreutils}/bin/chown "$OWNER" "$target_path" || echo "Warning: failed to set owner $OWNER"
98 ${pkgs.coreutils}/bin/chmod "$PERMS" "$target_path" || echo "Warning: failed to set permissions $PERMS"
99 else
100 # Apply defaults: root:root, 400
101 echo "Applying default metadata for $target_path"
102 ${pkgs.coreutils}/bin/chown root:root "$target_path" || echo "Warning: failed to set default owner"
103 ${pkgs.coreutils}/bin/chmod 400 "$target_path" || echo "Warning: failed to set default permissions"
104 fi
105 done
106 fi
107
108 else
109 echo "No installer configuration found in ISO"
110 fi
111
112 ${pkgs.util-linux}/bin/umount /mnt/iso
113 else
114 echo "Failed to mount ISO device $ISO_DEVICE"
115 fi
116 else
117 echo "No ISO device found"
118 fi
119 '';
120 deps = [ ];
121 };
122
123 # Service to install from flake URL if configured
124 systemd.services.installer-flake-install = {
125 description = "Install NixOS from configured flake URL";
126 wantedBy = [ "multi-user.target" ];
127 after = [
128 "installer-config.service"
129 "network-online.target"
130 ];
131 wants = [ "network-online.target" ];
132 path = with pkgs; [
133 git
134 nix
135 coreutils
136 ];
137 serviceConfig = {
138 Type = "oneshot";
139 RemainAfterExit = true;
140 };
141 script = ''
142 set -euo pipefail
143
144 if [ -f /var/lib/installer-flake-url ]; then
145 FLAKE_URL=$(cat /var/lib/installer-flake-url)
146 echo "Installing from flake: $FLAKE_URL"
147
148 # TODO: Add actual installation logic here
149 # This would typically involve:
150 # 1. Partitioning disks
151 # 2. Mounting filesystems
152 # 3. Running nixos-install with the flake
153 echo "Installation from flake not yet implemented"
154 echo "Flake URL: $FLAKE_URL"
155 else
156 echo "No flake URL configured for automatic installation"
157 fi
158 '';
159 };
160 };
161in
162{
163 flake = {
164 # Export the installer module for reuse
165 nixosModules.installer = installerModule;
166 };
167
168 perSystem =
169 { pkgs, ... }:
170 {
171 packages.build-installer-iso = (pkgs.writeShellApplication {
172 name = "build-installer-iso";
173 runtimeInputs = with pkgs; [
174 xorriso
175 coreutils
176 nix
177 ];
178 text = ''
179 # Export flake root for the script to use
180 export FLAKE_ROOT="${self}"
181 ${builtins.readFile ./build-installer-iso.sh}
182 '';
183 }).overrideAttrs { passthru.usage = builtins.readFile ./usage.kdl; };
184 };
185}