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 <boot|switch|test|dry-activate>
  (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
This commit is contained in:
m3ta-chiron committed 2026-10-04 13:42:02 +02:00
1 parent db0bfbc707
commit 0576b3c60b
9 files changed
+67 -2

No files matched your search

+1 -1
View File
@@ -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 | | **Enter dev shell** | `nix develop` | Includes alejandra, nixd, agenix, statix, deadnix |
| **Build host** | `sudo nixos-rebuild switch --flake .#m3-ares` | Replace hostname as needed | | **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 | | **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 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) | | **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 | | **Preview deploy** | `nix develop -c colmena build --on m3-atlas` | Colmena: build closure without switching |
+2 -1
View File
@@ -163,7 +163,8 @@
nixpkgs = import nixpkgs { nixpkgs = import nixpkgs {
system = "x86_64-linux"; system = "x86_64-linux";
overlays = []; overlays = [];
}; specialArgs = mkSpecialArgs null; };
specialArgs = mkSpecialArgs null;
nodeSpecialArgs = nodeSpecialArgs =
nixpkgs.lib.mapAttrs nixpkgs.lib.mapAttrs
(_: host: mkSpecialArgs host.hostname) (_: host: mkSpecialArgs host.hostname)
@@ -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/<toplevel>
# sudo -H -- /nix/store/<toplevel>/bin/switch-to-configuration <switch|boot|test|dry-activate>
#
# 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"];
}
];
}
];
};
}
+1
View File
@@ -1,5 +1,6 @@
{ {
imports = [ imports = [
./colmena-deploy.nix
./flatpak.nix ./flatpak.nix
./podman.nix ./podman.nix
./ollama.nix ./ollama.nix
+1
View File
@@ -43,6 +43,7 @@
]; ];
extraServices = { extraServices = {
colmenaDeploy.enable = true;
flatpak.enable = true; flatpak.enable = true;
ollama.enable = false; ollama.enable = false;
podman.enable = true; podman.enable = true;
+1
View File
@@ -42,6 +42,7 @@
]; ];
extraServices = { extraServices = {
colmenaDeploy.enable = true;
flatpak.enable = false; flatpak.enable = false;
ollama.enable = false; ollama.enable = false;
podman.enable = true; podman.enable = true;
+1
View File
@@ -42,6 +42,7 @@
]; ];
extraServices = { extraServices = {
colmenaDeploy.enable = true;
flatpak.enable = true; flatpak.enable = true;
ollama.enable = false; ollama.enable = false;
podman.enable = true; podman.enable = true;
+1
View File
@@ -8,6 +8,7 @@
]; ];
extraServices = { extraServices = {
colmenaDeploy.enable = true;
flatpak.enable = false; flatpak.enable = false;
ollama.enable = false; ollama.enable = false;
podman.enable = false; podman.enable = false;
+1
View File
@@ -43,6 +43,7 @@
]; ];
extraServices = { extraServices = {
colmenaDeploy.enable = true;
flatpak.enable = true; flatpak.enable = true;
ollama.enable = true; ollama.enable = true;
podman.enable = true; podman.enable = true;