|
|
| Line 26: |
Line 26: |
| === Declarative Configuration === | | === Declarative Configuration === |
|
| |
|
| One of NixOS's most distinguishing features is the ability to ''declaratively configure'' the whole system. This is done by specifying a configuration file which specifies the entire system state, including which packages should be installed and all the various system settings and options. This configuration file is normally located at <code>/etc/nixos/configuration.nix</code> (although another location may be specified using the environment variable <code>NIX_PATH</code>, or using <code>nixos-rebuild</code>'s <code>-I</code> option to override the <code><nixos></code> [https://nix.dev/manual/nix/latest/language/constructs/lookup-path.html#lookup-path lookup path]); after the configuration file is modified, the new configuration is then made active by running <code>nixos-rebuild switch</code>. The switch is atomic and can be rolled back if necessary. Most users keep the configuration files under <code>/etc/nixos</code> in a version control system such as Git. | | One of NixOS's defining features is its declarative configuration model, where the entire system state — including installed packages, system services, and settings — is described in configuration files. The primary file is typically located at <code>/etc/nixos/configuration.nix</code>. |
|
| |
|
| Conventional distributions require users to manually modify configuration files, but these changes are not tracked. If distributions change the default contents of configuration files, these changes often have to be manually merged by users if they have previously modified the file, or the distribution modifications may not be integrated at all, leading to undesired configuration drift. Configuration settings and changes are rarely recorded in a version control system. These shortcomings are often rectified after-the-fact if at all by configuration management solutions such as Puppet or Chef. These tools reconcile system configuration with a description of the expected state. However, these tools are not integrated into the operating system design and are simply layered on top, and OS configuration may still vary where an aspect of OS configuration has not been specified in the description of expected state.
| | Changes to the configuration are applied atomically using <code>nixos-rebuild switch</code>, ensuring reproducibility and the ability to roll back to previous states. Most users track their configuration files in a version control system, enabling consistent and portable system setups. These shortcomings are often rectified after-the-fact if at all by configuration management solutions such as Puppet, Ansible or Chef. These tools reconcile system configuration with a description of the expected state. However, these tools are not integrated into the operating system design and are simply layered on top, and OS configuration may still vary where an aspect of OS configuration has not been specified in the description of expected state. |
|
| |
|
| By comparison, NixOS's declarative configuration system provides a fully integrated facility for OS configuration management. Failure to specify any given item of configuration results in that item having a well-defined state, rather than being allowed to drift unmonitored. Because the full system configuration is captured in the NixOS configuration system, this also makes NixOS highly suited to the automatic deployment of configuration in environments such as automated server farms; tools such as [[NixOps]] make this easy.
| | Unlike conventional distributions, where system configuration is often scattered across manually edited files, NixOS integrates configuration management directly into the operating system. This eliminates configuration drift and makes NixOS particularly well-suited for automated, reproducible deployments. |
|
| |
|
| Here is a simple example of a NixOS system configuration:
| | For more details and examples on NixOS configurations, see [[NixOS system configuration]]. |
| | |
| {{file|/etc/nixos/configuration.nix|nix|<nowiki>
| |
| { config, pkgs, ... }:
| |
| | |
| {
| |
| # Import other configuration modules
| |
| # (hardware-configuration.nix is autogenerated upon installation)
| |
| # paths in nix expressions are always relative the file which defines them
| |
| imports = [
| |
| ./hardware-configuration.nix
| |
| ./my-dev-tools.nix
| |
| ./my-desktop-env.nix
| |
| ./etc.nix
| |
| ];
| |
| | |
| # Name your host machine
| |
| networking.hostName = "mymachine";
| |
| | |
| # Set your time zone.
| |
| time.timeZone = "Europe/Utrecht";
| |
| | |
| # Enter keyboard layout
| |
| services.xserver.layout = "us";
| |
| services.xserver.xkbVariant = "altgr-intl";
| |
| | |
| # Define user accounts
| |
| users.users.myuser = {
| |
| extraGroups = [ "wheel" "networkmanager" ];
| |
| isNormalUser = true;
| |
| };
| |
|
| |
| # Install some packages
| |
| environment.systemPackages = with pkgs; [
| |
| ddate
| |
| testdisk
| |
| ];
| |
|
| |
| # Enable the OpenSSH daemon
| |
| services.openssh.enable = true;
| |
|
| |
| }
| |
| </nowiki>}}
| |
| | |
| For inspiration, a variety of NixOS configuration files made by community members can be found in the [[Configuration Collection]].
| |
|
| |
|
| === Imperative Operations === | | === Imperative Operations === |