Skip to content

Commit

Permalink
env: Enable passing through Hetzner Arch Linux repo to distrobox
Browse files Browse the repository at this point in the history
Make function easy to run to allow enabling easily in virtual machines
as well.

Signed-off-by: Nathan Chancellor <[email protected]>
  • Loading branch information
nathanchance committed Dec 7, 2024
1 parent a9a8862 commit 79a12c9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
41 changes: 41 additions & 0 deletions fish/functions/add_hetzner_mirror_to_repos.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2024 Nathan Chancellor

function add_hetzner_mirror_to_repos -d "Add Hetzner's internal Arch Linux mirror to /etc/pacman.conf"
set program "import arch
from pathlib import Path
import sys
# Not using pacman, bail out gracefully
if not (pacman_conf := Path('/etc/pacman.conf')).exists():
sys.exit(0)
orig_pacman_conf_txt = pacman_conf.read_text(encoding='utf-8')
new_pacman_conf_txt = arch.add_hetzner_mirror_to_repos(orig_pacman_conf_txt)
if orig_pacman_conf_txt != new_pacman_conf_txt:
pacman_conf.write_text(new_pacman_conf_txt, encoding='utf-8')"

if test (count $argv) -eq 0
sudo env PYTHONPATH=$PYTHON_FOLDER/setup python -c "$program"
return
end

for arg in $argv
switch $arg
case -p --print-container-routine
printf '\n%s\n%s\n%s%s%s\n%s\n' \
'# Import Hetzner mirror from the host configuration if using pacman' \
'if command -v python3 >/dev/null 2>&1; then' \
' PYTHONPATH=/run/host'$PYTHON_FOLDER'/setup python3 -c "' \
"$program" \
'" || exit' \
'fi'

case '*'
print_error "Unknown argument: $arg"
return 1
end
end
end
4 changes: 4 additions & 0 deletions fish/functions/dbxc.fish
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ if grep -q host-spawn /etc/fish/conf.d/distrobox_config.fish; then
sed -i /host-spawn/d /etc/fish/conf.d/distrobox_config.fish
fi' >$init_hook_sh

if is_hetzner
add_hetzner_mirror_to_repos -p >>$init_hook_sh
end

# If we are using docker, we need to explicitly set the container's
# kvm group to the same group ID as the host's kvm group if it exists
# so that accelerated VMs work within a container. Do this with an init hook.
Expand Down
10 changes: 10 additions & 0 deletions fish/functions/is_hetzner.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2024 Nathan Chancellor

function is_hetzner -d "Checks if current running machine is a Hetzner machine"
if test $LOCATION = hetzner # duh
return 0
end
PYTHONPATH=$PYTHON_FOLDER/setup python3 -c 'from arch import is_hetzner; import sys; sys.exit(0 if is_hetzner() else 1)'
end

0 comments on commit 79a12c9

Please sign in to comment.