Repository navigation
feat(amd): passwordless agent-rebuild-switch from main #11959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| { | ||
| config, | ||
| pkgs, | ||
| ... | ||
| }: | ||
| let | ||
| # The only source this command will ever switch the laptop to: the | ||
| # amd configuration as merged on main. It deliberately does NOT build | ||
| # from the local checkout (~/git/code): that tree is writable by every | ||
| # agent running as `user`, so a passwordless switch from it would let | ||
| # any of them become root. Building from main means only reviewed, | ||
| # merged changes can reach the running system through this path. | ||
| flakeRef = "github:developing-today/code/main#amd"; | ||
|
|
||
| # Same token file ./rebuild (lib/auth.sh) already uses. Read as root | ||
| # inside the command, validated as a bare token so a stray line in it | ||
| # cannot inject other nix.conf settings into a root nix invocation. | ||
| tokenFile = "/home/user/auth"; | ||
|
|
||
| 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. | ||
| inheritPath = false; | ||
| runtimeInputs = [ | ||
| config.system.build.nixos-rebuild | ||
| config.nix.package | ||
| pkgs.git | ||
| pkgs.coreutils | ||
| pkgs.util-linux | ||
| pkgs.systemd | ||
| ]; | ||
| text = '' | ||
| # Argument contract (AGENTS.md, #3352): this command takes no | ||
| # arguments. The sudo rule also forbids them; this is the second wall. | ||
| if (($# != 0)); then | ||
| echo "usage: sudo agent-rebuild-switch (takes no arguments)" >&2 | ||
| exit 2 | ||
| fi | ||
| if ((EUID != 0)); then | ||
| echo "agent-rebuild-switch: run it with sudo" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # One rebuild at a time. | ||
| exec 9>/run/agent-rebuild-switch.lock | ||
| if ! flock -n 9; then | ||
| echo "agent-rebuild-switch: another rebuild is already running" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # lib/auth.sh raises this for large evaluations too. | ||
| ulimit -n "$(ulimit -Hn)" || true | ||
|
|
||
| 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 | ||
| echo "agent-rebuild-switch: ignoring ${tokenFile} (not a bare GitHub token)" >&2 | ||
| fi | ||
| unset token | ||
| 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. | ||
| cd / | ||
| if nixos-rebuild switch --refresh --accept-flake-config --flake ${flakeRef}; then | ||
| logger -t agent-rebuild-switch "switch to ${flakeRef} succeeded" | ||
| else | ||
| rc=$? | ||
| logger -t agent-rebuild-switch "switch to ${flakeRef} failed (exit $rc)" | ||
| exit "$rc" | ||
| fi | ||
| ''; | ||
| }; | ||
| 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 ]; | ||
|
|
||
| security.sudo.extraRules = [ | ||
| { | ||
| users = [ "user" ]; | ||
| commands = [ | ||
| { | ||
| # `""` after the path is sudoers for "no arguments allowed". | ||
| command = ''${agentRebuildSwitch}/bin/agent-rebuild-switch ""''; | ||
| options = [ "NOPASSWD" ]; | ||
| } | ||
| ]; | ||
| } | ||
| ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.