refactor(sudo): rename interactive to login_shell

This commit is contained in:
AThePeanut4
2025-09-28 14:53:42 +02:00
committed by Gideon
parent 791993795a
commit a886d20a7b
2 changed files with 13 additions and 14 deletions

View File

@@ -549,7 +549,7 @@ pub fn run_nix_self_upgrade(ctx: &ExecutionContext) -> Result<()> {
let nix_args = nix_args(); let nix_args = nix_args();
if multi_user { if multi_user {
let sudo = ctx.require_sudo()?; let sudo = ctx.require_sudo()?;
sudo.execute_opts(ctx, &nix, SudoExecuteOpts::new().interactive())? sudo.execute_opts(ctx, &nix, SudoExecuteOpts::new().login_shell())?
.args(nix_args) .args(nix_args)
.arg("upgrade-nix") .arg("upgrade-nix")
.status_checked() .status_checked()

View File

@@ -38,8 +38,8 @@ pub enum SudoPreserveEnv<'a> {
/// default or unsupported. /// default or unsupported.
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct SudoExecuteOpts<'a> { pub struct SudoExecuteOpts<'a> {
/// Run the command "interactively", i.e. inside a login shell. /// Run the command inside a login shell.
pub interactive: bool, pub login_shell: bool,
/// Preserve environment variables across the sudo call. /// Preserve environment variables across the sudo call.
pub preserve_env: SudoPreserveEnv<'a>, pub preserve_env: SudoPreserveEnv<'a>,
/// Set the HOME environment variable to the target user's home directory. /// Set the HOME environment variable to the target user's home directory.
@@ -53,10 +53,10 @@ impl<'a> SudoExecuteOpts<'a> {
Self::default() Self::default()
} }
/// Run the command "interactively", i.e. inside a login shell. /// Run the command inside a login shell.
#[allow(unused)] #[allow(unused)]
pub fn interactive(mut self) -> Self { pub fn login_shell(mut self) -> Self {
self.interactive = true; self.login_shell = true;
self self
} }
@@ -221,11 +221,11 @@ impl Sudo {
) -> Result<Executor> { ) -> Result<Executor> {
// null sudo is very different, do separately // null sudo is very different, do separately
if let SudoKind::Null = self.kind { if let SudoKind::Null = self.kind {
if opts.interactive { if opts.login_shell {
// TODO: emulate running in a login shell with su/runuser // TODO: emulate running in a login shell with su/runuser
return Err(UnsupportedSudo { return Err(UnsupportedSudo {
sudo_kind: self.kind, sudo_kind: self.kind,
option: "interactive", option: "login_shell",
} }
.into()); .into());
} }
@@ -248,20 +248,19 @@ impl Sudo {
// self.path is only None for null sudo, which we've handled above // self.path is only None for null sudo, which we've handled above
let mut cmd = ctx.execute(self.path.as_ref().unwrap()); let mut cmd = ctx.execute(self.path.as_ref().unwrap());
if opts.interactive { if opts.login_shell {
match self.kind { match self.kind {
SudoKind::Sudo => { SudoKind::Sudo => {
cmd.arg("-i"); cmd.arg("-i");
} }
SudoKind::Gsudo => { SudoKind::Gsudo => {
// By default, gsudo runs all commands inside a shell, so it's effectively // By default, gsudo runs all commands inside a shell. If login_shell
// always "interactive". If interactive is *not* specified, we add `-d` // is *not* specified, we add `-d` to run outside of a shell - see below.
// to run outside of a shell - see below.
} }
SudoKind::Doas | SudoKind::WinSudo | SudoKind::Pkexec | SudoKind::Run0 | SudoKind::Please => { SudoKind::Doas | SudoKind::WinSudo | SudoKind::Pkexec | SudoKind::Run0 | SudoKind::Please => {
return Err(UnsupportedSudo { return Err(UnsupportedSudo {
sudo_kind: self.kind, sudo_kind: self.kind,
option: "interactive", option: "login_shell",
} }
.into()); .into());
} }
@@ -269,7 +268,7 @@ impl Sudo {
} }
} else if let SudoKind::Gsudo = self.kind { } else if let SudoKind::Gsudo = self.kind {
// The `-d` (direct) flag disables shell detection, running the command directly // The `-d` (direct) flag disables shell detection, running the command directly
// rather than through the current shell, making it "non-interactive". // rather than through the current shell.
// Additionally, if the current shell is pwsh >= 7.3.0, then not including this // Additionally, if the current shell is pwsh >= 7.3.0, then not including this
// gives errors if the command to run has spaces in it: see // gives errors if the command to run has spaces in it: see
// https://github.com/gerardog/gsudo/issues/297 // https://github.com/gerardog/gsudo/issues/297