From 29967fcf2cd937b318598117b9e45e2c407f2fc3 Mon Sep 17 00:00:00 2001 From: TrudeEH Date: Mon, 25 Mar 2024 13:19:08 +0000 Subject: [PATCH] Squashed commit of the following: commit baadd91ee2a879dee1f25380102b3a2771d001f3 Author: TrudeEH Date: Mon Mar 25 13:18:48 2024 +0000 Finished preparations to merge commit 4789d301aef540216b900141372bf6b925e7931e Author: TrudeEH Date: Mon Mar 25 13:03:14 2024 +0000 Update bashrc commit 465d5c43ea87928dcf1cb77efb2131cb7ddb4133 Author: TrudeEH Date: Mon Mar 25 12:56:15 2024 +0000 Fix bashrc on debian commit 193cd30089c2102909d3b8d22e335845455d9934 Author: TrudeEH Date: Mon Mar 25 12:52:25 2024 +0000 Attempt to reduce linking steps commit 39589aaeb92c6bdb87fc38fc5a6c9adf320630a4 Author: TrudeEH Date: Mon Mar 25 12:51:28 2024 +0000 Added --adopt flag to stow commit 6d575fb675408eecd721e82433ffd0f3391629b8 Author: TrudeEH Date: Mon Mar 25 12:49:28 2024 +0000 Try to force stow to override existing symlinks commit b646179a0e50f71057ad4c2f87b6cadd35be1eb1 Author: TrudeEH Date: Mon Mar 25 12:45:26 2024 +0000 Fix grep commit 614167978708a24e9e738fe2bdb3b9c5aff35c67 Author: TrudeEH Date: Mon Mar 25 12:42:44 2024 +0000 Fix last commit commit e460693bf708e9a3ad882da143daf7f621ae7b96 Author: TrudeEH Date: Mon Mar 25 12:41:33 2024 +0000 Test label fix commit a93ecf4bc09c3f6f6e2a0381eb83528c84e991b5 Author: TrudeEH Date: Mon Mar 25 12:40:09 2024 +0000 Update grep to separate "-" from normal package names commit e556aaf8d9acb54f05866da33223eb7bc03c8248 Author: TrudeEH Date: Mon Mar 25 12:36:46 2024 +0000 revert to last commit commit f2658d974dc0c738e4e2b62df900b0dcc286dda7 Author: TrudeEH Date: Mon Mar 25 12:34:01 2024 +0000 Try to fix incorrect search command commit 8f099335f510e31ae44a1d720c68f12b4c68aab6 Author: TrudeEH Date: Mon Mar 25 12:32:24 2024 +0000 Fix APT search output commit 0f0acbf13e4e82b59f5e4975a99c83b0636d7c7a Author: TrudeEH Date: Mon Mar 25 12:28:57 2024 +0000 fix apt search package command commit ef66be61203321c22d4968c0f0002559db1a72a4 Author: TrudeEH Date: Mon Mar 25 12:25:31 2024 +0000 Fix remove bug commit 627fdc72887d521b7a7f0b24ceb84a23bf787f5e Author: TrudeEH Date: Mon Mar 25 12:21:36 2024 +0000 Add warning if app isn't installed. commit b341caa088cce0134360757014cc8e06df206868 Author: TrudeEH Date: Mon Mar 25 12:20:05 2024 +0000 fix check input commit 399163f9b8cf62fe6c211e98733524fcb041bb29 Author: TrudeEH Date: Mon Mar 25 12:19:36 2024 +0000 debug print commit 2cb7904fe3a2ae5350a60b2f0f16975e397ed497 Author: TrudeEH Date: Mon Mar 25 12:14:09 2024 +0000 Fix string commit d83e4f95595683cdc8166162af14d160b30fd79d Author: TrudeEH Date: Mon Mar 25 12:13:11 2024 +0000 Fix check input commit 992343066539ab2b5780c5fa6e73ba3a68f0e8f2 Author: TrudeEH Date: Mon Mar 25 12:11:38 2024 +0000 debug print 2 commit bc0cf43a28c5a701bd90158eb10efcc1b1cae611 Author: TrudeEH Date: Mon Mar 25 12:10:28 2024 +0000 debug print commit 8fa90f9b602d5e18a4632022b6a2a131f2131150 Author: TrudeEH Date: Mon Mar 25 12:09:27 2024 +0000 style fix commit 3358c726a93d808f5ec53f321302ce6525a2111f Author: TrudeEH Date: Mon Mar 25 12:07:09 2024 +0000 fix nix return again commit e3db3959aff42722bc0e3076b92e5a83d40a52ce Author: TrudeEH Date: Mon Mar 25 12:05:06 2024 +0000 tr with nix for newline commit 165c3e8c86581b3ecc5c4b7f801d4a3570c394a1 Author: TrudeEH Date: Mon Mar 25 12:03:12 2024 +0000 test nix return fix commit 16b6825362a2e456ea8758fbe0a20e4763a58562 Author: TrudeEH Date: Mon Mar 25 12:02:25 2024 +0000 test nix return commit a108b47ac189bb4ddd4891c80bbb5044da4501de Author: TrudeEH Date: Mon Mar 25 12:01:35 2024 +0000 Fix wrong apt-get usage commit bcc3b9f32cead6fa439a057a995086943dfe9e36 Author: TrudeEH Date: Mon Mar 25 11:55:41 2024 +0000 Switch apt to apt-get commit eb5b272055e000a60e91042529e7c976c84be274 Author: TrudeEH Date: Mon Mar 25 11:53:24 2024 +0000 Fix return codes commit 07e424e3de6f115aefa50778d0ffdf89e13c2fe8 Author: TrudeEH Date: Mon Mar 25 11:47:14 2024 +0000 Removed unnecessary checks commit b6e08e978a6ce420e8f7bb85087aef30a151aede Author: TrudeEH Date: Mon Mar 25 11:41:47 2024 +0000 Attempt fix, again commit 5e95a13f75fa234da0a330f0a878fb2f4eec8b39 Author: TrudeEH Date: Mon Mar 25 11:37:11 2024 +0000 Fix last commit commit 2babffa9e124928e9e46395d9199138e1800c265 Author: TrudeEH Date: Mon Mar 25 11:35:32 2024 +0000 Check if program is installed before installing it. commit 938fd9fe5ea39c4e55944c0488f33437de06e7b7 Author: TrudeEH Date: Mon Mar 25 11:26:04 2024 +0000 Dotfiles rewrite (using stew) Full rewrite; Still in testing --- BrewFile | 94 --------- Linux/scripts/arch-maintenance.sh | 31 +++ Linux/scripts/debian-maintenance.sh | 20 ++ README.md | 73 +------ home/.bashrc | 45 ++++ home/.config/nixpkgs/config.nix | 3 + home/.zshrc | 46 +--- install.sh | 39 +--- darwinSettings.sh => macOS/darwinSettings.sh | 0 {scripts => macOS/scripts}/brewBundle.sh | 0 .../scripts}/macos-add-small-spacer.sh | 0 .../scripts}/macos-add-spacer.sh | 0 macOS/scripts/macos-maintenance.sh | 8 + {scripts => macOS/scripts}/moveAppsToBrew.sh | 0 scripts/color.sh | 2 - scripts/p.sh | 199 ++++++++++++++++++ 16 files changed, 320 insertions(+), 240 deletions(-) delete mode 100644 BrewFile create mode 100755 Linux/scripts/arch-maintenance.sh create mode 100755 Linux/scripts/debian-maintenance.sh create mode 100644 home/.bashrc create mode 100644 home/.config/nixpkgs/config.nix rename darwinSettings.sh => macOS/darwinSettings.sh (100%) rename {scripts => macOS/scripts}/brewBundle.sh (100%) rename {scripts => macOS/scripts}/macos-add-small-spacer.sh (100%) rename {scripts => macOS/scripts}/macos-add-spacer.sh (100%) create mode 100755 macOS/scripts/macos-maintenance.sh rename {scripts => macOS/scripts}/moveAppsToBrew.sh (100%) create mode 100755 scripts/p.sh diff --git a/BrewFile b/BrewFile deleted file mode 100644 index 9f5fe337..00000000 --- a/BrewFile +++ /dev/null @@ -1,94 +0,0 @@ -tap "homebrew/bundle" -tap "homebrew/cask" -tap "homebrew/cask-fonts" -tap "homebrew/services" -# Play, record, convert, and stream audio and video -brew "ffmpeg" -# Quickly generate a temporary email address -brew "mailsy" -# Create, run, and share large language models (LLMs) -brew "ollama", restart_service: true -# Safe, concurrent, practical language -brew "rust" -# Internet file retriever -brew "wget" -# Allows connection to a computer remotely -cask "anydesk" -# Reclaim tens of gigabytes of your storage used for various Xcode caches -cask "devcleaner" -# Matrix collaboration client -cask "element" -# Web browser -cask "firefox" -# Developer targeted fonts with a high number of glyphs -cask "font-jetbrains-mono-nerd-font" -# Menu bar application that displays the CPU speed limit due to thermal issues -cask "hot" -# Modpack for Minecraft 1.7.10 and 1.8.9 -cask "lunar-client" -# Free and open-source RSS reader -cask "netnewswire" -# Knowledge base that works on top of a local folder of plain text Markdown files -cask "obsidian" -# Control your tools with a few keystrokes -cask "raycast" -# Tool that provides consistent, highly configurable symbols for apps -cask "sf-symbols" -# Instant messaging application focusing on security -cask "signal" -# Video game digital distribution service -cask "steam" -# Open-source media center -cask "stremio" -# Mesh VPN based on Wireguard -cask "tailscale" -# Open-source BitTorrent client -cask "transmission" -# Virtual machines UI using QEMU -cask "utm" -# Open-source code editor -cask "visual-studio-code" -# Wine wrapper built with SwiftUI -cask "whisky" -vscode "10nates.ollama-autocoder" -vscode "adpyke.codesnap" -vscode "bbenoist.nix" -vscode "christian-kohler.path-intellisense" -vscode "donjayamanne.githistory" -vscode "dustypomerleau.rust-syntax" -vscode "dzhavat.bracket-pair-toggler" -vscode "ecmel.vscode-html-css" -vscode "esbenp.prettier-vscode" -vscode "formulahendry.code-runner" -vscode "foxundermoon.shell-format" -vscode "grapecity.gc-excelviewer" -vscode "gruntfuggly.todo-tree" -vscode "hars.cppsnippets" -vscode "kevinrose.vsc-python-indent" -vscode "kisstkondoros.vscode-gutter-preview" -vscode "mcu-debug.debug-tracker-vscode" -vscode "ms-python.black-formatter" -vscode "ms-python.debugpy" -vscode "ms-python.python" -vscode "ms-python.vscode-pylance" -vscode "ms-toolsai.jupyter" -vscode "ms-toolsai.jupyter-keymap" -vscode "ms-toolsai.jupyter-renderers" -vscode "ms-vscode.cpptools" -vscode "ms-vscode.cpptools-extension-pack" -vscode "ms-vsliveshare.vsliveshare" -vscode "njpwerner.autodocstring" -vscode "oderwat.indent-rainbow" -vscode "pkief.material-icon-theme" -vscode "redhat.vscode-yaml" -vscode "ritwickdey.liveserver" -vscode "rust-lang.rust-analyzer" -vscode "serayuzgur.crates" -vscode "simonsiefke.svg-preview" -vscode "sswg.swift-lang" -vscode "streetsidesoftware.code-spell-checker" -vscode "streetsidesoftware.code-spell-checker-portuguese" -vscode "vadimcn.vscode-lldb" -vscode "visualstudioexptteam.intellicode-api-usage-examples" -vscode "visualstudioexptteam.vscodeintellicode" -vscode "wix.vscode-import-cost" diff --git a/Linux/scripts/arch-maintenance.sh b/Linux/scripts/arch-maintenance.sh new file mode 100755 index 00000000..52af15d5 --- /dev/null +++ b/Linux/scripts/arch-maintenance.sh @@ -0,0 +1,31 @@ +#! /bin/bash + +source ~/dotfiles/scripts/p.sh +source ~/dotfiles/scripts/color.sh + +echo -e "${GREEN}[+] Updating...${ENDCOLOR}" +sudo paru -Syu + +echo -e "${GREEN}[+] Cleaning orphaned (unneeded) packages...${ENDCOLOR}" +sudo paru -Rsn $(paru -Qdtq) + +echo -e "${GREEN}[+] Cleaning old pacman cache...${ENDCOLOR}" +if p c pacman-contrib &>/dev/null; then + paccache -rk1 +else + p i pacman-contrib + paccache -rk1 +fi + +echo -e "${GREEN}[+] Removing logs older than 7d...${ENDCOLOR}" +sudo journalctl --vacuum-time=7d + +if p c flatpak &>/dev/null; then + echo -e "${GREEN}[+] Cleaning flatpak...:${ENDCOLOR}" + flatpak remove --unused +fi + +echo -e "${GREEN}[i] AUR Packages installed:${ENDCOLOR}" +pacman -Qim | awk '/^Name/{name=$3} /^Installed Size/{print $4$5, name}' | sort -h + +# Installed packages: pacman -Qen diff --git a/Linux/scripts/debian-maintenance.sh b/Linux/scripts/debian-maintenance.sh new file mode 100755 index 00000000..032d6101 --- /dev/null +++ b/Linux/scripts/debian-maintenance.sh @@ -0,0 +1,20 @@ +#! /bin/bash + +source ~/dotfiles/scripts/color.sh +source ~/dotfiles/scripts/p.sh + +echo -e "${GREEN}[+] Updating...${ENDCOLOR}" +sudo apt update +sudo apt upgrade +sudo apt dist-upgrade +echo -e "${GREEN}[+] Cleaning...${ENDCOLOR}" +sudo apt autoremove +sudo apt autoclean + +echo -e "${GREEN}[+] Removing logs older than 7d...${ENDCOLOR}" +sudo journalctl --vacuum-time=7d + +if p c flatpak &>/dev/null; then + echo -e "${GREEN}[+] Cleaning flatpak...:${ENDCOLOR}" + flatpak remove --unused +fi diff --git a/README.md b/README.md index 51955ca3..f5a5f75f 100644 --- a/README.md +++ b/README.md @@ -3,75 +3,8 @@ **Run `./install.sh`.** ## Dependencies -- None (HomeBrew will be installed when `p` is called for the first time.) +- Nix (installed automatically) -## Scripts +## Custom commands -### Custom commands - -- `p` Wrapper for Brew. Acts as a shortcut automate parts of brew. - -### Custom scripts -My collection of scripts is available in the scripts folder. You may find something useful there. - -## Other (useful) MacOS Commands (built-in) - -**Password-protected Zip:** -```sh -zip -e protected.zip /file/to/protect/ -``` - -**Remove dotfiles:** -```sh -dot_clean . -``` - -**Show all files in finder:** -```sh -defaults write com.apple.Finder AppleShowAllFiles 1 -``` - -**Search using Spotlight:** -```sh -mdfind "file name” -``` - -**Rebuild Spotlight index:** -```sh -mdutil -E -``` - -**Turn off Spotlight indexing:** -```sh -dutil -i off -``` - -**Repair Disk permissions:** -```sh -sudo /usr/libexec/repair_packages --repair --standard-pkgs --volume / -``` - -**Generate SHA-1 digest of a file:** -```sh -/usr/bin/openssl sha1 download.dmg -``` - -**Disable sleep temporarily:** -```sh -caffeinate -``` - -**Open multiple instances of a program:** -```sh -open multiple instances open -n /Applications/Safari.app/ -``` - -**Check network speed:** -```sh -networkQuality -``` - -**Convert files (txt, html, rtf, rtfd, doc, docx):** -```sh -textutil -convert html journal.doc -``` +- `p` "Universal" package manager written in bash \ No newline at end of file diff --git a/home/.bashrc b/home/.bashrc new file mode 100644 index 00000000..65596ebf --- /dev/null +++ b/home/.bashrc @@ -0,0 +1,45 @@ +# +# ~/.bashrc +# + +source ~/dotfiles/scripts/p.sh + +alias ls='ls --color=auto' +alias grep='grep --color=auto' +PS1='\n[\[\e[37m\]\u\[\e[0m\]@\[\e[37;2m\]\h\[\e[0m\]] \[\e[1m\]\w \[\e[0;2m\]J:\[\e[0m\]\j\n\$ ' + +bind -s 'set completion-ignore-case on' + +compress() { + FILE=$1 + shift + case $FILE in + *.tar.bz2) tar cjf $FILE $* ;; + *.tar.gz) tar czf $FILE $* ;; + *.tgz) tar czf $FILE $* ;; + *.zip) zip $FILE $* ;; + *.rar) rar $FILE $* ;; + *) echo "Filetype not recognized" ;; + esac +} + +extract() { + if [ -f $1 ]; then + case $1 in + *.tar.bz2) tar xjf $1 ;; + *.tar.gz) tar xzf $1 ;; + *.bz2) bunzip2 $1 ;; + *.rar) unrar e $1 ;; + *.gz) gunzip $1 ;; + *.tar) tar xf $1 ;; + *.tbz2) tar xjf $1 ;; + *.tgz) tar xzf $1 ;; + *.zip) unzip $1 ;; + *.Z) uncompress $1 ;; + *.7z) 7z x $1 ;; + *) echo "'$1' cannot be extracted via extract()" ;; + esac + else + echo "'$1' is not a valid file" + fi +} diff --git a/home/.config/nixpkgs/config.nix b/home/.config/nixpkgs/config.nix new file mode 100644 index 00000000..afff6faf --- /dev/null +++ b/home/.config/nixpkgs/config.nix @@ -0,0 +1,3 @@ +{ + allowUnfree = true; +} \ No newline at end of file diff --git a/home/.zshrc b/home/.zshrc index c00f7630..cd7570a9 100644 --- a/home/.zshrc +++ b/home/.zshrc @@ -8,51 +8,7 @@ fi # Open VSCode from the terminal code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;} -# Shortcuts for brew and macOS update -p () { - if ! command -v brew &> /dev/null; then - echo "Brew not found. Installing..." - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') > ~/.zprofile - eval "$(/opt/homebrew/bin/brew shellenv)" - fi - - case $1 in - install|i) - shift - brew install $* - ;; - remove|r|) - shift - brew uninstall $* - ;; - update|u) - brew update - brew upgrade - brew autoremove - brew cleanup - ;; - list|l) - brew list - ;; - bundle|b) - brew bundle --file $2 - ;; - macOSupdate|m) - echo "Updating MacOS..." - echo "THE DEVICE WILL RESTART IF NECESSARY." - sudo softwareupdate -iaR - ;; - *) - echo "Usage: p {(i)nstall|(r)emove|(u)pdate|(l)ist|(b)undle|(m)acOSupdate} " - ;; - esac - - # Generate a new bundle in the dotfiles. - echo "Updating bundle in $HOME/dotfiles..." - rm -f $HOME/dotfiles/BrewFile - brew bundle dump --describe --file=$HOME/dotfiles/BrewFile -} +source $HOME/dotfiles/scripts/p.sh # Shortcuts to extract files extract () { diff --git a/install.sh b/install.sh index cf99270f..ae29a402 100755 --- a/install.sh +++ b/install.sh @@ -1,32 +1,13 @@ -#! /bin/zsh +#! /bin/bash -source ./scripts/color.sh -source ./scripts/symlink-files.sh -source ./home/.zshrc +source scripts/p.sh +source scripts/color.sh -# Link configs +# ============== CONFIG ============== + +# Install Dependency: stow +p i stow + +# Link dotfile home to $HOME echo -e "${GREEN}[+] Symlinking dotfiles...${ENDCOLOR}" -symlink_files "./home" $HOME - -echo -n "Run MacOS config script? This will overwrite the system dock. (y/n): " -read runConfig -if [[ $runConfig == 'y' ]]; then - echo -e "${GREEN}[+] Configuring MacOS...${ENDCOLOR}" - bash ./darwinSettings.sh -fi - -# Restore Brew Installs -echo -e "${GREEN}[+] Restoring Brew from BrewFile...${ENDCOLOR}" -p b BrewFile - -# Install Oh-My-ZSH -echo -e "${GREEN}[+] Installing Oh-My-ZSH...${ENDCOLOR}" -sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" -git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k - -# Update MacOS -echo -e "${GREEN}[+] Updating MacOS...${ENDCOLOR}" -echo -e "${GREEN}[I] THE DEVICE WILL RESTART IF NECESSARY.${ENDCOLOR}" -sudo softwareupdate -iaR - -echo -e "${GREEN}${BOLD}[i] All done.${ENDCOLOR}" +stow -v -t $HOME home --adopt diff --git a/darwinSettings.sh b/macOS/darwinSettings.sh similarity index 100% rename from darwinSettings.sh rename to macOS/darwinSettings.sh diff --git a/scripts/brewBundle.sh b/macOS/scripts/brewBundle.sh similarity index 100% rename from scripts/brewBundle.sh rename to macOS/scripts/brewBundle.sh diff --git a/scripts/macos-add-small-spacer.sh b/macOS/scripts/macos-add-small-spacer.sh similarity index 100% rename from scripts/macos-add-small-spacer.sh rename to macOS/scripts/macos-add-small-spacer.sh diff --git a/scripts/macos-add-spacer.sh b/macOS/scripts/macos-add-spacer.sh similarity index 100% rename from scripts/macos-add-spacer.sh rename to macOS/scripts/macos-add-spacer.sh diff --git a/macOS/scripts/macos-maintenance.sh b/macOS/scripts/macos-maintenance.sh new file mode 100755 index 00000000..ce3e6d29 --- /dev/null +++ b/macOS/scripts/macos-maintenance.sh @@ -0,0 +1,8 @@ +brew update +brew upgrade +brew autoremove +brew cleanup +echo "Updating MacOS..." + +echo "THE DEVICE WILL RESTART IF NECESSARY." +sudo softwareupdate -iaR diff --git a/scripts/moveAppsToBrew.sh b/macOS/scripts/moveAppsToBrew.sh similarity index 100% rename from scripts/moveAppsToBrew.sh rename to macOS/scripts/moveAppsToBrew.sh diff --git a/scripts/color.sh b/scripts/color.sh index d6e9f9bf..6c3e36cf 100755 --- a/scripts/color.sh +++ b/scripts/color.sh @@ -1,5 +1,3 @@ -#! /bin/bash - RED="\e[31m" GREEN="\e[32m" YELLOW="\e[33m" diff --git a/scripts/p.sh b/scripts/p.sh new file mode 100755 index 00000000..d077c702 --- /dev/null +++ b/scripts/p.sh @@ -0,0 +1,199 @@ +#! /bin/bash +# Cross-distro package manager wrapper + +source ~/dotfiles/scripts/color.sh + +detectDistro() { + if [ "$(uname -s)" = "Darwin" ]; then + echo "macOS" + else + if [ "$(grep -Ei 'debian|buntu|mint' /etc/*release)" ]; then + echo "Debian" + elif [ "$(grep -Ei 'arch|manjaro|artix' /etc/*release)" ]; then + echo "Arch" + else + echo 1 + fi + fi +} + +oneline() { + # Print a command's output as a single line only. + # Example usage: for f in 'first line' 'second line' '3rd line'; do echo "$f"; sleep 1; done | oneline + local ws + while IFS= read -r line; do + if ((${#line} >= $COLUMNS)); then + # Moving cursor back to the front of the line so user input doesn't force wrapping + printf '\r%s\r' "${line:0:$COLUMNS}" + else + ws=$(($COLUMNS - ${#line})) + # by writing each line twice, we move the cursor back to position + # thus: LF, content, whitespace, LF, content + printf '\r%s%*s\r%s' "$line" "$ws" " " "$line" + fi + done + echo +} + +p() ( + distro=$(detectDistro) + update() { + if [[ $distro == "Arch" ]]; then + ~/dotfiles/Linux/scripts/arch-maintenance.sh + elif [[ $distro == "Debian" ]]; then + ~/dotfiles/Linux/scripts/debian-maintenance.sh + elif [[ $distro == "macOS" ]]; then + ~/dotfiles/macOS/scripts/macos-maintenance.sh + else + echo -e "${RED}[E] System not supported.${ENDCOLOR}" + return 1 + fi + } + + check() { + nix_apps=$(nix-env -q) + if [[ $distro == "Debian" ]]; then + distro_apps=$(dpkg-query -l | grep '^ii' | awk '{print $2}') + elif [[ $distro == "Arch" ]]; then + distro_apps=$(pacman -Q) + else + distro_apps="" + fi + + app_name=$(echo "$1" | tr '[:upper:]' '[:lower:]') + app_name=$(echo "$app_name" | tr " " -) + + echo $nix_apps | grep -wq $app_name + nix_success=$? + if [[ $nix_success == 0 ]]; then + echo -e "${GREEN}Nix: $(echo $nix_apps | tr ' ' '\n' | grep -w $app_name)${ENDCOLOR}" + fi + + echo $distro_apps | grep -Eq "(^|\s)$app_name($|\s)" + distro_success=$? + if [[ $distro_success == 0 ]]; then + echo -e "${GREEN}$distro: $(echo $distro_apps | tr ' ' '\n' | grep -E "(^|\s)$app_name($|\s)")${ENDCOLOR}" + fi + + if [[ $nix_success == 0 && $distro_success == 0 ]]; then + return 2 #both + elif [[ $nix_success == 0 ]]; then + return 3 #nix + elif [[ $distro_success == 0 ]]; then + return 4 #distro + else + echo -e "${YELLOW}$app_name not installed.${ENDCOLOR}" + return 1 + fi + + # Get a list of all apps -> see if it's installed and print what installed it, and version. + } + + if [[ $distro == "Debian" ]]; then + install="sudo apt-get install" + remove="sudo apt-get remove" + search="apt-cache search" + elif [[ $distro == "Arch" ]]; then + if pacman -Qs paru >/dev/null; then + install="paru -S" + remove="paru -R" + search="paru -Si" + else + install="sudo pacman -S" + remove="sudo pacman -R" + search="pacman -Ss" + fi + fi + + # Install Nix. + if ! nix --version &>/dev/null; then + echo -e "${YELLOW}[E] Nix not found.${ENDCOLOR}" + echo -e "${GREEN}[+] Installing the Nix package manager...${ENDCOLOR}" + curl -L https://nixos.org/nix/install | sh + echo -e "${GREEN}[I] Installed Nix. Please reboot your device.${ENDCOLOR}" + return 2 + fi + + # If no parameter + if [ -z $1 ] || [ $1 = "u" ]; then + echo -e "${GREEN}[+] Running update script...${ENDCOLOR}" + update + echo -e "${GREEN}[+] Updating Nix...${ENDCOLOR}" + nix-env -u + return 0 + elif [ $1 = "i" ]; then # If first parameter is i (install) + check $2 + if [[ $? != 1 ]]; then + echo "$2 is already installed." + return 1 + fi + + nix search nixpkgs $2 --extra-experimental-features 'nix-command flakes' &>/dev/null + nix_found=$? + + $search $2 &>/dev/null + distro_found=$? + + if [[ $nix_found == 0 && $distro_found == 0 ]]; then + echo "1. Nix: $2 found." + echo "2. $distro: $2 found." + + echo -n "Choose a package manager: " + read p_choice + if [[ $p_choice == 1 ]]; then + nix-env -iA nixpkgs.$2 + elif [[ $p_choice == 2 ]]; then + $install $2 + else + return 1 + fi + return 0 + fi + if [[ $nix_found == 0 ]]; then + nix-env -iA nixpkgs.$2 + elif [[ $distro_found == 0 ]]; then + $install $2 + else + echo "$2 not found." + return 1 + fi + return 0 + elif [ $1 = "r" ]; then # If first parameter is r (remove) + check $2 + check_result=$? + + if [[ $check_result == 2 ]]; then + echo "1. Nix: $2 installed." + echo "2. $distro: $2 installed." + + echo -n "Choose a package manager: " + read p_choice + if [[ $p_choice == 1 ]]; then + nix-env -e $2 + elif [[ $p_choice == 2 ]]; then + $remove $2 + else + return 1 + fi + return 0 + fi + if [[ $check_result == 3 ]]; then + nix-env -e $2 + elif [[ $check_result == 4 ]]; then + $remove $2 + else + echo "$2 is not installed." + return 1 + fi + return 0 + elif [ $1 = "c" ]; then # If first parameter is c (check) + check $2 + else + echo -e "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}" + echo -e "p (u) ${FAINT}- update os${ENDCOLOR}" + echo -e "p i package ${FAINT}- install package${ENDCOLOR}" + echo -e "p r package ${FAINT}- remove package${ENDCOLOR}" + echo -e "p c package ${FAINT}- check if package is installed (true/false; 2 -> ERROR)${ENDCOLOR}" + return 1 + fi +)