Subject: bug#42983: "sudo -E guix pull" breaks ~/.config/guix/current for regular user

From: Danny Milosavljevic <[email-masked]>
Date: Mon, 24 Aug 2020 02:11:13 +0200

On Sat, 22 Aug 2020 12:27:50 +0200
Danny Milosavljevic <[email-masked]> wrote:

> /var/guix/profiles/per-user/dannym/current-guix

Follow-up errors (translated to English manually):

 dannym@dayas ~$ guix pull
Migrating profile generations to „/var/guix/profiles/per-user/dannym“ …
guix pull: error: symlink: file exists: "/var/guix/profiles/per-user/dannym/current-guix"
1 dannym@dayas ~$ rm ~/.config/guix/current
 dannym@dayas ~$ cd .config/guix/
 dannym@dayas ~/.config/guix$ ln -s /var/guix/profiles/per-user/dannym/current-guix current
 dannym@dayas ~/.config/guix$ guix pull
Refreshing channel „guix“ from Git-Repository „https://git.savannah.gnu.org/git/guix.git“ …
guix pull: error: Git-Error: failed open - '/home/dannym/.cache/guix/checkouts/pjmkglp4t7znuugeurpurzikxq3tnlaywmisyr27shj7apsnalwq/.git/FETCH_HEAD' is locked: Permission denied

That's some weird guix pull state.

The culprit, I think, is this:

(define (ensure-default-profile)
  (ensure-profile-directory)

  ;; In 0.15.0+ we'd create ~/.config/guix/current-[0-9]*-link symlinks.  Move
  ;; them to %PROFILE-DIRECTORY.
  ;;
  ;; XXX: Ubuntu's 'sudo' preserves $HOME by default, and thus the second
  ;; condition below is always false when one runs "sudo guix pull".  As a
  ;; workaround, skip this code when $SUDO_USER is set.  See
  ;; <https://bugs.gnu.org/36785>.
  (unless (or (getenv "SUDO_USER")
              (string=? %profile-directory
                        (dirname
                         (canonicalize-profile %user-profile-directory))))
    (migrate-generations %user-profile-directory %profile-directory))

where

  %profile-directory = "/var/guix/profiles/per-user/dannym"
  %user-profile-directory = "/home/dannym/.config/guix/current" (which is a
symlink to /var/guix/profiles/per-user/root/current-guix)