skip to Main Content

The initial problem: Install the nix package manager then install other programs (e. g. lua) using the nix by launching a bash script only once.

The solution: I have written the following bash script install.sh:

#!/bin/bash

install_nix() (
        echo "installing nix..."
        echo ""

        curl --location https://releases.nixos.org/nix/nix-2.18.1/install | sh -s -- --daemon

        echo ""
)

if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        install_nix
fi

nix-env --install --attr nixpkgs.lua

The problem: the script results in the following:

./install.sh: line 18: nix-env: command not found

because the nix package manager requires the shell restart after installation

—- Reminders —————————————————————–

[ 1 ]

Nix won’t work in active shell sessions until you restart them.

So what do I want: somehow restart the shell inside the script and continue the execution from there so I do not need to restart the shell and launch the script one more time manually

What have I tried to solve the problem:

1 exec bash

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        install_nix

        exec bash
fi
...

2 exec $0 $@

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        install_nix

        exec @0 $@
fi
...

3 exec bash "$0" "$@"

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        install_nix

        exec bash @0 $@
fi
...

4 subshell

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        ( install_nix )
fi
...

5 command substitution $()

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        $(install_nix)
fi
...

6 command substitution “

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        `install_nix`
fi
...

7 just bash

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        bash install_nix
fi
...

8 bash + source

...
if ! command -v nix-env &> /dev/null; then
        echo "nix is not installed"

        new_shell = $(bash)
        source install_nix
        disown $new_shell
fi
...

It is all happening inside WSL Ubuntu 22.04.2 LTS

So the question is: how to somehow restart the shell inside the script and continue the execution from there so it does not require restarting the shell and launching the script one more time manually?

2

Answers


  1. You can restart the script, not bash

    exec "$0" "$@"
    

    and inside your script detect different conditions (i.e. nix already installed).

    Login or Signup to reply.
  2. source /etc/profile && nix-env --install --attr nixpkgs.lua

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search