From 3a16ebdf726a7ba5abb073bb069fbe8344826e88 Mon Sep 17 00:00:00 2001 From: ant-arctica <74963360+ant-arctica@users.noreply.github.com> Date: Wed, 7 Apr 2021 19:17:39 +0200 Subject: [PATCH] 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 --- doc/man-home-manager.xml | 18 +++++++- home-manager/home-manager | 84 ++++++++++++++++++++++++++++++++---- modules/home-environment.nix | 3 ++ 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/doc/man-home-manager.xml b/doc/man-home-manager.xml index 3bfe0230..ceb6a29f 100644 --- a/doc/man-home-manager.xml +++ b/doc/man-home-manager.xml @@ -66,6 +66,10 @@ -I path + + --flake flake-uri + + -b ext @@ -160,7 +164,7 @@ Description This command updates the user environment so that it corresponds to the - configuration specified in ~/.config/nixpkgs/home.nix. + configuration specified in ~/.config/nixpkgs/home.nix or ~/.config/nixpkgs/flake.nix. All operations using this tool expects a sub-command that indicates the @@ -343,6 +347,18 @@ + + + + + + + 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. + + + diff --git a/home-manager/home-manager b/home-manager/home-manager index d1007c9d..47ef2d69 100644 --- a/home-manager/home-manager +++ b/home-manager/home-manager @@ -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 diff --git a/modules/home-environment.nix b/modules/home-environment.nix index e5b6cc3a..b9b3a8cc 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -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