Flakes/fr: Difference between revisions
Created page with "* Une [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html interface de ligne de commandes expérimentale] accepte les références flocon pour les expressions qui créent, exécutent et déploient des packages." |
Updating to match new version of source page Tags: Mobile edit Mobile web edit |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
<languages /> | <languages /> | ||
= | <div class="mw-translate-fuzzy"> | ||
Les '''flocons Nix''' (ou flakes) sont une [https://nixos.org/manual/nix/stable/contributing/experimental-Features.html fonctionnalité expérimentale] introduite avec Nix 2.4 ([https://nixos.org/manual/nix/unstable/release-notes/rl-2.4.html voir notes de version]). | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
'''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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
<code> | 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>. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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}} | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
== 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"; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
}; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
outputs = { self, nixpkgs }: { | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
packages.x86_64-linux.default = self.packages.x86_64-linux.hello; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
}; | |||
}</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.}} | |||
</div> | |||
<span id=" | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== 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>.}} | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
== Setup == | |||
</div> | |||
<span id="Enabling_flakes_temporarily"></span> | |||
<div class="mw-translate-fuzzy"> | |||
====Activer temporairement les flocons==== | ====Activer temporairement les flocons==== | ||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
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> | |||
<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"> | |||
=== Enabling flakes permanently === | |||
</div> | |||
<span id=" | <span id="NixOS"></span> | ||
<div class="mw-translate-fuzzy"> | |||
====Activer les flocons de manière permanente dans NixOS==== | ====Activer les flocons de manière permanente dans NixOS==== | ||
</div> | |||
Ajoutez ce qui suit à la [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |configuration NixOS]]: | |||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
Line 40: | Line 95: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<span id=" | <span id="Home_Manager"></span> | ||
<div class="mw-translate-fuzzy"> | |||
=====Autres Distributions, avec Home-Manager===== | =====Autres Distributions, avec Home-Manager===== | ||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
Ajouter ce qui suis à votre configuration home-manager: | Ajouter ce qui suis à votre configuration home-manager: | ||
</div> | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
Line 49: | Line 108: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<span id=" | <span id="Nix_standalone"></span> | ||
<div class="mw-translate-fuzzy"> | |||
=====Autres Distributions, sans Home-Manager===== | =====Autres Distributions, sans Home-Manager===== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Line 56: | Line 117: | ||
</div> | </div> | ||
Ajoutez ce qui suit à <code>~/.config/nix/nix.conf</code> ou <code>/etc/nix/nix.conf</code>: | |||
<syntaxHighlight lang=text> | <syntaxHighlight lang=text> | ||
experimental-features = nix-command flakes | experimental-features = nix-command flakes | ||
</syntaxHighlight> | </syntaxHighlight> | ||
<span id="Usage"></span> | |||
<div class="mw-translate-fuzzy"> | |||
===Utilisation de base des Flocons=== | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
{{Warning | Puisque le contenu des fichiers flake sont copié dans le dossier <code>/nix</code> lisible par tous les utilisateurs, il ne faut inclure aucun secret qui ne soit pas chiffré. Vous devez à la place utiliser un [[Comparison of secret managing schemes|système de gestion de secrets]].}} | |||
</div> | |||
<div class="mw-translate-fuzzy"> | |||
Pour les flocons dans un dépôt git, seuls les fichiers dans l'arbre de travail seront copiés dans le store. | |||
</div> | |||
< | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== | Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}} | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=== The nix flakes command === | |||
{{Main|Nix (command)}} | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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}}. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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: | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
= | <syntaxhighlight lang="console"> | ||
$ nix flake show | |||
└───packages | |||
└───x86_64-linux | |||
├───default: package 'hello-2.12.2' | |||
└───hello: package 'hello-2.12.2' | |||
</syntaxhighlight> | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
==== Development shells ==== | |||
</div> | </div> | ||
< | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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>. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
<syntaxhighlight lang="nix"> | |||
{ | |||
description = "Example flake with a devShell"; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
outputs = { self, nixpkgs}: | |||
let | |||
system = "x86_64-linux"; | |||
pkgs = import nixpkgs { inherit system; }; | |||
in { | |||
devShells.x86_64-linux.default = pkgs.mkShell { | |||
buildInputs = with pkgs; [ | |||
hello | |||
]; | |||
shellHook = '' | |||
echo "Welcome to the devShell!" | |||
''; | |||
}; | |||
}; | |||
} | |||
</syntaxhighlight> | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
To enter the development shell environment: | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
<syntaxhighlight lang="console"> | |||
$ nix develop | |||
</syntaxhighlight> | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
{{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).}} | |||
</div> | </div> | ||
< | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
==== | ==== Build specific attributes in a flake repository ==== | ||
</div> | |||
<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: | |||
</div> | </div> | ||
<syntaxHighlight lang= | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
nix | <syntaxHighlight lang=console> | ||
$ nix build .#hello | |||
</syntaxHighlight> | </syntaxHighlight> | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Likewise, you can specify an attribute with the run command: <code>nix run .#hello</code> and the develop command: <code>nix develop .#hello</code>. | |||
</div> | |||
<span id="Flake_schema"></span> | <span id="Flake_schema"></span> | ||
Line 117: | Line 246: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* <code>nixConfig</code> is an attribute set of values which reflect the [https://nixos.org/manual/nix/stable/command-ref/conf-file.html values given to nix.conf]. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a binary cache. | * <code>nixConfig</code> is an attribute set of values which reflect the [https://nixos.org/manual/nix/stable/command-ref/conf-file.html values given to nix.conf]. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a [[Binary Cache|binary cache]]. | ||
</div> | </div> | ||
Line 129: | Line 258: | ||
L'attribut d'entrée (inputs) définit les dépendances du flocon. Par exemple, nixpkgs doit être défini comme une dépendance pour un flocon système afin que le système se construise correctement. | L'attribut d'entrée (inputs) définit les dépendances du flocon. Par exemple, nixpkgs doit être défini comme une dépendance pour un flocon système afin que le système se construise correctement. | ||
<div class="mw-translate-fuzzy"> | |||
Nixpkgs peut être défini en utilisant le code suivant: | Nixpkgs peut être défini en utilisant le code suivant: | ||
</div> | |||
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> | <code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> | ||
Nixpkgs can alternatively also point to an url cached by the NixOS organization: | |||
<code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code> | |||
In this example the input would point to the `nixpkgs-unstable` channel. | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Line 162: | Line 300: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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: | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
<syntaxhighlight lang="nix"> | |||
inputs.self.submodules = true; | |||
</syntaxhighlight> | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Line 248: | Line 396: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== | == Core usage patterns == | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Making your evaluations pure === | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Nix flakes are evaluated in a pure evaluation mode, meaning that access to the external environment is restricted to ensure reproducibility. To maintain purity when working with flakes, consider the following: | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* {{Nixpkgs Manual|name=fetchurl|anchor=#sec-pkgs-fetchers-fetchurl-inputs}} and {{Nixpkgs Manual|name=fetchzip|anchor=#sec-pkgs-fetchers-fetchzip-inputs}} require a <code>sha256</code> argument to be considered pure. | |||
</div> | </div> | ||
<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 evauluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory. | |||
and | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== | === Defining a flake for multiple architectures === | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
description = "A flake targeting multiple architectures"; | |||
</div> | </div> | ||
< | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
{ | inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
}; | }; | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
outputs = { self, nixpkgs }: let | |||
systems = [ "x86_64-linux" "aarch64-linux" ]; | |||
forAllSystems = f: builtins.listToAttrs (map (system: { | |||
name = system; | |||
value = f system; | |||
}) systems); | |||
in { | |||
packages = forAllSystems (system: let | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | pkgs = nixpkgs.legacyPackages.${system}; | ||
{ | in { | ||
hello = pkgs.hello; | |||
default = pkgs.hello; | |||
}); | |||
{ | |||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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]] | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== | === Using overlays === | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Enable unfree software === | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
inputs.nixpkgs.url = "github: | inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
outputs = { self, nixpkgs, flake-compat }: | |||
outputs = | let | ||
system = "x86_64-linux"; | |||
pkgs = import nixpkgs { inherit system; config.allowUnfree = true;}; | |||
in { | |||
... | |||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== NixOS configuration with flakes == | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
It is possible to manage a [[NixOS]] system configuration using flakes, gaining the benefits of reproducible, declarative inputs and streamlined updates. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
For details and examples, see [[NixOS system configuration#Defining NixOS as a flake]]. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== Development tricks == | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Automatically switch nix shells with | === Automatically switch nix shells with direnv === | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
It is possible to automatically activate different Nix shells when navigating between project directories by using [[Direnv]]. Additional Nix integration with Direnv can be achieved with [https://github.com/nix-community/nix-direnv nix-direnv]. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== Pushing Flakes to Cachix == | === Pushing Flakes to Cachix === | ||
</div> | </div> | ||
Line 572: | Line 524: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Flake support in projects without flakes === | |||
= | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Another project that allows consuming flakes from non-flake projects is [https://github.com/fricklerhandwerk/flake-inputs flake-inputs]. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== | === Accessing flakes from Nix expressions === | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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>. | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Efficiently build multiple flake outputs === | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
To push ''all'' flake outputs automatically, checkout [https://github.com/srid/devour-flake#usage devour-flake]. | |||
</div> | </div> | ||
Line 782: | Line 580: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
When a git folder exists, flake will only copy files added in git to maximize reproducibility (this way if you forgot to add a local file in your repo, you will directly get an error when you try to compile it). However, for development purpose you may want to create an alternative flake file, for instance containing configuration for your preferred editors as described [https://discourse.nixos.org/t/local-personal-development-tools-with-flakes/22714/8 here]… of course without committing this file since it contains only your own preferred tools. You can do so by doing something like that (say for a file called <code>extra/flake.nix</code>): | When a [[git]] folder exists, flake will only copy files added in git to maximize reproducibility (this way if you forgot to add a local file in your repo, you will directly get an error when you try to compile it). However, for development purpose you may want to create an alternative flake file, for instance containing configuration for your preferred editors as described [https://discourse.nixos.org/t/local-personal-development-tools-with-flakes/22714/8 here]… of course without committing this file since it contains only your own preferred tools. You can do so by doing something like that (say for a file called <code>extra/flake.nix</code>): | ||
</div> | </div> | ||
Line 827: | Line 625: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
== See also == | == See also == | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=== Official sources === | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [https://nix.dev/concepts/flakes Flakes] - nix.dev | * [https://nix.dev/concepts/flakes Flakes] - nix.dev | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
* [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html Nix flake command reference manual] - Many additional details about flakes, and their parts. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
* [https://github.com/NixOS/nix/blob/master/src/nix/flake.md spec describing flake inputs in more detail] | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [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 | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=== Guides === | |||
</div> | </div> | ||
Line 866: | Line 680: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [https:// | * [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube video | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
=== Useful flake modules === | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
* [[Flake Utils|flake-utils]]: Library to avoid some boiler-code when writing flakes | |||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [ | * [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [ | * [[Flake Compat|flake-compat]]: A compatibility layer for flakes | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
* [https:// | * [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | ||
</div> | </div> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
{{references}} | |||
</div> | </div> | ||
[[Category:Software]] | [[Category:Software]] | ||
[[Category:Nix]] | [[Category:Nix]] | ||
[[Category:Nix Language]] | |||
[[Category:Flakes]] | [[Category:Flakes]] |
Latest revision as of 12:07, 24 July 2025
Les flocons Nix (ou flakes) sont une fonctionnalité expérimentale introduite avec Nix 2.4 (voir notes de version).
Nix flakes are an experimental feature first introduced in the 2.4 Nix release,[1][2] 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.
A flake is a directory which directly contains a Nix file called flake.nix
, that follows a very specific structure. Flakes introduce a URL-like syntax[3] for specifying remote resources. To simplify the URL syntax, flakes use a registry of symbolic identifiers,[4] allowing the direct specification of resources through syntax such as github:NixOS/nixpkgs
.
Flakes also allow for locking references and versions, which can then be queried and updated programatically via the inputs [5][6]. Additionally, an experimental CLI utility accepts flake references for expressions that build, run, and deploy packages.[7]
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:
{
description = "A very basic flake";
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
outputs = { self, nixpkgs }: {
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
</div>
<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 nixos/nixpkgs
on the nixos-unstable
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 hello
. 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.
Nix configuration
It is possible to override the global Nix configuration set in your nix.conf
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:
{
...
nixConfig = {
extra-substituters = [
"https://nix-community.cachix.org"
];
extra-trusted-public-keys = [
"nix-community.cachix.org-1:...="
];
}
}
nix.settings
and not nix
. For example, you cannot specify the automatic storage optimisation under nix.optimisation.enable
.Setup
Activer temporairement les flocons
Lorsque vous utilisez une commande nix
, ajoutez les commandes suivantes:
--experimental-features 'nix-command flakes'
Enabling flakes permanently
Activer les flocons de manière permanente dans NixOS
Ajoutez ce qui suit à la configuration NixOS:
nix.settings.experimental-features = [ "nix-command" "flakes" ];
Autres Distributions, avec Home-Manager
Ajouter ce qui suis à votre configuration home-manager:
nix.settings.experimental-features = [ "nix-command" "flakes" ];
Autres Distributions, sans Home-Manager
Ajoutez ce qui suit à ~/.config/nix/nix.conf
ou /etc/nix/nix.conf
:
experimental-features = nix-command flakes
Utilisation de base des Flocons
/nix
lisible par tous les utilisateurs, il ne faut inclure aucun secret qui ne soit pas chiffré. Vous devez à la place utiliser un système de gestion de secrets.Pour les flocons dans un dépôt git, seuls les fichiers dans l'arbre de travail seront copiés dans le store.
Therefore, if you use git
for your flake, ensure to git add
any project files after you first create them.}}
The nix flakes command
- Main article: Nix (command)
The nix flake
subcommand is described in
command reference page of the Nix manual.
This flake produces a single flake output packages
. And within that, x86_64-linux
is a system-specifc attribute set. And within that, two package derivations default
and hello
. You can find outputs with the
show command of a flake as shown below:
$ nix flake show
└───packages
└───x86_64-linux
├───default: package 'hello-2.12.2'
└───hello: package 'hello-2.12.2'
Development shells
A devShell
is a Nix-provided 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 nix-shell
.
{
description = "Example flake with a devShell";
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
outputs = { self, nixpkgs}:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
in {
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = with pkgs; [
hello
];
shellHook = ''
echo "Welcome to the devShell!"
'';
};
};
}
To enter the development shell environment:
$ nix develop
Build specific attributes in a flake repository
Running nix build
will look in the legacyPackages
and packages
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 nix build .#<attr>
. In the example above, if you wanted to build the packages.x86_64-linux.hello
attribute, run:
$ nix build .#hello
Likewise, you can specify an attribute with the run command: nix run .#hello
and the develop command: nix develop .#hello
.
Schéma d'un Flocon
Un fichier flake.nix est un fichier Nix mais avec des restrictions spéciales (nous en reparlerons plus tard)
Il possède 4 attributs de premier niveau :
description
est une chaine de caractères décrivant le flocon.
inputs
est un attribut définissant toutes les dépendances du flocon. Le schéma est décrit ci-dessous.
outputs
est une fonction d'un argument qui prend un ensemble d'attributs de toutes les entrées réalisées et génère un autre ensemble d'attributs dont le schéma est décrit ci-dessous.
nixConfig
is an attribute set of values which reflect the values given to nix.conf. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a binary cache.
Schéma d'entrée
Le manuel d'entrée de flocon nix.
Le manuel de références de flocon nix.
L'attribut d'entrée (inputs) définit les dépendances du flocon. Par exemple, nixpkgs doit être défini comme une dépendance pour un flocon système afin que le système se construise correctement.
Nixpkgs peut être défini en utilisant le code suivant:
inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";
Nixpkgs can alternatively also point to an url cached by the NixOS organization:
inputs.nixpkgs.url = "https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz";
In this example the input would point to the `nixpkgs-unstable` channel.
For any repository with its own flake.nix file, the website must also be defined. Nix knows where the nixpkgs repository is, so stating that it's on GitHub is unnecessary.
For example, adding Hyprland as an input would look something like this:
inputs.hyprland.url = "github:hyprwm/Hyprland";
If you want to make Hyprland follow the nixpkgs input to avoid having multiple versions of nixpkgs, this can be done using the following code:
inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";
Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this:
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";
hyprland = {
url = "github:hyprwm/Hyprland";
inputs.nixpkgs.follows = "nixpkgs";
};
};
By default, Git submodules in package src
'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 2.27, you can enable submodules by:
inputs.self.submodules = true;
Output schema
This is described in the nix package manager src/nix/flake-check.md.
Once the inputs are resolved, they're passed to the function `outputs` along with with `self`, which is the directory of this flake in the store. `outputs` returns the outputs of the flake, according to the following schema.
Where:
<system>
is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin"
<name>
is an attribute name like "hello".
<flake>
is a flake name like "nixpkgs".
<store-path>
is a/nix/store..
path
{ self, ... }@inputs:
{
# Executed by `nix flake check`
checks."<system>"."<name>" = derivation;
# Executed by `nix build .#<name>`
packages."<system>"."<name>" = derivation;
# Executed by `nix build .`
packages."<system>".default = derivation;
# Executed by `nix run .#<name>`
apps."<system>"."<name>" = {
type = "app";
program = "<store-path>";
};
# Executed by `nix run . -- <args?>`
apps."<system>".default = { type = "app"; program = "..."; };
# Formatter (alejandra, nixfmt or nixpkgs-fmt)
formatter."<system>" = derivation;
# Used for nixpkgs packages, also accessible via `nix build .#<name>`
legacyPackages."<system>"."<name>" = derivation;
# Overlay, consumed by other flakes
overlays."<name>" = final: prev: { };
# Default overlay
overlays.default = final: prev: { };
# Nixos module, consumed by other flakes
nixosModules."<name>" = { config, ... }: { options = {}; config = {}; };
# Default module
nixosModules.default = { config, ... }: { options = {}; config = {}; };
# Used with `nixos-rebuild switch --flake .#<hostname>`
# nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
nixosConfigurations."<hostname>" = {};
# Used by `nix develop .#<name>`
devShells."<system>"."<name>" = derivation;
# Used by `nix develop`
devShells."<system>".default = derivation;
# Hydra build jobs
hydraJobs."<attr>"."<system>" = derivation;
# Used by `nix flake init -t <flake>#<name>`
templates."<name>" = {
path = "<store-path>";
description = "template description goes here?";
};
# Used by `nix flake init -t <flake>`
templates.default = { path = "<store-path>"; description = ""; };
}
You can also define additional arbitrary attributes, but these are the outputs that Nix knows about.
Core usage patterns
Making your evaluations pure
Nix flakes are evaluated in a pure evaluation mode, meaning that access to the external environment is restricted to ensure reproducibility. To maintain purity when working with flakes, consider the following:
builtins.currentSystem
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.
builtins.getEnv
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
Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures.
{
description = "A flake targeting multiple architectures";
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
outputs = { self, nixpkgs }: let
systems = [ "x86_64-linux" "aarch64-linux" ];
forAllSystems = f: builtins.listToAttrs (map (system: {
name = system;
value = f system;
}) systems);
in {
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
hello = pkgs.hello;
default = pkgs.hello;
});
};
}
You can also use third-parties projects like flake-utils or 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
To use Overlays with flakes, refer to Overlays#In a Nix flake page.
Enable unfree software
To allow for unfree software in a flake project, you need to explicitly allow it by setting config.allowUnree = true;
when importing Nixpkgs.
{
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
outputs = { self, nixpkgs, flake-compat }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; config.allowUnfree = true;};
in {
...
};
}
NixOS configuration with flakes
It is possible to manage a NixOS system configuration using flakes, gaining the benefits of reproducible, declarative inputs and streamlined updates.
For details and examples, see NixOS system configuration#Defining NixOS as a flake.
Development tricks
Automatically switch nix shells with direnv
It is possible to automatically activate different Nix shells when navigating between project directories by using Direnv. Additional Nix integration with Direnv can be achieved with nix-direnv.
Pushing Flakes to Cachix
https://docs.cachix.org/pushing#flakes
Flake support in projects without flakes
The flake-compat library provides a compatibility layer that allows projects using traditional default.nix
and shell.nix
files to operate with flakes. For more details and usage examples, see the Flake Compat page.
Another project that allows consuming flakes from non-flake projects is flake-inputs.
Accessing flakes from Nix expressions
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 (builtins.getFlake "/path/to/directory").packages.x86_64-linux.default
, where 'directory' is the directory that contains your flake.nix
.
Efficiently build multiple flake outputs
To push all flake outputs automatically, checkout devour-flake.
Build a package added in a PR
nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE>
this allows building a package that has not yet been added to nixpkgs.
note that this will download a full source tarball of nixpkgs. if you already have a local clone, using that may be faster due to delta compression:
git fetch upstream pull/<PR_NUMBER>/head && git checkout FETCH_HEAD && nix build .#PACKAGE
this allows building a package that has not yet been added to nixpkgs.
How to add a file locally in git but not include it in commits
When a git folder exists, flake will only copy files added in git to maximize reproducibility (this way if you forgot to add a local file in your repo, you will directly get an error when you try to compile it). However, for development purpose you may want to create an alternative flake file, for instance containing configuration for your preferred editors as described here… of course without committing this file since it contains only your own preferred tools. You can do so by doing something like that (say for a file called extra/flake.nix
):
git add --intent-to-add extra/flake.nix
git update-index --skip-worktree --assume-unchanged extra/flake.nix
Rapid iteration of a direct dependency
One common pain point with using Nix as a development environment is the need to completely rebuild dependencies and re-enter the dev shell every time they are updated. The nix develop --redirect <flake> <directory>
command allows you to provide a mutable dependency to your shell as if it were built by Nix.
Consider a situation where your executable, consumexe
, depends on a library, libdep
. You're trying to work on both at the same time, where changes to libdep
are reflected in real time for consumexe
. This workflow can be achieved like so:
cd ~/libdep-src-checkout/
nix develop # Or `nix-shell` if applicable.
export prefix="./install" # configure nix to install it here
buildPhase # build it like nix does
installPhase # install it like nix does
Now that you've built the dependency, consumexe
can take it as an input. In another terminal:
cd ~/consumexe-src-checkout/
nix develop --redirect libdep ~/libdep-src-checkout/install
echo $buildInputs | tr " " "\n" | grep libdep
# Output should show ~/libdep-src-checkout/ so you know it worked
If Nix warns you that your redirected flake isn't actually used as an input to the evaluated flake, try using the --inputs-from .
flag. If all worked well you should be able to buildPhase && installPhase
when the dependency changes and rebuild your consumer with the new version without exiting the development shell.
See also
Official sources
- Flakes - nix.dev
- Nix flake command reference manual - Many additional details about flakes, and their parts.
- RFC 49 (2019) - Original flakes specification
Guides
- Flakes aren't real and can't hurt you (Jade Lovelace, 2024)
- NixOS & Flakes Book(Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners.
- Nix Flakes: an Introduction (Xe Iaso, 2022)
- Practical Nix Flakes (Alexander Bantyev, 2021) - Intro article on working with Nix and Flakes
- Nix Flakes, Part 1: An introduction and tutorial (Eelco Dolstra, 2020)
- Nix Flakes, Part 2: Evaluation caching (Eelco Dolstra, 2020)
- Nix Flakes, Part 3: Managing NixOS systems (Eelco Dolstra, 2020)
- Nix flakes 101: Introduction to nix flakes (Jörg Thalheim, 2020) YouTube video
Useful flake modules
- flake-utils: Library to avoid some boiler-code when writing flakes
- flake-parts: Library to help write modular and organized flakes
- flake-compat: A compatibility layer for flakes
References
- ↑ Nix Reference Manual, §13.8. Experimental Features, 📖︎ flakes subsection
- ↑ Nix Reference Manual, §14.27. 📖︎ Release 2.4 (2021-11-01)
- ↑ Nix Reference Manual, §8.5.17. nix flake, 📖︎ URL-like syntax subsection
- ↑ Nix Reference Manual, §8.5.62. 📖︎ nix registry
- ↑ Nix Reference Manual, §7.5.19. 📖︎ nix flake lock
- ↑ Nix Reference Manual, §7.5.17. 📖︎ nix flake info
- ↑ Nix Reference Manual, §8.5.1. 📖︎ nix