Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-home-manager-generation/activate: line 67: nix-build: command not found #2178

Closed
colonelpanic8 opened this issue Jul 13, 2021 · 4 comments · Fixed by #2202
Closed

-home-manager-generation/activate: line 67: nix-build: command not found #2178

colonelpanic8 opened this issue Jul 13, 2021 · 4 comments · Fixed by #2202

Comments

@colonelpanic8
Copy link
Contributor

I'm getting the error nix-build: command not found, when trying to set up home-manager with nix-flakes for the first time.

It seems that nix-build is only unavailable when this service is started because the service definition sets path explicitly such that it does not include the path that nix-build is on. I'm not totally sure what is going on but i think that the environment might be coming from here:

environment = serviceEnvironment;

Seems like maybe the remedy should be to explicitly add nix as a dependency?

Environment="PATH=/nix/store/937f5738d2frws07ixcpg5ip176pfss1-coreutils-8.32/bin:/nix/store/aa4zln9ih27zqf5mncdaxq1sp89wps40-findutils-4.7.0/bin:/nix/store/aggfj36xik83sf1hp8rsfsxxdilxpz9m-gnugrep-3.6/bin:/nix/store/klwhsp6f58mqkil6aq59zcj4k7zgcqix-gnused-4.8/bin:/nix/store/vnjzzjsj5jv37lxa2xll07wf3i2yjvdg-systemd-247.6/bin:/nix/store/937f5738d2frws07ixcpg5ip176pfss1-coreutils-8.32/sbin:/nix/store/aa4zln9ih27zqf5mncdaxq1sp89wps40-findutils-4.7.0/sbin:/nix/store/aggfj36xik83sf1hp8rsfsxxdilxpz9m-gnugrep-3.6/sbin:/nix/store/klwhsp6f58mqkil6aq59zcj4k7zgcqix-gnused-4.8/sbin:/nix/store/vnjzzjsj5jv37lxa2xll07wf3i2yjvdg-systemd-247.6/sbin"

Here is the whole file

#!/nix/store/x0dcb2rxlzf32g0ddfkqqz1sfcyx4yay-bash-4.4-p23/bin/bash
set -eu
set -o pipefail

cd $HOME

export PATH="/nix/store/x0dcb2rxlzf32g0ddfkqqz1sfcyx4yay-bash-4.4-p23/bin:/nix/store/937f5738d2frws07ixcpg5ip176pfss1-coreutils-8.32/bin:/nix/store/bizkvxwm3972081hvjfgirf9w7v5nqkr-diffutils-3.7/bin:/nix/store/aa4zln9ih27zqf5mncdaxq1sp89wps40-findutils-4.7.0/bin:/nix/store/aggfj36xik83sf1hp8rsfsxxdilxpz9m-gnugrep-3.6/bin:/nix/store/klwhsp6f58mqkil6aq59zcj4k7zgcqix-gnused-4.8/bin:/nix/store/m4x0hqp8amgh987v1862x9hpxpi5rsgj-ncurses-6.2/bin${PATH:+:}$PATH"

. /nix/store/brmfvna1rca9qsgyp2k9hsylnlb0ijca-color-echo.sh

#!/usr/bin/env bash

