sector-f-linux

Sector F Linux
git clone https://git.sectorflabs.com/sector-f-linux.git
Log | Files | Refs | README | LICENSE

commit 266a18e09bdcc2e9962561743c09b905898d3b25
parent b7db1f8b6c1b107c057936b222310bc5dee68fd8
Author: Divan Visagie <me@divanv.com>
Date:   Sat, 28 Nov 2026 00:40:21 +0100

greeter: a "Give this computer a name" step between the clock and the account (ADR-0019)

What the user will see: after the time zone, "Give this computer a name." with one field that starts
on the name the machine has now (every machine from this image is "sectorf", so two on one network
clash). Continue is disabled until the name is valid and says what is wrong ("Use letters, numbers
and hyphens only"); Continue sets it, Skip keeps the old one, Back returns to the clock, Enter is
Continue. The steps now read "Step 2 of 3" and so on.

How:
- model::check_hostname: the rule hostnamed applies to a static name, one DNS label (letters, numbers,
  hyphens, not starting or ending with a hyphen, 63 at most); normalise_hostname lower-cases what was
  typed and trims it, so "Alices-Laptop" is accepted as "alices-laptop" rather than refused.
- Backend gets hostname() and set_hostname(): the real one reads hostnamed's StaticHostname (the
  running name if no static one is set) and calls SetStaticHostname over D-Bus; the mock keeps it in
  memory and fails on the same "broken" input as the login does, to show an error.
- Step gets Name between Clock and Account; one submit_step() so Enter is Continue on whichever step it
  is. The permission for it is in the commit before (polkit, the same first-run window).

Tests: 34 pass (6 new): the name starts as the machine's; Continue lower-cases and trims, sets it,
goes to the account; names that are not host names ("", "my laptop", "-x", "a_b") start no worker; the
same name asks nothing of the system; a name the system refuses stays on the step with the error; and
the host-name rule has its own test. Skip/Back now cover three steps. Clippy and fmt clean. Not
tried on the real machine yet.

Diffstat:
Mdocs/adr/0019-the-first-run-setup-is-several-steps.md | 4++++
Mgreeter/src/app.rs | 157+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mgreeter/src/backend.rs | 20++++++++++++++++++++
Mgreeter/src/model.rs | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgreeter/src/system.rs | 24++++++++++++++++++++++++
5 files changed, 252 insertions(+), 7 deletions(-)

