PortValhalla/scripts/lib/action.fish

70 lines
2.1 KiB
Fish

set -l dir (status dirname)
function runAction -V dir
source "$dir/hooks.fish"
source "$dir/nix.fish"
source "$dir/settings.fish"
echo "Preparing nix..."
and runHook prepareNix || installNixPkgs
if [ -z "$CONFIG_NAME" ]
selectProfile config
set -x CONFIG_NAME "$config"
end
and runHook prepareAction || true
and echo "Installing dependencies..."
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.'
echo "Flagging repository as safe..."
git config --system --add safe.directory (realpath "$(status dirname)/../..")
and runHook preAction || true
$argv
and runHook postAction || true
sudo git config remove-section --system safe || true
end
function runSetupUserAction -V dir
if [ (id -u) -eq 0 ]
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0)
if [ -z "$TMUX" ]
tmux new-session $cmdline
else
function setupUserAction -V dir -V sudoConfig -V cmdline
source "$dir/settings.fish"
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
set -l name (getOSConfig setupUser.name)
begin
echo "Creating setup user"
and useradd \
--comment "PortValhalla Setup User" \
--system \
--no-user-group \
--groups nix-users \
--create-home \
--uid (getOSConfig setupUser.id --json) \
"$name"
end
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
and sudo --preserve-env --set-home --user "$name" $cmdline
rm "$sudoConfig"
userdel -rf "$name"
end
runAction setupUserAction $argv
uninstallNixPkgs
read -P "setup user action finished. press enter"
end
else
$argv
end
end