From 0576b3c60b896eec3dee12efdef344721c37db54 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Sun, 4 Oct 2026 13:42:02 +0200 Subject: [PATCH] feat(colmena): passwordless activation via sudoers rules scoped to colmena's exact commands Colmena connects with BatchMode=yes and no TTY (src/nix/host/ssh.rs), so a sudo password prompt can never be answered - non-root deploys REQUIRE NOPASSWD. New extraServices.colmenaDeploy (enabled on all five hosts) grants m3tam3re passwordless sudo for exactly the two invocations colmena performs (verified against colmena 0.5/0.6 profile.rs + host/{ssh,local}.rs, default NixFlags are empty): - /run/current-system/sw/bin/nix-env --profile /nix/var/nix/profiles/system --set /nix/store/* (system profile switch, goals switch/boot) - /nix/store/*/bin/switch-to-configuration (activation, exact single argument) Everything else keeps requiring the sudo password; extraRules sort after the wheel rules, so last-match-wins keeps the exception scoped. Also enables passwordless 'colmena apply-local' on the desktops, which uses the same activation path. Residual risk (documented in the module): anyone holding the deploy user's SSH key can activate arbitrary store closures - that is the deploy capability itself, inherent to NixOS remote activation. One-time bootstrap per server (rules arrive WITH a deploy, and the first deploy cannot answer a password prompt): nixos-rebuild switch --flake .#m3-atlas --target-host m3tam3re@m3-atlas --use-remote-sudo --- AGENTS.md | 2 +- flake.nix | 3 +- hosts/common/extraServices/colmena-deploy.nix | 58 +++++++++++++++++++ hosts/common/extraServices/default.nix | 1 + hosts/m3-ares/default.nix | 1 + hosts/m3-atlas/default.nix | 1 + hosts/m3-helios/default.nix | 1 + hosts/m3-hermes/default.nix | 1 + hosts/m3-kratos/default.nix | 1 + 9 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 hosts/common/extraServices/colmena-deploy.nix diff --git a/AGENTS.md b/AGENTS.md index 5c0e855..7fd2d38 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -304,7 +304,7 @@ This provides the familiar shell/editor/CLI setup but with work git credentials | **Enter dev shell** | `nix develop` | Includes alejandra, nixd, agenix, statix, deadnix | | **Build host** | `sudo nixos-rebuild switch --flake .#m3-ares` | Replace hostname as needed | | **Dry run build** | `sudo nixos-rebuild dry-run --flake .#m3-ares` | Validate without applying | -| **Deploy servers** | `nix develop -c colmena apply --on @server` | Colmena: all hosts tagged `server` via SSH | +| **Deploy servers** | `nix develop -c colmena apply --on @server` | Colmena: all `server` hosts via SSH; activation is passwordless (scoped sudoers, see extraServices/colmena-deploy.nix) | | **Deploy one host** | `nix develop -c colmena apply --on m3-atlas` | Colmena: single remote host | | **Deploy this host** | `nix develop -c colmena apply-local` | Colmena: local desktop (uses sudo to activate) | | **Preview deploy** | `nix develop -c colmena build --on m3-atlas` | Colmena: build closure without switching | diff --git a/flake.nix b/flake.nix index d7f8cf8..318520a 100644 --- a/flake.nix +++ b/flake.nix @@ -163,7 +163,8 @@ nixpkgs = import nixpkgs { system = "x86_64-linux"; overlays = []; - }; specialArgs = mkSpecialArgs null; + }; + specialArgs = mkSpecialArgs null; nodeSpecialArgs = nixpkgs.lib.mapAttrs (_: host: mkSpecialArgs host.hostname) diff --git a/hosts/common/extraServices/colmena-deploy.nix b/hosts/common/extraServices/colmena-deploy.nix new file mode 100644 index 0000000..5ec7e7a --- /dev/null +++ b/hosts/common/extraServices/colmena-deploy.nix @@ -0,0 +1,58 @@ +# Passwordless privilege escalation for Colmena deployments, scoped to +# exactly the two commands colmena executes on a non-root target host +# (colmena src/nix/host/ssh.rs + profile.rs, verified for 0.5/0.6): +# +# sudo -H -- nix-env --profile /nix/var/nix/profiles/system --set /nix/store/ +# sudo -H -- /nix/store//bin/switch-to-configuration +# +# Colmena connects with BatchMode=yes and without a TTY, so a sudo +# password prompt can never be answered — non-root deploys REQUIRE +# NOPASSWD for these two invocations. Everything else the user runs via +# sudo still asks for a password: the exception covers only these exact +# command shapes. +# +# Residual risk (inherent to any NixOS remote activation): whoever can +# run these commands can activate any closure present in the store as +# the new system. That is the deploy capability itself, not an +# unintended side effect. +# +# NOTE: if colmena is invoked with extra nix flags (e.g. --show-trace, +# --option cores N) the nix-env argv grows trailing flags and no longer +# matches the pinned rule — plain `colmena apply` is what this covers. +{ + config, + lib, + ... +}: let + deployUser = "m3tam3re"; +in + with lib; { + options.extraServices.colmenaDeploy.enable = + mkEnableOption "passwordless colmena deployment (sudo NOPASSWD scoped to the system profile switch and switch-to-configuration activation only)"; + + config = mkIf config.extraServices.colmenaDeploy.enable { + security.sudo.extraRules = [ + { + users = [deployUser]; + commands = + # Activate a store closure. Only the four goals colmena + # passes, as the sole argument, are allowed. + map (goal: { + command = "/nix/store/*/bin/switch-to-configuration ${goal}"; + options = ["NOPASSWD"]; + }) ["boot" "switch" "test" "dry-activate"] + ++ [ + { + # Make a store closure the system profile (goals + # switch/boot). Over SSH "nix-env" deterministically + # resolves to /run/current-system/sw/bin/nix-env — it + # only ships in the system closure — which is the path + # sudo matches here. + command = "/run/current-system/sw/bin/nix-env --profile /nix/var/nix/profiles/system --set /nix/store/*"; + options = ["NOPASSWD"]; + } + ]; + } + ]; + }; + } diff --git a/hosts/common/extraServices/default.nix b/hosts/common/extraServices/default.nix index 25c54e2..b16a4bd 100644 --- a/hosts/common/extraServices/default.nix +++ b/hosts/common/extraServices/default.nix @@ -1,5 +1,6 @@ { imports = [ + ./colmena-deploy.nix ./flatpak.nix ./podman.nix ./ollama.nix diff --git a/hosts/m3-ares/default.nix b/hosts/m3-ares/default.nix index 695069b..ec00aa8 100644 --- a/hosts/m3-ares/default.nix +++ b/hosts/m3-ares/default.nix @@ -43,6 +43,7 @@ ]; extraServices = { + colmenaDeploy.enable = true; flatpak.enable = true; ollama.enable = false; podman.enable = true; diff --git a/hosts/m3-atlas/default.nix b/hosts/m3-atlas/default.nix index 47932d0..d05edae 100644 --- a/hosts/m3-atlas/default.nix +++ b/hosts/m3-atlas/default.nix @@ -42,6 +42,7 @@ ]; extraServices = { + colmenaDeploy.enable = true; flatpak.enable = false; ollama.enable = false; podman.enable = true; diff --git a/hosts/m3-helios/default.nix b/hosts/m3-helios/default.nix index 0d317fe..32a9111 100644 --- a/hosts/m3-helios/default.nix +++ b/hosts/m3-helios/default.nix @@ -42,6 +42,7 @@ ]; extraServices = { + colmenaDeploy.enable = true; flatpak.enable = true; ollama.enable = false; podman.enable = true; diff --git a/hosts/m3-hermes/default.nix b/hosts/m3-hermes/default.nix index 1cc59e5..e0e4f71 100644 --- a/hosts/m3-hermes/default.nix +++ b/hosts/m3-hermes/default.nix @@ -8,6 +8,7 @@ ]; extraServices = { + colmenaDeploy.enable = true; flatpak.enable = false; ollama.enable = false; podman.enable = false; diff --git a/hosts/m3-kratos/default.nix b/hosts/m3-kratos/default.nix index f483ee8..0ee8091 100644 --- a/hosts/m3-kratos/default.nix +++ b/hosts/m3-kratos/default.nix @@ -43,6 +43,7 @@ ]; extraServices = { + colmenaDeploy.enable = true; flatpak.enable = true; ollama.enable = true; podman.enable = true;