fix: pass hostname as a quoted string (#243)

This commit is contained in:
Silver 2025-08-02 12:50:38 +01:00 committed by GitHub
parent c6850451ef
commit 7ae6c9fa22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -102,7 +102,7 @@ fn find_flake_attr(flake_uri: &str, nix_options: &NixOptions) -> Result<String>
}
let hostname_os = nix::unistd::gethostname()?;
let hostname = hostname_os.to_string_lossy();
let hostname = escape_nix_string(&hostname_os.to_string_lossy());
let default = "default";
if let Some(full_uri) = try_flake_attr(flake, &hostname, nix_options, &system)? {
@ -113,6 +113,19 @@ fn find_flake_attr(flake_uri: &str, nix_options: &NixOptions) -> Result<String>
anyhow::bail!("No suitable flake attribute found, giving up.");
}
fn escape_nix_string(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
let mut i = 0;
for (j, _) in s.match_indices(['"', '\\']) {
out += &s[i..j];
i = j;
}
out += &s[i..];
out.push('"');
out
}
fn try_flake_attr(
flake: &str,
attr: &str,