# Common NixOS configuration shared between ISO and netboot
{ config, lib, pkgs, workshopSrc, ... }:

let
	workshopPackages = import ./packages.nix {
		inherit pkgs;
		kernelPackages = config.boot.kernelPackages;
	};
in
{
	# System identification
	system.stateVersion = "24.11";

	# Use a recent stable kernel
	boot.kernelPackages = pkgs.linuxPackages_6_12;

	# Set perf permissions for non-root users
	boot.kernel.sysctl = {
		"kernel.perf_event_paranoid" = 1;
		"kernel.kptr_restrict" = 0;  # Allow reading kernel symbols
	};

	# Workshop packages
	environment.systemPackages = workshopPackages ++ (with pkgs; [
		# Additional GUI utilities
		firefox  # for viewing flamegraphs
		xfce4-terminal
	]);

	# Create workshop user with auto-login
	users.users.workshop = {
		isNormalUser = true;
		description = "Workshop User";
		extraGroups = [ "wheel" "video" "audio" "networkmanager" ];
		initialPassword = "workshop";
		home = "/home/workshop";
	};

	# Auto-login to workshop user
	services.displayManager.autoLogin = {
		enable = true;
		user = "workshop";
	};

	# Allow passwordless sudo for workshop user
	security.sudo.wheelNeedsPassword = false;

	# XFCE desktop environment
	services.xserver.desktopManager.xfce.enable = true;
	services.displayManager.defaultSession = "xfce";

	# Embed workshop materials into home directory
	system.activationScripts.workshopMaterials = ''
		mkdir -p /home/workshop/perf-workshop
		cp -rT ${workshopSrc} /home/workshop/perf-workshop
		chmod -R u+w /home/workshop/perf-workshop
		chown -R workshop:users /home/workshop/perf-workshop
	'';

	# Desktop shortcut for workshop
	environment.etc."skel/Desktop/Workshop.desktop".text = ''
		[Desktop Entry]
		Type=Application
		Name=Performance Workshop
		Comment=Open terminal in workshop directory
		Exec=xfce4-terminal --working-directory=/home/workshop/perf-workshop
		Icon=utilities-terminal
		Terminal=false
		Categories=Development;
	'';

	# Copy desktop shortcut for workshop user
	system.activationScripts.workshopDesktop = ''
		mkdir -p /home/workshop/Desktop
		cp /etc/skel/Desktop/Workshop.desktop /home/workshop/Desktop/
		chown -R workshop:users /home/workshop/Desktop
		chmod +x /home/workshop/Desktop/Workshop.desktop
	'';

	# Networking
	networking = {
		hostName = "perf-workshop";
		networkmanager.enable = true;
	};

	# Enable SSH for remote access (useful for debugging)
	services.openssh = {
		enable = true;
		settings = {
			PermitRootLogin = "no";
			PasswordAuthentication = true;
		};
	};

	# Timezone (can be changed by user)
	time.timeZone = "Asia/Kolkata";

	# Locale
	i18n.defaultLocale = "en_US.UTF-8";
}
