Run a nix container with podman, with the container destination mounted as /mnt
:
DEST=~/containers/nixos
mkdir -p $DEST/{dev,proc,etc/nixos}
podman run -v $DEST:/mnt -it --rm --cap-add SYS_ADMIN docker.io/nixos/nix:latest
Note: you can use
docker run --privileged …
instead ofpodman run …
)
Setup the channel for nixos-23.05
nix-channel --add https://nixos.org/channels/nixos-23.05 nixpkgs
nix-channel --update
nix-channel --list
Install the nixos install tools:
nix-env -f '<nixpkgs>' -iA nixos-install-tools util-linux
Copy the configuration.nix
file and run the installation:
cat > /mnt/etc/nixos/configuration.nix
…see below…
<Ctrl-D>
# workaround for error: while setting up the build environment: mounting /proc: Operation not permitted «
mount --bind /proc/ /mnt/proc
mount --bind /dev/ /mnt/dev
nixos-install --root /mnt
Let's "boot" the container:
sudo systemd-nspawn --boot --network-veth --directory $DEST
…
# poweroff
System updates:
nixos-rebuild boot --upgrade
Release upgrade:
nix-channel --add https://nixos.org/channels/nixos-22.05 nixos
nix-channel --update
nixos-rebuild boot --upgrade
Move the container directory to /var/lib/machines/nixos
. Make the following file:
# /etc/systemd/nspawn/nixos.nspawn
[Exec]
Boot=yes
ResolvConf=off
# LinkJournal=try-guest
[Network]
Bridge=bridge0
Then run machinectl start nixos
to run it, or machinectl enable nixos
to make it run on boot. Use machinectl shell nixos
to enter the container. I my use-case I bridge the container with my LAN so it's ssh accessible from any local computer.