diff --git a/docs/adr/0019-the-first-run-setup-is-several-steps.md b/docs/adr/0019-the-first-run-setup-is-several-steps.md @@ -75,6 +75,10 @@ The layout that localed writes has to reach two compositors, and today neither w when Continue is pressed (nothing is asked of the system if it is unchanged). Its polkit rule is in the rules file. Seen in the greeter's mock; not yet on the real machine. +- **The Computer name step** (2026-11-28): one field, started on the name the machine has now + (`sectorf`), checked as a host name (letters, numbers and hyphens, 63 at most, case folded) before + Continue is allowed; set through hostnamed's `SetStaticHostname`. After the clock, before the account. + ## Open questions - Whether the clock should offer a guess when there is a network by then (Wi-Fi comes first). diff --git a/greeter/src/app.rs b/greeter/src/app.rs @@ -37,11 +37,12 @@ pub enum Screen { #[derive(Clone, Copy, PartialEq, Debug)] pub enum Step { Clock, + Name, Account, } impl Step { - const ALL: [Step; 2] = [Step::Clock, Step::Account]; + const ALL: [Step; 3] = [Step::Clock, Step::Name, Step::Account]; fn index(self) -> usize { Self::ALL.iter().position(|s| *s == self).unwrap_or(0) @@ -101,6 +102,7 @@ enum Done { Login(Result<(), LoginError>), Created(Result<(), String>), Zone(Result<(), String>), + Name(Result<(), String>), } struct Screenshot { @@ -133,6 +135,9 @@ pub struct App { zone: String, zone_filter: String, zone_scrolled: bool, + /// The name step: what is typed for the computer's name. + host: String, + host_focused: bool, screenshot: Option<Screenshot>, finished: bool, } @@ -171,6 +176,8 @@ impl App { zone: String::new(), zone_filter: String::new(), zone_scrolled: false, + host: String::new(), + host_focused: false, screenshot: None, finished: false, }; @@ -186,6 +193,7 @@ impl App { if self.zones.is_empty() { self.zones = self.backend.timezones(); self.zone = self.backend.timezone(); + self.host = self.backend.hostname(); } Screen::Setup } else { @@ -213,6 +221,14 @@ impl App { self.login.text.clear(); self.status = Status::Wrong; } + (Screen::Setup, "name") => { + self.step = Step::Name; + self.host = "alices-laptop".into(); + } + (Screen::Setup, "name-problem") => { + self.step = Step::Name; + self.host = "Alice's laptop".into(); + } (Screen::Setup, "clock") => { self.zone_filter = "stock".into(); self.zone = "Europe/Stockholm".into(); @@ -296,6 +312,33 @@ impl App { self.spawn(move |backend| Done::Zone(backend.set_timezone(&zone))); } + /// The name as it will be set: lower case, nothing round it. + fn host_name(&self) -> String { + model::normalise_hostname(&self.host) + } + + /// Sets the name typed and goes on; with it unchanged there is nothing to ask the system. + fn submit_name(&mut self) { + let name = self.host_name(); + if self.status == Status::Checking || model::check_hostname(&name).is_err() { + return; + } + if name == self.backend.hostname() { + return self.go_on(); + } + self.status = Status::Checking; + self.spawn(move |backend| Done::Name(backend.set_hostname(&name))); + } + + /// Continue, on whichever step this is. + fn submit_step(&mut self) { + match self.step { + Step::Clock => self.submit_clock(), + Step::Name => self.submit_name(), + Step::Account => self.submit_setup(), + } + } + /// To the next step, with nothing left over from this one. fn go_on(&mut self) { self.status = Status::Typing; @@ -359,7 +402,8 @@ impl App { self.status = Status::Problem(e); } Done::Zone(Ok(())) => self.go_on(), - Done::Zone(Err(e)) => self.status = Status::Problem(e), + Done::Zone(Err(e)) | Done::Name(Err(e)) => self.status = Status::Problem(e), + Done::Name(Ok(())) => self.go_on(), } } } @@ -413,7 +457,7 @@ impl App { .. } = event { - self.submit_clock(); + self.submit_step(); } return; } @@ -597,6 +641,7 @@ impl App { ui.add_space(4.0); let subtitle = match self.step { Step::Clock => "Choose your time zone, so the clock is right.", + Step::Name => "Give this computer a name.", Step::Account => "Let's set up this computer.", }; ui.label( @@ -607,6 +652,7 @@ impl App { ui.add_space(28.0); match self.step { Step::Clock => self.draw_clock(ui), + Step::Name => self.draw_name(ui), Step::Account => self.draw_account(ui), } ui.add_space(12.0); @@ -705,6 +751,34 @@ impl App { self.status_line(ui, &hint); } + /// The computer's name (ADR-0019): how it shows up on a network. + fn draw_name(&mut self, ui: &mut Ui) { + let checking = self.status == Status::Checking; + let edit = ui.add_enabled( + !checking, + egui::TextEdit::singleline(&mut self.host) + .hint_text("a name for this computer") + .desired_width(field::SIZE.x) + .margin(egui::Margin::symmetric(14, 10)), + ); + if !self.host_focused { + edit.request_focus(); + self.host_focused = true; + } + if edit.changed() { + self.status = Status::Typing; + } + ui.add_space(14.0); + let problem = model::check_hostname(&self.host_name()).err(); + let ready = problem.is_none() && !checking; + if wide_button(ui, "Continue", ready, checking).clicked() && ready { + self.submit_name(); + } + ui.add_space(10.0); + let hint = problem.unwrap_or("It is how this computer shows up on your network"); + self.status_line(ui, hint); + } + /// Under every step: where this one is among them, Back, and Skip (the account cannot be /// skipped: it is what the setup is for). fn draw_steps(&mut self, ui: &mut Ui) { @@ -1204,13 +1278,13 @@ mod tests { } #[test] - fn continuing_sets_the_zone_and_goes_to_the_account() { + fn continuing_sets_the_zone_and_goes_to_the_name() { let mut a = quick(&[]); a.zone = "Europe/Stockholm".into(); a.handle_keys(&[key(egui::Key::Enter, false)]); assert_eq!(a.status, Status::Checking); settle(&mut a); - assert_eq!(a.step, Step::Account); + assert_eq!(a.step, Step::Name); assert_eq!(a.status, Status::Typing); assert_eq!( a.backend.timezone(), @@ -1224,7 +1298,7 @@ mod tests { let mut a = quick(&[]); a.submit_clock(); assert_eq!(a.status, Status::Typing, "no worker was started"); - assert_eq!(a.step, Step::Account); + assert_eq!(a.step, Step::Name); } #[test] @@ -1242,8 +1316,14 @@ mod tests { fn skip_and_back_walk_the_steps_without_changing_anything() { let mut a = quick(&[]); a.go_on(); + a.go_on(); assert_eq!(a.step, Step::Account); + a.go_on(); + assert_eq!(a.step, Step::Account, "nothing comes after the account"); assert_eq!(a.backend.timezone(), "UTC"); + assert_eq!(a.backend.hostname(), "sectorf"); + a.go_back(); + assert_eq!(a.step, Step::Name); a.go_back(); assert_eq!(a.step, Step::Clock); a.go_back(); @@ -1254,6 +1334,60 @@ mod tests { ); } + /// A fresh machine, on the name step. + fn at_name() -> App { + let mut a = quick(&[]); + a.step = Step::Name; + a + } + + #[test] + fn the_name_starts_as_the_one_the_machine_has() { + assert_eq!(at_name().host, "sectorf"); + } + + #[test] + fn continuing_sets_the_name_in_lower_case_and_goes_to_the_account() { + let mut a = at_name(); + a.host = " Alices-Laptop ".into(); + a.handle_keys(&[key(egui::Key::Enter, false)]); + assert_eq!(a.status, Status::Checking); + settle(&mut a); + assert_eq!(a.step, Step::Account); + assert_eq!(a.backend.hostname(), "alices-laptop"); + } + + #[test] + fn a_name_that_is_not_a_host_name_is_not_sent() { + let mut a = at_name(); + for bad in ["", "my laptop", "-x", "a_b"] { + a.host = bad.into(); + a.submit_name(); + assert_eq!(a.status, Status::Typing, "{bad:?} started a worker"); + assert_eq!(a.step, Step::Name); + } + assert_eq!(a.backend.hostname(), "sectorf"); + } + + #[test] + fn the_same_name_asks_nothing_of_the_system() { + let mut a = at_name(); + a.submit_name(); + assert_eq!(a.status, Status::Typing); + assert_eq!(a.step, Step::Account); + } + + #[test] + fn a_name_the_system_refuses_is_shown_and_the_step_stays() { + let mut a = at_name(); + a.host = MOCK_BROKEN.into(); + a.submit_name(); + settle(&mut a); + assert_eq!(a.step, Step::Name); + assert!(matches!(&a.status, Status::Problem(e) if e.contains("hostnamed"))); + assert_eq!(a.backend.hostname(), "sectorf"); + } + #[test] fn typing_in_the_search_does_not_reach_the_account_fields() { let mut a = quick(&[]); @@ -1267,7 +1401,16 @@ mod tests { let mut a = quick(users); let ctx = a.ctx.clone(); egui_extras::install_image_loaders(&ctx); - for what in ["", "typing", "wrong", "problem", "filled", "clock"] { + for what in [ + "", + "typing", + "wrong", + "problem", + "filled", + "clock", + "name", + "name-problem", + ] { a.demo(what); let _ = ctx.run_ui(egui::RawInput::default(), |ui| a.draw(ui)); } diff --git a/greeter/src/backend.rs b/greeter/src/backend.rs @@ -49,6 +49,10 @@ pub trait Backend: Send + Sync { fn timezone(&self) -> String; /// Sets it, through timedated, which polkit lets the greeter do only on the first run. fn set_timezone(&self, zone: &str) -> Result<(), String>; + /// The computer's name now (ADR-0019, the setup's name step). + fn hostname(&self) -> String; + /// Sets it, through hostnamed, which polkit lets the greeter do only on the first run. + fn set_hostname(&self, name: &str) -> Result<(), String>; } /// A backend with accounts in memory, for the screens and the tests. The password that works is @@ -57,6 +61,7 @@ pub struct Mock { users: Mutex<Vec<User>>, delay: Duration, zone: Mutex<String>, + host: Mutex<String>, } /// The zones the mock knows, as a few of the real ones. @@ -93,6 +98,7 @@ impl Mock { ), delay, zone: Mutex::new("UTC".into()), + host: Mutex::new("sectorf".into()), } } @@ -170,6 +176,20 @@ impl Backend for Mock { *self.zone.lock().unwrap() = zone.into(); Ok(()) } + + fn hostname(&self) -> String { + self.host.lock().unwrap().clone() + } + + fn set_hostname(&self, name: &str) -> Result<(), String> { + std::thread::sleep(self.delay); + if name == MOCK_BROKEN { + return Err("could not set the name: hostnamed is not running".into()); + } + crate::model::check_hostname(name).map_err(str::to_string)?; + *self.host.lock().unwrap() = name.into(); + Ok(()) + } } #[cfg(test)] diff --git a/greeter/src/model.rs b/greeter/src/model.rs @@ -61,6 +61,33 @@ pub fn wipe(secret: &mut String) { secret.reserve(MAX_PASSWORD); } +/// A computer's name as hostnamed takes it for a static host name: one DNS label, so letters, +/// numbers and hyphens, not starting or ending with a hyphen, up to 63 characters. (Case is +/// folded before this is asked.) +pub fn check_hostname(name: &str) -> Result<(), &'static str> { + if name.is_empty() { + return Err("Give the computer a name"); + } + if name.len() > 63 { + return Err("Keep the name to 63 characters"); + } + if !name + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') + { + return Err("Use letters, numbers and hyphens only"); + } + if name.starts_with('-') || name.ends_with('-') { + return Err("Start and end the name with a letter or number"); + } + Ok(()) +} + +/// What is typed for the name, as it will be set: no spaces round it, lower case. +pub fn normalise_hostname(typed: &str) -> String { + typed.trim().to_lowercase() +} + /// The first-run form, in the order it is filled in. #[derive(Default)] pub struct SetupForm { @@ -120,6 +147,33 @@ mod tests { } #[test] + fn host_names_are_single_dns_labels() { + for ok in [ + "sectorf", + "a", + "alices-laptop", + "x1", + "9lives", + &"a".repeat(63), + ] { + assert_eq!(check_hostname(ok), Ok(()), "{ok}"); + } + for bad in [ + "", + "-a", + "a-", + "my laptop", + "a_b", + "a.b", + "Å", + &"a".repeat(64), + ] { + assert!(check_hostname(bad).is_err(), "{bad:?}"); + } + assert_eq!(normalise_hostname(" Alices-Laptop "), "alices-laptop"); + } + + #[test] fn full_names_are_checked() { assert_eq!(check_full_name("Alice Example"), Ok(())); assert_eq!(check_full_name("Åsa Öberg"), Ok(())); diff --git a/greeter/src/system.rs b/greeter/src/system.rs @@ -14,6 +14,8 @@ const ACCOUNTS_PATH: &str = "/org/freedesktop/Accounts"; const LOGIND: &str = "org.freedesktop.login1"; const TIMEDATE: &str = "org.freedesktop.timedate1"; const TIMEDATE_PATH: &str = "/org/freedesktop/timedate1"; +const HOSTNAME: &str = "org.freedesktop.hostname1"; +const HOSTNAME_PATH: &str = "/org/freedesktop/hostname1"; /// The first human uid, as in /etc/login.defs. const FIRST_UID: u32 = 1000; @@ -244,6 +246,28 @@ impl Backend for System { .call::<_, _, ()>("SetTimezone", &(zone, false)) .map_err(|e| format!("could not set the time zone: {e}")) } + + fn hostname(&self) -> String { + let get = || -> Result<String, String> { + let bus = Self::bus()?; + let hostnamed = Self::proxy(&bus, HOSTNAME, HOSTNAME_PATH, HOSTNAME)?; + // The static name is the one that persists; the running one when none is set. + match hostnamed.get_property::<String>("StaticHostname") { + Ok(name) if !name.is_empty() => Ok(name), + _ => hostnamed + .get_property::<String>("Hostname") + .map_err(|e| e.to_string()), + } + }; + get().unwrap_or_default() + } + + fn set_hostname(&self, name: &str) -> Result<(), String> { + let bus = Self::bus()?; + Self::proxy(&bus, HOSTNAME, HOSTNAME_PATH, HOSTNAME)? + .call::<_, _, ()>("SetStaticHostname", &(name, false)) + .map_err(|e| format!("could not set the name: {e}")) + } } #[cfg(test)]