This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Nix.sh

115 lines
2.4 KiB
Bash
Raw Normal View History

2024-01-24 17:05:52 +03:00
# Find Nix package path.
2024-01-24 18:49:45 +03:00
# Usage: nix_find <PACKAGE>
function nix_find() {
2024-01-24 17:05:52 +03:00
local IFS=$'\n'
local package="${1}"
if [[ "${package}" = "" ]]; then
help find_nix
return 2
fi
local found=$(ls --classify /nix/store/ | grep "${package}".*/)
if [[ "${found}" != "" ]]; then
echo "/nix/store/${found%/}"
else
false
fi
}
2024-01-24 18:56:14 +03:00
2024-01-31 03:32:45 +03:00
# Rebuild system.
# Optionally force the hostname.
2024-01-31 03:32:45 +03:00
# Usage: nix_rebuild [HOSTNAME]
function nix_rebuild() {
local target="${1}"
[[ "${target}" = "" ]] && target="${HOSTNAME}"
2024-01-30 22:23:58 +03:00
cd ${HOME}/.config/linux/system
2024-01-31 17:34:34 +03:00
nixos-rebuild boot --flake .#${target}
2024-01-31 03:32:45 +03:00
cd -
2024-01-24 19:06:50 +03:00
}
2024-02-03 00:04:38 +03:00
# Rebuild and switch system.
# Optionally force the hostname.
# Usage: nix_switch [HOSTNAME]
function nix_switch() {
local target="${1}"
[[ "${target}" = "" ]] && target="${HOSTNAME}"
cd ${HOME}/.config/linux/system
nixos-rebuild switch --flake .#${target}
cd -
}
2024-01-31 03:32:45 +03:00
# Update system.
# Optionally force the hostname.
2024-01-31 03:32:45 +03:00
# Usage: nix_update [HOSTNAME]
function nix_update() {
local target="${1}"
[[ "${target}" = "" ]] && target="${HOSTNAME}"
2024-01-30 22:23:58 +03:00
cd ${HOME}/.config/linux/system
2024-01-31 03:32:45 +03:00
nix flake update
cd -
2024-01-24 18:56:14 +03:00
}
2024-01-24 19:42:52 +03:00
# Free up root space.
function nix_clean() {
nix-collect-garbage -d
2024-01-28 21:27:27 +03:00
_is_root && nix-store --gc
2024-01-24 19:42:52 +03:00
}
2024-01-24 23:58:03 +03:00
# Spawn shell with specified nix environment.
# Uses flake.nix in current dir by default.
2024-01-28 20:57:01 +03:00
# Usage: nix_shell [NAME]
2024-01-25 17:21:51 +03:00
function nix_shell() {
2024-01-28 20:57:01 +03:00
local name="${1,,}"
2024-01-25 17:21:51 +03:00
if [[ "${name}" = "" ]]; then
2024-02-06 23:16:21 +03:00
NIX_SHELL=$(parse_alnum "${PWD##*/}") nix develop
else
NIX_SHELL="${name}" nix develop -f ~/.config/linux/shell/"${name^}".nix
fi
2024-01-24 23:58:03 +03:00
}
2024-01-28 21:27:27 +03:00
alias shell="nix_shell"
2024-01-28 21:21:35 +03:00
# Spawn nix-shell with specified packages.
2024-01-28 21:27:27 +03:00
# Usage: nix_tmpshell <PACKAGES>
function nix_tmpshell() {
2024-02-03 03:05:46 +03:00
local IFS=$'\n'
local input=("${@}")
local pkgs=()
2024-01-28 21:21:35 +03:00
2024-02-03 03:05:46 +03:00
if [[ "${input}" = "" ]]; then
2024-01-28 21:27:27 +03:00
help nix_tmpshell
2024-01-28 21:21:35 +03:00
return 2
fi
2024-02-03 03:05:46 +03:00
for pkg in ${input[@]}; do
pkgs+=("nixpkgs#${pkg}")
done
NIX_SHELL="${1}" nix shell ${pkgs[@]}
2024-01-28 21:21:35 +03:00
}
2024-01-28 21:27:27 +03:00
alias tmpshell="nix_tmpshell"
# Autocomplete with available hosts.
function _comp_hosts() {
local IFS=$'\n'
local targets=($(ls --classify ~/.config/linux/system/ | grep /$ | sed -e "s/\/$//"))
_autocomplete_first ${targets[@]}
}
2024-01-28 20:57:01 +03:00
# Autocomplete with available shells.
function _comp_shells() {
local IFS=$'\n'
local targets=($(ls ~/.config/linux/shell/ | sed -e "s/.nix$//" | tr '[:upper:]' '[:lower:]'))
_autocomplete_first ${targets[@]}
}
complete -F _comp_hosts nix_update nix_upgrade
2024-01-28 21:27:27 +03:00
complete -F _comp_shells nix_shell shell