nixos-rebuild: restore local sudo functionality

This functionality used to exist and was then removed in #331741. The
issue was that $SUDO_USER is already used by `sudo` as an environment
variable.

This change makes it possible to checkout a flake repository, as a user,
enter the devshell, and with the additions below and apply changes with
`nixos-rebuild switch`.

```nix
{ pkgs }:
let
  nixos-rebuild = pkgs.writeShellApplication {
    name = "nixos-rebuild";
    runtimeInputs = [ pkgs.nixos-rebuild ];
    text = ''
      set -euo pipefail
      exec nixos-rebuild --flake "$PRJ_ROOT" --use-local-sudo "$@"
    '';
  };
in
pkgs.mkShellNoCC {
  packages = [
    nixos-rebuild
  ];

  shellHook = ''
    export PRJ_ROOT=$PWD
  '';
}
```
This commit is contained in:
zimbatm 2025-06-14 08:57:13 +02:00
parent d022a1e7bc
commit 7d8a9fa0e1

View File

@ -33,6 +33,7 @@ imageVariant=
buildHost=
targetHost=
remoteSudo=
localSudo=
noSSHTTY=
verboseScript=
noFlake=
@ -173,6 +174,9 @@ while [ "$#" -gt 0 ]; do
--use-remote-sudo)
remoteSudo=1
;;
--use-local-sudo)
localSudo=1
;;
--no-ssh-tty)
noSSHTTY=1
;;
@ -256,7 +260,7 @@ targetHostSudoCmd() {
t="-t"
fi
if [ -n "$remoteSudo" ]; then
if [[ -n "$remoteSudo" || -n "$localSudo" ]]; then
useSudo=1 SSHOPTS="$SSHOPTS $t" targetHostCmd "$@"
else
# While a tty might not be necessary, we apply it to be consistent with