Flakes: Difference between revisions
Warrenmwang (talk | contribs) m fix typo "packagage" -> "packages" |
Marked this version for translation |
||
(9 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
<languages /> | <languages /> | ||
<translate> | <translate> | ||
<!--T:1--> | <!--T:1--> | ||
{{Cleanup}} | |||
==== | <!--T:182--> | ||
'''Nix flakes''' are an [[Experimental Nix features|experimental feature]] first introduced in the 2.4 [[Nix]] release,{{Cite manual|nix|development/experimental-features|number=13.8|title=Experimental Features|subsection=xp-feature-flakes|subtitle=flakes}}{{Cite manual|nix|release-notes/rl-2.4|number=14.27|title=Release 2.4 (2021-11-01)}} aiming to address a number of areas of improvement for the Nix ecosystem: they provide a uniform structure for Nix projects, allow for pinning specific versions of each dependencies, and sharing these dependencies via lock files, and overall make it more convenient to write reproducible Nix expressions. | |||
<!--T: | <!--T:183--> | ||
Nix | A flake is a directory which directly contains a Nix file called <code>flake.nix</code>, that follows a very specific structure. Flakes introduce a URL-like syntax{{Cite manual|nix|command-ref/new-cli/nix3-flake|number=8.5.17|title=nix flake|subsection=url-like-syntax|subtitle=URL-like syntax}} for specifying remote resources. To simplify the URL syntax, flakes use a registry of symbolic identifiers,{{Cite manual|nix|command-ref/new-cli/nix3-registry|number=8.5.62|title=nix registry}} allowing the direct specification of resources through syntax such as <code>github:NixOS/nixpkgs</code>. | ||
<!--T: | <!--T:184--> | ||
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}} | |||
<!--T: | <!--T:185--> | ||
== 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"; | |||
<!--T: | <!--T:186--> | ||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
}; | |||
<!--T: | <!--T:187--> | ||
outputs = { self, nixpkgs }: { | |||
<!--T: | <!--T:188--> | ||
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello; | |||
<!--T: | <!--T:189--> | ||
packages.x86_64-linux.default = self.packages.x86_64-linux.hello; | |||
== | <!--T:190--> | ||
}; | |||
}</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.}} | |||
==== | <!--T:191--> | ||
=== 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 == <!--T:192--> | |||
=== Enabling flakes temporarily === <!--T:5--> | |||
<!--T:6--> | <!--T:6--> | ||
Line 38: | Line 66: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<translate> | <translate> | ||
=== Enabling flakes permanently === <!--T:193--> | |||
==== | ==== NixOS ==== <!--T:7--> | ||
<!--T:8--> | <!--T:8--> | ||
Line 50: | Line 79: | ||
<translate> | <translate> | ||
==== | ====Home Manager==== <!--T:10--> | ||
<!--T:11--> | <!--T:11--> | ||
Line 61: | Line 90: | ||
<translate> | <translate> | ||
==== | ====Nix standalone==== <!--T:13--> | ||
<!--T:14--> | <!--T:14--> | ||
Line 74: | Line 103: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<translate> | <translate> | ||
== Usage == <!--T:17--> | |||
== | |||
<!--T:20--> | <!--T:20--> | ||
Line 86: | Line 114: | ||
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.}} | ||
<!--T:64--> | |||
=== The nix flakes command === | |||
{{Main|Nix (command)}} | |||
== | |||
The | |||
{{ | |||
<!--T:65--> | <!--T:65--> | ||
The {{ic|nix flake}} subcommand is described in {{Nix Manual|name=command reference page of the Nix manual|anchor=command-ref/new-cli/nix3-flake}}. | The {{ic|nix flake}} subcommand is described in {{Nix Manual|name=command reference page of the Nix manual|anchor=command-ref/new-cli/nix3-flake}}. | ||
<!--T:194--> | |||
This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system-specifc attribute set. And within that, two package [[derivations]] <code>default</code> and <code>hello</code>. You can find outputs with the {{Nix Manual|name=show command|anchor=command-ref/new-cli/nix3-flake-show}} of a flake as shown below: | This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system-specifc attribute set. And within that, two package [[derivations]] <code>default</code> and <code>hello</code>. You can find outputs with the {{Nix Manual|name=show command|anchor=command-ref/new-cli/nix3-flake-show}} of a flake as shown below: | ||
<!--T:195--> | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix flake show | $ nix flake show | ||
Line 140: | Line 133: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Development shells ==== | ==== Development shells ==== <!--T:196--> | ||
<!--T:197--> | |||
A <code>devShell</code> is a Nix-provided [[Development_environment_with_nix-shell#nix develop|development environment]] defined within a flake. It lets you declare a reproducible shell environment with the tools, libraries, and environment variables you need for the development of a specific project. This is flake equivalent to defining a <code>nix-shell</code>. | A <code>devShell</code> is a Nix-provided [[Development_environment_with_nix-shell#nix develop|development environment]] defined within a flake. It lets you declare a reproducible shell environment with the tools, libraries, and environment variables you need for the development of a specific project. This is flake equivalent to defining a <code>nix-shell</code>. | ||
<!--T:198--> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
description = "Example flake with a devShell"; | description = "Example flake with a devShell"; | ||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | <!--T:199--> | ||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
outputs = { self, nixpkgs}: | <!--T:200--> | ||
outputs = { self, nixpkgs}: | |||
let | let | ||
system = "x86_64-linux"; | system = "x86_64-linux"; | ||
Line 157: | Line 154: | ||
devShells.x86_64-linux.default = pkgs.mkShell { | devShells.x86_64-linux.default = pkgs.mkShell { | ||
buildInputs = with pkgs; [ | buildInputs = with pkgs; [ | ||
hello | |||
]; | ]; | ||
shellHook = '' | shellHook = '' | ||
Line 167: | Line 164: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<!--T:201--> | |||
To enter the development shell environment: | To enter the development shell environment: | ||
<!--T:202--> | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix develop | $ nix develop | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<!--T:203--> | |||
{{note|You don’t need to define a devShell to enter a development shell using nix develop. | {{note|You don’t need to define a devShell to enter a development shell using nix develop. | ||
If no devShell is defined, nix develop will drop you into an environment containing the default build dependencies of the flake (if any).}} | If no devShell is defined, nix develop will drop you into an environment containing the default build dependencies of the flake (if any).}} | ||
Line 181: | Line 181: | ||
Running <code>nix build</code> will look in the <code>legacyPackages</code> and <code>packages</code> output attributes for the corresponding [[derivation]] and then your system architecture and build the default output. If you want to specify a build attribute in a flake repository, you can run <code>nix build .#<attr></code>. In the example above, if you wanted to build the <code>packages.x86_64-linux.hello</code> attribute, run: | Running <code>nix build</code> will look in the <code>legacyPackages</code> and <code>packages</code> output attributes for the corresponding [[derivation]] and then your system architecture and build the default output. If you want to specify a build attribute in a flake repository, you can run <code>nix build .#<attr></code>. In the example above, if you wanted to build the <code>packages.x86_64-linux.hello</code> attribute, run: | ||
<!--T:204--> | |||
<syntaxHighlight lang=console> | <syntaxHighlight lang=console> | ||
$ nix build .#hello | $ nix build .#hello | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<!--T:205--> | |||
Likewise, you can specify an attribute with the run command: <code>nix run .#hello</code> and the develop command: <code>nix develop .#hello</code>. | Likewise, you can specify an attribute with the run command: <code>nix run .#hello</code> and the develop command: <code>nix develop .#hello</code>. | ||
Line 264: | Line 266: | ||
<translate> | <translate> | ||
<!--T:206--> | |||
By default, Git submodules in package <code>src</code>'s won't get copied to the nix store, this may cause the build to fail. Flakes in Git repositories can declare that they need Git submodules to be enabled. Since Nix version [https://discourse.nixos.org/t/nix-2-27-0-released/62003 2.27], you can enable submodules by: | By default, Git submodules in package <code>src</code>'s won't get copied to the nix store, this may cause the build to fail. Flakes in Git repositories can declare that they need Git submodules to be enabled. Since Nix version [https://discourse.nixos.org/t/nix-2-27-0-released/62003 2.27], you can enable submodules by: | ||
<!--T:207--> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
inputs.self.submodules = true; | inputs.self.submodules = true; | ||
Line 346: | Line 350: | ||
You can also define additional arbitrary attributes, but these are the outputs that Nix knows about. | You can also define additional arbitrary attributes, but these are the outputs that Nix knows about. | ||
== Core usage patterns == | == Core usage patterns == <!--T:208--> | ||
=== Making your evaluations pure === <!--T:60--> | === Making your evaluations pure === <!--T:60--> | ||
Line 359: | Line 363: | ||
* <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the evauluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. | * <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the evauluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. | ||
<!--T:209--> | |||
* <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory. | * <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory. | ||
=== Defining a flake for multiple architectures === | === Defining a flake for multiple architectures === <!--T:210--> | ||
<!--T:211--> | |||
Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures. | Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures. | ||
<!--T:212--> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
description = "A flake targeting multiple architectures"; | description = "A flake targeting multiple architectures"; | ||
inputs = { | <!--T:213--> | ||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
}; | }; | ||
outputs = { self, nixpkgs }: let | <!--T:214--> | ||
outputs = { self, nixpkgs }: let | |||
systems = [ "x86_64-linux" "aarch64-linux" ]; | systems = [ "x86_64-linux" "aarch64-linux" ]; | ||
forAllSystems = f: builtins.listToAttrs (map (system: { | forAllSystems = f: builtins.listToAttrs (map (system: { | ||
Line 390: | Line 399: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<!--T:215--> | |||
You can also use third-parties projects like [[Flake Utils|flake-utils]] or [[Flake Parts|flake-parts]] that automatically provide code to avoid this boilerplate. To avoid re-defining the program multiple times, refer to [[Flake Utils#Defining a flake for multiple architectures]] | You can also use third-parties projects like [[Flake Utils|flake-utils]] or [[Flake Parts|flake-parts]] that automatically provide code to avoid this boilerplate. To avoid re-defining the program multiple times, refer to [[Flake Utils#Defining a flake for multiple architectures]] | ||
=== Using overlays === | === Using overlays === <!--T:216--> | ||
<!--T:217--> | |||
To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page. | To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page. | ||
=== Enable unfree software === <!--T:129--> | === Enable unfree software === <!--T:129--> | ||
<!--T:218--> | |||
To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config.allowUnree = true;</code> when importing Nixpkgs. | To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config.allowUnree = true;</code> when importing Nixpkgs. | ||
<!--T:219--> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
Line 413: | Line 426: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== NixOS configuration with flakes | == NixOS configuration with flakes == <!--T:220--> | ||
< | <!--T:221--> | ||
It is possible to manage a [[NixOS]] system configuration using flakes, gaining the benefits of reproducible, declarative inputs and streamlined updates. | |||
<!--T: | <!--T:222--> | ||
For details and examples, see [[NixOS system configuration#Defining NixOS as a flake]]. | |||
== Development tricks == <!--T:131--> | == Development tricks == <!--T:131--> | ||
Line 692: | Line 451: | ||
<!--T:51--> | <!--T:51--> | ||
The [https://github.com/edolstra/flake-compat flake-compat] library provides a compatibility layer that allows projects using traditional <code>default.nix</code> and <code>shell.nix</code> files to operate with flakes. For more details and usage examples, see the [[Flake Compat]] page. | The [https://github.com/edolstra/flake-compat flake-compat] library provides a compatibility layer that allows projects using traditional <code>default.nix</code> and <code>shell.nix</code> files to operate with flakes. For more details and usage examples, see the [[Flake Compat]] page. | ||
<!--T:223--> | |||
Another project that allows consuming flakes from non-flake projects is [https://github.com/fricklerhandwerk/flake-inputs flake-inputs]. | |||
=== Accessing flakes from Nix expressions === <!--T:58--> | === Accessing flakes from Nix expressions === <!--T:58--> | ||
Line 698: | Line 460: | ||
If you want to access a flake from within a regular Nix expression on a system that has flakes enabled, you can use something like <code>(builtins.getFlake "/path/to/directory").packages.x86_64-linux.default</code>, where 'directory' is the directory that contains your <code>flake.nix</code>. | If you want to access a flake from within a regular Nix expression on a system that has flakes enabled, you can use something like <code>(builtins.getFlake "/path/to/directory").packages.x86_64-linux.default</code>, where 'directory' is the directory that contains your <code>flake.nix</code>. | ||
=== Efficiently build multiple flake outputs === | === Efficiently build multiple flake outputs === <!--T:224--> | ||
<!--T:101--> | <!--T:101--> | ||
Line 773: | Line 535: | ||
== See also == <!--T:138--> | == See also == <!--T:138--> | ||
=== Official sources === | === Official sources === <!--T:225--> | ||
<!--T:139--> | <!--T:139--> | ||
Line 787: | Line 549: | ||
* [https://github.com/NixOS/rfcs/pull/49 RFC 49] (2019) - Original flakes specification | * [https://github.com/NixOS/rfcs/pull/49 RFC 49] (2019) - Original flakes specification | ||
=== Guides === | === Guides === <!--T:226--> | ||
<!--T:169--> | <!--T:169--> | ||
Line 813: | Line 575: | ||
* [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube video | * [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube video | ||
=== Useful flake modules === | === Useful flake modules === <!--T:227--> | ||
<!--T:179--> | <!--T:179--> | ||
* [[Flake Utils|flake-utils]]: Library to avoid some boiler-code when writing flakes | * [[Flake Utils|flake-utils]]: Library to avoid some boiler-code when writing flakes | ||
<!--T:228--> | |||
* [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | * [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | ||
<!--T:229--> | |||
* [[Flake Compat|flake-compat]]: A compatibility layer for flakes | * [[Flake Compat|flake-compat]]: A compatibility layer for flakes | ||
<!--T:181--> | <!--T:181--> | ||
* [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | * [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | ||
<!--T:230--> | |||
{{references}} | |||
</translate> | </translate> |