Skip to content

Instantly share code, notes, and snippets.

@derekmahar
Last active September 17, 2025 19:02
Show Gist options
  • Select an option

  • Save derekmahar/e1654fc78c1047aa1feb938c9df3af19 to your computer and use it in GitHub Desktop.

Select an option

Save derekmahar/e1654fc78c1047aa1feb938c9df3af19 to your computer and use it in GitHub Desktop.
How to Install Nix in an LXD Container

How to Install Nix in an LXD Container

Steps to install the Nix package manager inside an Ubuntu 24.04 Incus container using the images:ubuntu/noble image:

  1. On the Incus host, create an Ubuntu 24.04 container:
    lxc init images:ubuntu/noble container1
    
  2. On the Incus host, enable nested security on the container:
    incus config set container1 security.nesting true
    
  3. Start the container:
    incus start container1
    
  4. Inside the container, install packages curl, gnupg2, man, rsync, and xz-utils:
    incus exec container1 -- apt install --yes curl gnupg2 man-db rsync xz-utils
    
  5. Inside the container, install Nix as user ubuntu because root may not perform a single-user Nix installation:
    incus exec $container -- sudo --user ubuntu --login sh -c "curl --location --silent https://nixos.org/nix/install | sh"
    
#!/bin/sh
# Installs the Nix package manager (https://nixos.org/nix/) inside an Ubuntu 24.04
# Incus container.
#set -o xtrace
image=images:ubuntu/noble
if [ "$#" -eq 1 ]
then
container=$1
# Create a container from the given image and assign it the name from the
# script argument.
incus init $image $container > /dev/null
else
# Create a container from the given image and extract the random container
# name that Incus assigns.
container=`Incus init $image | grep "Instance name" | sed 's/^.* \(.*\)$/\1/'`
fi
echo "Created container $container."
# Enable nested security on the container.
# See https://github.com/NixOS/nix/issues/2649#issuecomment-518045796.
incus config set $container security.nesting true
# Start the container.
incus start $container
# Inside the container, install packages curl, gnupg2, man, rsync, and xz-utils.
incus exec $container -- apt install --yes curl gnupg2 man-db rsync xz-utils
# Inside the container, install the Nix package manager as user "ubuntu".
# (see https://discuss.linuxcontainers.org/t/useful-lxc-command-aliases/2547/4)
# because root may not perform a single-user Nix installation
# (see https://github.com/NixOS/nix/issues/1559).
incus exec $container -- sudo --user ubuntu --login sh -c "curl --location --silent https://nixos.org/nix/install | sh"
# Stop the container.
incus stop $container
@almereyda
Copy link

One could add xz-utils to the install job for Ubuntu 22.04 hosts.

@derekmahar
Copy link
Author

derekmahar commented Aug 10, 2022

Does the installation script fail on Ubuntu 22.04 containers?

@derekmahar
Copy link
Author

Does the installation script fail on Ubuntu 22.04 containers?

Yes, the script does fail when it attempts to install Nix:

$ lxc exec witty-koi -- sudo --user ubuntu --login sh -c "curl --location --silent https://nixos.org/nix/install | sh"
sh: you do not have 'xz' installed, which I need to unpack the binary tarball

@derekmahar
Copy link
Author

I added package xz-utils to the list of packages that the installation script installs inside the container.

@almereyda
Copy link

Very well.

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