feat(nixos): add homelab.workloadGrants option, grants.json projection, and instance.example.nix
- Define homelab.workloadGrants attrsOf submodule in nixos/modules/homeserver.nix with devices, groups, and readOnlyPaths - Add Nix assertions rejecting privileged groups (0, root, wheel, docker) and relative/traversal paths - Generate /etc/homelab/grants.json (root:homelab-operators 0640) and wire HOMELAB_GRANTS_FILE into homelab-appctl and homelab-gitops - Add tracked nixos/instance.example.nix and prioritize /etc/nixos/homelab-instance.nix over nixos/instance.nix - Add structural and security invariant tests for workloadGrants and instance.example.nix
This commit is contained in:
1 parent
e807a90017
commit
ce969f4678
5 files changed
+98
-2
No files matched your search
@@ -12,8 +12,8 @@
|
|||||||
./modules/traefik-deployments.nix # Traefik deployment secrets and env templates
|
./modules/traefik-deployments.nix # Traefik deployment secrets and env templates
|
||||||
./modules/dynu.nix # Dynu DDNS smart IP monitor service
|
./modules/dynu.nix # Dynu DDNS smart IP monitor service
|
||||||
./modules/shell.nix # Declarative bash environment for the server
|
./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 platform settings ────────────────────────────────────────────────
|
||||||
homelab.operatorUser = "operator";
|
homelab.operatorUser = "operator";
|
||||||
|
|||||||
@@ -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/<app>.
|
||||||
|
homelab.workloadGrants = {
|
||||||
|
# Example hardware-accelerated media workload:
|
||||||
|
# media-server = {
|
||||||
|
# devices = [ "/dev/dri/renderD128" ];
|
||||||
|
# groups = [ "render" ];
|
||||||
|
# readOnlyPaths = [ "/mnt/media" ];
|
||||||
|
# };
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -26,12 +26,69 @@
|
|||||||
default = "/home/${config.homelab.operatorUser}/Workloads";
|
default = "/home/${config.homelab.operatorUser}/Workloads";
|
||||||
description = "Path to Workloads directory (legacy option name: sitesDir)";
|
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
|
config = let
|
||||||
domainParts = lib.splitString "." config.homelab.domain;
|
domainParts = lib.splitString "." config.homelab.domain;
|
||||||
ldapBaseDn = lib.concatStringsSep "," (map (part: "dc=${part}") domainParts);
|
ldapBaseDn = lib.concatStringsSep "," (map (part: "dc=${part}") domainParts);
|
||||||
in {
|
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 ────────────────────────────────────────────────────
|
# ── Host Activation Guard ────────────────────────────────────────────────────
|
||||||
# Prevents activating on a live host if homelab.domain defaulted to example.test (e.g. forgot --impure)
|
# Prevents activating on a live host if homelab.domain defaulted to example.test (e.g. forgot --impure)
|
||||||
system.activationScripts.checkHomelabDomain = {
|
system.activationScripts.checkHomelabDomain = {
|
||||||
@@ -146,6 +203,7 @@
|
|||||||
WORKLOADS_DIR = config.homelab.sitesDir;
|
WORKLOADS_DIR = config.homelab.sitesDir;
|
||||||
SITES_DIR = config.homelab.sitesDir;
|
SITES_DIR = config.homelab.sitesDir;
|
||||||
HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml";
|
HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml";
|
||||||
|
HOMELAB_GRANTS_FILE = "/etc/homelab/grants.json";
|
||||||
};
|
};
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
@@ -226,6 +284,7 @@
|
|||||||
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
HOMELAB_STATE_ROOT = "/home/${config.homelab.operatorUser}/var/lib/homelab";
|
||||||
HOMELAB_CONFIG_DIR = "/home/${config.homelab.operatorUser}/.config/homelab";
|
HOMELAB_CONFIG_DIR = "/home/${config.homelab.operatorUser}/.config/homelab";
|
||||||
HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml";
|
HOMELAB_DEPLOYMENTS_FILE = "${config.homelab.sitesDir}/deployments.yaml";
|
||||||
|
HOMELAB_GRANTS_FILE = "/etc/homelab/grants.json";
|
||||||
};
|
};
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
|||||||
@@ -210,6 +210,7 @@ def test_platform_genericism_no_hardcoded_personal_domains_in_scripts_and_tests(
|
|||||||
CORE_ROOT / "flake.nix",
|
CORE_ROOT / "flake.nix",
|
||||||
CORE_ROOT / "docker-compose.yml",
|
CORE_ROOT / "docker-compose.yml",
|
||||||
CORE_ROOT / "nixos" / "configuration.nix",
|
CORE_ROOT / "nixos" / "configuration.nix",
|
||||||
|
CORE_ROOT / "nixos" / "instance.example.nix",
|
||||||
]
|
]
|
||||||
violations = []
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@ def test_required_architectural_paths_exist():
|
|||||||
"SECURITY.md",
|
"SECURITY.md",
|
||||||
"nixos",
|
"nixos",
|
||||||
"nixos/configuration.nix",
|
"nixos/configuration.nix",
|
||||||
|
"nixos/instance.example.nix",
|
||||||
"nixos/modules/homeserver.nix",
|
"nixos/modules/homeserver.nix",
|
||||||
"nixos/secrets.yaml",
|
"nixos/secrets.yaml",
|
||||||
"scripts/gitops_dispatcher.py",
|
"scripts/gitops_dispatcher.py",
|
||||||
|
|||||||
Reference in new issue
Block a user