nix-community/nix-on-droid

accidentally nix profile removed nix-on-droid

Closed this issue · 5 comments

because of doing that, I can't access any of the nix commands, nor does any of the mentioned methods of restoring the nix system help (like the one in the wiki), the reason seems to be that i am missing the "/data/data/com.termux.nix/files/home/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh" file and i am not quite sure how to bring it back

Try running /nix/var/nix/profiles/nix-on-droid/activate or /nix/var/nix/profiles/nix-on-droid/nix-on-droid-path/bin/nix-on-droid switch.

reports sh: /nix/var/nix/profiles/nix-on-droid/nix-on-droid-path/bin/nix-on-droid: inaccessible or not found for both

as of right now i have resorted to manually going through the commands that are ran by the bootstrap, after manually executing the contents of the login, i reached the

echo "Installing first Nix-on-Droid generation..."
    /nix/store/jqn2c18sd10aq7lddnbc3bqz0wr8gkam-nix-2.20.5/bin/nix --extra-experimental-features 'flakes nix-command' run github:nix-community/nix-on-droid/master -- switch --flake /data/data/com.termux.nix/files/home/.config/nix-on-droid

part (right before sourcing nix-on-droid-session-init, i presume that command will create the session init file so that i can source it), but after running that it gets stuck for a while at Building activation package... and it's been there for the good part of 20 minutes. any ideas?

would someone here who has the nix-on-droid-session-init script be able to send it in this thread? i think if i just save it i wouldn't need to run through the whole bootstrapping and whenever i mess up my env i could always just paste it in there...

Can you explain what exactly you did to your nix-on-droid installation? There should be a nix-on-droid profile in /nix/var/nix/profiles/nix-on-droid/.

Running your mentioned nix run ... should just work, it may take some time (comparable to the time it took to initially bootstrap nix-on-droid). There should also be the nix-on-droid cachix cache configured in /etc/nix/nix.conf, that is necessary to build any nix-on-droid generations.

