nix-setup/nixos-edit.script.nix

121 lines
3.2 KiB
Nix

with import <nixpkgs> {};
pkgs.writeShellScriptBin "nixos-edit" ''
#!/bin/sh
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 " - test -- edits the OS and switches"
echo " - push -- commits edits from test and pushes them"
echo " - update -- update the system"
echo " - clean -- does housekeeping"
echo " - init -- initialize the system (only run once)"
echo " - add -- installs a package"
echo
echo "edit is the default mode."
exit 0
fi
if [ "$1" = "init" ] ; then
if [ "$(whoami)" = root ] ; then
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
echo please make sure xorg is running
echo -n enter when ready
read
exec sudo -iu tudbut nixos-edit init || exit 1
fi
if [ "$DISPLAY" = "" ] ; then
export DISPLAY=:0
fi
sudo chown -R tudbut: /etc/nixos
echo dir setup done
echo "> setting up ssh"
echo ssh setup
if ! [ -e ~/.ssh/id_*.pub ] ; then
ssh-keygen || exit
fi
echo ssh key created
cat ~/.ssh/id_*.pub | xclip -selection clipboard
echo "-> copied to clipboard for later"
echo adding it to syncfs
echo "-> you will need to input your password"
ssh root@tudbut.de "ssh -p 23 localhost 'echo -e \"# $(cat /etc/hostname)\n$(cat ~/.ssh/id_*.pub)\n\" >> .ssh/authorized_keys'" || exit 1
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
mkdir ~/sync 2> /dev/null
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
exit 0
fi
if [ "$(whoami)" = root ] ; then
echo "re-executing as tudbut"
sleep 2
exec sudo -iu tudbut nixos-edit $1 || exit 1
fi
if [ "$1" = "clean" ] ; then
echo "> running gc"
sudo nix-collect-garbage --delete-older-than 60d
echo "> hard-linking"
sudo nix-store --optimise
echo "> all done"
exit
fi
[ "$1" = "edit" ] ||
[ "$1" = "" ] ||
[ "$1" = "push" ] ||
PUSH=$?
cd /etc/nixos
if [ "$1" = "pull" ] ; then
git pull
fi
if [ "$1" = "edit" ] || [ "$1" = "" ] || [ "$1" = "test" ] ; then
sudo hx .
fi
if [ "$1" = "add" ] ; then
sed -i -E "s/#marker.pkgs.end#/$2\n #marker.pkgs.end#/" home.nix
git diff home.nix
sleep 0.5
git commit -m "pkgs.user: add $2" home.nix || exit 1
git push
fi
if [ $PUSH = 0 ] ; then
git add .
EDITOR=hx git commit -a || exit
git push
if [ "$1" = "push" ] ; then
exit 0
fi
fi
if [ "$1" = "update" ] ; then
sudo nixos-rebuild switch --upgrade
else
sudo nixos-rebuild switch
fi
''