Skip to content

Commit

Permalink
Add root check bypass flag, fixes #95
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Aug 20, 2024
1 parent dd8bdc8 commit a014415
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nh"
version = "3.5.23"
version = "3.5.24"
edition = "2021"
license = "EUPL-1.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 4 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ pub struct OsRebuildArgs {
/// Extra arguments passed to nix build
#[arg(last = true)]
pub extra_args: Vec<String>,

/// Bypass the check to call nh as root directly.
#[arg(short = 'R', long)]
pub bypass_root_check: bool,
}

#[derive(Debug, Args)]
Expand Down
34 changes: 21 additions & 13 deletions src/nixos.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::ops::Deref;
use std::{env, vec};

use color_eyre::eyre::{bail, Context};
use color_eyre::Result;

use tracing::{debug, info};
use tracing::{debug, info, warn};

use crate::interface::NHRunnable;
use crate::interface::OsRebuildType::{self, Boot, Build, Switch, Test};
Expand All @@ -27,9 +28,19 @@ impl NHRunnable for interface::OsArgs {

impl OsRebuildArgs {
pub fn rebuild(&self, rebuild_type: &OsRebuildType) -> Result<()> {
if nix::unistd::Uid::effective().is_root() {
bail!("Don't run nh os as root. I will call sudo internally as needed");
}
let use_sudo = if self.bypass_root_check {
warn!("Bypassing root check, now running nix as root");
false
} else {
if nix::unistd::Uid::effective().is_root() {
bail!("Don't run nh os as root. I will call sudo internally as needed");
}
true
};

// TODO: add a .maybe_arg to CommandBuilder
// so that I can do .maybe_arg( Option<T> )
let sudo_args: &[_] = if use_sudo { &["sudo"] } else { &[] };

let hostname = match &self.hostname {
Some(h) => h.to_owned(),
Expand Down Expand Up @@ -134,21 +145,17 @@ impl OsRebuildArgs {
let switch_to_configuration = switch_to_configuration.to_str().unwrap();

commands::CommandBuilder::default()
.args(["sudo", switch_to_configuration, "test"])
.args(sudo_args)
.args([switch_to_configuration, "test"])
.message("Activating configuration")
.build()?
.exec()?;
}

if let Boot(_) | Switch(_) = rebuild_type {
commands::CommandBuilder::default()
.args([
"sudo",
"nix-env",
"--profile",
SYSTEM_PROFILE,
"--set",
])
.args(sudo_args)
.args(["nix-env", "--profile", SYSTEM_PROFILE, "--set"])
.args([out_path.get_path()])
.build()?
.exec()?;
Expand All @@ -161,7 +168,8 @@ impl OsRebuildArgs {
let switch_to_configuration = switch_to_configuration.to_str().unwrap();

commands::CommandBuilder::default()
.args(["sudo", switch_to_configuration, "boot"])
.args(sudo_args)
.args([switch_to_configuration, "boot"])
.message("Adding configuration to bootloader")
.build()?
.exec()?;
Expand Down

0 comments on commit a014415

Please sign in to comment.