fix(nix): enumerate modern per-user profiles in sys list
This commit is contained in:
parent
9573f2e37a
commit
406f9b4b33
1 changed files with 102 additions and 5 deletions
|
|
@ -336,11 +336,29 @@ pub struct NixProfileInventory {
|
||||||
fn active_profile_links(root: &Path) -> Vec<(String, Option<String>, PathBuf)> {
|
fn active_profile_links(root: &Path) -> Vec<(String, Option<String>, PathBuf)> {
|
||||||
let base = target_path(root, "nix/var/nix/profiles");
|
let base = target_path(root, "nix/var/nix/profiles");
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
|
// Deduplicate by canonical target so a home XDG link pointing at the same
|
||||||
|
// generation as a `per-user` link is only enumerated once.
|
||||||
|
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
|
||||||
|
let push_link = |label: String,
|
||||||
|
user: Option<String>,
|
||||||
|
path: PathBuf,
|
||||||
|
out: &mut Vec<(String, Option<String>, PathBuf)>,
|
||||||
|
seen: &mut std::collections::HashSet<String>| {
|
||||||
|
if path.symlink_metadata().is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Best-effort dedup key: canonical path when resolvable, else the
|
||||||
|
// link path itself.
|
||||||
|
let key = std::fs::canonicalize(&path)
|
||||||
|
.map(|p| p.to_string_lossy().to_string())
|
||||||
|
.unwrap_or_else(|_| path.to_string_lossy().to_string());
|
||||||
|
if seen.insert(key) {
|
||||||
|
out.push((label, user, path));
|
||||||
|
}
|
||||||
|
};
|
||||||
for name in ["default", "profile"] {
|
for name in ["default", "profile"] {
|
||||||
let path = base.join(name);
|
let path = base.join(name);
|
||||||
if path.symlink_metadata().is_ok() {
|
push_link(name.to_string(), None, path, &mut out, &mut seen);
|
||||||
out.push((name.to_string(), None, path));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let per_user = base.join("per-user");
|
let per_user = base.join("per-user");
|
||||||
if let Ok(users) = std::fs::read_dir(&per_user) {
|
if let Ok(users) = std::fs::read_dir(&per_user) {
|
||||||
|
|
@ -350,10 +368,45 @@ fn active_profile_links(root: &Path) -> Vec<(String, Option<String>, PathBuf)> {
|
||||||
}
|
}
|
||||||
let user_name = user.file_name().to_string_lossy().to_string();
|
let user_name = user.file_name().to_string_lossy().to_string();
|
||||||
let path = user.path().join("profile");
|
let path = user.path().join("profile");
|
||||||
if path.symlink_metadata().is_ok() {
|
push_link(
|
||||||
out.push((format!("per-user:{user_name}"), Some(user_name), path));
|
format!("per-user:{user_name}"),
|
||||||
|
Some(user_name),
|
||||||
|
path,
|
||||||
|
&mut out,
|
||||||
|
&mut seen,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Modern per-user profiles (Nix >= 2.14 default): the user daemon writes
|
||||||
|
// `~/.local/state/nix/profiles/profile`; older setups use `~/.nix-profile`.
|
||||||
|
// Scan `root/home/*` and `root/root` so `sys list --nix` sees packages
|
||||||
|
// installed with `nix profile install` as a normal user.
|
||||||
|
let mut home_dirs: Vec<(String, PathBuf)> = Vec::new();
|
||||||
|
if let Ok(entries) = std::fs::read_dir(target_path(root, "home")) {
|
||||||
|
for entry in entries.flatten() {
|
||||||
|
if !entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let name = entry.file_name().to_string_lossy().to_string();
|
||||||
|
if name.starts_with('.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
home_dirs.push((name, entry.path()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
home_dirs.push(("root".to_string(), target_path(root, "root")));
|
||||||
|
home_dirs.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
for (user_name, home) in home_dirs {
|
||||||
|
for rel in [".local/state/nix/profiles/profile", ".nix-profile"] {
|
||||||
|
let path = home.join(rel);
|
||||||
|
push_link(
|
||||||
|
format!("per-user:{user_name}"),
|
||||||
|
Some(user_name.clone()),
|
||||||
|
path,
|
||||||
|
&mut out,
|
||||||
|
&mut seen,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out.sort_by(|a, b| a.0.cmp(&b.0));
|
out.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
out
|
out
|
||||||
|
|
@ -825,6 +878,50 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn modern_xdg_user_profiles_are_enumerated_by_sys_list() {
|
||||||
|
let runner = RecordingRunner::new();
|
||||||
|
let exec = make_exec(runner, false);
|
||||||
|
let root = Path::new(&exec.root);
|
||||||
|
// Generation store dir with a v3 manifest.
|
||||||
|
let store = root.join("nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-hello-2.12");
|
||||||
|
std::fs::create_dir_all(&store).unwrap();
|
||||||
|
std::fs::write(
|
||||||
|
store.join("manifest.json"),
|
||||||
|
r#"{
|
||||||
|
"version": 3,
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"active": true,
|
||||||
|
"attrPath": "legacyPackages.x86_64-linux.hello",
|
||||||
|
"storePaths": ["/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-hello-2.12"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
// Modern XDG home profile for user `phoebus`.
|
||||||
|
let home_profile_dir = root.join("home/phoebus/.local/state/nix/profiles");
|
||||||
|
std::fs::create_dir_all(&home_profile_dir).unwrap();
|
||||||
|
std::os::unix::fs::symlink(&store, home_profile_dir.join("profile")).unwrap();
|
||||||
|
// Legacy `~/.nix-profile` for user `legacy` pointing at the same
|
||||||
|
// generation must not double-count.
|
||||||
|
let legacy_home = root.join("home/legacy");
|
||||||
|
std::fs::create_dir_all(&legacy_home).unwrap();
|
||||||
|
std::os::unix::fs::symlink(&store, legacy_home.join(".nix-profile")).unwrap();
|
||||||
|
|
||||||
|
let inventory = installed_packages(root);
|
||||||
|
let hello: Vec<_> = inventory
|
||||||
|
.packages
|
||||||
|
.iter()
|
||||||
|
.filter(|p| p.name == "hello")
|
||||||
|
.collect();
|
||||||
|
assert_eq!(hello.len(), 1, "{:?}", inventory);
|
||||||
|
assert_eq!(hello[0].profile, "per-user:legacy");
|
||||||
|
assert_eq!(hello[0].user.as_deref(), Some("legacy"));
|
||||||
|
assert_eq!(hello[0].version.as_deref(), Some("2.12"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_manifest_v1_store_path_is_supported() {
|
fn parse_manifest_v1_store_path_is_supported() {
|
||||||
let text = r#"{
|
let text = r#"{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue