home-manager: use nix-env to list generations

Using the `nix-env` command is far more robust. It also has the
benefit that if the per-user `profiles` and `gcroots` directories do
not exist then they will be created with the correct permissions.

Because of the second point this commit also removes the `mkdir` step
of the installation instructions.

PR #1239
Closes #474, #948, #1091

(cherry picked from commit 9ec9f004e1)
This commit is contained in:
Robert Helgesson 2020-05-16 18:09:09 +02:00
parent a378bccd60
commit 25ddbf4824
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 24 additions and 50 deletions

View file

@ -4,9 +4,6 @@ os:
- linux - linux
- osx - osx
before_script:
- mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER
script: script:
- ./format -c - ./format -c
- nix-shell . -A install - nix-shell . -A install

View file

@ -46,21 +46,12 @@ Installation
Currently the easiest way to install Home Manager is as follows: Currently the easiest way to install Home Manager is as follows:
1. Make sure you have a working Nix installation. If you are not 1. Make sure you have a working Nix installation. Specifically, make
using NixOS then you may here have to run sure that your user is able to build and install Nix packages. For
example, you should be able to successfully run a command like
```console `nix-instantiate '<nixpkgs>' -A hello` without having to switch to
$ mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER the root user. For a multi-user install of Nix this means that
``` your user must be covered by the
since Home Manager uses these directories to manage your profile
generations. On NixOS these should already be available.
Also make sure that your user is able to build and install Nix
packages. For example, you should be able to successfully run a
command like `nix-instantiate '<nixpkgs>' -A hello` without having
to switch to the root user. For a multi-user install of Nix this
means that your user must be covered by the
[`allowed-users`][nixAllowedUsers] Nix option. On NixOS you can [`allowed-users`][nixAllowedUsers] Nix option. On NixOS you can
control this option using the control this option using the
[`nix.allowedUsers`][nixosAllowedUsers] system option. [`nix.allowedUsers`][nixosAllowedUsers] system option.

View file

@ -44,22 +44,12 @@
<orderedlist> <orderedlist>
<listitem> <listitem>
<para> <para>
Make sure you have a working Nix installation. If you are not using NixOS Make sure you have a working Nix installation. Specifically, make
then it may be necessary to run sure that your user is able to build and install Nix packages.
</para> For example, you should be able to successfully run a command
<screen> like <literal>nix-instantiate '&lt;nixpkgs&gt;' -A hello</literal>
<prompt>$</prompt> <userinput>mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER</userinput> without having to switch to the root user. For a multi-user
</screen> install of Nix this means that your user must be covered by the
<para>
since Home Manager uses these directories to manage your profile
generations. On NixOS these should already be available.
</para>
<para>
Also make sure that your user is able to build and install Nix packages.
For example, you should be able to successfully run a command like
<literal>nix-instantiate '&lt;nixpkgs&gt;' -A hello</literal> without
having to switch to the root user. For a multi-user install of Nix this
means that your user must be covered by the
<link xlink:href="https://nixos.org/nix/manual/#conf-allowed-users"><literal>allowed-users</literal></link> <link xlink:href="https://nixos.org/nix/manual/#conf-allowed-users"><literal>allowed-users</literal></link>
Nix option. On NixOS you can control this option using the Nix option. On NixOS you can control this option using the
<link xlink:href="https://nixos.org/nixos/manual/options.html#opt-nix.allowedUsers"><literal>nix.allowedUsers</literal></link> <link xlink:href="https://nixos.org/nixos/manual/options.html#opt-nix.allowedUsers"><literal>nix.allowedUsers</literal></link>

View file

@ -3,13 +3,16 @@
function setupVars() { function setupVars() {
local profilesPath="/nix/var/nix/profiles/per-user/$USER" local profilesPath="/nix/var/nix/profiles/per-user/$USER"
local gcPath="/nix/var/nix/gcroots/per-user/$USER" local gcPath="/nix/var/nix/gcroots/per-user/$USER"
local greatestGenNum
genProfilePath="$profilesPath/home-manager"
newGenPath="@GENERATION_DIR@";
newGenGcPath="$gcPath/current-home"
local greatestGenNum
greatestGenNum=$( \ greatestGenNum=$( \
find "$profilesPath" -name 'home-manager-*-link' \ nix-env --list-generations --profile "$genProfilePath" \
| sed 's/^.*-\([0-9]*\)-link$/\1/' \ | tail -1 \
| sort -rn \ | sed -E 's/ *([[:digit:]]+) .*/\1/')
| head -1)
if [[ -n $greatestGenNum ]] ; then if [[ -n $greatestGenNum ]] ; then
oldGenNum=$greatestGenNum oldGenNum=$greatestGenNum
@ -18,15 +21,15 @@ function setupVars() {
newGenNum=1 newGenNum=1
fi fi
if [[ -e $gcPath/current-home ]] ; then if [[ -e $profilesPath/home-manager ]] ; then
oldGenPath="$(readlink -e "$gcPath/current-home")" oldGenPath="$(readlink -e "$profilesPath/home-manager")"
fi fi
$VERBOSE_ECHO "Sanity checking oldGenNum and oldGenPath" $VERBOSE_ECHO "Sanity checking oldGenNum and oldGenPath"
if [[ -v oldGenNum && ! -v oldGenPath if [[ -v oldGenNum && ! -v oldGenPath
|| ! -v oldGenNum && -v oldGenPath ]]; then || ! -v oldGenNum && -v oldGenPath ]]; then
errorEcho "Invalid profile number and GC root values! These must be" errorEcho "Invalid profile number and current profile values! These"
errorEcho "either both empty or both set but are now set to" errorEcho "must be either both empty or both set but are now set to"
errorEcho " '${oldGenNum:-}' and '${oldGenPath:-}'" errorEcho " '${oldGenNum:-}' and '${oldGenPath:-}'"
errorEcho "If you don't mind losing previous profile generations then" errorEcho "If you don't mind losing previous profile generations then"
errorEcho "the easiest solution is probably to run" errorEcho "the easiest solution is probably to run"
@ -35,12 +38,6 @@ function setupVars() {
errorEcho "and trying home-manager switch again. Good luck!" errorEcho "and trying home-manager switch again. Good luck!"
exit 1 exit 1
fi fi
genProfilePath="$profilesPath/home-manager"
newGenPath="@GENERATION_DIR@";
newGenProfilePath="$profilesPath/home-manager-$newGenNum-link"
newGenGcPath="$gcPath/current-home"
} }
if [[ -v VERBOSE ]]; then if [[ -v VERBOSE ]]; then
@ -78,6 +75,5 @@ else
fi fi
$VERBOSE_ECHO " newGenPath=$newGenPath" $VERBOSE_ECHO " newGenPath=$newGenPath"
$VERBOSE_ECHO " newGenNum=$newGenNum" $VERBOSE_ECHO " newGenNum=$newGenNum"
$VERBOSE_ECHO " newGenProfilePath=$newGenProfilePath"
$VERBOSE_ECHO " newGenGcPath=$newGenGcPath" $VERBOSE_ECHO " newGenGcPath=$newGenGcPath"
$VERBOSE_ECHO " genProfilePath=$genProfilePath" $VERBOSE_ECHO " genProfilePath=$genProfilePath"