From c400e5d2bc41c14302e90f9f3faee6c450e58bc3 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 23:37:01 +0000 Subject: [PATCH 1/3] feat(amd): trigger agent rebuilds via a polkit-gated unit, not sudo Replace the NOPASSWD sudo rule with agent-rebuild-switch.service and a polkit rule that lets user start only that unit. Agents run systemctl start --no-block agent-rebuild-switch.service, so sudo can stay blocked in Desktop Commander. The unit runs the same no-argument wrapper (switch to main#amd) from / with a fixed environment. Co-Authored-By: Claude Opus 5.5 --- nixos/hosts/amd-agent-rebuild.nix | 66 ++++++++++++++++++------------- nixos/hosts/default.nix | 4 +- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/nixos/hosts/amd-agent-rebuild.nix b/nixos/hosts/amd-agent-rebuild.nix index afb5a544e2..baafcd35eb 100644 --- a/nixos/hosts/amd-agent-rebuild.nix +++ b/nixos/hosts/amd-agent-rebuild.nix @@ -19,8 +19,8 @@ let agentRebuildSwitch = pkgs.writeShellApplication { name = "agent-rebuild-switch"; - # No inherited PATH: under sudo the caller's PATH would otherwise - # decide which nix/git/nixos-rebuild run as root. + # No inherited PATH: the caller's environment must never decide + # which nix/git/nixos-rebuild run as root. inheritPath = false; runtimeInputs = [ config.system.build.nixos-rebuild @@ -32,13 +32,13 @@ let ]; text = '' # Argument contract (AGENTS.md, #3352): this command takes no - # arguments. The sudo rule also forbids them; this is the second wall. + # arguments; the systemd unit passes none. if (($# != 0)); then - echo "usage: sudo agent-rebuild-switch (takes no arguments)" >&2 + echo "usage: agent-rebuild-switch (takes no arguments; start agent-rebuild-switch.service)" >&2 exit 2 fi if ((EUID != 0)); then - echo "agent-rebuild-switch: run it with sudo" >&2 + echo "agent-rebuild-switch: must run as root (start agent-rebuild-switch.service)" >&2 exit 1 fi @@ -63,10 +63,10 @@ let fi logger -t agent-rebuild-switch "switch to ${flakeRef} started by ''${SUDO_USER:-unknown}" - # sudo keeps the caller's working directory, and nix resolves an - # installable that contains a slash against the cwd, following a - # symlink there into the store. Run from root-owned / so a caller - # cannot plant an entry that stands in for the flake reference. + # Nix resolves an installable that contains a slash against the + # cwd, following a symlink there into the store. Run from root-owned + # / so no caller can plant an entry that stands in for the flake + # reference (the unit also sets WorkingDirectory=/). cd / if nixos-rebuild switch --refresh --accept-flake-config --flake ${flakeRef}; then logger -t agent-rebuild-switch "switch to ${flakeRef} succeeded" @@ -79,23 +79,35 @@ let }; in { - # Passwordless rebuilds for agents on this laptop, scoped to one - # command: `sudo agent-rebuild-switch` (no arguments) switches the - # system to the amd config on main. Nothing else becomes passwordless: - # ./rebuild, nixos-rebuild with its own flags, and every other sudo - # still ask for the password. - environment.systemPackages = [ agentRebuildSwitch ]; + # Passwordless rebuilds for agents on this laptop, without sudo. + # `systemctl start --no-block agent-rebuild-switch.service` runs the + # wrapper above as root; it switches the system to the amd config on + # main and takes no input. A polkit rule lets `user` start (only start) + # this one unit, so sudo can stay blocked for agents entirely. + # Follow a run with `journalctl -u agent-rebuild-switch -f`. + systemd.services.agent-rebuild-switch = { + description = "Switch amd to the config on main (agent-triggered)"; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${agentRebuildSwitch}/bin/agent-rebuild-switch"; + WorkingDirectory = "/"; + Environment = [ "HOME=/root" ]; + }; + # The switch this unit runs must not stop or restart the unit itself. + restartIfChanged = false; + stopIfChanged = false; + }; - security.sudo.extraRules = [ - { - users = [ "user" ]; - commands = [ - { - # `""` after the path is sudoers for "no arguments allowed". - command = ''${agentRebuildSwitch}/bin/agent-rebuild-switch ""''; - options = [ "NOPASSWD" ]; - } - ]; - } - ]; + security.polkit.extraConfig = '' + polkit.addRule(function (action, subject) { + if ( + action.id == "org.freedesktop.systemd1.manage-units" && + action.lookup("unit") == "agent-rebuild-switch.service" && + action.lookup("verb") == "start" && + subject.user == "user" + ) { + return polkit.Result.YES; + } + }); + ''; } diff --git a/nixos/hosts/default.nix b/nixos/hosts/default.nix index 8965f56044..05de0da107 100644 --- a/nixos/hosts/default.nix +++ b/nixos/hosts/default.nix @@ -316,8 +316,8 @@ in ./amd-rustdesk.nix ./amd-ipv4-preference.nix ./amd-openclaw-secrets.nix - # Passwordless `sudo agent-rebuild-switch` for agents: switches - # only to the amd config on main, takes no arguments. + # Agent-triggered rebuilds without sudo: user may start only + # agent-rebuild-switch.service, which switches to the amd config on main. ./amd-agent-rebuild.nix ]; }; From 12646ff20c85c25db77aabfcaf39d101b9a11c4a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 00:10:05 +0000 Subject: [PATCH 2/3] feat(amd): keep the sudo rule alongside the polkit-gated unit Drew asked to allow both for now: restore the NOPASSWD rule for sudo agent-rebuild-switch (no arguments) next to the agent-rebuild-switch.service polkit path. Both run the same wrapper. Co-Authored-By: Claude Opus 5.5 --- nixos/hosts/amd-agent-rebuild.nix | 35 +++++++++++++++++++++++-------- nixos/hosts/default.nix | 4 ++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/nixos/hosts/amd-agent-rebuild.nix b/nixos/hosts/amd-agent-rebuild.nix index baafcd35eb..f230bae3c5 100644 --- a/nixos/hosts/amd-agent-rebuild.nix +++ b/nixos/hosts/amd-agent-rebuild.nix @@ -32,13 +32,13 @@ let ]; text = '' # Argument contract (AGENTS.md, #3352): this command takes no - # arguments; the systemd unit passes none. + # arguments. The sudo rule forbids them and the unit passes none. if (($# != 0)); then - echo "usage: agent-rebuild-switch (takes no arguments; start agent-rebuild-switch.service)" >&2 + echo "usage: agent-rebuild-switch (takes no arguments; or start agent-rebuild-switch.service)" >&2 exit 2 fi if ((EUID != 0)); then - echo "agent-rebuild-switch: must run as root (start agent-rebuild-switch.service)" >&2 + echo "agent-rebuild-switch: run it with sudo, or start agent-rebuild-switch.service" >&2 exit 1 fi @@ -79,12 +79,29 @@ let }; in { - # Passwordless rebuilds for agents on this laptop, without sudo. - # `systemctl start --no-block agent-rebuild-switch.service` runs the - # wrapper above as root; it switches the system to the amd config on - # main and takes no input. A polkit rule lets `user` start (only start) - # this one unit, so sudo can stay blocked for agents entirely. - # Follow a run with `journalctl -u agent-rebuild-switch -f`. + # Passwordless rebuilds for agents on this laptop, two ways in, both + # running the same no-argument wrapper (switch to the amd config on + # main) and nothing else: + # - `sudo agent-rebuild-switch` (sudo rule below), and + # - `systemctl start --no-block agent-rebuild-switch.service`, which a + # polkit rule lets `user` start (only start) without sudo, so sudo + # can stay blocked for agents in Desktop Commander. + # Follow a service run with `journalctl -u agent-rebuild-switch -f`. + environment.systemPackages = [ agentRebuildSwitch ]; + + security.sudo.extraRules = [ + { + users = [ "user" ]; + commands = [ + { + # `""` after the path is sudoers for "no arguments allowed". + command = ''${agentRebuildSwitch}/bin/agent-rebuild-switch ""''; + options = [ "NOPASSWD" ]; + } + ]; + } + ]; + systemd.services.agent-rebuild-switch = { description = "Switch amd to the config on main (agent-triggered)"; serviceConfig = { diff --git a/nixos/hosts/default.nix b/nixos/hosts/default.nix index 05de0da107..d2eefbd8d1 100644 --- a/nixos/hosts/default.nix +++ b/nixos/hosts/default.nix @@ -316,8 +316,8 @@ in ./amd-rustdesk.nix ./amd-ipv4-preference.nix ./amd-openclaw-secrets.nix - # Agent-triggered rebuilds without sudo: user may start only - # agent-rebuild-switch.service, which switches to the amd config on main. + # Agent-triggered rebuilds to the amd config on main: passwordless + # `sudo agent-rebuild-switch`, or start agent-rebuild-switch.service. ./amd-agent-rebuild.nix ]; }; From cc9a035820d80b21d9f615ce67223a0c11c01ef7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 00:10:56 +0000 Subject: [PATCH 3/3] fix(amd): agent-rebuild-switch skips a GitHub token that returns 401 The token in /home/user/auth now gets 401 Bad credentials from GitHub, which fails every flake fetch, public inputs included. Check it first (header on stdin, never in argv) and fetch without it when rejected. Co-Authored-By: Claude Opus 5.5 --- nixos/hosts/amd-agent-rebuild.nix | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nixos/hosts/amd-agent-rebuild.nix b/nixos/hosts/amd-agent-rebuild.nix index f230bae3c5..b7100b7ad8 100644 --- a/nixos/hosts/amd-agent-rebuild.nix +++ b/nixos/hosts/amd-agent-rebuild.nix @@ -27,6 +27,7 @@ let config.nix.package pkgs.git pkgs.coreutils + pkgs.curl pkgs.util-linux pkgs.systemd ]; @@ -54,10 +55,18 @@ let if [[ -f ${tokenFile} && ! -L ${tokenFile} ]]; then token="$(head -n 1 ${tokenFile} | tr -d '\r\n')" - if [[ $token =~ ^[A-Za-z0-9_]+$ ]]; then - export NIX_CONFIG="access-tokens = github.com=$token" - else + if [[ ! $token =~ ^[A-Za-z0-9_]+$ ]]; then echo "agent-rebuild-switch: ignoring ${tokenFile} (not a bare GitHub token)" >&2 + # The header goes in on stdin so the token never shows in ps. + elif [[ "$(printf 'Authorization: Bearer %s\n' "$token" | + curl -s -o /dev/null -w '%{http_code}' --max-time 15 -H @- \ + https://api.github.com/rate_limit)" == 401 ]]; then + # An expired or revoked token makes every GitHub fetch fail with + # 401, even for public inputs; fetching without one still works. + echo "agent-rebuild-switch: GitHub rejects the token in ${tokenFile} (401); continuing without it" >&2 + logger -t agent-rebuild-switch "token in ${tokenFile} rejected by GitHub (401); continuing without it" + else + export NIX_CONFIG="access-tokens = github.com=$token" fi unset token fi