refactor: Improve NixOS configuration modularity and add Traefik setup utility

This commit is contained in:
2025-08-16 15:46:08 +02:00
parent 6c32b42f48
commit 94b1ec68a5
2 changed files with 52 additions and 50 deletions

View File

@@ -1,15 +1,23 @@
{ pkgs, allParticipantNames, ... }: { { pkgs, lib ? pkgs.lib, cloudServerNames, isLiveIso ? false, ... }:
let
# Only include isoImage config when building ISO
isoConfig = lib.optionalAttrs isLiveIso {
isoImage = {
makeEfiBootable = true;
makeUsbBootable = true;
};
};
in
isoConfig // {
system.stateVersion = "25.05"; system.stateVersion = "25.05";
# Conditional ISO image settings networking = {
${pkgs.lib.mkIf isLiveIso { wireless.enable = true;
isoImage.makeEfiBootable = true; networkmanager.enable = true;
isoImage.makeUsbBootable = true; hostName = if isLiveIso then "workshop-live" else "workshop-vm";
}} };
networking.wireless.enable = true;
networking.networkmanager.enable = true;
networking.hostName = "workshop-live";
# Enable Docker for local development # Enable Docker for local development
virtualisation.docker.enable = true; virtualisation.docker.enable = true;
@@ -33,7 +41,6 @@
xterm xterm
docker docker
docker-compose docker-compose
# For local abra installation
bash bash
wget wget
jq jq
@@ -57,7 +64,7 @@
sleep 3 sleep 3
done done
# Install abra for workshop user (DO NOT change installation method) # Install abra for workshop user
if [ ! -f /home/workshop/.local/bin/abra ]; then if [ ! -f /home/workshop/.local/bin/abra ]; then
sudo -u workshop mkdir -p /home/workshop/.local/bin sudo -u workshop mkdir -p /home/workshop/.local/bin
cd /home/workshop cd /home/workshop
@@ -102,6 +109,21 @@
# Ensure abra is in PATH # Ensure abra is in PATH
export PATH="$HOME/.local/bin:$PATH" export PATH="$HOME/.local/bin:$PATH"
setup-traefik() {
echo "🔧 Setting up local Traefik proxy..."
if ! command -v abra &> /dev/null; then
echo " Abra not found. Run 'sudo systemctl restart workshop-abra-setup'"
return 1
fi
abra app new traefik -S --domain=traefik.workshop.local
abra app deploy traefik.workshop.local
echo " Traefik deployed! Dashboard: http://traefik.workshop.local"
echo "🚀 Now you can deploy apps with 'deploy <recipe>'"
}
deploy() { deploy() {
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Usage: deploy <recipe>" echo "Usage: deploy <recipe>"
@@ -116,13 +138,11 @@
echo "🚀 Deploying $recipe locally..." echo "🚀 Deploying $recipe locally..."
echo "Domain: $domain" echo "Domain: $domain"
# Check if abra is available
if ! command -v abra &> /dev/null; then if ! command -v abra &> /dev/null; then
echo " Abra not found. Run 'sudo systemctl restart workshop-abra-setup'" echo " Abra not found. Run 'sudo systemctl restart workshop-abra-setup'"
return 1 return 1
fi fi
# Deploy with abra
abra app new "$recipe" -S --domain="$domain" abra app new "$recipe" -S --domain="$domain"
abra app deploy "$domain" abra app deploy "$domain"
@@ -214,10 +234,8 @@
services.xserver = { services.xserver = {
enable = true; enable = true;
desktopManager.xfce.enable = true; desktopManager.xfce.enable = true;
displayManager = { displayManager.lightdm.enable = true;
lightdm.enable = true; # Don't set autoLogin here - it conflicts with the VM config
autoLogin.enable = false; # Manual desktop start
};
}; };
# Don't auto-start GUI, let user choose # Don't auto-start GUI, let user choose

View File

@@ -14,8 +14,8 @@
system = "x86_64-linux"; system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
# All possible participant names for the workshop # Server names for cloud connections
allParticipantNames = [ cloudServerNames = [
"hopper" "hopper"
"curie" "curie"
"lovelace" "lovelace"
@@ -33,25 +33,11 @@
"rich" "rich"
]; ];
# Dynamic participant count (default 3, max 15) # Common configuration
participantsEnv = builtins.getEnv "PARTICIPANTS"; commonConfig = { isLiveIso ? false }:
numParticipants = import ./common.nix {
if participantsEnv != "" && builtins.match "^[0-9]+$" participantsEnv != null inherit pkgs cloudServerNames isLiveIso;
then };
let num = builtins.fromJSON participantsEnv;
in if num >= 1 && num <= 15 then num else 3
else 3;
# Selected participant names based on count
# Selected participant names based on count
participantNames = builtins.genList
(i: builtins.elemAt allParticipantNames i)
numParticipants;
# Common configuration for both live-iso and local-vm
commonConfig =
{ isLiveIso ? false, ... } @ args:
import ./common.nix (args // { inherit pkgs allParticipantNames participantNames; });
in in
{ {
packages.${system} = { packages.${system} = {
@@ -60,10 +46,8 @@
live-iso = nixos-generators.nixosGenerate { live-iso = nixos-generators.nixosGenerate {
inherit system; inherit system;
format = "iso"; format = "iso";
modules = [ modules = [
commonConfig (commonConfig { isLiveIso = true; })
{ isLiveIso = true; }
]; ];
}; };
}; };
@@ -79,9 +63,11 @@
nixosConfigurations.workshop-vm = nixpkgs.lib.nixosSystem { nixosConfigurations.workshop-vm = nixpkgs.lib.nixosSystem {
inherit system; inherit system;
modules = [ modules = [
commonConfig "${nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix"
{ isLiveIso = false; }
({ config, pkgs, ... }: { (commonConfig { isLiveIso = false; })
({ config, pkgs, lib, ... }: {
boot.loader.grub.enable = false; boot.loader.grub.enable = false;
boot.loader.generic-extlinux-compatible.enable = true; boot.loader.generic-extlinux-compatible.enable = true;
@@ -90,9 +76,8 @@
networking.networkmanager.enable = true; networking.networkmanager.enable = true;
networking.firewall.enable = false; networking.firewall.enable = false;
# Auto-login for VM # Fix the auto-login conflict with mkForce
services.getty.autologinUser = "workshop"; services.displayManager.autoLogin = lib.mkForce {
services.displayManager.autoLogin = {
enable = true; enable = true;
user = "workshop"; user = "workshop";
}; };
@@ -110,4 +95,3 @@
}; };
}; };
} }