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 Answer
Use the nixos.org installer with the
--yes
option.Examples:
Use the DeterminateSystems installer with the
--no-confirm
option.NOTE: Not tested
Not sure how to use this option withcurl --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:
- [
Nix reference manual
] 3. Installation - [
Nix reference manual
] 3.5 Security - [
Nix reference manual
] 3.5.2 Multi-user Mode (a.k.a. "What is the Nix daemon?) - [
NixOS discourse
] What are the specific differences between (and perhaps use cases for) single-user and multi-user Nix installations?
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.
-
1what is the
nix-deamon.sh
script about? It seems it is not existing on my machine. What does thethen
clause do in that case?– snaeilCommented Jun 11 at 7:39 -
1
-
1