From 94b1ec68a5084b31f1eb80e2832fce77c3be0926 Mon Sep 17 00:00:00 2001 From: Michael Czechowski Date: Sat, 16 Aug 2025 15:46:08 +0200 Subject: [PATCH] refactor: Improve NixOS configuration modularity and add Traefik setup utility --- common.nix | 54 ++++++++++++++++++++++++++++++++++++------------------ flake.nix | 48 ++++++++++++++++-------------------------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/common.nix b/common.nix index d19bcc8..b303a1e 100644 --- a/common.nix +++ b/common.nix @@ -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"; - # Conditional ISO image settings - ${pkgs.lib.mkIf isLiveIso { - isoImage.makeEfiBootable = true; - isoImage.makeUsbBootable = true; - }} - - networking.wireless.enable = true; - networking.networkmanager.enable = true; - networking.hostName = "workshop-live"; + networking = { + wireless.enable = true; + networkmanager.enable = true; + hostName = if isLiveIso then "workshop-live" else "workshop-vm"; + }; # Enable Docker for local development virtualisation.docker.enable = true; @@ -33,7 +41,6 @@ xterm docker docker-compose - # For local abra installation bash wget jq @@ -57,7 +64,7 @@ sleep 3 done - # Install abra for workshop user (DO NOT change installation method) + # Install abra for workshop user if [ ! -f /home/workshop/.local/bin/abra ]; then sudo -u workshop mkdir -p /home/workshop/.local/bin cd /home/workshop @@ -101,6 +108,21 @@ # Ensure abra is in 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 '" + } deploy() { if [ -z "$1" ]; then @@ -116,13 +138,11 @@ echo "🚀 Deploying $recipe locally..." echo "Domain: $domain" - # Check if abra is available if ! command -v abra &> /dev/null; then echo "❌ Abra not found. Run 'sudo systemctl restart workshop-abra-setup'" return 1 fi - # Deploy with abra abra app new "$recipe" -S --domain="$domain" abra app deploy "$domain" @@ -214,10 +234,8 @@ services.xserver = { enable = true; desktopManager.xfce.enable = true; - displayManager = { - lightdm.enable = true; - autoLogin.enable = false; # Manual desktop start - }; + displayManager.lightdm.enable = true; + # Don't set autoLogin here - it conflicts with the VM config }; # Don't auto-start GUI, let user choose diff --git a/flake.nix b/flake.nix index 5c45845..f9370bd 100644 --- a/flake.nix +++ b/flake.nix @@ -14,10 +14,10 @@ system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; - # All possible participant names for the workshop - allParticipantNames = [ + # Server names for cloud connections + cloudServerNames = [ "hopper" - "curie" + "curie" "lovelace" "noether" "hamilton" @@ -33,25 +33,11 @@ "rich" ]; - # Dynamic participant count (default 3, max 15) - participantsEnv = builtins.getEnv "PARTICIPANTS"; - numParticipants = - if participantsEnv != "" && builtins.match "^[0-9]+$" participantsEnv != null - 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; }); + # Common configuration + commonConfig = { isLiveIso ? false }: + import ./common.nix { + inherit pkgs cloudServerNames isLiveIso; + }; in { packages.${system} = { @@ -60,10 +46,8 @@ live-iso = nixos-generators.nixosGenerate { inherit system; format = "iso"; - modules = [ - commonConfig - { isLiveIso = true; } + (commonConfig { isLiveIso = true; }) ]; }; }; @@ -79,9 +63,11 @@ nixosConfigurations.workshop-vm = nixpkgs.lib.nixosSystem { inherit system; modules = [ - commonConfig - { isLiveIso = false; } - ({ config, pkgs, ... }: { + "${nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" + + (commonConfig { isLiveIso = false; }) + + ({ config, pkgs, lib, ... }: { boot.loader.grub.enable = false; boot.loader.generic-extlinux-compatible.enable = true; @@ -90,9 +76,8 @@ networking.networkmanager.enable = true; networking.firewall.enable = false; - # Auto-login for VM - services.getty.autologinUser = "workshop"; - services.displayManager.autoLogin = { + # Fix the auto-login conflict with mkForce + services.displayManager.autoLogin = lib.mkForce { enable = true; user = "workshop"; }; @@ -110,4 +95,3 @@ }; }; } -