home-manager: Add --flake option to home-manager (#1856)

Implements a --flake options for build and switch, along with the usual
flake related optons (for lock-files etc).

Configurations in the flake are automatically discovered in the
following order:
1. `outputs.homeConfigurations."$flake-uri"` (the `--flake parameter`)
2. `outputs.homeConfigurations."$USERNAME@$HOSTNAME"`
3. `outputs.homeConfigurations."$USERNAME"`

Make home-manager use default configuration from
~/.config/nixpkgs/flake.nix, if it exists and nothing else is
specified.

Co-authored-by: Nicolas Berbiche <nicolas@normie.dev>
This commit is contained in:
ant-arctica 2021-04-07 19:17:39 +02:00 committed by GitHub
parent e5a260a569
commit 3a16ebdf72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 10 deletions

View File

@ -66,6 +66,10 @@
-I <replaceable>path</replaceable>
</arg>
<arg>
--flake <replaceable>flake-uri</replaceable>
</arg>
<arg>
-b <replaceable>ext</replaceable>
</arg>
@ -160,7 +164,7 @@
<title>Description</title>
<para>
This command updates the user environment so that it corresponds to the
configuration specified in <filename>~/.config/nixpkgs/home.nix</filename>.
configuration specified in <filename>~/.config/nixpkgs/home.nix</filename> or <filename>~/.config/nixpkgs/flake.nix</filename>.
</para>
<para>
All operations using this tool expects a sub-command that indicates the
@ -343,6 +347,18 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--flake <replaceable>flake-uri[#name]</replaceable></option>
</term>
<listitem>
<para>
Build Home Manager configuration from the flake, which must contain the
output homeConfigurations.name. If no name is specified it will first try
username@hostname and then username.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-b <replaceable>extension</replaceable></option>

View File

@ -74,7 +74,35 @@ function setHomeManagerNixPath() {
done
}
function setFlakeAttribute() {
local configFlake="${XDG_CONFIG_HOME:-$HOME/.config}/nixpkgs/flake.nix"
if [[ -z $FLAKE_ARG && ! -v HOME_MANAGER_CONFIG && -e "$configFlake" ]]; then
FLAKE_ARG="$(dirname "$(readlink -f "$configFlake")")"
fi
if [[ -n "$FLAKE_ARG" ]]; then
local flake="${FLAKE_ARG%#*}"
case $FLAKE_ARG in
*#*)
local name="${FLAKE_ARG#*#}"
;;
*)
local name="$USER@$(hostname)"
if [ "$(nix eval "$flake#homeConfigurations" --apply "x: x ? \"$name\"")" = "false" ]; then
name="$USER"
fi
;;
esac
export FLAKE_CONFIG_URI="$flake#homeConfigurations.\"$name\""
fi
}
function doInstantiate() {
setFlakeAttribute
if [[ -v FLAKE_CONFIG_URI ]]; then
errorEcho "Can't instantiate a flake configuration"
exit 1
fi
setConfigFile
setHomeManagerNixPath
@ -178,6 +206,17 @@ function doBuild() {
return 1
fi
setFlakeAttribute
if [[ -v FLAKE_CONFIG_URI ]]; then
local exitCode=0
nix build \
"${PASSTHROUGH_OPTS[@]}" \
${DRY_RUN+--dry-run} \
"$FLAKE_CONFIG_URI.activationPackage" \
|| exitCode=1
return $exitCode
fi
setWorkDir
local newsInfo
@ -194,6 +233,16 @@ function doBuild() {
}
function doSwitch() {
setFlakeAttribute
if [[ -v FLAKE_CONFIG_URI ]]; then
local exitCode=0
nix run \
"${PASSTHROUGH_OPTS[@]}" \
"$FLAKE_CONFIG_URI.activationPackage" \
|| exitCode=1
return $exitCode
fi
setWorkDir
local newsInfo
@ -409,15 +458,16 @@ function doHelp() {
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 " -b EXT Move existing files to new path rather than fail."
echo " -v Verbose output"
echo " -n Do a dry run, only prints what actions would be taken"
echo " -h Print this help"
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 " --flake flake-uri Use home-manager configuration at flake-uri"
echo " -b EXT Move existing files to new path rather than fail."
echo " -v Verbose output"
echo " -n Do a dry run, only prints what actions would be taken"
echo " -h Print this help"
echo
echo "Options passed on to nix-build(1)"
echo
@ -467,6 +517,7 @@ HOME_MANAGER_CONFIG_ATTRIBUTE=""
PASSTHROUGH_OPTS=()
COMMAND=""
COMMAND_ARGS=()
FLAKE_ARG=""
while [[ $# -gt 0 ]]; do
opt="$1"
@ -491,6 +542,21 @@ while [[ $# -gt 0 ]]; do
HOME_MANAGER_CONFIG="$1"
shift
;;
--flake)
FLAKE_ARG="$1"
shift
;;
--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
PASSTHROUGH_OPTS+=("$opt")
;;
--update-input)
PASSTHROUGH_OPTS+=("$opt" "$1")
shift
;;
--override-input)
PASSTHROUGH_OPTS+=("$opt" "$1" "$2")
shift 2
;;
-h|--help)
doHelp
exit 0

View File

@ -552,6 +552,9 @@ in
cp ${activationScript} $out/activate
mkdir $out/bin
ln -s $out/activate $out/bin/home-manager-generation
substituteInPlace $out/activate \
--subst-var-by GENERATION_DIR $out