Flakes: Difference between revisions

Pigs (talk | contribs)
m add citation to lock file and showing flake inputs
DoggoBit (talk | contribs)
Move section about structure to the top, add nixConfig attribute
Line 9: Line 9:
Flakes also allow for locking references and versions, which can then be queried and updated programatically via the inputs {{cite manual|nix|command-ref/new-cli/nix3-flake-lock|number=7.5.19|title=nix flake lock}}{{cite manual|nix|command-ref/new-cli/nix3-flake-info|number=7.5.17|title=nix flake info}}. Additionally, an experimental CLI utility accepts flake references for expressions that build, run, and deploy packages.{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}}
Flakes also allow for locking references and versions, which can then be queried and updated programatically via the inputs {{cite manual|nix|command-ref/new-cli/nix3-flake-lock|number=7.5.19|title=nix flake lock}}{{cite manual|nix|command-ref/new-cli/nix3-flake-info|number=7.5.17|title=nix flake info}}. Additionally, an experimental CLI utility accepts flake references for expressions that build, run, and deploy packages.{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}}


=== Enabling flakes ===
== Flake file structure ==
Minimally, a flake file contains a description of the flake, a set of input dependencies and an output. You can generate a very basic flake file at any time using nix flake init. This will populate the current directory with a file called flake.nix that will contain something akin to:
{{File|3=<nowiki>{
  description = "A very basic flake";
 
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
 
  outputs = { self, nixpkgs }: {
 
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
 
    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
 
  };
}</nowiki>|name=flake.nix|lang=nix}}
In the example above, you can see the description, the input specified as a GitHub repository with a specific branch (here <code>nixos/nixpkgs</code> on the <code>nixos-unstable</code> branch), and an output that makes use of the input. The output simply specifies that the flake contains one package for the x86_64 architecture called <code>hello</code>. Even if your flake's output wouldn't use its input (however, in practice, that is highly unlikely), the output still needs to be a Nix function.
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}}
 
=== Nix configuration ===
It is possible to override the global Nix configuration set in your <code>nix.conf</code> file for the purposes of evaluating a flake. This can be useful, for example, for setting up binary caches specific to certain projects, while keeping the global configuration untouched. The flake file can contain a nixConfig attribute with any relevant configuration settings supplied. For example, enabling the nix-community binary cache would be achieved by:
{{File|3=<nowiki>{
  ...
  nixConfig = {
    extra-substituters = [
      "https://nix-community.cachix.org"
    ];
    extra-trusted-public-keys = [
      "nix-community.cachix.org-1:...="
    ];
}</nowiki>|name=flake.nix|lang=nix}}{{Note|If you are used to configuring your Nix settings via the NixOS configuration, these options are under <code>nix.settings</code> and not <code>nix</code>. For example, you cannot specify the automatic storage optimisation under <code>nix.optimisation.enable</code>.}}
 
== Setup ==


====Enable flakes temporarily==== <!--T:5-->
=== Enabling flakes temporarily ===<!--T:5-->


<!--T:6-->
<!--T:6-->
Line 20: Line 53:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
=== Enabling flakes permanently ===


====Enable flakes permanently in NixOS==== <!--T:7-->
==== NixOS ====<!--T:7-->


<!--T:8-->
<!--T:8-->
Line 32: Line 66:
<translate>
<translate>


====With Home Manager==== <!--T:10-->
====Home Manager==== <!--T:10-->


<!--T:11-->
<!--T:11-->
Line 43: Line 77:
<translate>
<translate>


====Other Distros, without Home Manager==== <!--T:13-->
====Nix standalone==== <!--T:13-->


<!--T:14-->
<!--T:14-->
Line 56: Line 90:
</syntaxHighlight>
</syntaxHighlight>
<translate>
<translate>
 
== Usage ==<!--T:17-->
===Basic Usage of Flake=== <!--T:17-->


<!--T:20-->
<!--T:20-->
Line 68: Line 101:
Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}}
Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}}


====Generate flake.nix file==== <!--T:24-->
=== The nix flakes command ===
 
{{Main|Nix (command)}}<!--T:64-->
<!--T:25-->
To initialize a flake, run the following flake command in the project directory:
 
</translate>
<syntaxhighlight lang="console">
$ nix flake init
</syntaxhighlight>
<translate>
 
====Common structure====
 
The above command will provide a very simple flake file looking like:
 
</translate>
<syntaxHighlight lang=nix>
{
  description = "A very basic flake";
 
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
 
  outputs = { self, nixpkgs }: {
 
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
 
    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
 
  };
}
</syntaxHighlight>
<translate>
 
You will then be able to build this flake with <code>nix build</code> and run it with <code>nix run</code>
 
{{note|Flakes force you to specify a program for each supported architecture. To avoid this, refer to [[#Defining a flake for multiple architectures]] section of the wiki.}}
 
==== The nix flakes command ==== <!--T:64-->


<!--T:65-->
<!--T:65-->