hometools

Access Mac Keychain Access Passwords from a Rust App

April 2026

I use the built-in macOS Keychain Access app to store credentials for my various scripts and tools. I start off by creating a New Password Item... from the Keychain Access menu and filling in the details:

The new password prompt from the macOS KeyChain Access app

Then, I use this rust code to pull in passwords when I need them:

src/main.rs
use anyhow::Result;
use security_framework::passwords::get_generic_password;

fn main() -> Result<()> {
  let retreived_password = get_password("al9000-post-example")?;
  println!("{}", retreived_password);
  Ok(())
}

fn get_password(key: &str) -> Result<String> {
  let account = "alan";
  let response = get_generic_password(key, account)?;
  Ok(String::from_utf8(response)?)
}
with this Cargo.toml:
Cargo.toml
[package]
name = "access-mac-keychain-passwords"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.102"
security-framework = "3.7.0"
When I run the app from the command line I get a prompt from Keychain Access to enter the keychain's main password to unlock it.

They macOS Keychain Access app prompt for the main password.

Assuming I click "Always Allow" when I enter the proper password Keychain Access provides the requested password back to the app. From that point forward the app can get the password again without triggering the prompt.

It's hard to beat.

-a

Notes

References