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
sudoso 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.
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!
: 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`
-c security.nesting=true is needed, otherwise Nix won’t install in an LXC container; see Nix issue #5460.)
They are not documented; here’s a very sloppy draft PR. The gist is:
install- The Nix installer
COMMENT Yes, these are implementation details, not a description; will need to refine.
It provides the following options:
From a comment in the source: “Use this command-line option to fetch the tarballs using nar-serve or Cachix”
nix-install.shwill download the latest Nix release, unpack the tarball, and it seems that
install-nix-from-closure.shwill be called at one point. It provides the following options:
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.
Simple, single-user installation that does not require root and is trivial to uninstall. (default)
Run the script non-interactively, accepting all prompts.
Don’t add any channels.
nixpkgs-unstableis installed by default.
Don’t modify the user profile to automatically load nix.
Number of build users to create. Defaults to 32, if this option is not provided.
Path to nix.conf to prepend when installing
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
- 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.