PortValhalla/scripts/Common/OS/install.fish

107 lines
3.1 KiB
Fish
Raw Normal View History

2024-07-13 13:12:35 +00:00
#!/bin/env fish
set -l dir (status dirname)
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0)
2024-10-31 23:38:33 +00:00
source "$dir/../Scripts/settings.fish"
2024-07-13 13:12:35 +00:00
source "$dir/../Scripts/hooks.fish"
if [ (id -u) -eq 0 ]
source "$dir/../../config.fish"
2024-07-13 13:12:35 +00:00
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
2024-10-31 21:55:33 +00:00
set -l channelDir /nix/var/nix/profiles/per-user/root/channels/nixpkgs
rm ~/.bash_profile
2024-07-13 13:12:35 +00:00
2024-10-31 21:55:33 +00:00
if ! git status -C (status dirname) &> /dev/null
git config --system --add safe.directory (realpath "$(status dirname)/../../..")
end
2024-10-31 20:52:01 +00:00
if [ -z "$TMUX" ]
2024-10-31 21:55:33 +00:00
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
2024-10-31 20:52:01 +00:00
else
2024-10-31 21:55:33 +00:00
set -l name (getOSConfig setupUser.name)
2024-10-31 20:52:01 +00:00
begin
echo "Creating setup user"
2024-09-22 22:23:41 +00:00
2024-10-31 20:52:01 +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-07-13 13:12:35 +00:00
2024-10-31 20:52:01 +00:00
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
2024-07-13 13:12:35 +00:00
and sudo --preserve-env --set-home --user "$name" $cmdline
2024-10-31 20:52:01 +00:00
rm "$sudoConfig"
userdel -rf "$name"
2024-10-31 21:55:54 +00:00
rm -rf "$channelDir"
2024-10-31 20:52:01 +00:00
end
2024-07-13 13:12:35 +00:00
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)
2024-07-13 13:49:55 +00:00
runHook initialize || true
2024-09-13 13:10:29 +00:00
and runHook installOS || true
2024-07-19 22:46:02 +00:00
2024-09-13 13:31:42 +00:00
and runHook installSoftware || begin
2024-09-22 22:23:41 +00:00
echo "Installing software..."
and if [ -n "$deployScript" ]
source $deployScript
end
end
2024-09-26 22:54:27 +00:00
and runHook addUsers || begin
source "$dir/users.fish"
end
runHook initializeUsers || begin
if [ -n "$deployScript" ]
2024-10-31 23:38:33 +00:00
source "$dir/../Scripts/settings.fish"
2024-09-22 22:23:41 +00:00
for name in (getUsers | jq '.[]' --raw-output0 | string split0)
2024-09-22 22:23:41 +00:00
echo "Configuring user `$name`..."
and source $deployScript userConfig $name
end
end
end
2024-09-13 13:31:42 +00:00
and runHook postInstall || true
2024-09-26 22:50:29 +00:00
and sudo git config remove-section --system safe || true
2024-09-22 13:14:42 +00:00
and begin
2024-09-22 22:23:41 +00:00
echo "Cleaning installation scripts..."
2024-09-22 13:14:42 +00:00
set -l projectPath (realpath "$(status dirname)/../../..")
cd (dirname "$projectPath")
sudo rm -rf "$projectPath"
end
and echo "The installation finished successfully!"
2024-09-19 20:29:50 +00:00
and echo "This machine will reboot in 5 seconds..."
and echo "Press CTRL-C to abort..."
and sleep 5
2024-09-20 03:35:15 +00:00
and systemctl reboot -i
2024-07-13 13:12:35 +00:00
end