Skip to content

Instantly share code, notes, and snippets.

@mfenniak
Created January 6, 2023 15:39
Show Gist options
  • Save mfenniak/dd551fa7409823ba5425103ef1396a51 to your computer and use it in GitHub Desktop.
Save mfenniak/dd551fa7409823ba5425103ef1396a51 to your computer and use it in GitHub Desktop.
NixOS module that provides hourly notifications when a reboot is needed for a kernel, initrd, or kernel-module upgrade
{ pkgs, ... }:
let
readlink = "${pkgs.coreutils}/bin/readlink";
notify-send = "${pkgs.libnotify}/bin/notify-send";
in {
systemd.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";
};
};
systemd.user.timers.detect-reboot-for-upgrade = {
wantedBy = [ "timers.target" ];
partOf = [ "detect-reboot-for-upgrade.service" ];
timerConfig = {
OnCalendar = "hourly";
Unit = "detect-reboot-for-upgrade.service";
};
};
}
@duckunix
Copy link

duckunix commented Jan 7, 2023

So, for the new NixOS person, how would I use this? Just including it in my configuration.nix file?

@mfenniak
Copy link
Author

mfenniak commented Jan 7, 2023

Hey @duckunix -- what I would recommend is to put it into a separate file next to your configuration.nix file, and then add it to imports in your main configuration.nix. For example, here's a snippet of my configuration.nix...

{
  ...
  imports = [
    ./smb-mounts.nix
    ./libvirtd.nix
    ./detect-reboot-needed.nix
  ];
  ...
}

It's a little easier this way to keep your main configuration.nix file nicely organized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment