#!/bin/env fish set -l dir (status dirname) set -l cmdline (cat /proc/$fish_pid/cmdline | string split0) source "$dir/../Scripts/settings.fish" source "$dir/../Scripts/hooks.fish" if [ (id -u) -eq 0 ] set -l nixVersion nixos-24.05 set -l sudoConfig "/etc/sudoers.d/PortValhalla" set -l channelDir /nix/var/nix/profiles/per-user/root/channels/nixpkgs rm ~/.bash_profile 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 if [ ! -d "$channelDir" ] mkdir -p "$channelDir" and git clone https://github.com/NixOS/nixpkgs.git --depth=1 -b "$nixVersion" "$channelDir" and rm -rf "$channelDir/.git" end and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.' 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" rm -rf "$channelDir" end else if not type -q getDeploymentScript function getDeploymentScript echo "No deployment script specified! No software will be installed." 1>&2 false end end set -l deployScript (getDeploymentScript) runHook initialize || true and runHook installOS || true and runHook installSoftware || begin echo "Installing software..." and if [ -n "$deployScript" ] source $deployScript end end and runHook addUsers || begin source "$dir/users.fish" end runHook initializeUsers || begin if [ -n "$deployScript" ] source "$dir/../Scripts/settings.fish" for name in (getUsers | jq '.[]' --raw-output0 | string split0) echo "Configuring user `$name`..." and source $deployScript userConfig $name end end end and runHook postInstall || true and sudo git config remove-section --system safe || true and begin echo "Cleaning installation scripts..." set -l projectPath (realpath "$(status dirname)/../../..") cd (dirname "$projectPath") sudo rm -rf "$projectPath" end and echo "The installation finished successfully!" and echo "This machine will reboot in 5 seconds..." and echo "Press CTRL-C to abort..." and sleep 5 and systemctl reboot -i end