Skip to content

Commit

Permalink
fish: Initial systemd-nspawn tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
  • Loading branch information
nathanchance committed Dec 20, 2024
1 parent b02675a commit eb4e225
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
26 changes: 19 additions & 7 deletions fish/config.fish
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,36 @@ if test "$LOCATION" = mac

set -gx SHELL /opt/homebrew/bin/fish
else
# If /tmp/tmux-1000 is a mountpoint, it means we are in a systemd-nspawn
# container. If TMUX is not already set, we should set it so that we can
# interact with the host's tmux server. This needs to be done before the
# call to start_tmux below so that a tmux session is not started in the
# container.
if mountpoint -q /tmp/tmux-1000; and not set -q TMUX
set -gx TMUX /tmp/tmux-1000/default
end

if not string match -qr tty (tty); and status is-interactive
start_tmux
end

if in_container
# distrobox may add duplicates to PATH, clean it up :/
# https://github.com/89luca89/distrobox/issues/1145
set --local --path deduplicated_path
set --local item
if in_dbx
set --local --path deduplicated_path
set --local item

for item in $PATH
if not contains $item $deduplicated_path
set -a deduplicated_path $item
for item in $PATH
if not contains $item $deduplicated_path
set -a deduplicated_path $item
end
end
set --export --global --path PATH $deduplicated_pathi
end
set --export --global --path PATH $deduplicated_path

if test "$USE_CBL" = 1
# distrobox uses $USE_CBL, systemd-spawn uses /etc/use-cbl
if test -r /use-cbl; or test "$USE_CBL" = 1
for item in $CBL_QEMU_BIN $CBL_TC_BNTL $CBL_TC_LLVM
fish_add_path -gm $item
end
Expand Down
10 changes: 9 additions & 1 deletion fish/functions/_hydro_addon_nathan.fish
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function _hydro_addon_nathan -d "Hydro prompt customizations"
end

# Print symbol if we are in a container (like default toolbox prompt)
if in_container
if in_dbx
# If CONTAINER_ID is a part of the hostname (i.e., distrobox prior to
# https://github.com/89luca89/distrobox/commit/d626559baaa4e6ccb35b3bb0befc9d46b7aa837e),
# just show a symbol to know we are in a distrobox.
Expand All @@ -25,6 +25,14 @@ function _hydro_addon_nathan -d "Hydro prompt customizations"
else
set container_str "($CONTAINER_ID)"
end
end
if in_nspawn; and set image_id (string match -gr 'IMAGE_ID="?([^"]+)' </usr/lib/os-release)
if test -e /etc/ephemeral
set image_id "$image_id +"
end
set container_str "($image_id)"
end
if set -q container_str
printf '%b%s ' (set_color AF8700) $container_str
end

Expand Down
8 changes: 8 additions & 0 deletions fish/functions/in_dbx.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2024 Nathan Chancellor

function in_dbx -d "Test if currently in a distrobox"
# https://github.com/89luca89/distrobox/blob/3bac964bf0952674848dce170af8b41d743abe57/docs/useful_tips.md?plain=1#L40
set -q CONTAINER_ID
end
7 changes: 7 additions & 0 deletions fish/functions/in_nspawn.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2024 Nathan Chancellor

function in_nspawn -d "Test if currently within a systemd-nspawn container"
test (systemd-detect-virt 2>/dev/null) = systemd-nspawn
end

0 comments on commit eb4e225

Please sign in to comment.