Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

silence warning if NIX_INDEX_DATABASE is set #73

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
env,
os::unix::prelude::CommandExt,
path::{Path, PathBuf},
process::Command,
Expand All @@ -12,28 +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() {
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
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down