Home Explore Blog Models CI



nixpkgs

1st chunk of `nixos/doc/manual/configuration/config-file.section.md`
fc4d7da68f4353af075c315caa879762868bf349f2446e6600000001000009f3
# NixOS Configuration File {#sec-configuration-file}

The NixOS configuration file generally looks like this:

```nix
{ config, pkgs, ... }:

{
  # option definitions
}
```

The first line (`{ config, pkgs, ... }:`) denotes that this is actually
a function that takes at least the two arguments `config` and `pkgs`.
(These are explained later, in chapter [](#sec-writing-modules)) The
function returns a *set* of option definitions (`{ ... }`).
These definitions have the form `name = value`, where `name` is the
name of an option and `value` is its value. For example,

```nix
{ config, pkgs, ... }:

{
  services.httpd.enable = true;
  services.httpd.adminAddr = "alice@example.org";
  services.httpd.virtualHosts.localhost.documentRoot = "/webroot";
}
```

defines a configuration with three option definitions that together
enable the Apache HTTP Server with `/webroot` as the document root.

Sets can be nested, and in fact dots in option names are shorthand for
defining a set containing another set. For instance,
[](#opt-services.httpd.enable) defines a set named
`services` that contains a set named `httpd`, which in turn contains an
option definition named `enable` with value `true`. This means that the
example above can also be written as:

```nix
{ config, pkgs, ... }:

{
  services = {
    httpd = {
      enable = true;
      adminAddr = "alice@example.org";
      virtualHosts = {
        localhost = {
          documentRoot = "/webroot";
        };
      };
    };
  };
}
```

which may be more convenient if you have lots of option definitions that
share the same prefix (such as `services.httpd`).

NixOS checks your option definitions for correctness. For instance, if
you try to define an option that doesn't exist (that is, doesn't have a
corresponding *option declaration*), `nixos-rebuild` will give an error
like:

```plain
The option `services.httpd.enable' defined in `/etc/nixos/configuration.nix' does not exist.
```

Likewise, values in option definitions must have a correct type. For
instance, `services.httpd.enable` must be a Boolean (`true` or `false`).
Trying to give it a value of another type, such as a string, will cause
an error:

```plain
The option value `services.httpd.enable' in `/etc/nixos/configuration.nix' is not a boolean.
```

Options have various types of values. The most important are:

Strings

:   Strings are enclosed in double quotes, e.g.

    ```nix
    {
      networking.hostName = "dexter";
    }
    ```

    Special characters can be escaped by prefixing them with a backslash

Title: NixOS Configuration File Structure and Basic Syntax
Summary
This chunk describes the fundamental structure of a NixOS configuration file, which is a Nix function that takes arguments like `config` and `pkgs` and returns a set of option definitions. It explains how options are defined using `name = value` pairs, demonstrated with an example for configuring Apache HTTP Server. The text clarifies that dots in option names serve as shorthand for nested sets, offering an alternative, more organized way to structure definitions. It also highlights NixOS's robust error checking, which validates both the existence of options and the correctness of their assigned value types, preventing configuration mistakes. Finally, it begins to detail common option value types, starting with strings.