Flakes: Difference between revisions
refactor and organize layout; include sections on multiple architectures and devshells |
m the flakes page is the most popular page, including a lot of links to improve seo |
||
| Line 7: | Line 7: | ||
<!--T:3--> | <!--T:3--> | ||
Nix flakes enforce a uniform structure for Nix projects, pin versions of their dependencies in a lock file, and make it more convenient to write reproducible Nix expressions. | Nix flakes enforce a uniform structure for [[Nix]] projects, pin versions of their dependencies in a lock file, and make it more convenient to write reproducible Nix expressions. | ||
<!--T:4--> | <!--T:4--> | ||
| Line 32: | Line 32: | ||
<!--T:6--> | <!--T:6--> | ||
When using any <code>nix</code> command, add the following command-line options: | When using any [[Nix command|<code>nix</code> command]], add the following command-line options: | ||
</translate> | </translate> | ||
<syntaxhighlight lang="shell"> | <syntaxhighlight lang="shell"> | ||
| Line 78: | Line 78: | ||
<!--T:20--> | <!--T:20--> | ||
{{Warning | Since contents of flake files are copied to the world-readable Nix store folder, do not put any unencrypted secrets in flake files. You should instead use a [[Comparison of secret managing schemes|secret managing scheme]].}} | {{Warning | Since contents of flake files are copied to the world-readable [[Nix_package_manager#Nix_store|Nix store]] folder, do not put any unencrypted secrets in flake files. You should instead use a [[Comparison of secret managing schemes|secret managing scheme]].}} | ||
<!--T:146--> | <!--T:146--> | ||
| Line 130: | Line 130: | ||
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}}. | ||
This flake produces a single flake output <code>packagage</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>packagage</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: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 142: | Line 142: | ||
==== Development shells ==== | ==== Development shells ==== | ||
A <code>devShell</code> 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 <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>. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 179: | Line 179: | ||
<!--T:103--> | <!--T:103--> | ||
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: | ||
<syntaxHighlight lang=console> | <syntaxHighlight lang=console> | ||
| Line 205: | Line 205: | ||
<!--T:149--> | <!--T:149--> | ||
* <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]]. | ||
=== Input schema === <!--T:31--> | === Input schema === <!--T:31--> | ||
| Line 219: | Line 219: | ||
<!--T:34--> | <!--T:34--> | ||
Nixpkgs can be defined using the following code: | [[Nixpkgs]] can be defined using the following code: | ||
</translate> | </translate> | ||
| Line 398: | Line 398: | ||
=== Enable unfree software === <!--T:129--> | === Enable unfree software === <!--T:129--> | ||
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. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 418: | Line 418: | ||
<!--T:69--> | <!--T:69--> | ||
<code>nixos-rebuild switch</code> will read its configuration from <code>/etc/nixos/flake.nix</code> if it is present. | By default, [[Nixos-rebuild|<code>nixos-rebuild switch</code>]] will read its [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration|configuration]] from <code>/etc/nixos/flake.nix</code> if it is present. | ||
<!--T:70--> | <!--T:70--> | ||
A basic | A basic [[NixOS]] flake.nix could look like this: | ||
</translate> | </translate> | ||
| Line 658: | Line 658: | ||
<!--T:128--> | <!--T:128--> | ||
An alternative approach to the above shell alias is omitting <code>repl</code> from <code>nix.nixPath</code> and creating a shell script: | An alternative approach to the above shell alias is omitting <code>repl</code> from <code>nix.nixPath</code> and creating a [[Shell Scripts|shell script]]: | ||
</translate> | </translate> | ||
| Line 729: | Line 729: | ||
<!--T:133--> | <!--T:133--> | ||
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>): | ||
</translate> | </translate> | ||
| Line 819: | Line 819: | ||
* [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | * [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | ||
* [[Flake Compat|flake-compat]]: A compatibility layer for flakes | |||
<!--T:181--> | <!--T:181--> | ||
| Line 826: | Line 828: | ||
[[Category:Software]] | [[Category:Software]] | ||
[[Category:Nix]] | [[Category:Nix]] | ||
[[Category:Nix Language]] | |||
[[Category:Flakes]] | [[Category:Flakes]] | ||