Skip to content

Commit 1cf582b

Browse files
author
Shelvacu
committed
deduplicate cache_dir definition
1 parent 03b8617 commit 1cf582b

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/bin/nix-index.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Tool for generating a nix-index database.
2-
use std::ffi::OsStr;
32
use std::fs::{self, File};
43
use std::io::{self, Write};
54
use std::path::PathBuf;
@@ -108,13 +107,6 @@ async fn update_index(args: &Args) -> Result<()> {
108107
Ok(())
109108
}
110109

111-
fn cache_dir() -> &'static OsStr {
112-
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
113-
let cache_dir = Box::new(base.get_cache_home());
114-
let cache_dir = Box::leak(cache_dir);
115-
cache_dir.as_os_str()
116-
}
117-
118110
/// Builds an index for nix-locate
119111
#[derive(Debug, Parser)]
120112
#[clap(author, about, version)]
@@ -124,7 +116,7 @@ struct Args {
124116
jobs: usize,
125117

126118
/// Directory where the index is stored
127-
#[clap(short, long = "db", default_value_os = cache_dir(), env = "NIX_INDEX_DATABASE")]
119+
#[clap(short, long = "db", default_value_os = nix_index::cache_dir(), env = "NIX_INDEX_DATABASE")]
128120
database: PathBuf,
129121

130122
/// Path to nixpkgs for which to build the index, as accepted by nix-env -f

src/bin/nix-locate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tool for searching for files in nixpkgs packages
22
use std::collections::HashSet;
3-
use std::ffi::OsStr;
43
use std::path::PathBuf;
54
use std::process;
65
use std::result;
@@ -240,7 +239,7 @@ struct Opts {
240239
pattern: String,
241240

242241
/// Directory where the index is stored
243-
#[clap(short, long = "db", default_value_os = cache_dir(), env = "NIX_INDEX_DATABASE")]
242+
#[clap(short, long = "db", default_value_os = nix_index::cache_dir(), env = "NIX_INDEX_DATABASE")]
244243
database: PathBuf,
245244

246245
/// Treat PATTERN as regex instead of literal text. Also applies to NAME.

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ pub mod package;
2020
pub mod util;
2121
pub mod workset;
2222

23+
pub fn cache_dir() -> &'static std::ffi::OsStr {
24+
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap();
25+
let cache_dir = Box::new(base.get_cache_home());
26+
let cache_dir = Box::leak(cache_dir);
27+
cache_dir.as_os_str()
28+
}
29+
2330
/// The URL of the binary cache that we use to fetch file listings and references.
2431
///
2532
/// Hardcoded for now, but may be made a configurable option in the future.

0 commit comments

Comments
 (0)