nixfleet_agent/
host_facts.rs1use std::path::Path;
4
5use anyhow::{Context, Result};
6use nixfleet_proto::agent_wire::GenerationRef;
7
8#[cfg(target_os = "macos")]
9mod darwin;
10#[cfg(target_os = "linux")]
11mod linux;
12
13#[cfg(target_os = "macos")]
14pub use darwin::{boot_id, pending_generation};
15#[cfg(target_os = "linux")]
16pub use linux::{boot_id, pending_generation};
17
18pub const CURRENT_SYSTEM: &str = "/run/current-system";
25
26pub fn current_closure_hash() -> Result<String> {
27 let target =
28 std::fs::read_link(CURRENT_SYSTEM).with_context(|| format!("readlink {CURRENT_SYSTEM}"))?;
29 Ok(closure_hash_from_path(&target))
30}
31
32pub(crate) fn closure_hash_from_path(p: &Path) -> String {
35 let s = p.to_string_lossy();
36 s.rsplit('/')
37 .next()
38 .map(str::to_string)
39 .unwrap_or_else(|| s.to_string())
40}
41
42pub fn current_generation_ref() -> Result<GenerationRef> {
44 Ok(GenerationRef {
45 closure_hash: current_closure_hash()?,
46 channel_ref: None,
47 boot_id: boot_id()?,
48 })
49}