Skip to content

Commit

Permalink
No longer require sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Jan 8, 2024
1 parent 21affdd commit 0e5b724
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
24 changes: 17 additions & 7 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,18 @@ class NixInstallerAction {
}

private async setup_kvm(): Promise<boolean> {
const current_user = userInfo();
const is_root = current_user.username === "root";
const maybe_sudo = is_root ? "sudo" : "";

const kvm_rules =
"/etc/udev/rules.d/99-determinate-nix-installer-kvm.rules";
try {
const write_file_exit_code = await actions_exec.exec(
"sh",
[
"-c",
`echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee ${kvm_rules} > /dev/null`,
`echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | ${maybe_sudo} tee ${kvm_rules} > /dev/null`,
],
{
silent: true,
Expand All @@ -664,11 +668,15 @@ class NixInstallerAction {
);
}

const debug_run_throw = async (
const debug_root_run_throw = async (
action: string,
command: string,
args: string[],
): Promise<void> => {
if (!is_root) {
args = [command, ...args];
command = "sudo";
}
const reload_exit_code = await actions_exec.exec(command, args, {
silent: true,
listeners: {
Expand All @@ -694,21 +702,23 @@ class NixInstallerAction {
}
};

await debug_run_throw("reloading udev rules", `sudo`, [
"udevadm",
await debug_root_run_throw("reloading udev rules", "udevadm", [
"control",
"--reload-rules",
]);

await debug_run_throw("triggering udev against kvm", `sudo`, [
"udevadm",
await debug_root_run_throw("triggering udev against kvm", "udevadm", [
"trigger",
"--name-match=kvm",
]);

return true;
} catch (error) {
await actions_exec.exec("sudo", ["rm", "-f", kvm_rules]);
if (is_root) {
await actions_exec.exec("rm", ["-f", kvm_rules]);
} else {
await actions_exec.exec("sudo", ["rm", "-f", kvm_rules]);
}

return false;
}
Expand Down

0 comments on commit 0e5b724

Please sign in to comment.