reduce complexity

This commit is contained in:
2025-08-12 17:50:32 +02:00
parent b80fe3c27c
commit ad46e3cf07
3 changed files with 80 additions and 312 deletions

View File

@@ -1,80 +1,20 @@
# Load .env file if it exists .PHONY: help vm-run clean
-include .env
export
.PHONY: help cloud-deploy cloud-status cloud-destroy usb-build usb-flash local-vm-run local-shell clean
DOMAIN := $(or $(WORKSHOP_DOMAIN),codecrispi.es)
USB_DEVICE := /dev/sdX
help: help:
@echo "🍪 CODE CRISPIES Workshop" @echo "🍪 Workshop VM with Containers"
@echo "" @echo ""
@echo "Usage: make <command>" @echo "Commands:"
@echo " make vm-run - Start VM with participant containers"
@echo " make clean - Clean build artifacts"
@echo "" @echo ""
@echo "--- Local VM Development ---" @echo "Inside the VM:"
@echo " make local-vm-run - Start the complete workshop environment in a VM" @echo " ssh root@192.168.100.11 # Connect to hopper container"
@echo "" @echo " ssh root@192.168.100.12 # Connect to curie container"
@echo "--- Cloud Infrastructure ---"
@echo " make deploy-cloud - Deploy VMs to Hetzner"
@echo " make status-cloud - Check the status of cloud servers"
@echo " make destroy-cloud - Destroy all cloud infrastructure"
@echo ""
@echo "--- USB Drive Creation ---"
@echo " make build-usb - Build the NixOS ISO for the workshop"
@echo " make flash-usb - Flash the ISO to a USB drive (set USB_DEVICE)"
@echo ""
@echo "--- Host Development Shell ---"
@echo " make local-shell - Enter a dev shell with terraform, etc., on your main machine"
vm-run:
# --- Local Development --- @echo "🚀 Starting workshop VM with containers..."
local-vm-run: @echo "VM will open with desktop. Terminal shows SSH commands."
@echo "🚀 Starting local workshop environment inside a VM..."
@echo " (A new window with a desktop will open. Close it to stop the VM.)"
nix run --impure .#local-vm nix run --impure .#local-vm
# --- Cloud Infrastructure ---
deploy-cloud:
@echo "🚀 Deploying to Hetzner Cloud..."
cd terraform && terraform init
cd terraform && terraform apply -auto-approve \
-var="hcloud_token=${HCLOUD_TOKEN}" \
-var="domain=${DOMAIN}" \
-var="ssh_public_key=$$(cat ~/.ssh/id_rsa.pub)"
@echo "🔍 Running health checks..."
./scripts/deploy.sh
@echo "✅ Cloud deployment complete and verified!"
status-cloud:
@echo "📊 Checking server status..."
@for name in hopper curie lovelace noether hamilton franklin johnson clarke goldberg liskov wing rosen shaw karp rich; do \
echo -n "$$name.${DOMAIN}: "; \
if curl -s -f https://traefik.$$name.${DOMAIN}/ping >/dev/null 2>&1; then \
echo "✅ Ready"; \
else \
echo "❌ Not ready"; \
fi; \
done
destroy-cloud:
cd terraform && terraform destroy -auto-approve
# --- USB Boot Drive ---
build-usb:
@echo "🔨 Building NixOS workshop ISO..."
nix build .#live-iso
@echo "✅ ISO built: result/iso/nixos.iso"
flash-usb: build-usb
@echo "⚠️ Flashing to ${USB_DEVICE} - THIS WILL ERASE THE DEVICE!"
@read -p "Continue? [y/N]: " confirm && [ "$$confirm" = "y" ]
sudo dd if=result/iso/nixos.iso of=${USB_DEVICE} bs=4M status=progress oflag=sync
@echo "✅ USB drive ready!"
# --- Host Development ---
local-shell:
nix develop
clean: clean:
rm -rf result .direnv rm -rf result .direnv

