From ad29e9ebcc9c227d38aaa9a4ce6301fd6d28e93c Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Sun, 19 May 2024 13:22:30 +0200 Subject: [PATCH] nix tooling, cleanup --- flake.lock | 26 ++++++++++++++ flake.nix | 84 +++++++++++++++++++++++++++++++++++++++++++++ nix/package.nix | 17 +++++++++ rust-toolchain.toml | 2 ++ src/args.rs | 13 +++---- src/main.rs | 82 +++++++++++++++++++++++-------------------- 6 files changed, 179 insertions(+), 45 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/package.nix create mode 100644 rust-toolchain.toml diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..eada13c --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..e45b99c --- /dev/null +++ b/flake.nix @@ -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; + } + ); + }; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..b57b121 --- /dev/null +++ b/nix/package.nix @@ -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; + }; +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..292fe49 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "stable" diff --git a/src/args.rs b/src/args.rs index 0585278..3692f0f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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, - + /// 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, } diff --git a/src/main.rs b/src/main.rs index 27a4a79..bd17e6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = env::args().collect(); - let xdg_data_dirs = std::env::var("XDG_DATA_DIRS").unwrap(); - let data_dirs = xdg_data_dirs.split(":"); - + let xdg_data_dirs = std::env::var("XDG_DATA_DIRS").unwrap(); + 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 - } - - 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 - } - } - } + 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 = 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); } }