Configuring NixOSThis chapter describes how to configure various aspects of a
NixOS machine through the configuration file
/etc/nixos/configuration.nix. As described in
, changes to that file only take
effect after you run nixos-rebuild.NetworkingSecure shell accessSecure shell (SSH) access to your machine can be enabled by
setting:
services.openssh.enable = true;
By default, root logins using a password are disallowed. They can be
disabled entirely by setting
services.openssh.permitRootLogin to
"no".You can declaratively specify authorised RSA/DSA public keys for
a user as follows:
users.extraUsers.alice.openssh.authorizedKeys.keys =
[ "ssh-dss AAAAB3NzaC1kc3MAAACBAPIkGWVEt4..." ];
IPv4 configurationBy default, NixOS uses DHCP (specifically,
(dhcpcd)) to automatically configure network
interfaces. However, you can configure an interface manually as
follows:
networking.interfaces.eth0 = { ipAddress = "192.168.1.2"; prefixLength = 24; };
(The network prefix can also be specified using the option
subnetMask,
e.g. "255.255.255.0", but this is deprecated.)
Typically you’ll also want to set a default gateway and set of name
servers:
networking.defaultGateway = "192.168.1.1";
networking.nameservers = [ "8.8.8.8" ];
Statically configured interfaces are set up by the systemd
service
interface-name-cfg.service.
The default gateway and name server configuration is performed by
network-setup.service.The host name is set using :
networking.hostName = "cartman";
The default host name is nixos. Set it to the
empty string ("") to allow the DHCP server to
provide the host name.IPv6 configurationIPv6 is enabled by default. Stateless address autoconfiguration
is used to automatically assign IPv6 addresses to all interfaces. You
can disable IPv6 support globally by setting:
networking.enableIPv6 = false;
FirewallNixOS has a simple stateful firewall that blocks incoming
connections and other unexpected packets. The firewall applies to
both IPv4 and IPv6 traffic. It can be enabled as follows:
networking.firewall.enable = true;
You can open specific TCP ports to the outside world:
networking.firewall.allowedTCPPorts = [ 80 443 ];
Note that TCP port 22 (ssh) is opened automatically if the SSH daemon
is enabled (). UDP
ports can be opened through
. Also of
interest is
networking.firewall.allowPing = true;
to allow the machine to respond to ping requests. (ICMPv6 pings are
always allowed.)Wireless networks
NixOS will start wpa_supplicant for you if you enable this setting:
networking.wireless.enable = true;
NixOS currently does not generate wpa_supplicant's
configuration file, /etc/wpa_supplicant.conf. You should edit this file
ourself to define wireless networks, WPA keys and so on (see
wpa_supplicant.conf(5)).
If you are using WPA2 the wpa_passphrase tool might be useful
to generate the wpa_supplicant.conf.
$ wpa_passphrase ESSID PSK > /etc/wpa_supplicant.conf
After you have edited the wpa_supplicant.conf,
you need to restart the wpa_supplicant service.
$ systemctl restart wpa_supplicant.serviceAd-hoc configurationYou can use to specify
shell commands to be run at the end of
network-setup.service. This is useful for doing
network configuration not covered by the existing NixOS modules. For
instance, to statically configure an IPv6 address:
networking.localCommands =
''
ip -6 addr add 2001:610:685:1::1/64 dev eth0
'';