From 6da3019f2ab7a0e92cd5247af15c7acae03a6c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 10 Mar 2024 12:24:24 +0100 Subject: [PATCH 1/2] silence warning if NIX_INDEX_DATABASE is set Related to https://github.com/nix-community/nix-index-database/issues/92 --- src/index.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.rs b/src/index.rs index 47b16a9..90ceefa 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,8 +1,5 @@ use std::{ - os::unix::prelude::CommandExt, - path::{Path, PathBuf}, - process::Command, - time::{Duration, SystemTime}, + env, os::unix::prelude::CommandExt, path::{Path, PathBuf}, process::Command, time::{Duration, SystemTime} }; /// Update the local nix-index database. @@ -21,6 +18,12 @@ pub fn check_database_exists() { /// Prints a warning if the nix-index database is out of date. pub fn check_database_updated() { + if env::var("NIX_INDEX_DATABASE").is_ok() { + // If the user has set NIX_INDEX_DATABASE, they are responsible for keeping it up to date + // because if it's part of the nix store, the timestamp be always 1970-01-01. + // This environment variable is set by nix-index-database. + return; + } let database_file = get_database_file(); if is_database_old(&database_file) { eprintln!( From 10c65154dbc53e31bd042c6a3b37e2aad509c664 Mon Sep 17 00:00:00 2001 From: Artturin Date: Tue, 12 Mar 2024 03:14:44 +0200 Subject: [PATCH 2/2] don't check database date if it's not writable --- src/index.rs | 44 ++++++++++++++++++++++++++++---------------- src/main.rs | 8 ++++++-- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/index.rs b/src/index.rs index 90ceefa..ee5cb07 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,5 +1,9 @@ use std::{ - env, os::unix::prelude::CommandExt, path::{Path, PathBuf}, process::Command, time::{Duration, SystemTime} + env, + os::unix::prelude::CommandExt, + path::{Path, PathBuf}, + process::Command, + time::{Duration, SystemTime}, }; /// Update the local nix-index database. @@ -9,34 +13,42 @@ pub fn update_database() { } /// Prints a warning if the nix-index database is non-existent -pub fn check_database_exists() { +pub fn check_database_exists() -> Result<(), ()> { let database_file = get_database_file(); if !database_file.exists() { eprintln!("Warning: Nix-index database does not exist, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`."); + return Err(()) } + Ok(()) } /// Prints a warning if the nix-index database is out of date. pub fn check_database_updated() { - if env::var("NIX_INDEX_DATABASE").is_ok() { - // If the user has set NIX_INDEX_DATABASE, they are responsible for keeping it up to date - // because if it's part of the nix store, the timestamp be always 1970-01-01. - // This environment variable is set by nix-index-database. - return; - } let database_file = get_database_file(); - if is_database_old(&database_file) { - eprintln!( - "Warning: Nix-index database is older than 30 days, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`." - ); - } + if check_database_exists().is_ok() { + if database_file.metadata().unwrap().permissions().readonly() { + // If db is not writable, they are responsible for keeping it up to date + // because if it's part of the nix store, the timestamp will always 1970-01-01. + return; + }; + if is_database_old(&database_file) { + eprintln!( + "Warning: Nix-index database is older than 30 days, either obtain a prebuilt database from https://github.com/Mic92/nix-index-database or try updating with `nix run 'nixpkgs#nix-index' --extra-experimental-features 'nix-command flakes'`." + ); + } + }; } /// Get the location of the nix-index database file fn get_database_file() -> PathBuf { - let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap(); - let cache_dir = base.get_cache_home(); - cache_dir.join("files") + match env::var("NIX_INDEX_DATABASE") { + Ok(db) => PathBuf::from(db), + Err(_) => { + let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap(); + let cache_dir = base.get_cache_home(); + cache_dir.join("files") + } + } } /// Test whether the database is more than 30 days old diff --git a/src/main.rs b/src/main.rs index e3c5ad5..4f116d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,6 @@ fn main() -> ExitCode { .expect("failed to execute nix-locate"); if !nix_locate_output.status.success() { - index::check_database_exists(); match std::str::from_utf8(&nix_locate_output.stderr) { Ok(stderr) => eprintln!("nix-locate failed with: {stderr}"), Err(_) => eprint!("nix-locate failed"), @@ -165,7 +164,12 @@ struct Opt { #[clap(short = 'P', long, env = "COMMA_PICKER", default_value = "fzy")] picker: String, - #[clap(short = 'F', long, env = "COMMA_NIXPKGS_FLAKE", default_value = "nixpkgs")] + #[clap( + short = 'F', + long, + env = "COMMA_NIXPKGS_FLAKE", + default_value = "nixpkgs" + )] nixpkgs_flake: String, /// DEPRECATED Update nix-index database