needs_reboot/lib.rs
1// Copyright (C) 2022 Stephane Raux. Distributed under the 0BSD license.
2
3//! # Overview
4//! - [📦 crates.io](https://crates.io/crates/needs-reboot)
5//! - [📖 Documentation](https://docs.rs/needs-reboot)
6//! - [âš– 0BSD license](https://spdx.org/licenses/0BSD.html)
7//!
8//! Crate to tell if a linux system needs to be rebooted
9//!
10//! This can help to decide whether to reboot a system after updating it. Currently only NixOS is
11//! supported.
12//!
13//! # Features
14//! - `tool`: Enables a CLI tool to check if a system should be rebooted.
15//!
16//! # Contribute
17//! All contributions shall be licensed under the [0BSD license](https://spdx.org/licenses/0BSD.html).
18
19#![deny(missing_docs)]
20#![deny(warnings)]
21
22use std::{io, path::Path};
23
24/// Returns `true` is the system should be rebooted, else `false`.
25pub fn check() -> Result<bool, io::Error> {
26 let booted = Path::new("/run/booted-system");
27 let new = Path::new("/nix/var/nix/profiles/system");
28 ["initrd", "kernel", "kernel-modules"]
29 .into_iter()
30 .map(
31 |component| Ok(booted.join(component).read_link()? != new.join(component).read_link()?),
32 )
33 .find(|needs| !matches!(needs, Ok(false)))
34 .unwrap_or(Ok(false))
35}