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

81 lines
1.8 KiB
Bash
Raw Normal View History

2024-02-20 19:46:24 +03:00
export _nix_system_config="${HOME}/.config/linux/system"
2024-02-20 19:29:01 +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-02-20 19:29:01 +03:00
nixos-rebuild boot --flake "${_nix_system_config}#${target}"
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}"
2024-02-20 19:29:01 +03:00
nixos-rebuild switch --flake "${_nix_system_config}#${target}"
2024-02-03 00:04:38 +03:00
}
2024-02-08 06:36:40 +03:00
# Update system versions.
# Usage: nix_update
2024-01-31 03:32:45 +03:00
function nix_update() {
2024-02-20 20:12:49 +03:00
nix flake update "${_nix_system_config}"
2024-01-24 18:56:14 +03:00
}
2024-01-24 19:42:52 +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-02-08 06:36:40 +03:00
local target="${1}"
[[ "${target}" = "" ]] && target="default"
2024-01-25 17:21:51 +03:00
2024-02-08 06:48:33 +03:00
if [[ "${target}" = "default" ]]; then
NIX_SHELL="${target}" nix develop
else
NIX_SHELL="${target}" nix develop ".#${target}"
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-02-22 20:23:59 +03:00
local tag="${NIX_SHELL}"
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-22 20:23:59 +03:00
[[ "${tag}" = "" ]] && tag="${1}"
2024-02-03 03:05:46 +03:00
for pkg in ${input[@]}; do
pkgs+=("nixpkgs#${pkg}")
done
2024-02-22 20:23:59 +03:00
NIX_SHELL="${tag}" nix shell ${pkgs[@]}
2024-01-28 21:21:35 +03:00
}
2024-01-28 21:27:27 +03:00
alias tmpshell="nix_tmpshell"
2024-02-20 19:29:01 +03:00
# Build live image.
function nix_live() {
2024-02-20 20:00:46 +03:00
nix build "${_nix_system_config}#nixosConfigurations.live.config.system.build.isoImage"
2024-02-20 19:29:01 +03:00
}
# Autocomplete with available hosts.
function _comp_hosts() {
local IFS=$'\n'
2024-02-07 20:55:24 +03:00
local targets=($(ls ~/.config/linux/system/host/))
_autocomplete_first ${targets[@]}
}
2024-02-08 06:36:40 +03:00
complete -F _comp_hosts nix_switch nix_rebuild