Jump to content

Flakes

From NixOS Wiki
Revision as of 09:13, 25 July 2020 by imported>Mic92 (add nixos example)

Nix flakes is some upcoming feature in the Nix package manager. It allows to download nix expressions from other sources in a declarative way by specifying them a flake.nix file. Those sources are called flakes and also have a flake.nix where they can describe their own dependencies. Sources can be tarballs, git, local directories or mercurial repositories. It makes evaluation reproducible with providing a lock called flake.lock. This lock file describes provides hashes for sources and locks the revision of external version control system. Nix flakes intents to replace nix channels and the nix search path (NIX_PATH).

Installing nix flakes

Right now nix flakes are only available in the unstable nix version and need to be enabled in nix.conf as well. In NixOS this can be achieved with the following line in configuration.nix

{ pkgs, ... }: {
   nix = {
    package = pkgs.nixUnstable;
    extraOptions = ''
      experimental-features = nix-command flakes
    '';
   };
}

On non-nixos system install `nixUnstable` in your environment and add these lines to your /etc/nix/nix.conf:

experimental-features = nix-command flakes

There is no official installer yet, but it is possible to download the latest snapshot nix from hydra as shown in this github action.

Using nix flakes with NixOS

nixos-rebuild switch will reads its configuration from /etc/nixos/flake.nix if is present.


A basic nixos flake.nix could look like this:

{
  description = "NixOS configuration with flakes";
  outputs = { self, nixpkgs }: {
     # replace 'joes-desktop' with your hostname here.
     nixosConfigurations.joes-desktop = nixpkgs.lib.nixosSystem {
       system = "x86_64-linux";
       modules = [ ./configuration.nix ];
     }
  };
}

nixos-rebuild also allows to specify different flake using the --flake flag:

$ sudo nixos-rebuild switch --flake '.#'

By default nixos-rebuild will use the currents system hostname to lookup the right nixos configuration in nixosConfigurations. You can also override this by using appending it to the flake parameter:

$ sudo nixos-rebuild switch --flake '/etc/nixos#joes-desktop'