nix-setup/nixos-edit.script.nix

93 lines
2.4 KiB
Nix
Raw Normal View History

with import <nixpkgs> {};
pkgs.writeShellScriptBin "nixos-edit" ''
#!/bin/sh
2024-10-19 03:37:32 +02:00
if [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "nixos-edit by tudbut"
echo
echo "supports commands:"
echo " - pull -- pulls the changes and switches to them"
echo " - edit -- edits the OS, pushes it, and switches"
echo " - update -- update the system"
echo " - init -- initialize the system (only run once)"
echo
echo "edit is the default mode."
exit 0
fi
2024-10-19 02:39:35 +02:00
if [ "$1" = "init" ] ; then
2024-10-19 03:07:57 +02:00
if [ "$(whoami)" = root ] ; then
2024-10-19 03:02:29 +02:00
echo "re-executing as tudbut"
echo -n "set password for tudbut? (Y/n) "
read yn
if [ "$yn" != "n" ] ; then
passwd tudbut || exit 1
fi
sleep 2
2024-10-19 03:10:34 +02:00
echo please make sure xorg is running
echo -n enter when ready
read
exec sudo -iu tudbut nixos-edit init || exit 1
2024-10-19 03:02:29 +02:00
fi
2024-10-19 03:10:34 +02:00
if [ "$DISPLAY" = "" ] ; then
export DISPLAY=:0
fi
2024-10-19 02:39:35 +02:00
sudo chown -R tudbut: /etc/nixos
echo dir setup done
2024-10-19 03:02:29 +02:00
echo "> setting up ssh"
echo ssh setup
2024-10-19 03:15:57 +02:00
if ! [ -e ~/.ssh/id_*.pub ] ; then
2024-10-19 03:02:29 +02:00
ssh-keygen || exit
fi
echo ssh key created
2024-10-19 03:13:34 +02:00
cat ~/.ssh/id_*.pub | xclip -selection clipboard
2024-10-19 03:02:29 +02:00
echo "-> copied to clipboard for later"
echo adding it to syncfs
echo "-> you will need to input your password"
2024-10-19 03:29:06 +02:00
ssh root@tudbut.de "ssh -p 23 localhost 'echo -e \"# $(cat /etc/hostname)\n$(cat ~/.ssh/id_*.pub)\n\" >> .ssh/authorized_keys'" || exit 1
2024-10-19 03:02:29 +02:00
echo "> ssh setup done"
echo "> setting up git"
git config --global user.name "TudbuT"
git config --global user.email "forge-public@mail.tudbut.de"
git config --global init.defaultBranch main
git config --global push.default current
echo configs set
echo mounting syncfs
2024-10-19 03:21:35 +02:00
mkdir ~/sync 2> /dev/null
2024-10-19 03:02:29 +02:00
startsync > /dev/null 2>&1 &
echo opening git key add page
echo "-> close firefox when complete."
firefox "https://git.tudbut.de/user/login?redirect_to=%2fuser%2fsettings%2fkeys" > /dev/null 2>&1
echo correcting remote
cd /etc/nixos
git remote set-url origin 'ssh://git@tudbut.de:222/TudbuT/nix-setup.git'
echo done
2024-10-19 02:39:35 +02:00
exit 0
fi
if [ "$(whoami)" = root ] ; then
echo "re-executing as tudbut"
sleep 2
exec sudo -iu tudbut nixos-edit $1 || exit 1
fi
cd /etc/nixos
if [ "$1" = "pull" ] ; then
git pull
fi
if [ "$1" = "edit" ] || [ "$1" = "" ] ; then
sudo hx .
git add .
EDITOR=hx git commit -a
git push
fi
if [ "$1" = "update" ] ; then
git push
sudo nixos-rebuild switch --upgrade
else
sudo nixos-rebuild switch
fi
''