NixOS-Configs/workstation/systemd.nix

75 lines
2.4 KiB
Nix

{ pkgs, ... }:
let
readlink = "${pkgs.coreutils}/bin/readlink";
notify-send = "${pkgs.libnotify}/bin/notify-send";
in {
systemd = {
services = {
tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
# set this service as a oneshot job
serviceConfig.Type = "oneshot";
# have the job run this shell script
script = with pkgs; ''
# wait for tailscaled to settle
sleep 2
# check if we are already authenticated to tailscale
status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
exit 0
fi
# otherwise authenticate with tailscale
${tailscale}/bin/tailscale up --operator=don --authkey tskey-api-kDQcva6CNTRL-kvcJzSix6yLb2dgjr1Pi
'';
};
clean-keychain = {
description = "Clean up .keychain on boot";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils-full}/bin/rm -rf /home/don/.keychain";
};
};
};
user = {
services = {
detect-reboot-for-upgrade = {
script = ''
set -eu -o pipefail
booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
if [[ "''${booted}" != "''${built}" ]];
then
echo "Looks like we need a reboot!"
${notify-send} --urgency=low --icon=system-reboot "Reboot is needed for a NixOS upgrade."
fi
'';
serviceConfig = {
Type = "oneshot";
};
};
};
timers = {
detect-reboot-for-upgrade = {
wantedBy = [ "timers.target" ];
partOf = [ "detect-reboot-for-upgrade.service" ];
timerConfig = {
OnCalendar = "hourly";
Unit = "detect-reboot-for-upgrade.service";
};
};
};
};
};
}