After I ran the switch command, it said there was some sort of duplicate packages and that i should pick one and uninstall or give higher priority to the other. I think it was something about the [ command at first, but when I nix profile removed it, it turns out I removed nix-on-droid entirely.

Also, that command actually completely hanged on that environment. I kept it for about 2 hours but it still did nothing-- reinstalling Nix-On-Droid took about 10 minutes or less, for what it's worth.

Anyways, I figured out a few things but I gave up in the end and reinstalled.
The few things being: firstly, here's nix-om-droid-session-init.sh:

# Only source this once.
[ -n "$__NOD_SESS_INIT_SOURCED" ] && return
export __NOD_SESS_INIT_SOURCED=1

. "/data/data/com.termux.nix/files/home/profiled_nix.sh"

# workaround for nix 2.4, see https://github.com/NixOS/nixpkgs/issues/149791
if [[ ":$NIX_PATH:" != *":/data/data/com.termux.nix/files/home/.nix-defexpr/channels:"* ]]; then
      export NIX_PATH="nixpkgs=/data/data/com.termux.nix/files/home/.nix-defexpr/channels/''${NIX_PATH:+:}$NIX_PATH"
fi



# Workaround for https://github.com/NixOS/nix/issues/1865
if [[ ":$NIX_PATH:" != *":nixpkgs=/data/data/com.termux.nix/files/home/.nix-defexpr/channels/nixpkgs/:"* ]]; then
      export NIX_PATH="nixpkgs=/data/data/com.termux.nix/files/home/.nix-defexpr/channels/nixpkgs/''${NIX_PATH:+:}$NIX_PATH"
fi

(do note, the profiled_nix.sh file is the next file under the nix.sh section, but written to $HOME because i have permission to write there)
secondly, after that, it seems ~/.nix-profile/etc/profile.d/nix.sh is required, but the removal also removed that file, which happens to be

nix.sh
if [ -n "$HOME" ] && [ -n "$USER" ]; then

    # Set up the per-user profile.

    NIX_LINK="$HOME/.nix-profile"
    if [ -n "${XDG_STATE_HOME-}" ]; then
        NIX_LINK_NEW="$XDG_STATE_HOME/nix/profile"
    else
        NIX_LINK_NEW="$HOME/.local/state/nix/profile"
    fi
    if [ -e "$NIX_LINK_NEW" ]; then
        NIX_LINK="$NIX_LINK_NEW"
    else
        if [ -t 2 ] && [ -e "$NIX_LINK_NEW" ]; then
            warning="\033[1;35mwarning:\033[0m"
            printf "$warning Both %s and legacy %s exist; using the latter.\n" "$NIX_LINK_NEW" "$NIX_LINK" 1>&2
            if [ "$(realpath "$NIX_LINK")" = "$(realpath "$NIX_LINK_NEW")" ]; then
                printf "         Since the profiles match, you can safely delete either of them.\n" 1>&2
            else
                # This should be an exceptionally rare occasion: the only way to get it would be to
                # 1. Update to newer Nix;
                # 2. Remove .nix-profile;
                # 3. Set the $NIX_LINK_NEW to something other than the default user profile;
                # 4. Roll back to older Nix.
                # If someone did all that, they can probably figure out how to migrate the profile.
                printf "$warning Profiles do not match. You should manually migrate from %s to %s.\n" "$NIX_LINK" "$NIX_LINK_NEW" 1>&2
            fi
        fi
    fi

    # Set up environment.
    # This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix
    export NIX_PROFILES="/nix/var/nix/profiles/default $NIX_LINK"

    # Populate bash completions, .desktop files, etc
    if [ -z "${XDG_DATA_DIRS-}" ]; then
        # According to XDG spec the default is /usr/local/share:/usr/share, don't set something that prevents that default
        export XDG_DATA_DIRS="/usr/local/share:/usr/share:$NIX_LINK/share:/nix/var/nix/profiles/default/share"
    else
        export XDG_DATA_DIRS="$XDG_DATA_DIRS:$NIX_LINK/share:/nix/var/nix/profiles/default/share"
    fi

    # Set $NIX_SSL_CERT_FILE so that Nixpkgs applications like curl work.
    if [ -e /etc/ssl/certs/ca-certificates.crt ]; then # NixOS, Ubuntu, Debian, Gentoo, Arch
        export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
    elif [ -e /etc/ssl/ca-bundle.pem ]; then # openSUSE Tumbleweed
        export NIX_SSL_CERT_FILE=/etc/ssl/ca-bundle.pem
    elif [ -e /etc/ssl/certs/ca-bundle.crt ]; then # Old NixOS
        export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
    elif [ -e /etc/pki/tls/certs/ca-bundle.crt ]; then # Fedora, CentOS
        export NIX_SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
    elif [ -e "$NIX_LINK/etc/ssl/certs/ca-bundle.crt" ]; then # fall back to cacert in Nix profile
        export NIX_SSL_CERT_FILE="$NIX_LINK/etc/ssl/certs/ca-bundle.crt"
    elif [ -e "$NIX_LINK/etc/ca-bundle.crt" ]; then # old cacert in Nix profile
        export NIX_SSL_CERT_FILE="$NIX_LINK/etc/ca-bundle.crt"
    fi

    # Only use MANPATH if it is already set. In general `man` will just simply
    # pick up `.nix-profile/share/man` because is it close to `.nix-profile/bin`
    # which is in the $PATH. For more info, run `manpath -d`.
    if [ -n "${MANPATH-}" ]; then
        export MANPATH="$NIX_LINK/share/man:$MANPATH"
    fi

    export PATH="$NIX_LINK/bin:$PATH"
    unset NIX_LINK NIX_LINK_NEW
fi

But even if you have that and launch a shell, you will not be able to execute anything without long paths, like /nix/store/jqnxxxxxxxxx-nix/bin/nix run nixpkgs#ls. I do not know why.
That is as far as I went before giving up on it, hopefully it's helpful for anyone else.

Thank you Gerschtli for your reply, as well.