|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +// Copyright (C) 2026 Mohamed Hammad |
| 3 | + |
| 4 | +//! Top-level [`State`] struct and the `[managed.*]` / `[adopted.*]` records |
| 5 | +//! it owns. |
| 6 | +
|
| 7 | +use crate::failure::FailureRef; |
| 8 | +use crate::history::HistoryEntry; |
| 9 | +use crate::reconciliation::ReconciliationEntry; |
| 10 | +use schemars::JsonSchema; |
| 11 | +use serde::{Deserialize, Serialize}; |
| 12 | +use std::collections::BTreeMap; |
| 13 | +use std::path::PathBuf; |
| 14 | +use time::OffsetDateTime; |
| 15 | + |
| 16 | +/// Current `schema_version` written by this build. |
| 17 | +/// |
| 18 | +/// Incremented when the on-disk shape changes incompatibly. Migration |
| 19 | +/// from older versions lands in `migrate.rs` (chunk M1-W1-F). |
| 20 | +pub const SCHEMA_VERSION: u32 = 1; |
| 21 | + |
| 22 | +/// In-memory mirror of `/var/lib/pearlite/state.toml`. |
| 23 | +/// |
| 24 | +/// The full schema is specified in PRD §7.2. `State` is mutated only by |
| 25 | +/// `pearlite-engine`; every other crate consumes it read-only. |
| 26 | +/// |
| 27 | +/// `Eq` is intentionally **not** derived: the `reserved` field carries a |
| 28 | +/// `toml::Value` map for forward-compatibility, and `toml::Value::Float` |
| 29 | +/// wraps an `f64` that is not `Eq`. Use `PartialEq` (which is derived) |
| 30 | +/// for equality checks in tests. |
| 31 | +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] |
| 32 | +pub struct State { |
| 33 | + /// Schema version of the file at last write. |
| 34 | + pub schema_version: u32, |
| 35 | + /// Hostname this state belongs to. Mismatch with `hostname()` is |
| 36 | + /// preflight Class 1. |
| 37 | + pub host: String, |
| 38 | + /// Pearlite version that last wrote the file. |
| 39 | + pub tool_version: String, |
| 40 | + /// Path to the user's Pearlite config repository at last apply. |
| 41 | + pub config_dir: PathBuf, |
| 42 | + /// Timestamp of the last successful `apply`. |
| 43 | + #[serde(with = "time::serde::iso8601::option", default)] |
| 44 | + #[schemars(with = "Option<String>")] |
| 45 | + pub last_apply: Option<OffsetDateTime>, |
| 46 | + /// Timestamp of the last write of any kind. |
| 47 | + #[serde(with = "time::serde::iso8601::option", default)] |
| 48 | + #[schemars(with = "Option<String>")] |
| 49 | + pub last_modified: Option<OffsetDateTime>, |
| 50 | + /// Pearlite-managed packages, configs, services, kernel, and user envs. |
| 51 | + #[serde(default)] |
| 52 | + pub managed: Managed, |
| 53 | + /// User-flagged "leave alone" packages. |
| 54 | + #[serde(default)] |
| 55 | + pub adopted: Adopted, |
| 56 | + /// Append-only log of successful applies. |
| 57 | + #[serde(default)] |
| 58 | + pub history: Vec<HistoryEntry>, |
| 59 | + /// Log of `pearlite reconcile --commit` invocations. |
| 60 | + #[serde(default)] |
| 61 | + pub reconciliations: Vec<ReconciliationEntry>, |
| 62 | + /// Pointers to `/var/lib/pearlite/failures/<plan-id>.json` records. |
| 63 | + #[serde(default)] |
| 64 | + pub failures: Vec<FailureRef>, |
| 65 | + /// Forward-compat namespace; arbitrary unknown keys preserved on |
| 66 | + /// round-trip via the toml_edit-based atomic writer. Excluded from |
| 67 | + /// the JSON Schema because its values are untyped by design. |
| 68 | + #[serde(default)] |
| 69 | + #[schemars(skip)] |
| 70 | + pub reserved: BTreeMap<String, toml::Value>, |
| 71 | +} |
| 72 | + |
| 73 | +/// `[managed.*]` namespace: everything Pearlite has installed or written. |
| 74 | +#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)] |
| 75 | +pub struct Managed { |
| 76 | + /// pacman/AUR packages installed via Pearlite. |
| 77 | + #[serde(default)] |
| 78 | + pub pacman: Vec<String>, |
| 79 | + /// Cargo crates installed via Pearlite. |
| 80 | + #[serde(default)] |
| 81 | + pub cargo: Vec<String>, |
| 82 | + /// Per-file metadata for managed `/etc` config files. |
| 83 | + #[serde(default)] |
| 84 | + pub config_files: Vec<ConfigFileRecord>, |
| 85 | + /// systemd unit state at last apply. |
| 86 | + #[serde(default)] |
| 87 | + pub services: ServicesState, |
| 88 | + /// Per-user Home Manager generation and config hash. |
| 89 | + #[serde(default)] |
| 90 | + pub user_env: Vec<UserEnvRecord>, |
| 91 | + /// Kernel package, version, cmdline, modules at last apply. |
| 92 | + #[serde(default)] |
| 93 | + pub kernel: Option<KernelRecord>, |
| 94 | +} |
| 95 | + |
| 96 | +/// One managed `/etc` config file's record. |
| 97 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 98 | +pub struct ConfigFileRecord { |
| 99 | + /// Absolute destination path on the host. |
| 100 | + pub target: PathBuf, |
| 101 | + /// Source path within the user's config repo. |
| 102 | + pub source_in_repo: PathBuf, |
| 103 | + /// SHA-256 of the file contents at last successful write, hex-encoded. |
| 104 | + pub sha256: String, |
| 105 | + /// File mode (`stat(2).st_mode & 0o7777`). |
| 106 | + pub mode: u32, |
| 107 | + /// Owning user. |
| 108 | + pub owner: String, |
| 109 | + /// Owning group. |
| 110 | + pub group: String, |
| 111 | +} |
| 112 | + |
| 113 | +/// systemd-unit state recorded by Pearlite at apply time. |
| 114 | +#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)] |
| 115 | +pub struct ServicesState { |
| 116 | + /// Units enabled (and started) by Pearlite. |
| 117 | + #[serde(default)] |
| 118 | + pub enabled: Vec<String>, |
| 119 | + /// Units disabled by Pearlite. |
| 120 | + #[serde(default)] |
| 121 | + pub disabled: Vec<String>, |
| 122 | + /// Units masked by Pearlite. |
| 123 | + #[serde(default)] |
| 124 | + pub masked: Vec<String>, |
| 125 | +} |
| 126 | + |
| 127 | +/// One user's Home Manager generation pointer. |
| 128 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 129 | +pub struct UserEnvRecord { |
| 130 | + /// Login name. |
| 131 | + pub user: String, |
| 132 | + /// Home Manager generation number at last apply. |
| 133 | + pub generation: u64, |
| 134 | + /// SHA-256 of the user's HM config directory at last apply. |
| 135 | + pub config_hash: String, |
| 136 | +} |
| 137 | + |
| 138 | +/// Kernel record: package, version, cmdline, modules at last apply. |
| 139 | +#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)] |
| 140 | +pub struct KernelRecord { |
| 141 | + /// Kernel package name (e.g. `linux-cachyos`). |
| 142 | + pub package: String, |
| 143 | + /// Installed kernel version string. |
| 144 | + pub version: String, |
| 145 | + /// Kernel command-line as last applied. |
| 146 | + #[serde(default)] |
| 147 | + pub cmdline: Vec<String>, |
| 148 | + /// Loaded modules as last applied. |
| 149 | + #[serde(default)] |
| 150 | + pub modules: Vec<String>, |
| 151 | +} |
| 152 | + |
| 153 | +/// `[adopted.*]` namespace: drift items the user has explicitly told |
| 154 | +/// Pearlite to leave alone. |
| 155 | +#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)] |
| 156 | +pub struct Adopted { |
| 157 | + /// Adopted pacman/AUR packages. |
| 158 | + #[serde(default)] |
| 159 | + pub pacman: Vec<String>, |
| 160 | + /// Adopted cargo crates. |
| 161 | + #[serde(default)] |
| 162 | + pub cargo: Vec<String>, |
| 163 | +} |
0 commit comments