Jump to content

Flakes: Difference between revisions

From NixOS Wiki
imported>Mic92
Initial documentation for nix flakes
 
imported>Mic92
add nixos example
Line 8: Line 8:
This lock file describes provides hashes for sources and locks the revision
This lock file describes provides hashes for sources and locks the revision
of external version control system.
of external version control system.
Nix flakes intents to replace nix channels and the nix search path (NIX_PATH).
Nix flakes intents to replace nix channels and the nix search path (<code>NIX_PATH</code>).


== Installing nix flakes ==
== Installing nix flakes ==
Line 26: Line 26:
</syntaxHighlight>
</syntaxHighlight>


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


```
<syntaxHighlight>
experimental-features = nix-command flakes
experimental-features = nix-command flakes
```
</syntaxHighlight>
 
There is no official installer yet, but it is possible to download the latest snapshot nix from hydra
as shown in this [https://github.com/Mic92/dotfiles/blob/27a5b81964ecb8722796018a8c4ed7b149f05b96/.github/workflows/build.yml#L33 github action].
 
== Using nix flakes with NixOS ==
 
nixos-rebuild switch will reads its configuration from <code>/etc/nixos/flake.nix</code> if is present.
 
 
A basic nixos flake.nix could look like this:
 
<syntaxHighlight lang=nix>
{
  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 ];
    }
  };
}
</syntaxHighlight>


There is no official installer yet, but it is possible to download the latest snapshot nix from hydra:  
nixos-rebuild also allows to specify different flake using the <code>--flake</code> flag:


https://github.com/Mic92/dotfiles/blob/27a5b81964ecb8722796018a8c4ed7b149f05b96/.github/workflows/build.yml#L33
<syntaxHighlight lang=console>
$ sudo nixos-rebuild switch --flake '.#'
</syntaxHighlight>
 
By default nixos-rebuild will use the currents system hostname to lookup the right nixos configuration in <code>nixosConfigurations</code>. You can also override this by using appending it to the flake parameter:
 
<syntaxHighlight lang=console>
$ sudo nixos-rebuild switch --flake '/etc/nixos#joes-desktop'
</syntaxHighlight>

Revision as of 09:13, 25 July 2020

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'