1
0
mirror of https://github.com/nix-community/home-manager synced 2024-06-03 05:23:32 +02:00
home-manager/home-manager/home-manager
Robert Helgesson 67391395ef
home-manager: use NIX_PATH to locate modules
This removes the old argument based method that Home Manager used to
find its modules by a `NIX_PATH` based method. Specifically, this adds
a `home-manager` Nix path prefix that can be overridden much like with
the `nixpkgs` path prefix.
2017-06-03 00:42:37 +02:00

167 lines
3.6 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
function doBuild() {
if [[ -z "$1" ]]; then
echo "Need to provide path to configuration file."
exit 1
fi
if [[ -z "$2" ]]; then
echo "Need to provide generation output path."
exit 1
fi
if [[ -e "$2" ]]; then
echo "The output path $2 already exists."
exit 1
fi
local confFile output
confFile="$(realpath "$1")"
if [[ $? -ne 0 ]]; then
exit 1
fi
if [[ ! -r "$confFile" ]]; then
echo "No such configuration file: $1"
exit 1
fi
output="$(realpath "$2")"
if [[ $? -ne 0 ]]; then
exit 1
fi
export NIX_PATH="$NIX_PATH${NIX_PATH:+:}home-manager=@MODULES_PATH@"
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 "$confFile" \
-A activation-script \
-o "$output"
}
function doSwitch() {
local wrkdir
wrkdir="$(mktemp -d)"
if doBuild "$1" "$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. Default is ~/.nixpkgs/home.nix"
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"
}
CONFIG_FILE="$HOME/.nixpkgs/home.nix"
EXTRA_NIX_PATH=()
while getopts f:I:vnh opt; do
case $opt in
f)
CONFIG_FILE=$OPTARG
;;
I)
EXTRA_NIX_PATH+=("$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 "$CONFIG_FILE" "result"
;;
switch)
doSwitch "$CONFIG_FILE"
;;
generations)
doListGens
;;
packages)
doListPackages
;;
help|--help)
doHelp
;;
*)
echo "Unknown command: $cmd"
doHelp >&2
exit 1
;;
esac