Files
nixos-core/nixos/configuration.nix
T
kiskaadee 11aaf6038b feat(nixos,security): provision read-only agents SSH user and ForceCommand allowlist
- Provision users.users.agents in nixos/configuration.nix with homelab-operators and systemd-journal groups (excluding wheel and docker) and canopy-agents SSH key
- Add Match User agents ForceCommand wrapper and SSH hardening (AllowTcpForwarding, X11Forwarding, AllowAgentForwarding, PermitTTY, PasswordAuthentication disabled)
- Implement nixos/scripts/agents-force-command.sh allowlisting read-only docker ps/logs/inspect, journalctl, and appctl read subcommands while rejecting metacharacters and mutating commands/flags
- Bind socket-proxy to 127.0.0.1:2375:2375 in docker-compose.yml for loopback-only read-only Docker API access
- Add TDD security and structural tests in tests/security/test_agents_access.py
2026-11-18 22:46:39 -05:00

197 lines
8.5 KiB
Nix

# 🌐 Server NixOS Configuration — Turnkey Headless Homelab Appliance
# Standalone declarative configuration for the homelab server.
# No Home Manager. No shared desktop modules.
# Goal: wipe & restore from any NixOS installation via `nixos-install --flake ~/Core#server`
{ config, lib, pkgs, inputs, ... }:
{
imports = [
./hardware-configuration.nix # Server-specific disk and CPU configuration
./modules/homeserver.nix # Core services: systemd units, Authelia, SOPS secrets
./modules/traefik-deployments.nix # Traefik deployment secrets and env templates
./modules/dynu.nix # Dynu DDNS smart IP monitor service
./modules/shell.nix # Declarative bash environment for the server
] ++ lib.optional (builtins.pathExists ./instance.nix) ./instance.nix
++ lib.optional (builtins.pathExists /etc/nixos/homelab-instance.nix) /etc/nixos/homelab-instance.nix;
# ── Homelab platform settings ────────────────────────────────────────────────
homelab.operatorUser = "operator";
# ── Host Git Safe Directory ──────────────────────────────────────────────────
programs.git = {
enable = true;
config.safe.directory = [
"/home/${config.homelab.operatorUser}/Core"
"/home/${config.homelab.operatorUser}/Workloads"
"/home/${config.homelab.operatorUser}/Workloads/*"
"/home/${config.homelab.operatorUser}/Sites/*"
];
};
# ── SOPS secrets configuration ──────────────────────────────────────────────
# Set once here to avoid duplicate-option conflicts across submodules
sops.defaultSopsFile = ./secrets.yaml;
sops.defaultSopsFormat = "yaml";
# ── Nix settings & maintenance ───────────────────────────────────────────────
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# Pin nixpkgs registry to the flake input (avoids GitHub API calls from the CLI)
nix.registry.nixpkgs.flake = inputs.nixpkgs;
# Automated garbage collection and store hardlink deduplication
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 14d";
};
nix.optimise.automatic = true;
nixpkgs.config.allowUnfree = true;
# ── Regional & locale settings ───────────────────────────────────────────────
time.timeZone = "America/Bogota";
i18n.defaultLocale = "en_US.UTF-8";
# ── Console ──────────────────────────────────────────────────────────────────
console = {
font = "Lat2-Terminus16";
keyMap = "us";
};
# ── Bootloader ───────────────────────────────────────────────────────────────
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
# ── Network ──────────────────────────────────────────────────────────────────
networking.hostName = "server";
networking.networkmanager.enable = true;
# ── User accounts ────────────────────────────────────────────────────────────
users.users.${config.homelab.operatorUser} = {
isNormalUser = true;
description = "Primary server administrator";
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICAVPPMk+WApUd/fo78+lsRGnLKSlY4GDZiNGKwBAifD"
];
extraGroups = [
"wheel" # sudo access
"docker" # run docker without sudo
"homelab-operators" # access /run/homelab/appctl.sock
"networkmanager" # modify network settings
];
};
users.users.agents = {
isNormalUser = true;
description = "Read-only agent inspection user";
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE1pxTdygG5696q0jT7clfJZWtx48ugfUjpzvkHzovVd canopy-agents"
];
extraGroups = [
"homelab-operators" # read-only access via /run/homelab/appctl.sock
"systemd-journal" # read-only access to journalctl
];
};
# ── SSH ──────────────────────────────────────────────────────────────────────
services.openssh = let
agentsForceCommand = pkgs.writeShellScriptBin "agents-force-command" ''
export CORE_DIR="''${CORE_DIR:-/home/${config.homelab.operatorUser}/Core}"
export WORKLOADS_DIR="''${WORKLOADS_DIR:-/home/${config.homelab.operatorUser}/Workloads}"
export DOCKER_HOST="''${DOCKER_HOST:-tcp://127.0.0.1:2375}"
exec ${pkgs.bash}/bin/bash ${./scripts/agents-force-command.sh} "$@"
'';
in {
enable = true;
ports = [ 22 ];
settings.PermitRootLogin = "no";
extraConfig = ''
Match User agents
ForceCommand ${agentsForceCommand}/bin/agents-force-command
AllowTcpForwarding no
X11Forwarding no
AllowAgentForwarding no
PermitTTY no
PasswordAuthentication no
'';
};
# ── Virtualisation ───────────────────────────────────────────────────────────
virtualisation.docker = {
enable = true;
daemon.settings = {
dns = [ "1.1.1.1" "1.0.0.1" ];
};
};
# ── Dynamic binary support ───────────────────────────────────────────────────
programs.nix-ld = {
enable = true;
libraries = with pkgs; [
stdenv.cc.cc.lib
zlib
glib
];
};
# ── Headless optimizations ───────────────────────────────────────────────────
# Disable peripheral hardware & desktop services not needed on a headless server
services.pipewire.enable = false;
services.printing.enable = false;
hardware.bluetooth.enable = false;
# Disable power-saving sleep/suspend to keep the homelab always active
systemd.targets.sleep.enable = false;
systemd.targets.suspend.enable = false;
systemd.targets.hibernate.enable = false;
systemd.targets.hybrid-sleep.enable = false;
# Kernel-level power management tuning
powerManagement.powertop.enable = true;
# ── CLI tools ────────────────────────────────────────────────────────────────
# Minimal server-appropriate tooling (no desktop apps or user dotfiles)
programs.tmux.enable = true;
programs.neovim = {
enable = true;
defaultEditor = true;
};
environment.systemPackages = with pkgs; [
# Core orchestrator wrapper (available system-wide including non-interactive SSH)
(writeShellScriptBin "appctl" ''
exec "''${CORE_DIR:-$HOME/Core}/scripts/appctl" "$@"
'')
# Monitoring & diagnostics
htop
iotop
iftop
ncdu
# Secrets tooling (required for sops-nix secret decryption and manual re-encryption)
sops
age
# Python runtime (hard requirement for appctl, core_manifest, and gitops_dispatcher.py)
(python3.withPackages (ps: with ps; [
pyyaml
pydantic
]))
# Modern shell utilities
eza # ls replacement
ripgrep # rg — fast grep
fd # fast find
tree # directory tree viewer
# Network & transfer
git
curl
wget
rclone
jq
];
# ── State version ─────────────────────────────────────────────────────────────
# Do not modify this value after the first install.
system.stateVersion = "26.05";
}