Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active March 17, 2023 18:42
Show Gist options
  • Save joepie91/043a51a7b70be5f50f1d to your computer and use it in GitHub Desktop.
Save joepie91/043a51a7b70be5f50f1d to your computer and use it in GitHub Desktop.
Nix in multi-user mode on a non-NixOS (eg. Debian) system

This post is deprecated!

Its contents have been moved to the NixOS wiki here, where they are being kept up to date. Please follow the instructions there instead!

The original post is below for posterity.

 

 

 

 

 

 

 

 

Original post

This is an installation walkthrough for the Nix package manager in multi-user mode, on a non-NixOS system. While the walkthrough focuses on Debian, instructions on different platforms should be similar.

1. Install dependencies.

For recent Debian:

apt-get install build-essential pkg-config autotools-dev dh-autoreconf libssl-dev libbz2-dev libsqlite3-dev libcurl4-openssl-dev liblzma-dev libgc-dev libdbi-perl libdbd-sqlite3-perl libwww-curl-perl libxml2 libxslt-dev

For other distributions, look for the equivalent packages.

2. Set up build users.

groupadd -r nixbld
for n in $(seq 1 10); do useradd -c "Nix build user $n" \
    -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" \
    nixbld$n; done

3. Install Nix.

wget http://nixos.org/releases/nix/nix-1.11.2/nix-1.11.2.tar.xz
tar -xvf nix-1.11.2.tar.xz
cd nix-1.11.2/
./configure --enable-gc
make -j 2
make install

If you have more than two CPU cores, you might want to increase the value of the -j flag for faster compilation.

4. Create a systemd unit file, for managing the Nix daemon.

Save this as /etc/systemd/system/nix.service:

[Unit]
Description=Nix daemon

[Service]
EnvironmentFile=-/etc/default/nix
ExecStart=/usr/local/bin/nix-daemon $EXTRA_OPTS
IgnoreSIGPIPE=false
KillMode=process

[Install]
WantedBy=multi-user.target

Create an empty /etc/default/nix:

touch /etc/default/nix

Enable and start the service:

systemctl enable nix
systemctl start nix

5. Set up user configuration

Source the following in your /root/.bashrc, either directly or indirectly:

nix-setup-user() {
        TARGET_USER="$1"
        SYMLINK_PATH="/home/$TARGET_USER/.nix-profile"
        PROFILE_DIR="/nix/var/nix/profiles/per-user/$TARGET_USER"

        echo "Creating profile $PROFILE_DIR..."
        echo "Profile symlink: $SYMLINK_PATH"

        rm "$SYMLINK_PATH"
        mkdir -p "$PROFILE_DIR"
        chown "$TARGET_USER:$TARGET_USER" "$PROFILE_DIR"
        
        ln -s "$PROFILE_DIR/profile" "$SYMLINK_PATH"
        chown -h "$TARGET_USER:$TARGET_USER" "$SYMLINK_PATH"
        
        echo "export NIX_REMOTE=daemon" >> "/home/$TARGET_USER/.bashrc"
        echo ". /usr/local/etc/profile.d/nix.sh" >> "/home/$TARGET_USER/.bashrc"
        
        su -lc "cd; . /usr/local/etc/profile.d/nix.sh; NIX_REMOTE=daemon nix-channel --update" "$TARGET_USER"
}

Now, whenever you create a new user - say, joepie91, you can simply do something like the following:

nix-setup-user joepie91

... and a few minutes later, joepie91 will be able to log in, and use Nix. Repeat for each user that needs access to Nix.

@makefu
Copy link

makefu commented Mar 17, 2023

@scarf005 the post is ~6 years old, now easier solutions are available to install nix:

  1. the official installer: https://nixos.org/download.html - the installer allows multi-user install on non-nixos systems, the https://devenv.sh/getting-started/ documentation may help here as well
  2. the nix-installer as described in https://zero-to-nix.com/start/install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment