mirror of
https://github.com/nix-community/home-manager
synced 2024-06-02 21:13:33 +02:00
199 lines
4.8 KiB
Bash
199 lines
4.8 KiB
Bash
#!@bash@/bin/bash
|
|
|
|
# This code explicitly requires GNU Core Utilities and we therefore
|
|
# need to ensure they are prioritized over any other similarly named
|
|
# tools on the system.
|
|
PATH=@coreutils@/bin:$PATH
|
|
|
|
set -euo pipefail
|
|
|
|
# Attempts to set the HOME_MANAGER_CONFIG global variable.
|
|
#
|
|
# If no configuration file can be found then this function will print
|
|
# an error message and exit with an error code.
|
|
function setConfigFile() {
|
|
if [[ -v HOME_MANAGER_CONFIG ]] ; then
|
|
if [[ ! -e "$HOME_MANAGER_CONFIG" ]] ; then
|
|
echo "No configure file found at $HOME_MANAGER_CONFIG"
|
|
exit 1
|
|
fi
|
|
|
|
HOME_MANAGER_CONFIG="$(realpath "$HOME_MANAGER_CONFIG")"
|
|
return
|
|
fi
|
|
|
|
local confFile
|
|
for confFile in "$HOME/.config/nixpkgs/home.nix" \
|
|
"$HOME/.nixpkgs/home.nix" ; do
|
|
if [[ -e "$confFile" ]] ; then
|
|
HOME_MANAGER_CONFIG="$confFile"
|
|
return
|
|
fi
|
|
done
|
|
|
|
echo "No configuration file found. " \
|
|
"Please create one at ~/.config/nixpkgs/home.nix"
|
|
exit 1
|
|
}
|
|
|
|
function setHomeManagerModulesPath() {
|
|
local modulesPath
|
|
for modulesPath in "@MODULES_PATH@" \
|
|
"$HOME/.config/nixpkgs/home-manager/modules" \
|
|
"$HOME/.nixpkgs/home-manager/modules" ; do
|
|
if [[ -e "$modulesPath" ]] ; then
|
|
export NIX_PATH="$NIX_PATH${NIX_PATH:+:}home-manager=$modulesPath"
|
|
return
|
|
fi
|
|
done
|
|
}
|
|
|
|
function doBuild() {
|
|
if [[ -z "$1" ]]; then
|
|
echo "Need to provide generation output path."
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -e "$1" ]]; then
|
|
echo "The output path $1 already exists."
|
|
exit 1
|
|
fi
|
|
|
|
setConfigFile
|
|
setHomeManagerModulesPath
|
|
|
|
output="$(realpath "$1")"
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
exit 1
|
|
fi
|
|
|
|
local extraArgs
|
|
extraArgs=""
|
|
|
|
for p in "${EXTRA_NIX_PATH[@]}"; do
|
|
extraArgs="$extraArgs -I $p"
|
|
done
|
|
|
|
if [[ -v VERBOSE ]]; then
|
|
extraArgs="$extraArgs --show-trace"
|
|
fi
|
|
|
|
nix-build $extraArgs \
|
|
"@HOME_MANAGER_EXPR_PATH@" \
|
|
--argstr confPath "$HOME_MANAGER_CONFIG" \
|
|
--argstr confAttr "$HOME_MANAGER_CONFIG_ATTRIBUTE" \
|
|
-A activation-script \
|
|
-o "$output"
|
|
}
|
|
|
|
function doSwitch() {
|
|
local wrkdir
|
|
wrkdir="$(mktemp -d)"
|
|
|
|
if doBuild "$wrkdir/generation" ; then
|
|
"$wrkdir/generation/activate"
|
|
fi
|
|
|
|
rm -r "$wrkdir"
|
|
}
|
|
|
|
function doListGens() {
|
|
pushd "/nix/var/nix/profiles/per-user/$USER" > /dev/null
|
|
ls --color=yes -gG --sort time home-manager-*-link \
|
|
| cut -d' ' -f 4-
|
|
popd > /dev/null
|
|
}
|
|
|
|
function doListPackages() {
|
|
local outPath
|
|
outPath="$(nix-env -q --out-path | grep -o '/.*home-manager-path$')"
|
|
if [[ -n "$outPath" ]] ; then
|
|
nix-store -q --references "$outPath" | sed 's/[^-]*-//'
|
|
else
|
|
echo "No home-manager packages seem to be installed."
|
|
fi
|
|
}
|
|
|
|
function doHelp() {
|
|
echo "Usage: $0 [OPTION] COMMAND"
|
|
echo
|
|
echo "Options"
|
|
echo
|
|
echo " -f FILE The home configuration file."
|
|
echo " Default is '~/.config/nixpkgs/home.nix'."
|
|
echo " -A ATTRIBUTE Optional attribute that selects a configuration"
|
|
echo " expression in the configuration file."
|
|
echo " -I PATH Add a path to the Nix expression search path."
|
|
echo " -v Verbose output"
|
|
echo " -n Do a dry run, only prints what actions would be taken"
|
|
echo " -h Print this help"
|
|
echo
|
|
echo "Commands"
|
|
echo " help Print this help"
|
|
echo " build Build configuration into result directory"
|
|
echo " switch Build and activate configuration"
|
|
echo " generations List all home environment generations"
|
|
echo " packages List all packages installed in home-manager-path"
|
|
}
|
|
|
|
EXTRA_NIX_PATH=()
|
|
HOME_MANAGER_CONFIG_ATTRIBUTE=""
|
|
|
|
while getopts f:I:A:vnh opt; do
|
|
case $opt in
|
|
f)
|
|
HOME_MANAGER_CONFIG="$OPTARG"
|
|
;;
|
|
I)
|
|
EXTRA_NIX_PATH+=("$OPTARG")
|
|
;;
|
|
A)
|
|
HOME_MANAGER_CONFIG_ATTRIBUTE="$OPTARG"
|
|
;;
|
|
v)
|
|
export VERBOSE=1
|
|
;;
|
|
n)
|
|
export DRY_RUN=1
|
|
;;
|
|
h)
|
|
doHelp
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Unknown option -$OPTARG" >&2
|
|
doHelp >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Get rid of the options.
|
|
shift "$((OPTIND-1))"
|
|
|
|
cmd="$*"
|
|
|
|
case "$cmd" in
|
|
build)
|
|
doBuild "result"
|
|
;;
|
|
switch)
|
|
doSwitch
|
|
;;
|
|
generations)
|
|
doListGens
|
|
;;
|
|
packages)
|
|
doListPackages
|
|
;;
|
|
help|--help)
|
|
doHelp
|
|
;;
|
|
*)
|
|
echo "Unknown command: $cmd"
|
|
doHelp >&2
|
|
exit 1
|
|
;;
|
|
esac
|