nix tooling, cleanup

This commit is contained in:
Grimmauld 2024-05-19 13:22:30 +02:00
parent 908a487ab0
commit ad29e9ebcc
Signed by: Grimmauld
GPG key ID: C2946668769F91FB
6 changed files with 179 additions and 45 deletions

26
flake.lock Normal file
View file

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1716097317,
"narHash": "sha256-1UMrLtgzielG/Sop6gl6oTSM4pDt7rF9j9VuxhDWDlY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8535fb92661f37ff9f0da3007fbc942f7d134b41",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixpkgs-unstable",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

84
flake.nix Normal file
View file

@ -0,0 +1,84 @@
{
description = "confwhich: easily obtain info about XDG_CONFIG useage";
inputs.nixpkgs.url = "nixpkgs/nixpkgs-unstable";
outputs =
{ nixpkgs, ... }:
let
systems = [
"x86_64-linux"
"aarch64-linux"
];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
in
{
devShells = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml));
libPath =
with pkgs;
lib.makeLibraryPath [
];
in
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
llvmPackages.bintools
rustup
];
RUSTC_VERSION = overrides.toolchain.channel;
# https://github.com/rust-lang/rust-bindgen#environment-variables
LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
'';
# Add precompiled library to rustc search path
RUSTFLAGS = (
builtins.map (a: ''-L ${a}/lib'') [
# add libraries here (e.g. pkgs.libvmi)
]
);
LD_LIBRARY_PATH = libPath;
# Add glibc, clang, glib, and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
# Includes normal include path
(builtins.map (a: ''-I"${a}/include"'') [
# add dev libraries here (e.g. pkgs.libvmi.dev)
pkgs.glibc.dev
])
# Includes with special directory paths
++ [
''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
''-I"${pkgs.glib.dev}/include/glib-2.0"''
''-I${pkgs.glib.out}/lib/glib-2.0/include/''
];
packages = with pkgs; [
# base toolchain
pkg-config
rustup
nil
];
};
}
);
packages = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
deskwhich-pkg = (pkgs.callPackage ./nix/package.nix { });
in
{
deskwhich = deskwhich-pkg;
default = deskwhich-pkg;
}
);
};
}

17
nix/package.nix Normal file
View file

@ -0,0 +1,17 @@
{ lib, rustPlatform }:
rustPlatform.buildRustPackage {
pname = "deskwhich";
version = "unstable-2024-05-19";
src = lib.cleanSource ../.;
cargoHash = "sha256-l7xlDrAvPtf+747DWRLu2d/O0Thvj6gKtoMksGmj02o=";
meta = {
description = "tool to find the path of desktop entries";
homepage = "https://codeberg.org/axtlos/deskwhich";
license = lib.licenses.gpl3Only;
mainProgram = "deskwhich";
maintainers = with lib.maintainers; [ grimmauld ];
platforms = lib.platforms.linux;
};
}

2
rust-toolchain.toml Normal file
View file

@ -0,0 +1,2 @@
[toolchain]
channel = "stable"

View file

@ -3,29 +3,26 @@
use clap::Parser;
use clap_stdin::MaybeStdin;
#[derive(Debug, Parser)]
#[clap(name="deskwhich", version=env!("CARGO_PKG_VERSION"),about=env!("CARGO_PKG_DESCRIPTION"), author=env!("CARGO_PKG_AUTHORS"))]
pub struct Cli {
/// The desktop file to search for
pub search: MaybeStdin<String>,
/// Skip directories in XDG_DATA_DIRS that start with a dot
#[arg(long, default_value_t=false)]
#[arg(long, default_value_t = false)]
pub skip_dot: bool,
/// Skip directories in XDG_DATA_DIRS that start with the home directory
#[arg(long, default_value_t=false)]
#[arg(long, default_value_t = false)]
pub skip_home: bool,
/// Print all matches in XDG_DATA_DIRS, not just the first
#[arg(short, long, default_value_t=false)]
#[arg(short, long, default_value_t = false)]
pub all: bool,
//show_dot: bool,
/// Output a tilde for HOME directory
#[arg(long, default_value_t=false)]
#[arg(long, default_value_t = false)]
pub show_tilde: bool,
}

View file

@ -1,53 +1,61 @@
// SPDX-License-Identifier: GPL-3.0-only
mod args;
use std::fs;
use std::env;
use std::process::exit;
use clap::Parser;
use crate::args::Cli;
use clap::Parser;
use std::env;
use std::fs;
use std::process::exit;
fn main() {
let cli = Cli::parse();
let args: Vec<String> = env::args().collect();
let xdg_data_dirs = std::env::var("XDG_DATA_DIRS").unwrap();
let data_dirs = xdg_data_dirs.split(":");
let data_dirs = xdg_data_dirs.split(':');
let mut found = false;
let home = match env::var("HOME") {
Ok(var) => var,
Err(_) => exit(1),
Ok(var) => var,
Err(_) => exit(1),
};
for dirs in data_dirs {
if (dirs.starts_with(&home) && cli.skip_home) || (dirs.starts_with(".") && cli.skip_dot) {
continue
}
if (dirs.starts_with(&home) && cli.skip_home) || (dirs.starts_with('.') && cli.skip_dot) {
continue;
}
let files = match fs::read_dir(format!("{}/applications",dirs)) {
Ok(file) => file,
Err(_) => continue,
};
for file in files {
let desktop_file = format!("{}", file.as_ref().unwrap().file_name().into_string().unwrap());
let mut name = desktop_file.split(".");
if name.find(|x| *x == cli.search.strip_suffix(".desktop").unwrap_or(&cli.search)).is_some() {
let path = file.as_ref().unwrap().path();
let out = if cli.show_tilde {
format!("{}", path.display().to_string().replace(&home, "~"))
} else {
path.display().to_string()
};
println!("{}", out);
found = true;
if !cli.all {
return
}
}
}
let files = match fs::read_dir(format!("{}/applications", dirs)) {
Ok(file) => file,
Err(_) => continue,
};
for file in files {
let desktop_file = file
.as_ref()
.unwrap()
.file_name()
.into_string()
.unwrap()
.to_string();
let mut name = desktop_file.split('.');
if name
.any(|x| x == cli.search.strip_suffix(".desktop").unwrap_or(&cli.search))
{
let path = file.as_ref().unwrap().path();
let out = if cli.show_tilde {
path.display().to_string().replace(&home, "~").to_string()
} else {
path.display().to_string()
};
println!("{out}");
found = true;
if !cli.all {
return;
}
}
}
}
if !found {
eprintln!("No {} in ({})", args[1], xdg_data_dirs);
exit(1);
eprintln!("No {} in ({})", args[1], xdg_data_dirs);
exit(1);
}
}