function setupVars() {
    local nixStateDir="${NIX_STATE_DIR:-/nix/var/nix}"
    local profilesPath="$nixStateDir/profiles/per-user/$USER"
    local gcPath="$nixStateDir/gcroots/per-user/$USER"

    genProfilePath="$profilesPath/home-manager"
    newGenPath="/nix/store/hmcjzfivh5n7gvzahdmv42dkg3rxfvh0-home-manager-generation";
    newGenGcPath="$gcPath/current-home"

    local greatestGenNum
    greatestGenNum=$( \
        nix-env --list-generations --profile "$genProfilePath" \
            | tail -1 \
            | sed -E 's/ *([[:digit:]]+) .*/\1/')

    if [[ -n $greatestGenNum ]] ; then
        oldGenNum=$greatestGenNum
        newGenNum=$((oldGenNum + 1))
    else
        newGenNum=1
    fi

    if [[ -e $profilesPath/home-manager ]] ; then
        oldGenPath="$(readlink -e "$profilesPath/home-manager")"
    fi

    $VERBOSE_ECHO "Sanity checking oldGenNum and oldGenPath"
    if [[ -v oldGenNum && ! -v oldGenPath
            || ! -v oldGenNum && -v oldGenPath ]]; then
        errorEcho "Invalid profile number and current profile values! These"
        errorEcho "must be either both empty or both set but are now set to"
        errorEcho "    '${oldGenNum:-}' and '${oldGenPath:-}'"
        errorEcho "If you don't mind losing previous profile generations then"
        errorEcho "the easiest solution is probably to run"
        errorEcho "   rm $profilesPath/home-manager*"
        errorEcho "   rm $gcPath/current-home"
        errorEcho "and trying home-manager switch again. Good luck!"
        exit 1
    fi
}

if [[ -v VERBOSE ]]; then
    export VERBOSE_ECHO=echo
    export VERBOSE_ARG="--verbose"
else
    export VERBOSE_ECHO=true
    export VERBOSE_ARG=""
fi

echo "Starting home manager activation"

# Verify that we can connect to the Nix store and/or daemon. This will
# also create the necessary directories in profiles and gcroots.
$VERBOSE_ECHO "Sanity checking Nix"
nix-build --expr '{}' --no-out-link

setupVars

if [[ -v DRY_RUN ]] ; then
    echo "This is a dry run"
    export DRY_RUN_CMD=echo
else
    $VERBOSE_ECHO "This is a live run"
    export DRY_RUN_CMD=""
fi

if [[ -v VERBOSE ]]; then
    echo -n "Using Nix version: "
    nix-env --version
fi

$VERBOSE_ECHO "Activation variables:"
if [[ -v oldGenNum ]] ; then
    $VERBOSE_ECHO "  oldGenNum=$oldGenNum"
    $VERBOSE_ECHO "  oldGenPath=$oldGenPath"
else
    $VERBOSE_ECHO "  oldGenNum undefined (first run?)"
    $VERBOSE_ECHO "  oldGenPath undefined (first run?)"
fi
$VERBOSE_ECHO "  newGenPath=$newGenPath"
$VERBOSE_ECHO "  newGenNum=$newGenNum"
$VERBOSE_ECHO "  newGenGcPath=$newGenGcPath"
$VERBOSE_ECHO "  genProfilePath=$genProfilePath"


noteEcho Activating checkFilesChanged
function _cmp() {
  if [[ -d $1 && -d $2 ]]; then
    diff -rq "$1" "$2" &> /dev/null
  else
    cmp --quiet "$1" "$2"
  fi
}
declare -A changedFiles
unset -f _cmp


noteEcho Activating checkLinkTargets
function checkNewGenCollision() {
  local newGenFiles
  newGenFiles="$(readlink -e "$newGenPath/home-files")"
  find "$newGenFiles" \( -type f -or -type l \) \
      -exec bash /nix/store/0n6jw1z7z0pl7wf21srk4c48xq4jg1mb-check "$newGenFiles" {} +
}

checkNewGenCollision || exit 1


noteEcho Activating writeBoundary


noteEcho Activating installPackages
if nix-env -q | grep '^home-manager-path$'; then
  $DRY_RUN_CMD nix-env -e home-manager-path
fi


noteEcho Activating linkGeneration
function linkNewGen() {
  echo "Creating home file links in $HOME"

  local newGenFiles
  newGenFiles="$(readlink -e "$newGenPath/home-files")"
  find "$newGenFiles" \( -type f -or -type l \) \
    -exec bash /nix/store/n5kki1cd31p4iwbwhq7awpbbz8i87wns-link "$newGenFiles" {} +
}

