Flakes: Difference between revisions
m add citation to lock file and showing flake inputs |
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}} | ||
=== | == 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 == | |||
=== | === Enabling flakes temporarily ===<!--T:5--> | ||
<!--T:6--> | <!--T:6--> | ||
Line 20: | Line 53: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<translate> | <translate> | ||
=== Enabling flakes permanently === | |||
==== | ==== NixOS ====<!--T:7--> | ||
<!--T:8--> | <!--T:8--> | ||
Line 32: | Line 66: | ||
<translate> | <translate> | ||
==== | ====Home Manager==== <!--T:10--> | ||
<!--T:11--> | <!--T:11--> | ||
Line 43: | Line 77: | ||
<translate> | <translate> | ||
==== | ====Nix standalone==== <!--T:13--> | ||
<!--T:14--> | <!--T:14--> | ||
Line 56: | Line 90: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<translate> | <translate> | ||
== Usage ==<!--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.}} | ||
=== | === The nix flakes command === | ||
{{Main|Nix (command)}}<!--T:64--> | |||
<!--T:65--> | <!--T:65--> |