PortValhalla/scripts/lib/action.fish

75 lines
2.1 KiB
Fish
Raw Normal View History

2024-11-01 14:32:43 +00:00
set -l dir (status dirname)
function runActionSetup -V dir
2024-11-01 14:32:43 +00:00
source "$dir/hooks.fish"
source "$dir/nix.fish"
source "$dir/settings.fish"
2024-11-03 21:32:47 +00:00
echo "Installing dependencies..."
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.'
and echo "Preparing nix..."
2024-11-01 14:32:43 +00:00
and runHook prepareNix || installNixPkgs
and if [ -z "$CONFIG_NAME" ]
2024-11-01 14:32:43 +00:00
selectProfile config
set -gx CONFIG_NAME "$config"
2024-11-01 14:32:43 +00:00
end
and echo "Flagging repository as safe..."
and sudo git config --system --add safe.directory (realpath "$(status dirname)/../..")
and runHook actionPreRun || true
end
2024-11-01 14:32:43 +00:00
function disposeAction -V dir
source "$dir/hooks.fish"
and sudo git config remove-section --system safe || true
2024-11-04 00:34:43 +00:00
runHook actionPostRun || true
end
2024-11-01 14:32:43 +00:00
function runAction -V dir
runActionSetup
and $argv
and disposeAction
2024-11-01 14:32:43 +00:00
end
function runSetupUserAction -V dir
source "$dir/nix.fish"
if [ (id -u) -eq 0 ]
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0)
2024-11-01 14:32:43 +00:00
if [ -z "$TMUX" ]
runActionSetup
tmux new-session -e "CONFIG_NAME=$CONFIG_NAME" $cmdline
else
source "$dir/settings.fish"
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
set -l name (getOSConfig setupUser.name)
2024-11-01 14:32:43 +00:00
begin
echo "Creating setup user"
2024-11-01 14:32:43 +00:00
and useradd \
--comment "PortValhalla Setup User" \
--system \
--no-user-group \
--groups nix-users \
--create-home \
--uid (getOSConfig setupUser.id --json) \
"$name"
end
2024-11-01 14:32:43 +00:00
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
2024-11-01 14:32:43 +00:00
and sudo --preserve-env --set-home --user "$name" $cmdline
disposeAction
2024-11-04 00:34:43 +00:00
uninstallNixPkgs
rm "$sudoConfig"
userdel -rf "$name"
2024-11-01 14:32:43 +00:00
end
else
$argv
2024-11-01 14:32:43 +00:00
end
end