nix-setup/nixos-edit.script.nix

122 lines
3.2 KiB
Nix
Raw Permalink 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"
2024-10-19 18:15:16 +02:00
echo " - test -- edits the OS and switches"
echo " - push -- commits edits from test and pushes them"
2024-10-19 03:37:32 +02:00
echo " - update -- update the system"
2024-10-20 10:58:28 +02:00
echo " - clean -- does housekeeping"
2024-10-19 03:37:32 +02:00
echo " - init -- initialize the system (only run once)"
echo " - add -- installs a package"
2024-10-19 03:37:32 +02:00
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
2024-10-20 10:58:28 +02:00
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" = "" ] ||
2024-10-23 17:29:03 +02:00
[ "$1" = "push" ]
2024-10-19 18:15:16 +02:00
PUSH=$?
cd /etc/nixos
if [ "$1" = "pull" ] ; then
2024-10-31 17:40:26 +01:00
git pull --rebase
fi
2024-10-19 18:15:16 +02:00
if [ "$1" = "edit" ] || [ "$1" = "" ] || [ "$1" = "test" ] ; then
2024-11-15 21:22:41 +01:00
hx .
2024-10-19 18:15:16 +02:00
fi
if [ "$1" = "add" ] ; then
2024-10-23 10:04:14 +02:00
sed -i -E "s/#marker.pkgs.end#/$2\n #marker.pkgs.end#/" home.nix
2024-10-23 10:08:40 +02:00
git diff home.nix
sleep 0.5
git commit -m "pkgs.user: add $2" home.nix || exit 1
git push
fi
2024-10-19 18:15:16 +02:00
if [ $PUSH = 0 ] ; then
git add .
2024-10-22 21:18:47 +02:00
EDITOR=hx git commit -a || exit
git push
2024-10-19 18:15:16 +02:00
if [ "$1" = "push" ] ; then
exit 0
fi
fi
if [ "$1" = "update" ] ; then
sudo nixos-rebuild switch --upgrade
else
sudo nixos-rebuild switch
fi
''