From e1833e95f2ba403dede7befc0f2a9a6cb333f22d Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 31 Jan 2022 14:30:03 -0800 Subject: feat(wpcarro/blog): Create short post about scheduled reboots Slowly building the habit of blogging more (especially about nix). Change-Id: I13006a6555d746ab55b4b282ea83bc732573a58e Reviewed-on: https://cl.tvl.fyi/c/depot/+/5149 Tested-by: BuildkiteCI Reviewed-by: wpcarro Autosubmit: wpcarro --- users/wpcarro/website/blog/posts.nix | 7 ++++ .../website/blog/posts/auto-reboot-nixos.md | 40 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 users/wpcarro/website/blog/posts/auto-reboot-nixos.md diff --git a/users/wpcarro/website/blog/posts.nix b/users/wpcarro/website/blog/posts.nix index 3ed1671982..5a58c9309c 100644 --- a/users/wpcarro/website/blog/posts.nix +++ b/users/wpcarro/website/blog/posts.nix @@ -22,4 +22,11 @@ content = ./posts/send-mail-as-2fa.md; draft = false; } + { + key = "auto-reboot-nixos"; + title = "Automatically Reboot NixOS"; + date = 1643666914; + content = ./posts/auto-reboot-nixos.md; + draft = false; + } ] diff --git a/users/wpcarro/website/blog/posts/auto-reboot-nixos.md b/users/wpcarro/website/blog/posts/auto-reboot-nixos.md new file mode 100644 index 0000000000..24474e6dfe --- /dev/null +++ b/users/wpcarro/website/blog/posts/auto-reboot-nixos.md @@ -0,0 +1,40 @@ +## Show me the codes + +Regularly rebooting machines can be a useful, hygienic practice, but quite +frankly I cannot be relied on to remember to regularly reboot my machine. + +Let's free-up some wetware-RAM by automating this with Nix. The following +addition to your `configuration.nix` will schedule daily reboots at `03:00`: + +```nix +systemd.timers.auto-reboot = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = "*-*-* 03:00:00"; + Unit = "reboot.target"; + }; +}; +``` + +If you want to fiddle with the date format, `systemd-analyze` is your friend: + +```shell +λ systemd-analyze calendar '*-*-* 03:00:00' +Normalized form: *-*-* 03:00:00 + Next elapse: Tue 2022-02-01 03:00:00 PST + (in UTC): Tue 2022-02-01 11:00:00 UTC + From now: 12h left +``` + +After calling `nixos-rebuild switch`, you can verify that `systemd` started the +timer with: + +```shell +λ systemctl list-timers auto-reboot +# output omitted because I'm writing this from a different machine +``` + +## That's all, folks! + +I wanted to keep this post short-and-sweet, to build the habit of posting more +regularly. Hopefully someone out there found this useful. -- cgit 1.4.1