{
	description = "Linux Performance Engineering Workshop";

	inputs = {
		nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

		system-manager = {
			url = "github:numtide/system-manager";
			inputs.nixpkgs.follows = "nixpkgs";
		};

		flake-utils.url = "github:numtide/flake-utils";
	};

	outputs = { self, nixpkgs, system-manager, flake-utils }:
		let
			# Workshop source for embedding in ISO
			workshopSrc = builtins.path {
				path = ./.;
				name = "perf-workshop";
				filter = path: type:
					let
						baseName = baseNameOf path;
					in
					# Exclude nix build artifacts and git
					!(baseName == "result" ||
						baseName == ".git" ||
						baseName == "flake.lock" ||
						nixpkgs.lib.hasSuffix "~" baseName);
			};
		in
		flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (system:
			let
				pkgs = nixpkgs.legacyPackages.${system};
				workshopPackages = import ./nix/packages.nix { inherit pkgs; };
			in
			{
				# Development shell with all workshop tools
				devShells.default = pkgs.mkShell {
					packages = workshopPackages;
					shellHook = ''
						echo "Linux Performance Workshop Development Environment"
						echo ""
						echo "Available tools: perf, strace, py-spy, hyperfine, bpftrace, ..."
					'';
				};

				# Package containing workshop materials
				packages.workshop-materials = pkgs.runCommand "workshop-materials" {} ''
					mkdir -p $out/share/perf-workshop
					cp -r ${workshopSrc}/* $out/share/perf-workshop/
				'';
			}
		) // {
			# System-manager configuration for Ubuntu
			systemConfigs.default = system-manager.lib.makeSystemConfig {
				modules = [
					./nix/system-manager.nix
				];
			};

			# NixOS configuration for bootable workshop ISO
			nixosConfigurations.workshop-iso = nixpkgs.lib.nixosSystem {
				system = "x86_64-linux";
				specialArgs = { inherit workshopSrc; };
				modules = [
					"${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-graphical-base.nix"
					./nix/iso.nix
				];
			};

			# NixOS configuration for netboot
			nixosConfigurations.workshop-netboot = nixpkgs.lib.nixosSystem {
				system = "x86_64-linux";
				specialArgs = { inherit workshopSrc; };
				modules = [
					"${nixpkgs}/nixos/modules/installer/netboot/netboot.nix"
					"${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-graphical-base.nix"
					./nix/netboot.nix
				];
			};

			# Convenient alias for building the ISO
			packages.x86_64-linux.iso =
				self.nixosConfigurations.workshop-iso.config.system.build.isoImage;

			# Netboot artifacts
			packages.x86_64-linux.netboot-kernel =
				self.nixosConfigurations.workshop-netboot.config.system.build.kernel;
			packages.x86_64-linux.netboot-initrd =
				self.nixosConfigurations.workshop-netboot.config.system.build.netbootRamdisk;
			packages.x86_64-linux.netboot-ipxe =
				self.nixosConfigurations.workshop-netboot.config.system.build.netbootIpxeScript;

			# Bundle netboot files for easy serving
			packages.x86_64-linux.netboot = let
				pkgs = nixpkgs.legacyPackages.x86_64-linux;
				cfg = self.nixosConfigurations.workshop-netboot.config;
				kernel = "${cfg.system.build.kernel}/bzImage";
				initrd = "${cfg.system.build.netbootRamdisk}/initrd";
			in pkgs.runCommand "workshop-netboot" {} ''
				mkdir -p $out
				ln -s ${kernel} $out/bzImage
				ln -s ${initrd} $out/initrd
				cp ${cfg.system.build.netbootIpxeScript} $out/netboot.ipxe

				cat > $out/README.md << 'EOF'
# Workshop Netboot Files

## Quick Start with Pixiecore (easiest)

```bash
# Serve on your LAN (requires root for DHCP)
sudo pixiecore boot bzImage initrd --cmdline "$(grep -oP 'imgargs.*? \K.*' netboot.ipxe)"
```

## Using dnsmasq + HTTP server

1. Start HTTP server in this directory:
   ```bash
   python3 -m http.server 8080
   ```

2. Configure dnsmasq (adjust interface/IP):
   ```
   interface=eth0
   dhcp-range=192.168.1.100,192.168.1.200,12h
   enable-tftp
   tftp-root=/path/to/this/dir
   dhcp-boot=netboot.ipxe
   ```

3. For UEFI clients, you may need iPXE chainloading.

## Files

- bzImage: Linux kernel
- initrd: Initial ramdisk (contains full system)
- netboot.ipxe: iPXE boot script
EOF
			'';
		};
}
