Skip to content

Commit

Permalink
Merge pull request #73 from nix-community/nix-index-fix
Browse files Browse the repository at this point in the history
silence warning if NIX_INDEX_DATABASE is set
  • Loading branch information
Artturin committed Mar 15, 2024
2 parents 91c3006 + 10c6515 commit c236234
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
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

0 comments on commit c236234

Please sign in to comment.