2

I have a script that needs to be run on brand new virtual machines, but it depends on Nix being present, and I don't want to manually install it every time.

1

1 Answer 1

3
  1. Use the nixos.org installer with the --yes option.

    Examples:

    • nix.dev version:

      curl -L https://nixos.org/nix/install | sh -s -- --daemon --yes
      
    • nixos.org version

      sh <(curl -L https://nixos.org/nix/install) --daemon --yes
      
  2. Use the DeterminateSystems installer with the --no-confirm option.

    NOTE: Not tested
    Not sure how to use this option with

     curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
    

    but hoping to get to it at one point. See the documentation for other available flags / options.

This script seemed to do the trick for me:

#!/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

CAVEAT

The Nix installer uses sudo so a password will still have to be entered manually. (This is not an issue for me, as I'm just trying to save time, and not fully automate the process - at least for now. There are workarounds though on how to install Nix without root permissions.)


Frequently Asked Questions (FAQ)

1. Where are the Nix installer options documented?

They aren't documented. A pull request is forthcoming, and this NixOS Discourse thread lists all available options until then. (Also, here's a skeleton of a man page draft for the installer.)

2. What does the if .. then .. fi code snippet do?

Removing the distracting bits:

if [ -e 'path/nix-daemon.sh' ]; then
  . 'path/nix-daemon.sh'
fi

This Bash script checks if path/nix-daemon.sh exists (see -e Bash test), and if it does, then . path/nix-daemon.sh is executed (which is equivalent to source path/nix-daemon.sh; see more on . or source command).

Bash's if construct may be confusing. The basic syntax is

if ... ; then
  # TRUE branch
fi
if ... ; then
  # TRUE
else
  # FALSE
fi

so then does not imply branching, it's just part of the syntax. (See more here.)

3. What is the nix-deamon.sh script about?

There seems to be no official documentation for it at the time of this edit, but here's the gist ofNix issue #5848: nix-daemon.sh makes the Nix commands available in the current shell / terminal in case of multi-user Nix installations.

See the following for more on single-user / multi-user Nix:

3.1 "nix-daemon.sh does not exist on my machine"

You probably have a single-user Nix install in which case nix-profile.sh should be used instead of nix-daemon.sh. See section "2. What is the nix-deamon.sh script about?" above.

3
  • 1
    what is the nix-deamon.sh script about? It seems it is not existing on my machine. What does the then clause do in that case?
    – snaeil
    Commented Jun 11 at 7:39
  • 1
    @snaeil Updated the answer with answers to your questions.
    – toraritte
    Commented Jun 11 at 14:05
  • 1
    Thanks @toraritte !
    – snaeil
    Commented Jun 15 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .