Skip to content

Commit 2259d3f

Browse files
committed
WIP: fallback-settings file
This provides a way for snaps, distro packages and other such installs to set the default-toolchain for users that have not gone through the rustup-init one-time set of questions.
1 parent ed6f719 commit 2259d3f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/config.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub struct Cfg {
112112
pub profile_override: Option<dist::Profile>,
113113
pub rustup_dir: PathBuf,
114114
pub settings_file: SettingsFile,
115+
pub fallback_settings: Option<Settings>,
115116
pub toolchains_dir: PathBuf,
116117
pub update_hash_dir: PathBuf,
117118
pub download_dir: PathBuf,
@@ -133,6 +134,20 @@ impl Cfg {
133134

134135
let settings_file = SettingsFile::new(rustup_dir.join("settings.toml"));
135136

137+
// Centralised file for multi-user systems to provide admin/distributor set initial values.
138+
let fallback_settings = if cfg!(not(windows)) {
139+
let f = SettingsFile::new(PathBuf::from("/etc/rustup/settings.toml"));
140+
// Users cannot fix issues with missing/unreadable/invalid centralised files, so we simply trap all errors.
141+
// Ideally we would separate these into missing file vs others, so that we could log a message about the bad
142+
// situation rather than entirely ignoring it.
143+
match f.with(|s| Ok(Some(s.clone()))) {
144+
Ok(res) => res,
145+
Err(_) => None,
146+
}
147+
} else {
148+
None
149+
};
150+
136151
let toolchains_dir = rustup_dir.join("toolchains");
137152
let update_hash_dir = rustup_dir.join("update-hashes");
138153
let download_dir = rustup_dir.join("downloads");
@@ -191,6 +206,7 @@ impl Cfg {
191206
profile_override: None,
192207
rustup_dir,
193208
settings_file,
209+
fallback_settings,
194210
toolchains_dir,
195211
update_hash_dir,
196212
download_dir,
@@ -365,9 +381,7 @@ impl Cfg {
365381
}
366382

367383
pub fn find_default(&self) -> Result<Option<Toolchain<'_>>> {
368-
let opt_name = self
369-
.settings_file
370-
.with(|s| Ok(s.default_toolchain.clone()))?;
384+
let opt_name = self.get_default()?;
371385

372386
if let Some(name) = opt_name {
373387
let toolchain = self
@@ -509,7 +523,14 @@ impl Cfg {
509523
}
510524

511525
pub fn get_default(&self) -> Result<Option<String>> {
512-
self.settings_file.with(|s| Ok(s.default_toolchain.clone()))
526+
let user_opt = self.settings_file.with(|s| Ok(s.default_toolchain.clone()));
527+
if let Some(fallback_settings) = &self.fallback_settings {
528+
match user_opt {
529+
Err(_) | Ok(None) => return Ok(fallback_settings.default_toolchain.clone()),
530+
_ => {}
531+
};
532+
};
533+
user_opt
513534
}
514535

515536
pub fn list_toolchains(&self) -> Result<Vec<String>> {

0 commit comments

Comments
 (0)