diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..67b5f57 --- /dev/null +++ b/.env-example @@ -0,0 +1,13 @@ +# DOMAIN AND DNS CONFIGURATION +DOMAIN=yourdomain.com +ACME_EMAIL=your-email@provider.com +DYNU_API_KEY=your_dynu_api_key + +# DOCKER CONFIGURATION +DOCKER_API_VERSION=1.40 + +# AUTHELIA SECURITY SECRETS +# Generate these using: openssl rand -hex 64 +AUTHELIA_SESSION_SECRET=session_secret_hex +AUTHELIA_STORAGE_ENCRYPTION_KEY=storage_encryption_key_hex +AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET=jwt_secret_hex diff --git a/.gitignore b/.gitignore index 5540251..d06e7bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,28 +1,27 @@ # Environments & Secrets .env .env.* -**/.env -**/config/*.env -**/config/users.yml +secrets.txt +backup.zip # SSL / ACME Certificates & Keys -**/letsencrypt/acme.json +config/letsencrypt/acme.json **/*.key **/*.crt **/*.pem # Authelia Databases & Mail Logs -infra/authelia/config/db.sqlite3 -infra/authelia/config/emails.txt +config/authelia/db.sqlite3 +config/authelia/emails.txt -# Infrastructure Persistent Volumes & Logs -**/portainer_data/ -infra/diun/data/ -homepage/config/logs/ +# Diun Databases +config/diun/diun.db + +# Homepage Logs +config/homepage/logs/ # OS & Editor Metadata .DS_Store Thumbs.db .vscode/ .idea/ -backup.zip diff --git a/README.md b/README.md index 6ba6d0a..3b7b28e 100644 --- a/README.md +++ b/README.md @@ -1,105 +1,52 @@ -# Traefik Homelab +# Traefik Homelab Core This repository contains the core Control Plane for a modular, secure, and self-documenting homeserver architecture based on the **Hardened Hub** model. +It consolidates all routing, authentication, and core panel management into a single, unified Docker Compose stack, optimized for secure credentials management via `sops-nix`. + +--- + ## 🏗️ Architectural Overview The system is split into two distinct planes to ensure stability, security, and a clean separation of concerns: -### 1. Control Plane (`infra/` & `homepage/`) -The "nervous system" of the server. These services handle security, edge routing, identity, maintenance, and portal visualization. +### 1. Control Plane (This Repository) +The "nervous system" of the server. These services handle security, edge routing, identity, maintenance, and portal visualization. All services are defined in a single, root-level `docker-compose.yml` and state configurations are stored in `./config/`: -- **Core (`infra/core`):** - - **Traefik (v3.6):** The entry gateway. Handles HTTPS (Let's Encrypt DNS-01 challenges), routing, and load balancing. - - **Socket-Proxy:** The security boundary. Decouples core containers from the host's `/var/run/docker.sock`. -- **Identity (`infra/authelia`):** Provides Single Sign-On (SSO) and 2FA protection for administrative tools and exposed applications. -- **Management (`infra/portainer`):** Visual container and stack management. -- **Monitoring (`infra/dozzle`):** Real-time log streaming for core containers (SSO Protected). -- **Maintenance (`infra/watchtower`, `infra/diun`):** Automated image updates and registry monitoring. -- **Portal (`homepage/`):** Central landing dashboard at `arch-services.mywire.org` serving as the single entry point. +* **`traefik` (v3.6):** The edge gateway. Handles wildcard HTTPS (Let's Encrypt DNS-01 challenges via Dynu), reverse routing, and load balancing. +* **`socket-proxy`:** Security boundary. Decouples containers from the host's `/var/run/docker.sock`. +* **`authelia`:** Provides Single Sign-On (SSO) and 2FA protection for administrative tools and exposed applications. +* **`portainer`:** Visual container and stack management. +* **`dozzle`:** Real-time log streaming for core containers (SSO Protected). +* **`watchtower` & `diun`:** Automated image updates and registry monitoring. +* **`homepage`:** Central landing dashboard at the root domain (`arch-services.mywire.org`) serving as the entry portal. ### 2. Data Plane (Decoupled User Applications) -User applications (such as Gitea, Jellyfin, or Kanban boards) are kept fully decoupled in separate directories outside this repository. They integrate with the Control Plane dynamically via the shared external Docker network (`proxy-net`) and Traefik routing labels. +User applications (such as Gitea, Jellyfin, or Kanban boards) live in separate directories outside this repository (e.g. `/home/kiskaadee/Deployments/traefik-deployments`). They integrate with the Control Plane dynamically via the shared external Docker network (`proxy-net`) and Traefik routing labels. --- ## 🚀 Getting Started -### 1. Secrets Management -Secrets are kept out of the configuration files and stored in `.env` files (git-ignored). +All instructions on deployment, structure, and secrets management have been updated for this release: -- **Core Infrastructure:** Create `infra/core/.env` based on `.env-example`: - ```env - DYNU_API_KEY=your_key - ACME_EMAIL=your_email - ``` -- **Identity (Authelia):** Create `infra/authelia/.env` with the following: - ```env - AUTHELIA_SESSION_SECRET=long_random_string - AUTHELIA_STORAGE_ENCRYPTION_KEY=long_random_string - AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET=long_random_string - ``` - -### 2. Deployment -Use the `up.sh` script to launch the core stack. The script handles network creation, Let's Encrypt certificate file initialization, and starts all infrastructure services plus the Homepage portal. +👉 **See the [Setup & Secrets Management Guide](docs/setup_guide.md)** +### Quick Start (NixOS systemd Service) +If you are deploying on NixOS with the matching configuration, the service is managed declaratively. To check status or read logs: ```bash -# Start the entire Core Infrastructure + Homepage -./up.sh +systemctl status homeserver-core +journalctl -u homeserver-core -f ``` -### 3. Verification -Once deployed, verify your services are reachable: -- **Homepage Dashboard:** `https://arch-services.mywire.org` -- **Auth Portal:** `https://auth.arch-services.mywire.org` -- **Traefik Dashboard:** `https://traefik.arch-services.mywire.org` (Redirects to Auth) -- **Logs:** `https://logs.arch-services.mywire.org` (Redirects to Auth) +### Manual Verification +If running manually (or troubleshooting config edits): +```bash +docker compose --env-file /run/secrets/homeserver.env up -d --remove-orphans +``` --- -## 📊 Observability & Monitoring - -The system includes built-in tools for real-time monitoring and management: - -- **Traefik Dashboard**: `https://traefik.arch-services.mywire.org` — View routing, entrypoints, and certificate status (SSO Protected). -- **Dozzle**: `https://logs.arch-services.mywire.org` — Real-time log streaming for all containers (SSO Protected). -- **Portainer**: `https://portainer.arch-services.mywire.org` — Advanced container, volume, and stack management (SSO Protected). -- **Homepage**: `https://arch-services.mywire.org` — The central portal displaying status indicators for all services. - -### Maintenance Scripts -For rapid diagnostics and common fixes, use the included automation scripts: -- **`./scripts/check-ssl.sh`**: Instantly audits the SSL/TLS status across all configured public subdomains. -- **`./scripts/purge-dynu.sh`**: Clears stale ACME challenge records from Dynu to resolve "503 Quota" deadlocks. - ---- - -## 📜 Log Policy - -To prevent the host system from running out of disk space, a **Log Rotation** policy is enforced: - -- **Driver**: `json-file` -- **Max Size**: `10m` (10 Megabytes per file) -- **Max Files**: `3` (Keeps the last 3 files) - -This policy is currently enabled on core infrastructure and is recommended for all high-traffic application containers. - ---- - -## 🛡️ Security Features -- **Socket Isolation:** No container has access to the host's raw Docker socket. All management tools communicate via the read/write-controlled `socket-proxy`. -- **SSO Integration:** Critical management tools (Portainer, Dozzle) and sensitive user applications are behind Authelia Forward Auth. -- **Automatic TLS:** Traefik automatically manages wildcard Let's Encrypt certificates via DNS-01 challenges with Dynu. -- **Network Segmentation:** System traffic is isolated on `proxy-net` and `socket-net` networks. -- **Decoupled Architecture:** Application stacks are isolated from the core repository config, preventing Git history contamination and permitting independent CI/CD pipelines. - ---- - -## 📚 Detailed Documentation - -For more in-depth information, please refer to the following guides: - -- **[Server Architecture](https://kiskaadee.github.io/traefik-homelab/architecture.html)**: Deep dive into the layered model, networking, and security posture. -- **[Usability & Operations](https://kiskaadee.github.io/traefik-homelab/usability.html)**: How to use the hub, expose new decoupled apps, and manage updates. -- **[Troubleshooting](https://kiskaadee.github.io/traefik-homelab/troubleshooting.html)**: Solutions for common connectivity, SSL, and authentication issues. -- **[Project Roadmap](https://kiskaadee.github.io/traefik-homelab/roadmap.html)**: Current status and future planned features. -- **[Dynu IP Protocol](https://kiskaadee.github.io/traefik-homelab/dynu-ip-update-protocol.html)**: Technical details on the DDNS synchronization mechanism. +## 📚 Documentation +* **[Setup & Secrets Guide](docs/setup_guide.md)**: Detailed configuration, systemd service, and SOPS secret integration. +* **[Legacy Documentation](docs/legacy/)**: Archive of old setup files and html pages. diff --git a/infra/authelia/config/configuration.yml b/config/authelia/configuration.yml similarity index 56% rename from infra/authelia/config/configuration.yml rename to config/authelia/configuration.yml index 3ca41c7..ce8b781 100644 --- a/infra/authelia/config/configuration.yml +++ b/config/authelia/configuration.yml @@ -14,29 +14,29 @@ authentication_backend: access_control: default_policy: deny rules: - - domain: "auth.{{ env \"DOMAIN\" }}" + - domain: 'auth.{{ env "DOMAIN" }}' policy: bypass - - domain: "*.{{ env \"DOMAIN\" }}" + - domain: '*.{{ env "DOMAIN" }}' policy: one_factor session: name: authelia_session - secret: "{{ env \"AUTHELIA_SESSION_SECRET\" }}" + secret: '{{ env "AUTHELIA_SESSION_SECRET" }}' expiration: 3600 inactivity: 300 remember_me: 1M cookies: - - domain: "{{ env \"DOMAIN\" }}" - authelia_url: "https://auth.{{ env \"DOMAIN\" }}" + - domain: '{{ env "DOMAIN" }}' + authelia_url: 'https://auth.{{ env "DOMAIN" }}' identity_validation: reset_password: - jwt_secret: "{{ env \"AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET\" }}" + jwt_secret: '{{ env "AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET" }}' storage: local: path: /config/db.sqlite3 - encryption_key: "{{ env \"AUTHELIA_STORAGE_ENCRYPTION_KEY\" }}" + encryption_key: '{{ env "AUTHELIA_STORAGE_ENCRYPTION_KEY" }}' notifier: filesystem: diff --git a/config/authelia/users.yml b/config/authelia/users.yml new file mode 100644 index 0000000..4d16f25 --- /dev/null +++ b/config/authelia/users.yml @@ -0,0 +1,9 @@ +users: + admin: + displayname: "Admin" + # Password is 'password' (hashed with Authelia's default) + password: '$pbkdf2-sha512$310000$6AM/6rKEZJm9nNeMRl.Zfw$BpLugzfKmNdcX5kpf3/RQwIkO.dCUj7ZOYhKatY2H4iyDmMZP2KJ2Zj9u0rr7sBPHoTBS7C.1hWw7qjjUKAkCw' + email: admin@arch-services.mywire.org + groups: + - admins + - dev diff --git a/homepage/config/bookmarks.yaml b/config/homepage/bookmarks.yaml similarity index 100% rename from homepage/config/bookmarks.yaml rename to config/homepage/bookmarks.yaml diff --git a/homepage/config/custom.css b/config/homepage/custom.css similarity index 100% rename from homepage/config/custom.css rename to config/homepage/custom.css diff --git a/homepage/config/custom.js b/config/homepage/custom.js similarity index 100% rename from homepage/config/custom.js rename to config/homepage/custom.js diff --git a/homepage/config/docker.yaml b/config/homepage/docker.yaml similarity index 100% rename from homepage/config/docker.yaml rename to config/homepage/docker.yaml diff --git a/homepage/config/kubernetes.yaml b/config/homepage/kubernetes.yaml similarity index 100% rename from homepage/config/kubernetes.yaml rename to config/homepage/kubernetes.yaml diff --git a/homepage/config/proxmox.yaml b/config/homepage/proxmox.yaml similarity index 100% rename from homepage/config/proxmox.yaml rename to config/homepage/proxmox.yaml diff --git a/homepage/config/services.yaml b/config/homepage/services.yaml similarity index 100% rename from homepage/config/services.yaml rename to config/homepage/services.yaml diff --git a/homepage/config/settings.yaml b/config/homepage/settings.yaml similarity index 100% rename from homepage/config/settings.yaml rename to config/homepage/settings.yaml diff --git a/homepage/config/widgets.yaml b/config/homepage/widgets.yaml similarity index 100% rename from homepage/config/widgets.yaml rename to config/homepage/widgets.yaml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f37f2ca --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,237 @@ +services: + socket-proxy: + image: tecnativa/docker-socket-proxy + container_name: socket-proxy + restart: always + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + environment: + - CONTAINERS=1 + - NETWORKS=1 + - SERVICES=1 + - TASKS=1 + - IMAGES=1 + - VOLUMES=1 + - POST=1 + - DELETE=1 + - DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40} + - VERSION=1 + - INFO=1 + - EVENTS=1 + - PING=1 + - BUILD=0 + - COMMIT=0 + - CONFIGS=0 + - DISTRIBUTION=0 + - EXEC=0 + - NODES=0 + - PLUGINS=0 + - SECRETS=0 + - SWARM=0 + - SYSTEM=0 + networks: + - socket-net + + traefik: + image: traefik:v3.6 + container_name: traefik + restart: always + depends_on: + - socket-proxy + ports: + - "80:80" + - "443:443" + environment: + - DYNU_API_KEY=${DYNU_API_KEY} + - DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40} + command: + - "--log.level=INFO" + - "--api.dashboard=true" + - "--providers.docker=true" + - "--providers.docker.endpoint=tcp://socket-proxy:2375" + - "--providers.docker.exposedByDefault=false" + - "--entrypoints.web.address=:80" + - "--entrypoints.websecure.address=:443" + # Global TLS defaults for websecure + - "--entrypoints.websecure.http.tls.certResolver=myresolver" + - "--entrypoints.websecure.http.tls.domains[0].main=${DOMAIN}" + - "--entrypoints.websecure.http.tls.domains[0].sans=*.${DOMAIN}" + # SSL Setup + - "--certificatesresolvers.myresolver.acme.dnschallenge=true" + - "--certificatesresolvers.myresolver.acme.dnschallenge.provider=dynu" + - "--certificatesresolvers.myresolver.acme.email=${ACME_EMAIL}" + - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json" + volumes: + - ./config/letsencrypt:/letsencrypt + networks: + - socket-net + - proxy-net + labels: + - "traefik.enable=true" + + # Middleware: Redirect to HTTPS + - "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https" + - "traefik.http.middlewares.https-redirect.redirectscheme.permanent=true" + + # Middleware: Authelia Guard + - "traefik.http.middlewares.authelia-auth.forwardauth.address=http://authelia:9091/api/verify?rd=https://auth.${DOMAIN}/" + - "traefik.http.middlewares.authelia-auth.forwardauth.trustForwardHeader=true" + - "traefik.http.middlewares.authelia-auth.forwardauth.authResponseHeaders=Remote-User,Remote-Groups,Remote-Email,Remote-Name" + + # Dashboard - Public (HTTPS) + - "traefik.http.routers.traefik-dash.rule=Host(`traefik.${DOMAIN}`)" + - "traefik.http.routers.traefik-dash.entrypoints=websecure" + - "traefik.http.routers.traefik-dash.tls=true" + - "traefik.http.routers.traefik-dash.service=api@internal" + - "traefik.http.routers.traefik-dash.middlewares=authelia-auth@docker" + + # Dashboard - Public (Redirect) + - "traefik.http.routers.traefik-dash-red.rule=Host(`traefik.${DOMAIN}`)" + - "traefik.http.routers.traefik-dash-red.entrypoints=web" + - "traefik.http.routers.traefik-dash-red.middlewares=https-redirect@docker" + logging: + driver: "json-file" + options: + max-size: "10m" + max-file: "3" + + authelia: + image: authelia/authelia:latest + container_name: authelia + restart: always + volumes: + - ./config/authelia:/config + environment: + - DOMAIN=${DOMAIN} + - AUTHELIA_SESSION_SECRET=${AUTHELIA_SESSION_SECRET} + - AUTHELIA_STORAGE_ENCRYPTION_KEY=${AUTHELIA_STORAGE_ENCRYPTION_KEY} + - AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET=${AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET} + - X_AUTHELIA_CONFIG_FILTERS=template + networks: + - proxy-net + labels: + - "traefik.enable=true" + # Public (HTTPS) + - "traefik.http.routers.authelia.rule=Host(`auth.${DOMAIN}`)" + - "traefik.http.routers.authelia.entrypoints=websecure" + - "traefik.http.routers.authelia.tls=true" + # Public (Redirect) + - "traefik.http.routers.authelia-red.rule=Host(`auth.${DOMAIN}`)" + - "traefik.http.routers.authelia-red.entrypoints=web" + - "traefik.http.routers.authelia-red.middlewares=https-redirect@docker" + + portainer: + image: portainer/portainer-ce:latest + container_name: portainer + restart: always + command: -H tcp://socket-proxy:2375 + volumes: + - portainer_data:/data + networks: + - socket-net + - proxy-net + labels: + - "traefik.enable=true" + # Public (HTTPS) + - "traefik.http.routers.portainer.rule=Host(`portainer.${DOMAIN}`)" + - "traefik.http.routers.portainer.entrypoints=websecure" + - "traefik.http.routers.portainer.tls=true" + - "traefik.http.routers.portainer.middlewares=authelia-auth@docker" + - "traefik.http.routers.portainer.service=portainer-svc" + # Public (Redirect) + - "traefik.http.routers.portainer-red.rule=Host(`portainer.${DOMAIN}`)" + - "traefik.http.routers.portainer-red.entrypoints=web" + - "traefik.http.routers.portainer-red.middlewares=https-redirect@docker" + - "traefik.http.services.portainer-svc.loadbalancer.server.port=9000" + + dozzle: + image: amir20/dozzle:latest + container_name: dozzle + restart: always + environment: + - DOCKER_HOST=tcp://socket-proxy:2375 + - DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40} + networks: + - proxy-net + - socket-net + labels: + - "traefik.enable=true" + # Public (HTTPS) + - "traefik.http.routers.logs.rule=Host(`logs.${DOMAIN}`)" + - "traefik.http.routers.logs.entrypoints=websecure" + - "traefik.http.routers.logs.tls=true" + - "traefik.http.routers.logs.middlewares=authelia-auth@docker" + - "traefik.http.routers.logs.service=logs-svc" + # Public (Redirect) + - "traefik.http.routers.logs-red.rule=Host(`logs.${DOMAIN}`)" + - "traefik.http.routers.logs-red.entrypoints=web" + - "traefik.http.routers.logs-red.middlewares=https-redirect@docker" + - "traefik.http.services.logs-svc.loadbalancer.server.port=8080" + + watchtower: + image: containrrr/watchtower + container_name: watchtower + restart: always + environment: + - DOCKER_HOST=tcp://socket-proxy:2375 + - DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40} + - WATCHTOWER_CLEANUP=true + - WATCHTOWER_LABEL_ENABLE=true + networks: + - socket-net + + diun: + image: crazymax/diun:latest + container_name: diun + restart: always + command: serve + environment: + - TZ=UTC + - LOG_LEVEL=info + - LOG_JSON=false + - DIUN_WATCH_WORKERS=20 + - DIUN_WATCH_SCHEDULE=0 */6 * * * + - DIUN_PROVIDERS_DOCKER=true + - DIUN_PROVIDERS_DOCKER_ENDPOINT=tcp://socket-proxy:2375 + - DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40} + volumes: + - ./config/diun:/data + networks: + - socket-net + + homepage: + image: ghcr.io/gethomepage/homepage:latest + container_name: homepage + restart: always + volumes: + - ./config/homepage:/app/config + - /home:/home:ro + environment: + - HOMEPAGE_ALLOWED_HOSTS=${DOMAIN} + networks: + - proxy-net + - socket-net + labels: + - "traefik.enable=true" + # Public (HTTPS) + - "traefik.http.routers.homepage.rule=Host(`${DOMAIN}`)" + - "traefik.http.routers.homepage.entrypoints=websecure" + - "traefik.http.routers.homepage.tls=true" + - "traefik.http.routers.homepage.service=homepage-svc" + # Public (Redirect) + - "traefik.http.routers.homepage-red.rule=Host(`${DOMAIN}`)" + - "traefik.http.routers.homepage-red.entrypoints=web" + - "traefik.http.routers.homepage-red.middlewares=https-redirect@docker" + - "traefik.http.services.homepage-svc.loadbalancer.server.port=3000" + +networks: + proxy-net: + name: proxy-net + driver: bridge + socket-net: + name: socket-net + driver: bridge + internal: true + +volumes: + portainer_data: diff --git a/docs/adr-001-remove-localhost-routers.md b/docs/legacy/adr-001-remove-localhost-routers.md similarity index 100% rename from docs/adr-001-remove-localhost-routers.md rename to docs/legacy/adr-001-remove-localhost-routers.md diff --git a/docs/adr-002-migrate-dynu-discovery-to-nixos.md b/docs/legacy/adr-002-migrate-dynu-discovery-to-nixos.md similarity index 100% rename from docs/adr-002-migrate-dynu-discovery-to-nixos.md rename to docs/legacy/adr-002-migrate-dynu-discovery-to-nixos.md diff --git a/docs/architecture.html b/docs/legacy/architecture.html similarity index 100% rename from docs/architecture.html rename to docs/legacy/architecture.html diff --git a/docs/dynu-ip-update-protocol.html b/docs/legacy/dynu-ip-update-protocol.html similarity index 100% rename from docs/dynu-ip-update-protocol.html rename to docs/legacy/dynu-ip-update-protocol.html diff --git a/docs/index.html b/docs/legacy/index.html similarity index 100% rename from docs/index.html rename to docs/legacy/index.html diff --git a/docs/roadmap.html b/docs/legacy/roadmap.html similarity index 100% rename from docs/roadmap.html rename to docs/legacy/roadmap.html diff --git a/docs/style.css b/docs/legacy/style.css similarity index 100% rename from docs/style.css rename to docs/legacy/style.css diff --git a/docs/troubleshooting.html b/docs/legacy/troubleshooting.html similarity index 100% rename from docs/troubleshooting.html rename to docs/legacy/troubleshooting.html diff --git a/docs/usability.html b/docs/legacy/usability.html similarity index 100% rename from docs/usability.html rename to docs/legacy/usability.html diff --git a/docs/setup_guide.md b/docs/setup_guide.md new file mode 100644 index 0000000..9fe0c14 --- /dev/null +++ b/docs/setup_guide.md @@ -0,0 +1,119 @@ +# Setup & Secrets Management Guide (Traefik Homelab) + +This document provides a guide for deploying, managing, and securing the consolidated homeserver core stack. + +--- + +## 🏗️ Architectural Overview + +The core infrastructure is organized as a single, unified Docker Compose stack: + +* **/docker-compose.yml**: Root orchestrator defining all core services (`traefik`, `authelia`, `portainer`, `dozzle`, `watchtower`, `diun`, `homepage`, `socket-proxy`). +* **/config/**: Holds the configuration directories and sqlite databases for all services: + * `config/letsencrypt/`: Contains SSL certificate state (`acme.json`). + * `config/authelia/`: Contains single sign-on configs (`configuration.yml`, `users.yml`) and database (`db.sqlite3`). + * `config/homepage/`: Contains dashboard layout files (`services.yaml`, `bookmarks.yaml`, etc.). + * `config/diun/`: Diun registry monitoring database (`diun.db`). + +--- + +## 🔒 Credentials & Secrets Management + +Secrets are managed declaratively using **`sops-nix`** (Mozilla SOPS + age keys) inside your system configuration repository (`~/Config`). This keeps all private credentials completely out of Git in an encrypted format. + +### How Secrets Flow: + +``` +[~/Config/hosts/desktop/secrets.yaml] (Encrypted) + │ + ▼ (Decrypted at boot by sops-nix using SSH host key) +[/run/secrets/homeserver.env] (Runtime Environment File) + │ + ▼ (Fed via --env-file flag into Docker Compose) +[Docker Containers] (Active environment variables) +``` + +--- + +## 🔑 Adding/Editing Secrets + +To add or modify credentials (e.g. changing your Dynu API key or regenerating Authelia secrets): + +1. Navigate to your system configuration repository: + ```bash + cd ~/Config + ``` +2. Decrypt and open the secrets file in your editor using SOPS: + ```bash + nix-shell -p sops --run "sops hosts/desktop/secrets.yaml" + ``` +3. Append or edit the required keys under the root block: + ```yaml + dynu_api_key: "your_dynu_api_key" + acme_email: "your_email@provider.com" + authelia_session_secret: "secure_64_character_hex_string" + authelia_storage_encryption_key: "secure_64_character_hex_string" + authelia_identity_validation_reset_password_jwt_secret: "secure_64_character_hex_string" + ``` +4. Save and close the editor. SOPS will automatically encrypt the file and write it back to disk. + +--- + +## ⚙️ NixOS Integration + +The stack is managed as a declarative systemd service inside `/home/kiskaadee/Config/hosts/desktop/homeserver.nix`: + +### 1. Secret Environment Template +The module defines a SOPS template to gather all secrets and structure them as key-value pairs at `/run/secrets/homeserver.env`: +```nix +sops.templates."homeserver.env" = { + owner = "kiskaadee"; + content = lib.generators.toKeyValue {} { + DOMAIN = "arch-services.mywire.org"; + DOCKER_API_VERSION = "1.40"; + DYNU_API_KEY = config.sops.placeholder.dynu_api_key; + ACME_EMAIL = config.sops.placeholder.acme_email; + AUTHELIA_SESSION_SECRET = config.sops.placeholder.authelia_session_secret; + AUTHELIA_STORAGE_ENCRYPTION_KEY = config.sops.placeholder.authelia_storage_encryption_key; + AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET = config.sops.placeholder.authelia_identity_validation_reset_password_jwt_secret; + }; +}; +``` + +### 2. systemd Lifecycle Daemon +The service starts and stops the compose stack automatically: +```nix +systemd.services.homeserver-core = { + description = "Homeserver Core Stack"; + after = [ "network-online.target" "docker.service" ]; + wants = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + WorkingDirectory = "/home/kiskaadee/Deployments/homeserver"; + ExecStart = "${pkgs.docker-compose}/bin/docker-compose --env-file ${config.sops.templates."homeserver.env".path} up -d --remove-orphans"; + ExecStop = "${pkgs.docker-compose}/bin/docker-compose down"; + }; +}; +``` + +--- + +## 🚀 Running and Managing the Stack + +Because the systemd service manages the containers, standard `systemctl` commands can be used: + +* **Start the stack**: `sudo systemctl start homeserver-core` +* **Stop the stack**: `sudo systemctl stop homeserver-core` +* **Check service status**: `systemctl status homeserver-core` +* **Check logs**: `journalctl -u homeserver-core` + +### Dynamic Configuration Tweaks (Non-Nix Workflow) +If you modify `docker-compose.yml` (e.g. adding a new Traefik routing label), you do **not** need to rebuild NixOS! You can apply it instantly: +```bash +cd /home/kiskaadee/Deployments/homeserver +docker compose --env-file /run/secrets/homeserver.env up -d --remove-orphans +``` +This preserves the extreme agility of Docker Compose while keeping your system configuration and secrets declaratively managed. diff --git a/down.sh b/down.sh deleted file mode 100755 index b6fa104..0000000 --- a/down.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -stop_stack() { - local stack_path=$1 - shift - local env_flags=() - for env_file in "$@"; do - if [ -f "$env_file" ]; then - env_flags+=("--env-file" "$env_file") - fi - done - if [ -f "$stack_path/docker-compose.yml" ]; then - echo "Stopping stack: $stack_path..." - docker compose -f "$stack_path/docker-compose.yml" "${env_flags[@]}" down - fi -} - -echo "=== Stopping Portal Layer ===" -stop_stack "homepage" "infra/core/.env" - -echo "=== Stopping Infrastructure Layer ===" -stop_stack "infra/dozzle" -stop_stack "infra/diun" -stop_stack "infra/watchtower" -stop_stack "infra/portainer" -stop_stack "infra/authelia" "infra/core/.env" "infra/authelia/.env" -stop_stack "infra/core" "infra/core/.env" - -echo "All services stopped." diff --git a/homepage/docker-compose.yml b/homepage/docker-compose.yml deleted file mode 100644 index cbd2b1d..0000000 --- a/homepage/docker-compose.yml +++ /dev/null @@ -1,36 +0,0 @@ -services: - homepage: - image: ghcr.io/gethomepage/homepage:latest - container_name: homepage - restart: always - volumes: - - ./config:/app/config - - /home:/home:ro - environment: - - HOMEPAGE_ALLOWED_HOSTS=${DOMAIN} - networks: - - proxy-net - - socket-net - labels: - - "traefik.enable=true" - # Public (HTTPS) - - "traefik.http.routers.homepage.rule=Host(`${DOMAIN}`)" - - "traefik.http.routers.homepage.entrypoints=websecure" - - "traefik.http.routers.homepage.tls=true" - - "traefik.http.routers.homepage.service=homepage-svc" - - # Public (Redirect) - - "traefik.http.routers.homepage-red.rule=Host(`${DOMAIN}`)" - - "traefik.http.routers.homepage-red.entrypoints=web" - - "traefik.http.routers.homepage-red.middlewares=https-redirect@docker" - - - "traefik.http.services.homepage-svc.loadbalancer.server.port=3000" - - - -networks: - proxy-net: - external: true - socket-net: - external: true - diff --git a/infra/authelia/.env-example b/infra/authelia/.env-example deleted file mode 100644 index fd76f3c..0000000 --- a/infra/authelia/.env-example +++ /dev/null @@ -1,6 +0,0 @@ -# Authelia Configuration Environment Variables -# Generate secure random secrets (e.g., openssl rand -hex 32) for these fields - -AUTHELIA_SESSION_SECRET= -AUTHELIA_STORAGE_ENCRYPTION_KEY= -AUTHELIA_IDENTITY_VALIDATION_RESET_PASSWORD_JWT_SECRET= diff --git a/infra/authelia/docker-compose.yml b/infra/authelia/docker-compose.yml deleted file mode 100644 index 0003f00..0000000 --- a/infra/authelia/docker-compose.yml +++ /dev/null @@ -1,26 +0,0 @@ -services: - authelia: - image: authelia/authelia:latest - container_name: authelia - restart: always - volumes: - - ./config:/config - environment: - - DOMAIN=${DOMAIN} - networks: - - proxy-net - labels: - - "traefik.enable=true" - # Public (HTTPS) - - "traefik.http.routers.authelia.rule=Host(`auth.${DOMAIN}`)" - - "traefik.http.routers.authelia.entrypoints=websecure" - - "traefik.http.routers.authelia.tls=true" - - # Public (Redirect) - - "traefik.http.routers.authelia-red.rule=Host(`auth.${DOMAIN}`)" - - "traefik.http.routers.authelia-red.entrypoints=web" - - "traefik.http.routers.authelia-red.middlewares=https-redirect@docker" - -networks: - proxy-net: - external: true diff --git a/infra/core/.env-example b/infra/core/.env-example deleted file mode 100644 index df9e2fa..0000000 --- a/infra/core/.env-example +++ /dev/null @@ -1,3 +0,0 @@ -DYNU_API_KEY=your_secret_api_key_here -ACME_EMAIL=your-email@example.com -DOMAIN=somewhere.domain.com diff --git a/infra/core/docker-compose.yml b/infra/core/docker-compose.yml deleted file mode 100644 index 5d09f41..0000000 --- a/infra/core/docker-compose.yml +++ /dev/null @@ -1,103 +0,0 @@ -services: - socket-proxy: - image: tecnativa/docker-socket-proxy - container_name: socket-proxy - restart: always - volumes: - - /var/run/docker.sock:/var/run/docker.sock:ro # Only place the real socket is mounted - environment: - - CONTAINERS=1 # Traefik needs to see containers - - NETWORKS=1 # Traefik needs to see networks - - SERVICES=1 - - TASKS=1 - - IMAGES=1 # Needed for Watchtower/Portainer - - VOLUMES=1 # Needed for Portainer - - POST=1 # Needed for Portainer/Watchtower to start/stop/create containers - - DELETE=1 # Needed for Portainer/Watchtower to remove containers/images - - DOCKER_API_VERSION=1.40 - - VERSION=1 # Allow Traefik to query version - - INFO=1 # Allow Traefik to query info - - EVENTS=1 # Allow Traefik to monitor container changes - - PING=1 # Allow Traefik to check Docker health - - BUILD=0 - - COMMIT=0 - - CONFIGS=0 - - DISTRIBUTION=0 - - EXEC=0 - - NODES=0 - - PLUGINS=0 - - SECRETS=0 - - SWARM=0 - - SYSTEM=0 - networks: - - socket-net - - traefik: - image: traefik:v3.6 - container_name: traefik - restart: always - depends_on: - - socket-proxy - ports: - - "80:80" - - "443:443" - environment: - - DYNU_API_KEY=${DYNU_API_KEY} - - DOCKER_API_VERSION=1.40 - command: - - "--log.level=INFO" - - "--api.dashboard=true" - - "--providers.docker=true" - - "--providers.docker.endpoint=tcp://socket-proxy:2375" - - "--providers.docker.exposedByDefault=false" - - "--entrypoints.web.address=:80" - - "--entrypoints.websecure.address=:443" - # Global TLS defaults for websecure - - "--entrypoints.websecure.http.tls.certResolver=myresolver" - - "--entrypoints.websecure.http.tls.domains[0].main=${DOMAIN}" - - "--entrypoints.websecure.http.tls.domains[0].sans=*.${DOMAIN}" - # SSL Setup - - "--certificatesresolvers.myresolver.acme.dnschallenge=true" - - "--certificatesresolvers.myresolver.acme.dnschallenge.provider=dynu" - - "--certificatesresolvers.myresolver.acme.email=${ACME_EMAIL}" - - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json" - volumes: - - ./letsencrypt:/letsencrypt - networks: - - socket-net - - proxy-net - labels: - - "traefik.enable=true" - - # Middleware: Redirect to HTTPS - - "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https" - - "traefik.http.middlewares.https-redirect.redirectscheme.permanent=true" - - # Middleware: Authelia Guard - - "traefik.http.middlewares.authelia-auth.forwardauth.address=http://authelia:9091/api/verify?rd=https://auth.${DOMAIN}/" - - "traefik.http.middlewares.authelia-auth.forwardauth.trustForwardHeader=true" - - "traefik.http.middlewares.authelia-auth.forwardauth.authResponseHeaders=Remote-User,Remote-Groups,Remote-Name,Remote-Email" - - # Dashboard - Public (HTTPS) - - "traefik.http.routers.traefik-dash.rule=Host(`traefik.${DOMAIN}`)" - - "traefik.http.routers.traefik-dash.entrypoints=websecure" - - "traefik.http.routers.traefik-dash.tls=true" - - "traefik.http.routers.traefik-dash.service=api@internal" - - "traefik.http.routers.traefik-dash.middlewares=authelia-auth@docker" - - # Dashboard - Public (Redirect) - - "traefik.http.routers.traefik-dash-red.rule=Host(`traefik.${DOMAIN}`)" - - "traefik.http.routers.traefik-dash-red.entrypoints=web" - - "traefik.http.routers.traefik-dash-red.middlewares=https-redirect@docker" - - logging: - driver: "json-file" - options: - max-size: "10m" - max-file: "3" - -networks: - socket-net: - external: true - proxy-net: - external: true diff --git a/infra/core/traefik.yml b/infra/core/traefik.yml deleted file mode 100644 index e69de29..0000000 diff --git a/infra/diun/docker-compose.yml b/infra/diun/docker-compose.yml deleted file mode 100644 index f4e2aee..0000000 --- a/infra/diun/docker-compose.yml +++ /dev/null @@ -1,23 +0,0 @@ -services: - diun: - image: crazymax/diun:latest - container_name: diun - restart: always - command: serve - environment: - - TZ=UTC - - LOG_LEVEL=info - - LOG_JSON=false - - DIUN_WATCH_WORKERS=20 - - DIUN_WATCH_SCHEDULE=0 */6 * * * - - DIUN_PROVIDERS_DOCKER=true - - DIUN_PROVIDERS_DOCKER_ENDPOINT=tcp://socket-proxy:2375 - - DOCKER_API_VERSION=1.40 - volumes: - - "./data:/data" - networks: - - socket-net - -networks: - socket-net: - external: true diff --git a/infra/dozzle/docker-compose.yml b/infra/dozzle/docker-compose.yml deleted file mode 100644 index d937caf..0000000 --- a/infra/dozzle/docker-compose.yml +++ /dev/null @@ -1,29 +0,0 @@ -services: - dozzle: - image: amir20/dozzle:latest - container_name: dozzle - environment: - - DOCKER_HOST=tcp://socket-proxy:2375 - - DOCKER_API_VERSION=1.40 - networks: - - proxy-net - - socket-net - labels: - - "traefik.enable=true" - - "traefik.http.routers.logs.rule=Host(`logs.arch-services.mywire.org`)" - - "traefik.http.routers.logs.entrypoints=websecure" - - "traefik.http.routers.logs.tls=true" - - "traefik.http.routers.logs.middlewares=authelia-auth@docker" - - "traefik.http.routers.logs.service=logs-svc" - - "traefik.http.routers.logs-red.rule=Host(`logs.arch-services.mywire.org`)" - - "traefik.http.routers.logs-red.entrypoints=web" - - "traefik.http.routers.logs-red.middlewares=https-redirect@docker" - - - "traefik.http.services.logs-svc.loadbalancer.server.port=8080" - - -networks: - proxy-net: - external: true - socket-net: - external: true diff --git a/infra/portainer/docker-compose.yml b/infra/portainer/docker-compose.yml deleted file mode 100644 index de13706..0000000 --- a/infra/portainer/docker-compose.yml +++ /dev/null @@ -1,32 +0,0 @@ -services: - portainer: - image: portainer/portainer-ce:latest - container_name: portainer - restart: always - command: -H tcp://socket-proxy:2375 - volumes: - - portainer_data:/data - networks: - - socket-net - - proxy-net - labels: - - "traefik.enable=true" - - "traefik.http.routers.portainer.rule=Host(`portainer.arch-services.mywire.org`)" - - "traefik.http.routers.portainer.entrypoints=websecure" - - "traefik.http.routers.portainer.tls=true" - - "traefik.http.routers.portainer.service=portainer-svc" - - "traefik.http.routers.portainer-red.rule=Host(`portainer.arch-services.mywire.org`)" - - "traefik.http.routers.portainer-red.entrypoints=web" - - "traefik.http.routers.portainer-red.middlewares=https-redirect@docker" - - - "traefik.http.services.portainer-svc.loadbalancer.server.port=9000" - - -networks: - socket-net: - external: true - proxy-net: - external: true - -volumes: - portainer_data: diff --git a/infra/watchtower/docker-compose.yml b/infra/watchtower/docker-compose.yml deleted file mode 100644 index d1cc805..0000000 --- a/infra/watchtower/docker-compose.yml +++ /dev/null @@ -1,16 +0,0 @@ -services: - watchtower: - image: containrrr/watchtower - container_name: watchtower - restart: always - environment: - - DOCKER_HOST=tcp://socket-proxy:2375 - - DOCKER_API_VERSION=1.40 - - WATCHTOWER_CLEANUP=true - - WATCHTOWER_LABEL_ENABLE=true - networks: - - socket-net - -networks: - socket-net: - external: true diff --git a/scripts/purge-dynu.sh b/scripts/purge-dynu.sh index 245e373..681d1b0 100755 --- a/scripts/purge-dynu.sh +++ b/scripts/purge-dynu.sh @@ -2,7 +2,7 @@ # Path to your configuration # Changed to project relative path -ENV_FILE="infra/core/.env" +ENV_FILE="${ENV_FILE:-.env}" # 1. Load Environment Variables if [ -f "$ENV_FILE" ]; then @@ -44,6 +44,6 @@ fi # 4. Restart Traefik to trigger new challenge echo "Restarting Traefik..." -cd infra/core/ && docker compose restart traefik +docker compose restart traefik echo "Process complete. Monitor logs with: docker logs traefik 2>&1 | grep -iE 'acme|dynu'" diff --git a/up.sh b/up.sh deleted file mode 100755 index 8452c97..0000000 --- a/up.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -# --- 1. NETWORK & PERMISSIONS --- -docker network inspect proxy-net >/dev/null 2>&1 || docker network create proxy-net -docker network inspect socket-net >/dev/null 2>&1 || docker network create socket-net - -# SSL Permissions (Moved to infra/core) -if [ ! -f infra/core/letsencrypt/acme.json ]; then - echo "Initializing letsencrypt/acme.json..." - sudo mkdir -p infra/core/letsencrypt - sudo touch infra/core/letsencrypt/acme.json - sudo chmod 600 infra/core/letsencrypt/acme.json - sudo chown root:root infra/core/letsencrypt/acme.json -fi - -# --- 2. HELPER FUNCTIONS --- -start_stack() { - local stack_path=$1 - shift - local env_flags=() - for env_file in "$@"; do - if [ -f "$env_file" ]; then - env_flags+=("--env-file" "$env_file") - fi - done - if [ -f "$stack_path/docker-compose.yml" ]; then - echo "Starting stack: $stack_path..." - docker compose -f "$stack_path/docker-compose.yml" "${env_flags[@]}" up -d - else - echo "Warning: No docker-compose.yml found in $stack_path" - fi -} - -# --- 3. DEPLOY CONTROL PLANE & PORTAL --- -echo "=== Deploying Control Plane ===" -start_stack "infra/core" "infra/core/.env" -start_stack "infra/authelia" "infra/core/.env" "infra/authelia/.env" -start_stack "infra/portainer" -start_stack "infra/watchtower" -start_stack "infra/diun" -start_stack "infra/dozzle" - -echo "=== Deploying Homepage Portal ===" -start_stack "homepage" "infra/core/.env" - -echo "" -echo "Hardened Private Cloud Deployment Complete."