nix/module/common/bash/module/Nix.sh

84 lines
2.1 KiB
Bash
Raw Normal View History

export _nix_system_config="git+https://git.voronind.com/voronind/nixos.git"
# Rebuild system.
# Optionally force the hostname.
2024-03-08 22:12:45 +03:00
# Usage: nixos_rebuild [HOSTNAME]
function nixos_rebuild() {
local target="${1}"
[[ "${target}" = "" ]] && target="${HOSTNAME}"
2024-03-18 17:47:43 +03:00
nixos-rebuild boot --flake "${_nix_system_config}#${target}" --refresh ${@}
}
# Rebuild and switch system.
# Optionally force the hostname.
2024-03-08 22:12:45 +03:00
# Usage: nixos_switch [HOSTNAME]
function nixos_switch() {
local target="${1}"
[[ "${target}" = "" ]] && target="${HOSTNAME}"
2024-03-18 17:47:43 +03:00
nixos-rebuild switch --flake "${_nix_system_config}#${target}" --refresh ${@}
}
# Spawn shell with specified nix environment.
# Uses flake.nix in current dir by default.
# Usage: nix_shell [NAME]
function nix_shell() {
local target="${1}"
[[ "${target}" = "" ]] && target="default"
2024-03-08 22:12:45 +03:00
# Create Nix GC root in .NixRoot{NAME}.
nix build ".#devShells.${NIX_CURRENT_SYSTEM}.${target}" -o ".NixRoot${target^}"
2024-03-08 22:12:45 +03:00
NIX_SHELL="${target}" nix develop ".#devShells.${NIX_CURRENT_SYSTEM}.${target}"
}
alias shell="nix_shell"
2024-03-08 22:12:45 +03:00
# Spawn temporary nix-shell with specified packages.
# Usage: nix_tmpshell <PACKAGES>
function nix_tmpshell() {
local IFS=$'\n'
local input=("${@}")
local pkgs=()
local tag="${NIX_SHELL}"
if [[ "${input}" = "" ]]; then
help nix_tmpshell
return 2
fi
[[ "${tag}" = "" ]] && tag="${1}"
for pkg in ${input[@]}; do
pkgs+=("nixpkgs#${pkg}")
done
NIX_SHELL="${tag}" NIXPKGS_ALLOW_UNFREE=1 nix shell --impure ${pkgs[@]}
}
alias tmpshell="nix_tmpshell"
# Build live image.
2024-03-08 22:12:45 +03:00
function nixos_live() {
2024-03-18 17:47:43 +03:00
nix build "${_nix_system_config}#nixosConfigurations.live.config.system.build.isoImage" --refresh ${@}
}
2024-03-04 03:56:08 +03:00
# List nixos generations.
2024-03-08 22:12:45 +03:00
function nixos_generations() {
2024-03-04 03:56:08 +03:00
nix-env -p /nix/var/nix/profiles/system --list-generations
}
# Switch nix-on-droid.
function nixdroid_switch() {
2024-03-18 17:47:43 +03:00
nix-on-droid switch --flake "${_nix_system_config}" ${@}
}
# Autocomplete with available hosts.
function _comp_hosts() {
local IFS=$'\n'
local targets=($(ls ~/.config/linux/system/host/))
_autocomplete_first ${targets[@]}
}
complete -F _comp_hosts nix_switch nix_rebuild