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

121 lines
2.6 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
# Switch to Unstable branch.
function nix_unstable() {
nix-channel --remove nixos
# nix-channel --remove home-manager
2024-01-24 18:56:14 +03:00
nix-channel --add https://nixos.org/channels/nixos-unstable nixos
# nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
2024-01-24 18:56:14 +03:00
2024-01-25 19:49:45 +03:00
nix-channel --update
2024-01-24 18:56:14 +03:00
}
2024-01-25 00:39:42 +03:00
# Display current channel.
function nix_channel() {
nix-channel --list
}
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
2024-01-28 20:57:01 +03:00
# Spawn shell with specified nix environment. `Main` is default.
# 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,,}"
[[ "${name}" = "" ]] && name="main"
2024-01-25 17:21:51 +03:00
2024-01-28 20:57:01 +03:00
nix_shell="${name}" nix-shell ~/.config/linux/shell/"${name^}".nix
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-01-29 00:53:09 +03:00
local pkgs="${@}"
2024-01-28 21:21:35 +03:00
if [[ "${pkgs}" = "" ]]; then
2024-01-28 21:27:27 +03:00
help nix_tmpshell
2024-01-28 21:21:35 +03:00
return 2
fi
nix_shell="${1}" nix-shell -p ${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