Flakes/fr: Difference between revisions
Updating to match new version of source page Tags: Mobile edit Mobile web edit |
Updating to match new version of source page Tags: Mobile edit Mobile web edit |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<languages /> | <languages /> | ||
{{Cleanup}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
'''Nix flakes''' are an [ | '''Nix flakes''' are an [https://nix.dev/manual/nix/stable/contributing/experimental-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. | ||
</div> | </div> | ||
| Line 19: | Line 17: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== Flake file structure == | == Flake file structure == | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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: | 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: | ||
</div> | |||
{{File|3=<nowiki>{ | {{File|3=<nowiki>{ | ||
description = "A very basic flake"; | description = "A very basic flake"; | ||
inputs = { | |||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
}; | }; | ||
outputs = { self, nixpkgs }: { | |||
outputs = { self, nixpkgs }: { | packages.x86_64-linux = { | ||
</ | default = self.packages.x86_64-linux.hello; | ||
hello = nixpkgs.legacyPackages.x86_64-linux.hello; | |||
}; | |||
}; | |||
}</nowiki>|name=flake.nix|lang=nix}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}} | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=== Nix configuration === | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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: | 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: | ||
</div> | |||
{{File|3=<nowiki>{ | {{File|3=<nowiki>{ | ||
... | ... | ||
| Line 62: | Line 64: | ||
]; | ]; | ||
} | } | ||
}</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>.}} | }</nowiki>|name=flake.nix|lang=nix}} | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
{{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>.}} | |||
</div> | </div> | ||
| Line 77: | Line 82: | ||
Lorsque vous utilisez une commande <code>nix</code>, ajoutez les commandes suivantes: | Lorsque vous utilisez une commande <code>nix</code>, ajoutez les commandes suivantes: | ||
</div> | </div> | ||
<syntaxhighlight lang="shell"> | <syntaxhighlight lang="shell"> | ||
--experimental-features 'nix-command flakes' | --experimental-features 'nix-command flakes' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Enabling flakes permanently === | === Enabling flakes permanently === | ||
| Line 114: | Line 121: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
{{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default.}} | {{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default, but installs the proprietary Determinate Nix.}} | ||
</div> | </div> | ||
| Line 122: | Line 129: | ||
experimental-features = nix-command flakes | experimental-features = nix-command flakes | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<span id="Usage"></span> | <span id="Usage"></span> | ||
<div class="mw-translate-fuzzy"> | <div class="mw-translate-fuzzy"> | ||
| Line 141: | Line 149: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== The nix flakes command === | === The nix flakes command === | ||
</div> | |||
{{Main|Nix (command)}} | {{Main|Nix (command)}} | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 152: | Line 161: | ||
</div> | </div> | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix flake show | $ nix flake show | ||
| Line 160: | Line 168: | ||
└───hello: package 'hello-2.12.2' | └───hello: package 'hello-2.12.2' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 170: | Line 177: | ||
</div> | </div> | ||
<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"; | |||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
outputs = { self, nixpkgs }: | |||
outputs = { self, nixpkgs}: | |||
let | let | ||
system = "x86_64-linux"; | system = "x86_64-linux"; | ||
| Line 197: | Line 199: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 203: | Line 204: | ||
</div> | </div> | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix develop | $ nix develop | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 219: | Line 218: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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 [[derivations|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: | ||
</div> | </div> | ||
<syntaxHighlight lang=console> | <syntaxHighlight lang=console> | ||
$ nix build .#hello | $ nix build .#hello | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 264: | Line 261: | ||
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> | <code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Nixpkgs can alternatively also point to an url cached by the NixOS organization: | Nixpkgs can alternatively also point to an url cached by the NixOS organization: | ||
</div> | |||
<code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code> | <code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
In this example the input would point to the `nixpkgs-unstable` channel. | In this example the input would point to the `nixpkgs-unstable` channel. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 288: | Line 288: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this: | Using curly brackets (<code>{}</code>), we can shorten all of this and put it in a table. The code will look something like this: | ||
</div> | </div> | ||
| Line 305: | Line 305: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
inputs.self.submodules = true; | inputs.self.submodules = true; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 316: | Line 314: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
The output schema is described the [https://nix.dev/manual/nix/2.33/command-ref/new-cli/nix3-flake-check.html#evaluation-checks nix flake check manual page]. | |||
</div> | </div> | ||
| Line 343: | Line 341: | ||
</div> | </div> | ||
< | <syntaxhighlight lang="nix"> | ||
{ self, ... }@inputs: | { self, ... }@inputs: | ||
{ | { | ||
| Line 356: | Line 354: | ||
type = "app"; | type = "app"; | ||
program = "<store-path>"; | program = "<store-path>"; | ||
meta = {description = "..."; inherit otherMetaAttrs; }; | |||
}; | }; | ||
# Executed by `nix run . -- <args?>` | # Executed by `nix run . -- <args?>` | ||
apps."<system>".default = { type = "app"; program = "..."; }; | apps."<system>".default = { type = "app"; program = "..."; meta = {description = "..."; inherit otherMetaAttrs; }; }; | ||
# Formatter (alejandra, nixfmt or nixpkgs-fmt) | # Formatter (alejandra, nixfmt, treefmt-nix or nixpkgs-fmt) | ||
formatter."<system>" = derivation; | formatter."<system>" = derivation; | ||
# Used for nixpkgs packages, also accessible via `nix build .#<name>` | # Used for nixpkgs packages, also accessible via `nix build .#<name>` | ||
| Line 389: | Line 388: | ||
templates.default = { path = "<store-path>"; description = ""; }; | templates.default = { path = "<store-path>"; description = ""; }; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 412: | Line 411: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the | * <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the evaluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. | ||
</div> | </div> | ||
| Line 427: | Line 426: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
description = "A flake targeting multiple architectures"; | description = "A flake targeting multiple architectures"; | ||
inputs = { | inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
}; | }; | ||
outputs = { self, nixpkgs }: let | outputs = { self, nixpkgs }: let | ||
systems = [ "x86_64-linux" "aarch64-linux" ]; | systems = [ "x86_64-linux" "aarch64-linux" ]; | ||
| Line 456: | Line 450: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 478: | Line 471: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
| Line 490: | Line 482: | ||
}; | }; | ||
} | } | ||
</syntaxhighlight | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 618: | Line 609: | ||
# Output should show ~/libdep-src-checkout/ so you know it worked | # Output should show ~/libdep-src-checkout/ so you know it worked | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 702: | Line 694: | ||
* [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | * [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | ||
</div> | </div> | ||
{{references}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
[[Category:Software|Software]] | |||
[[Category:Nix|Nix]] | |||
[[Category:Nix Language|Nix Language]] | |||
[[Category:Flakes|Flakes]] | |||
</div> | </div> | ||