diff --git a/nixos/configuration.nix b/nixos/configuration.nix index d9cefec..46b916e 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -12,8 +12,8 @@ ./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; + ] ++ lib.optional (builtins.pathExists /etc/nixos/homelab-instance.nix) /etc/nixos/homelab-instance.nix + ++ lib.optional (!(builtins.pathExists /etc/nixos/homelab-instance.nix) && builtins.pathExists ./instance.nix) ./instance.nix; # ── Homelab platform settings ──────────────────────────────────────────────── homelab.operatorUser = "operator"; diff --git a/nixos/instance.example.nix b/nixos/instance.example.nix new file mode 100644 index 0000000..732d21b --- /dev/null +++ b/nixos/instance.example.nix @@ -0,0 +1,20 @@ +# Example host-local instance configuration for Canopy NixOS Core. +# Copy to /etc/nixos/homelab-instance.nix (recommended, loaded with --impure) +# or gitignored nixos/instance.nix, and customize for your host. +{ ... }: + +{ + homelab.domain = "example.com"; + homelab.operatorUser = "operator"; + + # Host-granted workload capabilities ("Compose Requests, Nix Grants" — ADR-0005). + # Keyed by workload name matching ~/Workloads/. + homelab.workloadGrants = { + # Example hardware-accelerated media workload: + # media-server = { + # devices = [ "/dev/dri/renderD128" ]; + # groups = [ "render" ]; + # readOnlyPaths = [ "/mnt/media" ]; + # }; + }; +} diff --git a/nixos/modules/homeserver.nix b/nixos/modules/homeserver.nix index decb377..9ae8d3b 100644 --- a/nixos/modules/homeserver.nix +++ b/nixos/modules/homeserver.nix @@ -26,12 +26,69 @@ default = "/home/${config.homelab.operatorUser}/Workloads"; description = "Path to Workloads directory (legacy option name: sitesDir)"; }; + workloadGrants = lib.mkOption { + type = lib.types.attrsOf (lib.types.submodule { + options = { + devices = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "Host device paths permitted for this workload (prefer specific nodes such as /dev/dri/renderD128)."; + }; + groups = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "Host group names or numeric GIDs permitted in group_add and user: primary GID."; + }; + readOnlyPaths = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "Host directories permitted for read-only (:ro) bind mounts."; + }; + }; + }); + default = {}; + description = "Host-granted hardware devices, groups, and read-only storage paths keyed by workload name."; + }; }; config = let domainParts = lib.splitString "." config.homelab.domain; ldapBaseDn = lib.concatStringsSep "," (map (part: "dc=${part}") domainParts); in { + # ── Workload Capability Grant Invariants (ADR-0005) ───────────────────────── + assertions = let + grantsList = lib.mapAttrsToList (app: grant: { inherit app grant; }) config.homelab.workloadGrants; + privilegedGroups = [ "0" "root" "wheel" "docker" ]; + hasInvalidPath = p: + !(lib.hasPrefix "/" p) + || (lib.hasInfix "/../" p) + || (lib.hasSuffix "/.." p) + || (p == ".."); + in + (map ({ app, grant }: { + assertion = lib.all (g: !(lib.elem (lib.toLower (lib.strings.trim g)) privilegedGroups)) grant.groups; + message = "homelab.workloadGrants.${app}.groups must not contain privileged groups (0, root, wheel, docker)."; + }) grantsList) + ++ (map ({ app, grant }: { + assertion = lib.all (p: !(hasInvalidPath p)) grant.devices; + message = "homelab.workloadGrants.${app}.devices must contain absolute paths without '..' traversal."; + }) grantsList) + ++ (map ({ app, grant }: { + assertion = lib.all (p: !(hasInvalidPath p)) grant.readOnlyPaths; + message = "homelab.workloadGrants.${app}.readOnlyPaths must contain absolute paths without '..' traversal."; + }) grantsList); + + # ── Host Capability Grants Projection (/etc/homelab/grants.json) ───────────── + environment.etc."homelab/grants.json" = { + user = "root"; + group = "homelab-operators"; + mode = "0640"; + text = builtins.toJSON { + schemaVersion = "1.0"; + workloads = config.homelab.workloadGrants; + }; + }; + # ── Host Activation Guard ──────────────────────────────────────────────────── # Prevents activating on a live host if homelab.domain defaulted to example.test (e.g. forgot --impure) system.activationScripts.checkHomelabDomain = { @@ -146,6 +203,7 @@ WORKLOADS_DIR = config.homelab.sitesDir; SITES_DIR = config.homelab.sitesDir; HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml"; + HOMELAB_GRANTS_FILE = "/etc/homelab/grants.json"; }; serviceConfig = { @@ -226,6 +284,7 @@ HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab"; HOMELAB_CONFIG_DIR = "/home/${config.homelab.operatorUser}/.config/homelab"; HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml"; + HOMELAB_GRANTS_FILE = "/etc/homelab/grants.json"; }; serviceConfig = { diff --git a/tests/security/test_security_invariants.py b/tests/security/test_security_invariants.py index 393f89f..9146e05 100644 --- a/tests/security/test_security_invariants.py +++ b/tests/security/test_security_invariants.py @@ -210,6 +210,7 @@ def test_platform_genericism_no_hardcoded_personal_domains_in_scripts_and_tests( CORE_ROOT / "flake.nix", CORE_ROOT / "docker-compose.yml", CORE_ROOT / "nixos" / "configuration.nix", + CORE_ROOT / "nixos" / "instance.example.nix", ] violations = [] @@ -554,4 +555,19 @@ def test_nixos_homeserver_defaults_to_workloads_dir(): ) +def test_nixos_workload_grants_option_and_projection(): + """Ensure homeserver.nix defines homelab.workloadGrants, /etc/homelab/grants.json, and HOMELAB_GRANTS_FILE.""" + homeserver_nix = CORE_ROOT / "nixos" / "modules" / "homeserver.nix" + content = homeserver_nix.read_text(encoding="utf-8") + + assert "workloadGrants = lib.mkOption" in content + assert 'environment.etc."homelab/grants.json"' in content + assert 'user = "root";' in content + assert 'group = "homelab-operators";' in content + assert 'mode = "0640";' in content + assert content.count('HOMELAB_GRANTS_FILE = "/etc/homelab/grants.json";') >= 2 + assert "PrivateUsers" not in content + + + diff --git a/tests/structural/test_repository_structure.py b/tests/structural/test_repository_structure.py index f0a6656..aa40145 100644 --- a/tests/structural/test_repository_structure.py +++ b/tests/structural/test_repository_structure.py @@ -20,6 +20,7 @@ def test_required_architectural_paths_exist(): "SECURITY.md", "nixos", "nixos/configuration.nix", + "nixos/instance.example.nix", "nixos/modules/homeserver.nix", "nixos/secrets.yaml", "scripts/gitops_dispatcher.py",