Practical Functional Database: Nix and NixOS
April 6, 2007 — amutiaraNix is a purely functional package manager. It allows multiple versions of a package to be installed side-by-side, ensures that dependency specifications are complete, supports atomic upgrades and rollbacks, allows non-root users to install software, and has many other features.
Nix is a purely functional package manager. This means that it treats packages like values in purely functional programming languages such as Haskell — they are built by functions that don’t have side-effects, and they never change after they have been built. Nix stores packages in the Nix store, usually the directory /nix/store, where each package has its own unique subdirectory such as
/nix/store/r8vvq9kq18pz08v249h8my6r9vs7s0n3-firefox-2.0.0.1/
where r8vvq9kq… is a unique identifier for the package that captures all its dependencies (it’s a cryptographic hash of the package’s build dependency graph). This enables many powerful features.
It is the basis of the NixOS Linux distribution, but it can be used equally well under other Unix systems.
NixOS is a Linux distribution based on Nix, a purely functional package management system. NixOS is an experiment to see if we can build an operating system in which software packages, configuration files, boot scripts and the like are all managaed in a purely functional way, that is, they are all built by deterministic functions and they never change after they have been built. Such an operating system should have all the nice characteristics that the Nix package manager has:
- The entire system — kernel, system services, configuration files, etc. — is built by a Nix expression in a deterministic and repeatable way.
- Since configuration changes are non-destructive (they don’t overwrite existing files), you can easily roll back to a previous configuration. For instance, the Grub boot menu in NixOS allows the user to boot into any previous system configuration that hasn’t been garbage collected yet. This is very nice if something goes wrong.
- Upgrading a configuration is as safe as installing from scratch, since the realisation of a configuration is not stateful. This is a result of being purely functional.
- Multi-user package management — any user can install software through the same mechanisms that the administrator uses. This is not the case for most package managers such as RPM.
Furthermore,
- NixOS allows us to verify that Nix packages are really pure. Nix prevents undeclared dependencies between packages in the Nix store, but when you run Nix on an existing OS such as SUSE Linux, Nix cannot prevent a build script from (say) calling programs in /bin. If there is no /bin, such a build script will fail.
Here are links to:
- the Nix package manager webpage, and
- Eelco Dolstra’s PhD thesis, The Purely Functional Software Deployment Model.