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:
parent
e5a260a569
commit
3a16ebdf72
|
@ -66,6 +66,10 @@
|
||||||
-I <replaceable>path</replaceable>
|
-I <replaceable>path</replaceable>
|
||||||
</arg>
|
</arg>
|
||||||
|
|
||||||
|
<arg>
|
||||||
|
--flake <replaceable>flake-uri</replaceable>
|
||||||
|
</arg>
|
||||||
|
|
||||||
<arg>
|
<arg>
|
||||||
-b <replaceable>ext</replaceable>
|
-b <replaceable>ext</replaceable>
|
||||||
</arg>
|
</arg>
|
||||||
|
@ -160,7 +164,7 @@
|
||||||
<title>Description</title>
|
<title>Description</title>
|
||||||
<para>
|
<para>
|
||||||
This command updates the user environment so that it corresponds to the
|
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>
|
||||||
<para>
|
<para>
|
||||||
All operations using this tool expects a sub-command that indicates the
|
All operations using this tool expects a sub-command that indicates the
|
||||||
|
@ -343,6 +347,18 @@
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</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>
|
<varlistentry>
|
||||||
<term>
|
<term>
|
||||||
<option>-b <replaceable>extension</replaceable></option>
|
<option>-b <replaceable>extension</replaceable></option>
|
||||||
|
|
|
@ -74,7 +74,35 @@ function setHomeManagerNixPath() {
|
||||||
done
|
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() {
|
function doInstantiate() {
|
||||||
|
setFlakeAttribute
|
||||||
|
if [[ -v FLAKE_CONFIG_URI ]]; then
|
||||||
|
errorEcho "Can't instantiate a flake configuration"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
setConfigFile
|
setConfigFile
|
||||||
setHomeManagerNixPath
|
setHomeManagerNixPath
|
||||||
|
|
||||||
|
@ -178,6 +206,17 @@ function doBuild() {
|
||||||
return 1
|
return 1
|
||||||
fi
|
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
|
setWorkDir
|
||||||
|
|
||||||
local newsInfo
|
local newsInfo
|
||||||
|
@ -194,6 +233,16 @@ function doBuild() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSwitch() {
|
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
|
setWorkDir
|
||||||
|
|
||||||
local newsInfo
|
local newsInfo
|
||||||
|
@ -414,6 +463,7 @@ function doHelp() {
|
||||||
echo " -A ATTRIBUTE Optional attribute that selects a configuration"
|
echo " -A ATTRIBUTE Optional attribute that selects a configuration"
|
||||||
echo " expression in the configuration file."
|
echo " expression in the configuration file."
|
||||||
echo " -I PATH Add a path to the Nix expression search path."
|
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 " -b EXT Move existing files to new path rather than fail."
|
||||||
echo " -v Verbose output"
|
echo " -v Verbose output"
|
||||||
echo " -n Do a dry run, only prints what actions would be taken"
|
echo " -n Do a dry run, only prints what actions would be taken"
|
||||||
|
@ -467,6 +517,7 @@ HOME_MANAGER_CONFIG_ATTRIBUTE=""
|
||||||
PASSTHROUGH_OPTS=()
|
PASSTHROUGH_OPTS=()
|
||||||
COMMAND=""
|
COMMAND=""
|
||||||
COMMAND_ARGS=()
|
COMMAND_ARGS=()
|
||||||
|
FLAKE_ARG=""
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
opt="$1"
|
opt="$1"
|
||||||
|
@ -491,6 +542,21 @@ while [[ $# -gt 0 ]]; do
|
||||||
HOME_MANAGER_CONFIG="$1"
|
HOME_MANAGER_CONFIG="$1"
|
||||||
shift
|
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)
|
-h|--help)
|
||||||
doHelp
|
doHelp
|
||||||
exit 0
|
exit 0
|
||||||
|
|
|
@ -552,6 +552,9 @@ in
|
||||||
|
|
||||||
cp ${activationScript} $out/activate
|
cp ${activationScript} $out/activate
|
||||||
|
|
||||||
|
mkdir $out/bin
|
||||||
|
ln -s $out/activate $out/bin/home-manager-generation
|
||||||
|
|
||||||
substituteInPlace $out/activate \
|
substituteInPlace $out/activate \
|
||||||
--subst-var-by GENERATION_DIR $out
|
--subst-var-by GENERATION_DIR $out
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue