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
-
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.
-
The shell needs to be re-started to able to use the Nix commands.
Maybe justsource
ing 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 installerSynopsis
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.
nix-install.sh
(fictitious name!)The shell script at https://nixos.org/nix/install (let’s call it
nix-install.sh
) is built fromscripts/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”
scripts/install-nix-from-closure.sh
nix-install.sh
will download the latest Nix release, unpack the tarball, and it seems thatinstall-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.
--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.