Nixos-install fails in container because it can't set host name

I’m in an alpine docker container that has Nix installed, nixos/nix:2.3.15. I want to create a NixOS container.

When I try to run nixos-install, I get an error:

# PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt
building the configuration in /mnt/etc/nixos/configuration.nix...
these derivations will be built:
  /nix/store/01kkz7nkava65h9kjb277qw8j2zj08yc-etc-nscd.conf.drv
  /nix/store/021racdn3dhlbwamdzaa3di8c2n4lsaa-etc-logind.conf.drv
<snip>
copying path '/nix/store/41rzlfrq0fz9kf9kwjli5q7j9vcgrfrw-kbd-2.4.0' from 'https://cache.nixos.org'...
error: while setting up the build environment: cannot set host name: Operation not permitted

I suspect you’re not allowed to set the hostname in a container for security reasons. I tried to work around this by setting the hostname in /mnt/etc/nixos/configuration.nix to match the container’s hostname, but I get the same error. Is there a workaround for this scenario?

1 Like

I also tried setting the hostname to "", to obtain it from a DHCP server, but still got the error. I don’t know if the host machine provides DHCP to containers.

You can try to disable the build sandbox (--option sandbox false). Probably overkill but should work I think.

Like this?

PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --option sandbox false --root /mnt

I still get the same error, unfortunately.