61
flake.lock generated
View File

@@ -1,75 +1,24 @@
{ {
"nodes": { "nodes": {
"nixlib": {
"locked": {
"lastModified": 1736643958,
"narHash": "sha256-tmpqTSWVRJVhpvfSN9KXBvKEXplrwKnSZNAoNPf/S/s=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "1418bc28a52126761c02dd3d89b2d8ca0f521181",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"nixos-generators": {
"inputs": {
"nixlib": "nixlib",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1751903740,
"narHash": "sha256-PeSkNMvkpEvts+9DjFiop1iT2JuBpyknmBUs0Un0a4I=",
"owner": "nix-community",
"repo": "nixos-generators",
"rev": "032decf9db65efed428afd2fa39d80f7089085eb",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixos-generators",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1736657626, "lastModified": 1754937576,
"narHash": "sha256-FWlPMUzp0lkQBdhKlPqtQdqmp+/C+1MBiEytaYfrCTY=", "narHash": "sha256-3sWA5WJybUE16kIMZ3+uxcxKZY/JRR4DFBqLdSLBo7w=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "2f9e2f85cb14a46410a1399aa9ea7ecf433e422e", "rev": "ddae11e58c0c345bf66efbddbf2192ed0e58f896",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixpkgs-unstable", "ref": "nixos-25.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1754725699,
"narHash": "sha256-iAcj9T/Y+3DBy2J0N+yF9XQQQ8IEb5swLFzs23CdP88=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "85dbfc7aaf52ecb755f87e577ddbe6dbbdbc1054",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixos-generators": "nixos-generators", "nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs_2"
} }
} }
}, },

225
flake.nix
View File

@@ -1,65 +1,31 @@
{ {
description = "CODE CRISPIES Workshop Infrastructure"; description = "Workshop VM with Participant Containers";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
nixos-generators.url = "github:nix-community/nixos-generators";
}; };
outputs = { self, nixpkgs, nixos-generators }: outputs = { self, nixpkgs }:
let let
system = "x86_64-linux"; system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
participantRange = nixpkgs.lib.range 1 2; participantNames = [ "hopper" "curie" ];
fullParticipantNames = [
"hopper" "curie" "lovelace" "noether" "hamilton"
"franklin" "johnson" "clarke" "goldberg" "liskov"
"wing" "rosen" "shaw" "karp" "rich"
];
in in
{ {
packages.${system} = { packages.${system} = {
live-iso = nixos-generators.nixosGenerate { local-vm = self.nixosConfigurations.workshop-vm.config.system.build.vm;
inherit system;
format = "iso";
modules = [
({ pkgs, ... }: {
system.stateVersion = "25.05";
networking.networkmanager.enable = true;
users.users.workshop = {
isNormalUser = true;
shell = pkgs.zsh;
extraGroups = [ "wheel" ];
password = "workshop";
};
services.getty.autologinUser = "workshop";
programs.zsh.enable = true;
environment.systemPackages = with pkgs; [ openssh curl git ];
services.xserver = {
enable = true;
displayManager.lightdm.enable = true;
desktopManager.xfce.enable = true;
};
services.displayManager.autoLogin.enable = true;
services.displayManager.autoLogin.user = "workshop";
})
];
};
local-vm = self.nixosConfigurations.workshop-local.config.system.build.vm;
}; };
nixosConfigurations.workshop-local = nixpkgs.lib.nixosSystem { nixosConfigurations.workshop-vm = nixpkgs.lib.nixosSystem {
system = "x86_64-linux"; inherit system;
modules = [ modules = [
({ config, pkgs, ... }: { ({ config, pkgs, ... }: {
system.stateVersion = "25.05"; system.stateVersion = "25.05";
boot.loader.grub.enable = false; boot.loader.grub.enable = false;
boot.loader.generic-extlinux-compatible.enable = true; boot.loader.generic-extlinux-compatible.enable = true;
# Enable IP forwarding for NAT
boot.kernel.sysctl."net.ipv4.ip_forward" = 1; boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
# Host VM user setup
users.users.workshop = { users.users.workshop = {
isNormalUser = true; isNormalUser = true;
extraGroups = [ "wheel" ]; extraGroups = [ "wheel" ];
@@ -67,75 +33,61 @@
shell = pkgs.bash; shell = pkgs.bash;
}; };
# GUI setup security.pam.services.login.allowNullPassword = true;
security.sudo.wheelNeedsPassword = false;
# CORRECTED GUI setup
services.xserver = { services.xserver = {
enable = true; enable = true;
desktopManager.xfce.enable = true; desktopManager.xfce.enable = true;
displayManager.lightdm.enable = true; displayManager = {
}; lightdm.enable = true;
services.displayManager.autoLogin.enable = true; autoLogin.enable = true;
services.displayManager.autoLogin.user = "workshop"; autoLogin.user = "workshop";
sessionCommands = ''
# Auto-open root terminal ${pkgs.xfce.xfce4-terminal}/bin/xfce4-terminal --title="Workshop Terminal" \
services.xserver.displayManager.sessionCommands = '' --command="bash -c 'echo \"🍪 Workshop VM Ready!\"; echo \"\"; echo \"SSH into containers:\"; echo \" ssh root@192.168.100.11 # hopper\"; echo \" ssh root@192.168.100.12 # curie\"; echo \"\"; bash'" &
${pkgs.xfce.xfce4-terminal}/bin/xfce4-terminal -T "Workshop Root Shell" -e "sudo -i" &
''; '';
# Workshop user convenience
programs.bash = {
shellAliases = {
connect = "sudo nixos-container login";
containers = "sudo nixos-container list";
root = "sudo -i";
}; };
}; };
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
gnumake nano nixos-container firefox git curl jq firefox curl git jq nano tree nixos-container
]; ];
security.pam.services.login.allowNullPassword = true;
security.sudo.wheelNeedsPassword = false;
networking.hostName = "workshop-vm";
# FIXED: Proper NAT configuration
networking = { networking = {
hostName = "workshop-vm";
firewall.enable = false;
nat = { nat = {
enable = true; enable = true;
internalInterfaces = ["ve-+"]; internalInterfaces = ["ve-+"];
externalInterface = "eth0"; externalInterface = "eth0";
# Ensure we have masquerading
extraCommands = ''
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE
'';
};
firewall = {
enable = true;
trustedInterfaces = [ "ve-+" ];
# Allow forwarding from containers
extraCommands = ''
iptables -A FORWARD -i ve-+ -j ACCEPT
iptables -A FORWARD -o ve-+ -j ACCEPT
'';
}; };
}; };
# Container configurations programs.bash.shellAliases = {
containers = builtins.listToAttrs (map (i: containers = "nixos-container list";
let participant = builtins.elemAt fullParticipantNames (i - 1); hopper = "ssh root@192.168.100.11";
curie = "ssh root@192.168.100.12";
};
# Container configs (same as before)
containers = builtins.listToAttrs (builtins.genList (i:
let
name = builtins.elemAt participantNames i;
ip = "192.168.100.${toString (11 + i)}";
in { in {
name = participant; inherit name;
value = { value = {
autoStart = true; autoStart = true;
privateNetwork = true; privateNetwork = true;
hostAddress = "192.168.100.1"; hostAddress = "192.168.100.1";
localAddress = "192.168.100.${toString (10 + i)}"; localAddress = ip;
config = { config = {
system.stateVersion = "25.05"; system.stateVersion = "25.05";
users.users.root = { users.users.root.password = "root";
password = "root";
openssh.authorizedKeys.keys = [];
};
users.users.workshop = { users.users.workshop = {
isNormalUser = true; isNormalUser = true;
password = "workshop"; password = "workshop";
@@ -150,124 +102,51 @@
}; };
}; };
# FIXED: Proper network configuration
networking = { networking = {
hostName = participant; hostName = name;
nameservers = [ "8.8.8.8" "1.1.1.1" ]; nameservers = [ "8.8.8.8" ];
defaultGateway = {
address = "192.168.100.1";
interface = "eth0";
};
firewall.enable = false; firewall.enable = false;
useHostResolvConf = false;
# Ensure eth0 is configured properly
interfaces.eth0.ipv4.addresses = [{
address = "192.168.100.${toString (10 + i)}";
prefixLength = 24;
}];
}; };
console.enable = true;
security.sudo.wheelNeedsPassword = false; security.sudo.wheelNeedsPassword = false;
virtualisation.docker.enable = true; virtualisation.docker.enable = true;
environment.systemPackages = with pkgs; [
docker git curl jq wget gnutar gzip util-linux
];
# FIXED: Better network initialization environment.systemPackages = with pkgs; [
systemd.services.container-network-setup = { docker curl git wget jq
wantedBy = [ "network.target" ]; ];
before = [ "network-online.target" ];
script = ''
# Ensure proper routing
${pkgs.iproute2}/bin/ip route add default via 192.168.100.1 dev eth0 || true
# Set up DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 1.1.1.1" >> /etc/resolv.conf
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
};
systemd.services.workshop-setup = { systemd.services.workshop-setup = {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" "docker.service" "container-network-setup.service" ]; after = [ "network-online.target" "docker.service" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
script = '' script = ''
export HOME=/root echo "🍪 Setting up ${name} container..."
export PATH=/run/current-system/sw/bin:$PATH
# Wait for network with better test for i in {1..10}; do
echo "Testing network connectivity..." if curl -s --max-time 5 google.com >/dev/null 2>&1; then
for attempt in {1..30}; do echo " Network ready"
if ${pkgs.curl}/bin/curl -s --max-time 10 --connect-timeout 5 https://install.abra.coopcloud.tech >/dev/null 2>&1; then
echo "Network is up!"
break break
fi fi
echo "Attempt $attempt: Waiting for network..." echo " Waiting for network... ($i/10)"
sleep 2 sleep 2
done done
# Docker swarm init ${pkgs.docker}/bin/docker swarm init --advertise-addr ${ip} || true
${pkgs.docker}/bin/docker swarm init --advertise-addr 192.168.100.${toString (10 + i)} || true
# Install abra echo " ${name} container ready!"
if [ ! -f /root/.local/bin/abra ]; then echo "SSH: ssh root@${ip} (password: root)"
echo "Installing abra..."
${pkgs.curl}/bin/curl -fsSL https://install.abra.coopcloud.tech | ${pkgs.bash}/bin/bash
fi
# Make abra globally available
if [ -f /root/.local/bin/abra ]; then
ln -sf /root/.local/bin/abra /usr/local/bin/abra || true
fi
# Add server
if command -v abra >/dev/null 2>&1; then
abra server add ${participant}.local || true
fi
''; '';
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
StandardOutput = "journal";
StandardError = "journal";
}; };
}; };
environment.sessionVariables = {
PATH = [ "/root/.local/bin" "$PATH" ];
};
}; };
}; };
} }
) participantRange); ) (builtins.length participantNames));
# DNS for .local domains
services.dnsmasq = {
enable = true;
settings = {
address = builtins.concatMap (i:
let participant = builtins.elemAt fullParticipantNames (i - 1);
in [ "/${participant}.local/192.168.100.${toString (10 + i)}" ]
) participantRange;
server = [ "8.8.8.8" "1.1.1.1" ];
};
};
}) })
]; ];
}; };
devShells.${system}.default = pkgs.mkShell {
buildInputs = with pkgs; [
terraform
nixos-rebuild
docker
openssh
];
};
}; };
} }