Skip to content

Commit

Permalink
feat: --nvidia flag for enter + export and status commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Aug 13, 2024
1 parent 25401c4 commit ec55c26
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 10 deletions.
17 changes: 17 additions & 0 deletions files/nopass-machinectl.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
polkit.addRule(function(action, subject) {
if (
(
action.id == "org.freedesktop.machine1.shell" ||
action.id == "org.freedesktop.machine1.manage-machines" ||
action.id == "org.freedesktop.machine1.manage-images" ||
action.id == "org.freedesktop.machine1.login" ||
(
action.id == "org.freedesktop.systemd1.manage-units" &&
RegExp('systemd-nspawn@[A-Za-z0-9_-]+.service').test(action.lookup("unit")) === true
)
)
&& subject.isInGroup("wheel")
) {
return polkit.Result.YES;
}
});
60 changes: 60 additions & 0 deletions src/lib/enter.nu
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export def --env "main enter" [
--runner: string = "nspawn" # Runner of the machine: nspawn or vmspawn
--no-kill # Do not kill machine preemptively before running launcher command
--yes # Do not print any confirmation prompts
--nvidia # Add binds for nvidia driver support
machine: string # Name of the target to be logged into (either a directory, machine name, or image name)
...args: string
] {
Expand Down Expand Up @@ -86,6 +87,7 @@ export def --env "main enter" [
$"--private-users=(if $no_user_bind { "no" } else { "pick" })"
$"--private-users-ownership=auto"
"--keep-unit"
"--link-journal=try-guest"
"--resolv-conf=replace-stub"
(if $boot { "--boot" } else { $"--chdir=($env.PWD? | default "/home")" } )
(if not $no_user_bind { $"--bind-user=($user)" } else { "--private-users=no" })
Expand Down Expand Up @@ -132,6 +134,64 @@ export def --env "main enter" [
] | each {|e| $"--bind=($e)"}))
}

if $nvidia {
# Taken from the arch wiki, not tested yet :(

$final_args = ($final_args | append ([
"/dev/nvidia0"
"/dev/nvidiactl"
"/dev/nvidia-modeset"
"/usr/bin/nvidia-bug-report.sh"
"/usr/bin/nvidia-cuda-mps-control"
"/usr/bin/nvidia-cuda-mps-server"
"/usr/bin/nvidia-debugdump"
"/usr/bin/nvidia-modprobe"
"/usr/bin/nvidia-ngx-updater"
"/usr/bin/nvidia-persistenced"
"/usr/bin/nvidia-powerd"
"/usr/bin/nvidia-sleep.sh"
"/usr/bin/nvidia-smi"
"/usr/bin/nvidia-xconfig"
"/usr/lib/gbm/nvidia-drm_gbm.so"
"/usr/lib/libEGL_nvidia.so"
"/usr/lib/libGLESv1_CM_nvidia.so"
"/usr/lib/libGLESv2_nvidia.so"
"/usr/lib/libGLX_nvidia.so"
"/usr/lib/libcuda.so"
"/usr/lib/libnvcuvid.so"
"/usr/lib/libnvidia-allocator.so"
"/usr/lib/libnvidia-cfg.so"
"/usr/lib/libnvidia-egl-gbm.so"
"/usr/lib/libnvidia-eglcore.so"
"/usr/lib/libnvidia-encode.so"
"/usr/lib/libnvidia-fbc.so"
"/usr/lib/libnvidia-glcore.so"
"/usr/lib/libnvidia-glsi.so"
"/usr/lib/libnvidia-glvkspirv.so"
"/usr/lib/libnvidia-ml.so"
"/usr/lib/libnvidia-ngx.so"
"/usr/lib/libnvidia-opticalflow.so"
"/usr/lib/libnvidia-ptxjitcompiler.so"
"/usr/lib/libnvidia-rtcore.so"
"/usr/lib/libnvidia-tls.so"
"/usr/lib/libnvidia-vulkan-producer.so"
"/usr/lib/libnvoptix.so"
"/usr/lib/modprobe.d/nvidia-utils.conf"
"/usr/lib/nvidia/wine/_nvngx.dll"
"/usr/lib/nvidia/wine/nvngx.dll"
"/usr/lib/nvidia/xorg/libglxserver_nvidia.so"
"/usr/lib/vdpau/libvdpau_nvidia.so"
"/usr/lib/xorg/modules/drivers/nvidia_drv.so"
"/usr/share/X11/xorg.conf.d/10-nvidia-drm-outputclass.conf"
"/usr/share/dbus-1/system.d/nvidia-dbus.conf"
"/usr/share/egl/egl_external_platform.d/15_nvidia_gbm.json"
"/usr/share/glvnd/egl_vendor.d/10_nvidia.json"
"/usr/share/licenses/nvidia-utils/LICENSE"
"/usr/share/vulkan/icd.d/nvidia_icd.json"
"/usr/share/vulkan/implicit_layer.d/nvidia_layers.json"
] | each {|e| $"--bind=($e)"}))
}

if ($extra_bind != "" and $extra_bind != null) {
$final_args = ($final_args | append ($extra_bind | split row "," | each {|e| $"--bind(if $ro_binds {"-ro"} else {""})=($e)"}))
}
Expand Down
36 changes: 26 additions & 10 deletions src/lib/export.nu
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
use logger.nu *
use meta.nu NAME

# Export programs from a machine by writing scripts and desktop files for exported apps
export def "main export" [] {}
export def "main export" [] {
$"Usage: ($NAME) export <command>"
}

export def "main export bin" [
# Generate a script that will run a binary in a machine through nuspawn enter
export def "main export binary" [
--extra-flags: string # Flags for the exported binary
machine: string
--export-path: path = "~/.local/bin" # Path where the binary will be exported to
--nuspawn-args: string # Arguments that will be passed to nuspawn
--nuspawn-prefix: string # Any prefix that will run before the nuspawn caller runs (e.g.: bwrap nuspawn)
--prefix: string # Any prefix that will run before the command gets ran (e.g.: gamescope steam)
--nuspawn-binary: string = "nuspawn" # Path to the nuspawn script (must be executable) (defaults to $PATH)
machine: string # Machine that the program will be executed on
binary: string # Binary that will be exported to the host
] {
let script = "
#!/bin/sh
nuspawn enter ($machine) --
"
mkdir $export_path
let exported_binary_path = $"($export_path)/($binary | path basename)"
$"#!/bin/sh
($nuspawn_prefix) ($nuspawn_binary) enter ($nuspawn_args) ($machine) /bin/sh -c \"($prefix) ($binary) ($extra_flags)\"
" | save -f $exported_binary_path
run-external "chmod" "+x" $exported_binary_path
logger success $"Succesfully exported binary to ($exported_binary_path)"
}

# Export desktop files and data from a program in a machine
export def "main export app" [] {
logger error "To be implemented"
}
export def "main export app" [] {}
1 change: 1 addition & 0 deletions src/lib/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export use rename.nu *
export use setup.nu *
export use start.nu *
export use oci.nu *
export use status.nu *
22 changes: 22 additions & 0 deletions src/lib/status.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use logger.nu *

# Get status for all the machines or just a specific one
export def "main status" [
machine?: string
] {
if $machine != null {
let data = (machinectl status $machine --output json | complete | get stdout | from json)
if ($data | length) == 0 {
logger warning $"Machine ($machine) is not running"
return
}
return
}

let data = (machinectl list --output json | from json)
if ($data | length) == 0 {
logger warning "No machine is currently running"
return
}
$data
}

0 comments on commit ec55c26

Please sign in to comment.