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.

@char16t
Copy link

char16t commented Jul 14, 2017

Try to install and run packages like a openconnect, unetbootin or any package that requires root rights. As I seen it works incorrect.

@Mic92
Copy link

Mic92 commented Oct 6, 2017

@char16t use sudo -E

Copy link

ghost commented Nov 2, 2017

centos packages:

No package build-essential available.

  • gcc gcc-c++ make openssl-devel

No package pkg-config available.

  • pkgconfig

No package autotools-dev available.

  • ?, seems like autoconf and automake are already there

No package libssl-dev available.

  • openssl-devel (above)

No package libbz2-dev available.

  • bzip2-devel

No package libsqlite3-dev available.

  • libsqlite3x-devel

No package libcurl4-openssl-dev available.

  • curl-devel - probably right ?

No package liblzma-dev available.

  • xz-devel

No package libgc-dev available.

  • libgc-devel

No package libdbi-perl available.
No package libdbd-sqlite3-perl available.
No package libwww-curl-perl available.

  • perl-DBI
  • perl-DBD-SQLite
  • perl-WWW-Curl

---- failed on strange extern.h error
add perl-devel
----- if using later 1.11.15
add libseccomp-devel
---- need wget generally

@makefu
Copy link

makefu commented Nov 10, 2017

Hey @joepie91 i've mirrored your great post to https://nixos.wiki/wiki/Install_Nix_in_multi-user_mode_on_non-NixOS , that way we can maintain changes in dependencies or even add build deps for other platforms (centos for example)

@infinity0
Copy link

@sajith
Copy link

sajith commented Jan 1, 2020

What is in /usr/local/etc/profile.d/nix.sh, and where does that come from?

@scarf005
Copy link

image

@makefu the link is empty. could you take a look?

@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