Skip to content

Commit d07172d

Browse files
committed
Support for machine wide installs: 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. /etc/rustup/settings.toml can more now just provide a default toolchain for users of rustup on the machine. More keys will be supported as needed. No equivalent file is defined for Windows OS machines at this time.
1 parent 6de2f76 commit d07172d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/config.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ impl Display for PgpPublicKey {
108108
}
109109
}
110110

111+
pub const UNIX_FALLBACK_SETTINGS: &str = "/etc/rustup/settings.toml";
112+
111113
pub struct Cfg {
112114
pub profile_override: Option<dist::Profile>,
113115
pub rustup_dir: PathBuf,
114116
pub settings_file: SettingsFile,
117+
pub fallback_settings: Option<Settings>,
115118
pub toolchains_dir: PathBuf,
116119
pub update_hash_dir: PathBuf,
117120
pub download_dir: PathBuf,
@@ -133,6 +136,20 @@ impl Cfg {
133136

134137
let settings_file = SettingsFile::new(rustup_dir.join("settings.toml"));
135138

139+
// Centralised file for multi-user systems to provide admin/distributor set initial values.
140+
let fallback_settings = if cfg!(not(windows)) {
141+
let f = SettingsFile::new(PathBuf::from(UNIX_FALLBACK_SETTINGS));
142+
// Users cannot fix issues with missing/unreadable/invalid centralised files, so we simply trap all errors.
143+
// Ideally we would separate these into missing file vs others, so that we could log a message about the bad
144+
// situation rather than entirely ignoring it.
145+
match f.with(|s| Ok(Some(s.clone()))) {
146+
Ok(res) => res,
147+
Err(_) => None,
148+
}
149+
} else {
150+
None
151+
};
152+
136153
let toolchains_dir = rustup_dir.join("toolchains");
137154
let update_hash_dir = rustup_dir.join("update-hashes");
138155
let download_dir = rustup_dir.join("downloads");
@@ -191,6 +208,7 @@ impl Cfg {
191208
profile_override: None,
192209
rustup_dir,
193210
settings_file,
211+
fallback_settings,
194212
toolchains_dir,
195213
update_hash_dir,
196214
download_dir,
@@ -365,9 +383,7 @@ impl Cfg {
365383
}
366384

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

372388
if let Some(name) = opt_name {
373389
let toolchain = self
@@ -509,7 +525,14 @@ impl Cfg {
509525
}
510526

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

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

0 commit comments

Comments
 (0)