How to run the Nix installer silently from a shell script?

I rely heavily on Nix in my scripts, and I figured that instead of installing it manually every time on system where it is not present, I could just add a conditional clause to do just that. Found the installer’s --yes option to be useful (see notes at the bottom), but

  1. The Nix installer uses sudo so a password will still have to be entered manually.

    There are workarounds on how to install Nix without root permissions, but this is not really an issue for me as the scripts don’t have to be fully automated - they are only for saving time at the moment.

  2. The shell needs to be re-started to able to use the Nix commands.

    Maybe just sourceing the shell profile would suffice?

    edit: Nope. The script below works like a charm1:

    #!/usr/bin/env bash
    
    curl -L https://nixos.org/nix/install | sh -s -- --daemon --yes
    
    if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
      . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
    fi
    
    # Testing whether Nix is available in subsequent commands
    nix --version
    

An obvious solution would be to just use the Linux distros package manager, but then I work on many different ones. Or is there another obvious solution I’m missing? Thanks!


[1]: On Ubuntu, at least. Tried it in an LXC container using the following commands:

launch -c security.nesting=true ubuntu:22.04 ubuntu2204
lxc exec ubuntu2204 -- adduser toraritte
lxc exec ubuntu2204 -- adduser toraritte sudo
lxc exec ubuntu2204 -- sudo --login --user toraritte
# then put the script in a file and ran it with `source`

(The option -c security.nesting=true is needed, otherwise Nix won’t install in an LXC container; see Nix issue #5460.)


Notes: Where are the Nix installer options documented?

They are not documented; here’s a very sloppy draft PR. The gist is:

Name

install - The Nix installer

Synopsis

install {--daemon | --no-daemon | --yes | --no-channel-add | --no-modify-profile | -daemon-user-count [number] | --nix-extra-conf-file | --tarball-url-prefix [???] }

Description

COMMENT Yes, these are implementation details, not a description; will need to refine.

  1. nix-install.sh (fictitious name!)

    The shell script at https://nixos.org/nix/install (let’s call it nix-install.sh) is built from scripts/install.in.

    It provides the following options:

    • --tarball-url-prefix

      From a comment in the source: “Use this command-line option to fetch the tarballs using nar-serve or Cachix

  2. scripts/install-nix-from-closure.sh

    nix-install.sh will download the latest Nix release, unpack the tarball, and it seems that install-nix-from-closure.sh will be called at one point. It provides the following options:

    • --daemon

      Installs and configures a background daemon that manages the store, providing multi-user support and better isolation for local builds. Both for security and reproducibility, this method is recommended if supported on your platform.

      See Installing a Binary Distribution - Nix Reference Manual

    • --no-daemon

      Simple, single-user installation that does not require root and is trivial to uninstall. (default)

    • --yes

      Run the script non-interactively, accepting all prompts.

    • --no-channel-add

      Don’t add any channels. nixpkgs-unstable is installed by default.

    • --no-modify-profile

      Don’t modify the user profile to automatically load nix.

    • --daemon-user-count

      Number of build users to create. Defaults to 32, if this option is not provided.

    • --nix-extra-conf-file

      Path to nix.conf to prepend when installing /etc/nix/nix.conf

COMMENT Things to note:

  • The install script is not provided by Nix (the application) but by the Nix project as a convencience to deploy the Nix application (hence it will never show up in the Nix commands.)
  • It is used indirectly most of the time via curl, wget, etc.
  • Building the Nix project repo will yield an install script that can be used directly.
  • Some aspects of the install script cannot be modified using options / flags, but only by editing its source (as noted at several places in the Installation section). Corollary: Document that it is a two-stage process.
1 Like

They are not, feel free to open a PR. I’ll prioritise this because it’s relevant for onboarding.

1 Like

Still not documented?

In fact it is now, at least indirectly. I still have to bump the latest manual version to 2.20, but it should be visible in the latest build (generally found here: Nix reference manual — nix.dev documentation).

The end of the first section here shows to invoke --help and where to find the source to identify the environment variables used: Installing a Binary Distribution - Nix Reference Manual

It’s not great yet, because you still have to dig into it manually, but I don’t have the capacity to maintain duplicate information. Also it’s currently not part of my priorities to add automation to render that particular stuff to the manual.

Again, contributions welcome! It wouldn’t be super hard to do, just takes time to figure it out and make it work smoothly. Would be a valuable addition!

1 Like