From ffac158deac550fab2a97bf78ba6890c050a4d4f Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 1 Nov 2024 15:32:43 +0100 Subject: [PATCH] Create a script for initiating actions --- scripts/Common/OS/install.fish | 53 ++----------- scripts/Common/OS/setup.fish | 134 ++++++++++++++++----------------- scripts/lib/action.fish | 71 +++++++++++++++++ scripts/lib/config.fish | 4 +- 4 files changed, 145 insertions(+), 117 deletions(-) create mode 100644 scripts/lib/action.fish diff --git a/scripts/Common/OS/install.fish b/scripts/Common/OS/install.fish index 5719c39c..97a67e8d 100755 --- a/scripts/Common/OS/install.fish +++ b/scripts/Common/OS/install.fish @@ -1,55 +1,10 @@ #!/bin/env fish set -l dir (status dirname) -set -l cmdline (cat /proc/$fish_pid/cmdline | string split0) -source "$dir/../../lib/settings.fish" -source "$dir/../../lib/hooks.fish" +source "$dir/../../lib/action.fish" -if [ (id -u) -eq 0 ] - source "$dir/../../lib/config.fish" - source "$dir/../../lib/nix.fish" - set -l sudoConfig "/etc/sudoers.d/PortValhalla" - rm ~/.bash_profile +function installAction -V dir + source "$dir/../../lib/hooks.fish" - if ! git status -C (status dirname) &> /dev/null - git config --system --add safe.directory (realpath "$(status dirname)/../../..") - end - - if [ -z "$TMUX" ] - if [ -z "$CONFIG_NAME" ] - selectProfile config - and set -x CONFIG_NAME "$config" - end - - and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.' - installNixPkgs - and tmux new-session $cmdline - else - 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" - uninstallNixPkgs - read -P "finished. press enter" - end -else if not type -q getDeploymentScript function getDeploymentScript echo "No deployment script specified! No software will be installed." 1>&2 @@ -100,3 +55,5 @@ else and sleep 5 and systemctl reboot -i end + +runSetupUserAction installAction diff --git a/scripts/Common/OS/setup.fish b/scripts/Common/OS/setup.fish index 8bcf3bde..bd8ea6b1 100755 --- a/scripts/Common/OS/setup.fish +++ b/scripts/Common/OS/setup.fish @@ -1,85 +1,85 @@ #!/bin/env fish function runSetup set -l dir (status dirname) - source "$dir/../../lib/settings.fish" - source "$dir/../../lib/hooks.fish" - source "$dir/../../lib/nix.fish" - - if [ -z "$CONFIG_NAME" ] - selectProfile config - set -x CONFIG_NAME "$config" - end - - set -l mountDir (getOSConfig partition.rootDir) + source "$dir/../../lib/action.fish" set -l projectRoot (realpath "$dir/../../..") set -l projectName (basename "$projectRoot") set -l PROJECT_CLONE_ROOT "/opt/$(basename "$projectName")" - set -l script (mktemp) - chmod +x "$script" - if not type -q runChroot - function runChroot -S - command chroot $argv + function setupAction -V projectRoot -V PROJECT_CLONE_ROOT + source "$dir/../../lib/hooks.fish" + source "$dir/../../lib/settings.fish" + set -l mountDir (getOSConfig partition.rootDir) + set -l script (mktemp) + chmod +x "$script" + + if not type -q runChroot + function runChroot -S + command chroot $argv + end end + + function getCloneFile -S -a path + set -l relativeDir (realpath --relative-to "$projectRoot" "$dir") + set -l relativePath (realpath --relative-to "$dir" "$path") + echo "$PROJECT_CLONE_ROOT/$relativeDir/$relativePath" + end + + function runInOS -S + set -l script /root/run_once + wrapScript $argv | chroot "$mountDir" tee "$script" >/dev/null + and runChroot "$mountDir" chmod +x "$script" + and runChroot "$mountDir" "$script" + and runChroot "$mountDir" rm "$script" + end + + function wrapScript -S + printf %s\n \ + "cd $PROJECT_CLONE_ROOT" \ + "$argv" + end + + and echo "Cloning project..." + and source "$dir/../../lib/copy-repo.fish" "$mountDir$PROJECT_CLONE_ROOT" + runChroot "$mountDir" git config --system --add safe.directory "$PROJECT_CLONE_ROOT" + and runHook setupOS + + and echo "Preparing auto-login..." + and runHook --force autologin "Please set up a function `autologin` for setting up autologin for the `root` user" + + and begin + set -l script (string escape (getCloneFile (getInstallerScript))) + + wrapScript ( + string join " " \ + "CONFIG_NAME=$(string escape "$CONFIG_NAME")" \ + (string escape $script)) + + end | runChroot "$mountDir" tee /root/.bash_profile >/dev/null end - function getCloneFile -S -a path - set -l relativeDir (realpath --relative-to "$projectRoot" "$dir") - set -l relativePath (realpath --relative-to "$dir" "$path") - echo "$PROJECT_CLONE_ROOT/$relativeDir/$relativePath" - end + function prepareNix + source "$dir/../../lib/nix.fish" - function runInOS -S - set -l script /root/run_once - wrapScript $argv | chroot "$mountDir" tee "$script" >/dev/null - and runChroot "$mountDir" chmod +x "$script" - and runChroot "$mountDir" "$script" - and runChroot "$mountDir" rm "$script" - end - - function wrapScript -S - printf %s\n \ - "cd $PROJECT_CLONE_ROOT" \ - "$argv" - end - - echo "Partitioning drives..." - and getOSConfig partition.script >"$script" - and "$script" - and rm "$script" - - # Copy `nixpkgs` channel - and echo "Preparing nix..." - - and begin + # Copy `nixpkgs` channel mkdir -p (dirname "$mountDir/$nixPkgsDir") cp -r "$nixPkgsDir" "$mountDir/$nixPkgsDir" end - and echo "Installing dependencies..." - and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.' + function prepareAction + echo "Partitioning drives..." + and getOSConfig partition.script >"$script" + and "$script" + and rm "$script" + end - and echo "Cloning project..." - and source "$dir/../../lib/copy-repo.fish" "$mountDir$PROJECT_CLONE_ROOT" - runChroot "$mountDir" git config --system --add safe.directory "$PROJECT_CLONE_ROOT" - and runHook setupOS + function postAction + echo "Setup finished!" + and echo "This machine will reboot in 5 seconds..." + and echo "Press CTRL-C to abort..." + and sleep 5 + and systemctl reboot + end - and echo "Preparing auto-login..." - and runHook --force autologin "Please set up a function `autologin` for setting up autologin for the `root` user" - - and begin - set -l script (string escape (getCloneFile (getInstallerScript))) - - wrapScript ( - string join " " \ - "CONFIG_NAME=$(string escape "$CONFIG_NAME")" \ - (string escape $script)) - - end | runChroot "$mountDir" tee /root/.bash_profile >/dev/null - - and echo "Setup finished!" - and echo "This machine will reboot in 5 seconds..." - and echo "Press CTRL-C to abort..." - and sleep 5 - and systemctl reboot + runAction setupAction end diff --git a/scripts/lib/action.fish b/scripts/lib/action.fish new file mode 100644 index 00000000..3330e28a --- /dev/null +++ b/scripts/lib/action.fish @@ -0,0 +1,71 @@ +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..." + if ! git status -C (status dirname) &> /dev/null + git config --system --add safe.directory (realpath "$(status dirname)/../..") + end + + and runHook preAction || true + $argv + and runHook postAction || 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 diff --git a/scripts/lib/config.fish b/scripts/lib/config.fish index 84655f89..a4bf5aae 100644 --- a/scripts/lib/config.fish +++ b/scripts/lib/config.fish @@ -1,4 +1,4 @@ set projectName port-valhalla set nixPkgsVersion nixos-24.05 -set cacheRoot ~/".cache/$projectName" -set nixPkgsCache "$cacheRoot/nixpkgs/$nixPkgsVersion" +set valhallaCache ~/".cache/$projectName" +set nixPkgsCache "$valhallaCache/nixpkgs/$nixPkgsVersion"