function cleanOldGen() {
  if [[ ! -v oldGenPath ]] ; then
    return
  fi

  echo "Cleaning up orphan links from $HOME"

  local newGenFiles oldGenFiles
  newGenFiles="$(readlink -e "$newGenPath/home-files")"
  oldGenFiles="$(readlink -e "$oldGenPath/home-files")"

  # Apply the cleanup script on each leaf in the old
  # generation. The find command below will print the
  # relative path of the entry.
  find "$oldGenFiles" '(' -type f -or -type l ')' -printf '%P\0' \
    | xargs -0 bash /nix/store/zs0da64nls5z1m5s2rx7018hlnax3zlx-cleanup "$newGenFiles"
}

cleanOldGen

if [[ ! -v oldGenPath || "$oldGenPath" != "$newGenPath" ]] ; then
  echo "Creating profile generation $newGenNum"
  $DRY_RUN_CMD nix-env $VERBOSE_ARG --profile "$genProfilePath" --set "$newGenPath"
  $DRY_RUN_CMD ln -Tsf $VERBOSE_ARG "$newGenPath" "$newGenGcPath"
else
  echo "No change so reusing latest profile generation $oldGenNum"
fi

linkNewGen


noteEcho Activating onFilesChange


noteEcho Activating reloadSystemd
systemdStatus=$(XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} /nix/store/vnjzzjsj5jv37lxa2xll07wf3i2yjvdg-systemd-247.6/bin/systemctl --user is-system-running 2>&1 || true)

if [[ $systemdStatus == 'running' || $systemdStatus == 'degraded' ]]; then
  if [[ $systemdStatus == 'degraded' ]]; then
    warnEcho "The user systemd session is degraded:"
    XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} /nix/store/vnjzzjsj5jv37lxa2xll07wf3i2yjvdg-systemd-247.6/bin/systemctl --user --no-pager --state=failed
    warnEcho "Attempting to reload services anyway..."
  fi

  XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} \
    PATH=/nix/store/vnjzzjsj5jv37lxa2xll07wf3i2yjvdg-systemd-247.6/bin:$PATH \
bash /nix/store/lyvazadz3v9nck27nwcczqi4s9m402ix-systemd-activate.sh "${oldGenPath=}" "$newGenPath"

else
  echo "User systemd daemon not running. Skipping reload."
fi

unset systemdStatus



@colonelpanic8
Copy link
Contributor Author

This PR fixes the issue I was seeing, but I don't really feel like its the right fix: #2179

maybe someone who can understand a bit better what is going on can come up with a better fix.

@rycee
Copy link
Member

rycee commented Jul 19, 2021

Thanks for the report. I believe it should be safe to add the system's Nix package to the activation script PATH. Please give PR #2202 a try.

@rycee rycee closed this as completed in ddeeb03 Jul 23, 2021
@willcohen
Copy link

@rycee I'm running into this too on darwin, trying to switch my flake configuration to incorporate home-manager. Notably, the flake works on nixos, just not on mac. I'm still rather new to home-manager so I don't quite have a simple repro, but I can confirm that I've taken working "basic" flake configs for darwin that are confirmed to work on other people's machines and hitting it here, leading me to believe that it's either something that was funny in the circa-November-2021 Mac installer, or something I configured wrong on my path.

Either way, I'd love to know what I can provide or look into to help further pin down the issue.

berbiche added a commit to berbiche/home-manager that referenced this issue Feb 3, 2022
The fix for nix-community#2178
did not apply the patch to nix-darwin too.

See: nix-community#2178 (comment)
@berbiche
Copy link
Member

berbiche commented Feb 3, 2022

Hi @willcohen,

I have been using the fix in #2695 for awhile now and forgot to upstream it.

rycee pushed a commit that referenced this issue Feb 4, 2022
The fix for #2178
did not apply the patch to nix-darwin too.

See: #2178 (comment)
(cherry picked from commit 1fa809f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants