nushell-config/config.nu

257 lines
6.2 KiB
Plaintext
Raw Normal View History

2024-02-19 05:52:08 +01:00
# Nushell Config File
# Command existence checker function
def 'is-installed' [ app: string ] {
((which $app | length) > 0)
}
2024-04-01 23:37:50 +02:00
# Global vars and consts
let hostn = (sys | get host.hostname)
const MODDIR = ($nu.default-config-dir | path join "modules")
2024-03-17 19:36:23 +01:00
#
# MODULES
#
# weather
use ~/.config/nushell/modules/get-weather.nu get_weather
# ultimate-extractor
use ~/.config/nushell/modules/ultimate-extractor.nu extract
# ssh
use ~/.config/nushell/modules/ssh.nu
2024-04-01 23:37:50 +02:00
# Source distro-specific modules
const DEBIAN_CONF = ($MODDIR | path join "debian.nu")
const REDHAT_CONF = ($MODDIR | path join "redhat.nu")
const DISTRO_CONFIG = if ("/etc/debian_version" | path exists) {
$DEBIAN_CONF
} else if ("/etc/redhat-release" | path exists) {
$REDHAT_CONF
} else {
$nu.env-path
}
source $DISTRO_CONFIG
2024-03-17 19:36:23 +01:00
#
# ALIASES / CUSTOM COMMANDS
#
2024-04-13 03:35:15 +02:00
# Copy nushell config to git repo
def cpnurepo [] {
let repo = ($nu.home-path | path join "repos/codeberg.org/hyperreal/nushell-config")
cp -v $nu.config-path $repo
cp -v $nu.env-path $repo
cp -rfv $MODDIR $repo
echo "Nushell config copied to local repo"
}
2024-04-01 23:37:50 +02:00
# Custom commands for Doom Emacs
def doomdoc [] { ~/.config/emacs/bin/doom doctor }
def dsync [] { ~/.config/emacs/bin/doom sync }
def dclean [] { ~/.config/emacs/bin/doom clean }
def dcomp [] { ~/.config/emacs/bin/doom compile }
def dpurge [] { ~/.config/emacs/bin/doom purge }
def denv [] { ~/.config/emacs/bin/doom env }
def dupgrade [] { ~/.config/emacs/bin/doom upgrade }
2024-03-17 19:36:23 +01:00
# weather
2024-04-01 23:37:50 +02:00
export alias weath = get_weather
# firewalld aliases
export alias fw = sudo firewall-cmd
export alias fwp = sudo firewall-cmd --permanent
export alias fwr = sudo firewall-cmd --reload
export alias fwrp = sudo firewall-cmd --runtime-to-permanent
2024-03-17 19:36:23 +01:00
2024-02-19 05:52:08 +01:00
# Git aliases
export alias ga = git add
export alias gcl = git clone
export alias gcmsg = git commit -m
export alias gd = git diff
export alias gl = git pull
export alias gp = git push
export alias gr = git remote
export alias grbi = git rebase -i
export alias grm = git rm
export alias grv = git remote -v
export alias gst = git status
2024-03-17 19:36:23 +01:00
# ls's
export alias la = ls -a
export alias ll = ls -l
export alias lal = ls -al
export alias lse = ^ls -lZ
2024-04-01 23:37:50 +02:00
# Copy trimmed GPG fingerprint to clipboard
def gpgfpr [] {
gpg -K --with-colons
| jc --gpg
| from json
| get 1.user_id
| xclip -sel clipboard
}
2024-02-19 05:52:08 +01:00
# Copy SSH public key to clipboard
2024-04-01 23:37:50 +02:00
def pubkey [] {
open --raw ~/.ssh/id_ed25519.pub
| str trim
| xclip -selection clipboard
}
2024-02-19 05:52:08 +01:00
# Get public ip info
def pubip [] { curl --silent ipinfo.io | from json }
# Get Mullvad info
2024-04-01 23:37:50 +02:00
def amimullvad [] {
curl -sSL https://am.i.mullvad.net/json
| from json
}
2024-02-19 05:52:08 +01:00
# Uptime info
2024-03-17 19:36:23 +01:00
def upt [] { jc uptime | from json }
2024-02-19 05:52:08 +01:00
# df info
2024-03-17 19:36:23 +01:00
def dfj [] { jc df | from json }
2024-02-19 05:52:08 +01:00
# /etc/fstab info
2024-04-01 23:37:50 +02:00
def etc-fstab [] {
open --raw /etc/fstab
| jc --fstab
| from json
}
2024-02-19 05:52:08 +01:00
# memory free info
2024-03-17 19:36:23 +01:00
def memfree [] { jc free | from json }
2024-02-19 05:52:08 +01:00
# /etc/group
2024-04-01 23:37:50 +02:00
def etc-group [] {
open --raw /etc/group
| jc --group
| from json
}
2024-02-19 05:52:08 +01:00
# /etc/passwd
2024-04-01 23:37:50 +02:00
def etc-passwd [] {
open --raw /etc/passwd
| jc --passwd
| from json
}
2024-02-19 05:52:08 +01:00
# Network connections
2024-03-17 19:36:23 +01:00
def netcons [] { jc lsof -i | from json }
2024-02-19 05:52:08 +01:00
# Ports
2024-03-17 19:36:23 +01:00
def tulp [] { jc ss -tulp | from json }
2024-02-19 05:52:08 +01:00
# Open ports
2024-04-01 23:37:50 +02:00
def openports [] {
sudo jc lsof -i
| from json
| find "LISTEN"
}
2024-02-19 05:52:08 +01:00
# List sockets in use
2024-03-17 19:36:23 +01:00
def lsock [] { sudo jc lsof -nP | from json }
2024-02-19 05:52:08 +01:00
# List UDP sockets in use
2024-03-17 19:36:23 +01:00
def lsocku [] { sudo jc lsof -nP | from json | find "UDP" }
2024-02-19 05:52:08 +01:00
# List TCP sockets in use
2024-03-17 19:36:23 +01:00
def lsockt [] { sudo jc lsof -nP | from json | find "TCP" }
2024-02-19 05:52:08 +01:00
# Print timestamp as %FT%T%:z
def tstampz [] { date now | format date "%FT%T%:z" }
# Create new directory and enter it
def --env mkd [path] { mkdir $path; cd $path }
# Display pid info of command
2024-04-13 03:35:15 +02:00
def pids [cmd] { ps | where name == $cmd }
2024-02-19 05:52:08 +01:00
# Get time in specific time zone
2024-04-01 23:37:50 +02:00
def whattimein [string] {
date now
| date to-timezone $string
}
2024-02-19 05:52:08 +01:00
# cd to home and clear screen
def --env rsrc [] { cd $env.HOME; clear }
2024-03-17 19:36:23 +01:00
# extract
def x [filename: path] { extract $filename }
2024-02-19 05:52:08 +01:00
# Convert filename to given case
# Ex. caseify camel hello_world.txt
# => helloWorld.txt
# Based on https://github.com/nushell/nu_scripts/blob/main/sourced/cool-oneliners/file_convert_naming_case.nu
def caseify [case: string, filename: path] {
let ext = (echo $filename | path parse | get extension)
let cur_stem = (echo $filename | path parse | get stem)
let new_name = match $case {
"camel" => (((echo $cur_stem | str camel-case) + "." + $ext) | str join),
"kebab" => (((echo $cur_stem | str kebab-case) + "." + $ext) | str join),
"lower" => (((echo $cur_stem | str downcase) + "." + $ext) | str join),
"pascal" => (((echo $cur_stem | str pascal-case) + "." + $ext) | str join),
"screamsnake" => (((echo $cur_stem | str screaming-snake-case) + "." + $ext) | str join),
"snake" => (((echo $cur_stem | str snake-case) + "." + $ext) | str join),
_ => "Invalid case option"
}
mv $filename $new_name
}
2024-03-17 19:36:23 +01:00
# Select a host to SSH into
2024-02-19 05:52:08 +01:00
def nussh [] {
2024-04-13 03:35:15 +02:00
let ssh_host = (ssh ssh-list | get Host | input list $"\n(ansi red)Which host to SSH into?(ansi reset)")
2024-02-19 05:52:08 +01:00
if ($ssh_host | describe) != "string" {
echo "Operation cancelled"
return
}
2024-03-17 19:36:23 +01:00
autossh -M 0 $ssh_host
2024-02-19 05:52:08 +01:00
}
2024-03-17 19:36:23 +01:00
# Select an open source license and save to LICENSE in current working directory
2024-02-19 05:52:08 +01:00
def nulicense [] {
let licenses = (http get https://api.github.com/licenses)
let license_name = ($licenses | get name | input list $"(ansi yellow)Choose an open source license:(ansi reset)")
if ($license_name | describe) != "string" {
echo "Operation cancelled"
return
}
let license_body = (http get ($licenses | where name == $license_name).url.0).body
$license_body | save --raw -f LICENSE
}
2024-03-17 19:36:23 +01:00
# Convert YAML to JSON
2024-02-19 05:52:08 +01:00
def yaml2json [filename: path] {
let stem = (echo $filename | path parse | get stem)
open --raw $filename | from yaml | to json | save --raw ($stem + ".json" | str join)
}
2024-03-17 19:36:23 +01:00
# Download a file with httpie
2024-04-01 23:37:50 +02:00
def httpie [url: string] {
2024-03-17 19:36:23 +01:00
^http -d $url
}
2024-02-19 05:52:08 +01:00
$env.config = {
rm: {
always_trash: true
}
table: {
show_empty: false
}
keybindings: [
{
name: nussh
modifier: alt
keycode: char_s
mode: [emacs vi_normal vi_insert]
event: { send: executehostcommand cmd: nussh }
}
]
}