From 4a642739c4bad7ba4b4ebc9d06f1f8165b24817e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 4 Nov 2024 01:34:22 +0100 Subject: [PATCH] Initialize and dispose actions properly --- scripts/Common/OS/setup.fish | 2 +- scripts/lib/action.fish | 99 +++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/scripts/Common/OS/setup.fish b/scripts/Common/OS/setup.fish index 1639aa07..01efef1c 100755 --- a/scripts/Common/OS/setup.fish +++ b/scripts/Common/OS/setup.fish @@ -66,7 +66,7 @@ function runSetup cp -r "$nixPkgsDir" "$mountDir/$nixPkgsDir" end - function actionInitialization + function actionPreRun echo "Partitioning drives..." and getOSConfig partition.script >"$script" and "$script" diff --git a/scripts/lib/action.fish b/scripts/lib/action.fish index 52776fc5..293d0a16 100644 --- a/scripts/lib/action.fish +++ b/scripts/lib/action.fish @@ -1,6 +1,6 @@ set -l dir (status dirname) -function runAction -V dir +function runActionSetup -V dir source "$dir/hooks.fish" source "$dir/nix.fish" source "$dir/settings.fish" @@ -9,62 +9,67 @@ function runAction -V dir and echo "Preparing nix..." and runHook prepareNix || installNixPkgs - if [ -z "$CONFIG_NAME" ] + and if [ -z "$CONFIG_NAME" ] selectProfile config - set -x CONFIG_NAME "$config" + set -gx CONFIG_NAME "$config" end - and runHook actionInitialization || true - - echo "Flagging repository as safe..." - sudo git config --system --add safe.directory (realpath "$(status dirname)/../..") - + and echo "Flagging repository as safe..." + and sudo git config --system --add safe.directory (realpath "$(status dirname)/../..") and runHook actionPreRun || true - $argv - and runHook actionPostRun || true - sudo git config remove-section --system safe || true +end + +function disposeAction -V dir + source "$dir/hooks.fish" + source "$dir/nix.fish" + runHook actionPostRun || true + and uninstallNixPkgs + and sudo git config remove-section --system safe || true +end + +function runAction -V dir + runActionSetup + and $argv + and disposeAction end function runSetupUserAction -V dir source "$dir/nix.fish" - function setupUserAction -V dir -V sudoConfig - if [ (id -u) -eq 0 ] - set -l cmdline (cat /proc/$fish_pid/cmdline | string split0) + if [ (id -u) -eq 0 ] + set -l cmdline (cat /proc/$fish_pid/cmdline | string split0) - if [ -z "$TMUX" ] - 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) - - 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 + if [ -z "$TMUX" ] + runActionSetup + tmux new-session -e "CONFIG_NAME=$CONFIG_NAME" $cmdline else - $argv - uninstallNixPkgs - end - end + source "$dir/settings.fish" + set -l sudoConfig "/etc/sudoers.d/PortValhalla" + set -l name (getOSConfig setupUser.name) - runAction setupUserAction $argv + 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 + disposeAction + rm "$sudoConfig" + userdel -rf "$name" + end + else + $argv + end end