Files
nixos/home/modules/baseline/xonsh_footer.xsh
T

136 lines
4.0 KiB
Plaintext
Raw Normal View History

2024-10-19 00:30:25 -05:00
# vim: set ft=xonsh :
2022-11-10 10:54:22 -06:00
2024-10-19 00:30:25 -05:00
from tempfile import NamedTemporaryFile, TemporaryDirectory
2024-07-17 12:05:21 -05:00
2024-06-12 13:47:02 -05:00
def bw_unlock():
2024-06-12 14:26:32 -05:00
"""Unlocks the BitWarden CLI and adds the resulting session code to the
current environment variables. Also returns the code for them."""
2024-06-12 13:47:02 -05:00
if "BW_SESSION" in ${...}:
return $BW_SESSION
2024-06-12 14:26:32 -05:00
result = $(bw unlock)
while "BW_SESSION" not in result:
result = $(bw unlock)
lines = result.split("\n")
l = [k for k in lines if 'BW_SESSION="' in k][0]
left, right = l.split("=", 1)
2024-06-12 13:47:02 -05:00
token = right[1:-1]
$BW_SESSION = token
return token
2024-07-17 12:05:21 -05:00
def vpn(con, bwname):
2024-11-08 10:34:20 -06:00
nmcli c down @(con)
2024-07-17 12:05:21 -05:00
bw_unlock()
base=$(bw get password @(bwname))
secret=$(bw get totp @(bwname))
#echo vpn.secrets.password:${base}$(oathtool -b -d "${digits}" -s "${period}" --totp "${secret}") > "${f}"
with NamedTemporaryFile(delete_on_close=False) as fp:
secret = f"vpn.secrets.password:{base}{secret}"
fp.write(secret.encode("utf-8"))
fp.close()
nmcli c up @(con) passwd-file @(fp.name)
def _unlock(args):
bw_unlock()
2024-08-04 20:25:35 -05:00
aliases['unlock'] = _unlock
2024-07-17 12:05:21 -05:00
def _ivr(args):
vpn("350Main", "IVR Technology")
2024-08-04 20:25:35 -05:00
aliases['ivr'] = _ivr
2024-07-17 12:05:21 -05:00
2024-10-01 14:19:12 -05:00
def _ivr2(args):
vpn("gregory_hellings@ra.ivrtechnology.com", "IVR Technology")
aliases['ivr2'] = _ivr2
2024-06-20 23:27:00 -05:00
def _glrestart(args):
sudo nixos-container run gitlab -- systemctl restart gitlab
sudo nixos-container run gitlab -- systemctl restart nginx
2024-08-04 20:25:35 -05:00
aliases['glrestart'] = _glrestart
2024-06-20 23:27:00 -05:00
2024-07-31 22:56:37 -05:00
def _aws_creds(args):
$AWS_ACCESS_KEY_ID=$(bw get username "AWS Access Key")
$AWS_SECRET_ACCESS_KEY=$(bw get password "AWS Access Key")
2024-08-04 20:25:35 -05:00
aliases['aws_creds'] = _aws_creds
2024-07-31 22:56:37 -05:00
2022-11-10 10:54:22 -06:00
def _rebuild(args):
2023-05-10 16:49:06 -05:00
system = uname()
2024-10-19 00:30:25 -05:00
hostname = system.nodename
2022-11-10 10:54:22 -06:00
if system.sysname == 'Darwin':
2023-05-09 12:01:29 -05:00
darwin-rebuild --flake ~/.config/darwin switch
2022-11-10 10:54:22 -06:00
else:
2024-10-19 00:30:25 -05:00
with TemporaryDirectory() as td:
pushd @(td)
2024-10-19 00:54:11 -05:00
nom build f"/etc/nixos#nixosConfigurations.{hostname}.config.system.build.toplevel"
2024-10-19 00:30:25 -05:00
if g`result`:
nvd diff /run/current-system result
2024-11-04 10:20:41 -06:00
sudo nixos-rebuild switch
2024-10-19 00:30:25 -05:00
popd
2024-08-04 20:25:35 -05:00
aliases['rebuild'] = _rebuild
2022-11-10 10:54:22 -06:00
def _yaml2json(args, stdin=None, stdout=None):
import sys, yaml, json
from yaml import CLoader
json.dump(yaml.load(stdin, Loader=CLoader), stdout, indent=4)
2024-08-04 20:25:35 -05:00
aliases['yaml2json'] = _yaml2json
2022-11-10 10:54:22 -06:00
def _py2env(args):
vox new @(args[0]) -p /usr/bin/python2
2024-08-04 20:25:35 -05:00
aliases['py2env'] = _py2env
2022-11-10 10:54:22 -06:00
def _py3env(args):
vox new @(args[0])
2024-08-04 20:25:35 -05:00
aliases['py3env'] = _py3env
2022-11-10 10:54:22 -06:00
def _rundock(args):
2023-05-10 16:49:06 -05:00
if Path('/usr/bin/podman').exists():
2022-11-10 10:54:22 -06:00
e = 'podman'
else:
e = 'docker'
@(e) exec -ti @(args[0]) /bin/bash
2024-08-04 20:25:35 -05:00
aliases['rundock'] = _rundock
2022-11-10 10:54:22 -06:00
def _pip_extras(args):
import importlib_metadata
print(importlib_metadata.metadata(args[0]).get_all('Provides-Extra'))
2024-08-04 20:25:35 -05:00
aliases['pip_extras'] = _pip_extras
2022-11-10 10:54:22 -06:00
# Container stuff
def _newdock(args):
2023-05-10 16:49:06 -05:00
if Path('/usr/bin/podman').exists():
2022-11-10 10:54:22 -06:00
e = 'podman'
else:
e = 'docker'
2023-05-10 16:49:06 -05:00
@(e) run -P --privileged=true -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v @(getcwd()):/dmnt -v /etc/pki:/etc/pki:ro -d --name @(args[1]) @(args[0]) /sbin/init
2022-11-10 10:54:22 -06:00
rundock @(args[1])
2024-08-04 20:25:35 -05:00
aliases['newdock'] = _newdock
2022-11-10 10:54:22 -06:00
def _unknown_host(args):
sed -i -e @(args[0])d ~/.ssh/known_hosts
2024-08-04 20:25:35 -05:00
aliases['unknown_host'] = _unknown_host
2022-11-10 10:54:22 -06:00
def _bake(args):
from pathlib import Path
templates = Path("~/.copier-templates/").expanduser()
if not templates.exists():
git clone src:greg/copier-templates.git ~/.copier-templates
copier copy @(str(templates / args[0])) .
aliases['bake'] = _bake
2024-08-04 20:25:35 -05:00
2024-08-20 22:43:31 -05:00
def _cleanup(args):
sudo nix profile wipe-history --profile ~/.local/state/nix/profiles/home-manager --older-than '30d'
sudo nix-collect-garbage --delete-older-than 30d
sudo nix store optimise
aliases['cleanup'] = _cleanup
2022-11-10 10:54:22 -06:00
###
#
# Other random nice-to-have things
#
###
# Allows identifying JSON as if it was Python by adding some new builtins to the language
import builtins
builtins.true = True
builtins.false = False
builtins.null = None