Flakes: Difference between revisions

imported>SjLambda
Cleaned out introductory language, mostly style.
imported>SjLambda
cleaned up the basic section, made it clearer
Line 1: Line 1:
'''Nix flakes''' is an [https://nixos.org/manual/nix/stable/contributing/experimental-features.html experimental feature] of the Nix package manager.
'''Nix flakes''' is an [https://nixos.org/manual/nix/stable/contributing/experimental-features.html experimental feature] of the Nix package manager. * Flakes was introduced with Nix 2.4 ([https://nixos.org/manual/nix/unstable/release-notes/rl-2.4.html see release notes]).


== Introduction ==
====Introduction====


Flakes is a feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations. Flakes manages dependencies between Nix expressions, which are the primary protocols for specifying packages. Flakes implements these protocols in a consistent schema with a common set of policies for managing packages.
Flakes is a feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations. Flakes manages dependencies between Nix expressions, which are the primary protocols for specifying packages. Flakes implements these protocols in a consistent schema with a common set of policies for managing packages.
Line 12: Line 12:
* Flakes also allow for locking references and versions that can then be easily queried and updated programmatically.
* Flakes also allow for locking references and versions that can then be easily queried and updated programmatically.
*  [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html Nix command-line interface] accepts flake references for expressions that build, run, and deploy packages.
*  [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html Nix command-line interface] accepts flake references for expressions that build, run, and deploy packages.
* Flakes was introduced with Nix 2.4 ([https://nixos.org/manual/nix/unstable/release-notes/rl-2.4.html see release notes]).


== Enable flakes ==
====Enable flakes temporarily====


Flakes were introduced with Nix 2.4 ([https://nixos.org/manual/nix/unstable/release-notes/rl-2.4.html release notes]).
When using any <code>nix3</code> command, add the following command-line options:
 
<syntaxHighlight lang=text>
===Temporary===
--experimental-features 'nix-command flakes'
 
</syntaxHighlight>
Add <code>--experimental-features 'nix-command flakes'</code> when using any <code>nix3</code> commands.


=== Permanent ===
====Enable flakes permanently in NixOS====


==== NixOS ====
Add the following to the [https://nixos.wiki/wiki/Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration] ([https://nixos.wiki/wiki/Flakes#Using_nix_flakes_with_NixOS flakes]):
 
Add the following to your [https://nixos.wiki/wiki/Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration] ([https://nixos.wiki/wiki/Flakes#Using_nix_flakes_with_NixOS flakes]):


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 32: Line 28:
</syntaxHighlight>
</syntaxHighlight>


==== Other Distros: With Home-Manager ====
=====Other Distros, with Home-Manager=====


If you use HM, add the following to your home-manager config:
Add the following to your home-manager config:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 43: Line 39:
</syntaxHighlight>
</syntaxHighlight>


==== Other Distros: Without Home-Manager ====
=====Other Distros, without Home-Manager=====


{{ note | Flakes are already enabled if you installed Nix using the [https://github.com/DeterminateSystems/nix-installer Determinate Installer]. }}
{{Note | The  [https://github.com/DeterminateSystems/nix-installer Nix Determinate Installer enables flakes by default.}}


Add the following to <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code>:
Add the following to <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code>:
Line 53: Line 49:
</syntaxHighlight>
</syntaxHighlight>


== Basic project usage ==
===Basic Usage of Flake===


{{warning | Files that are part of the flake will be copied to the world-readable nix store when the flake is evaluated; avoid putting unencrypted secrets in your flake.}}
Before running any nix commands at this point, please note the two warnings below: one for encryption and the other for git.


In your project, run <code>nix flake init</code> to generate the flake.nix file.
====Encryption WARNING====


{{Warning | Since contents of flake files are copied to the world-readable Nix store folder, do not put any unencrypted secrets in flake files.}}
====Git WARNING====
For flakes in git repos, only files in the working tree will be copied to the store.
For flakes in git repos, only files in the working tree will be copied to the store.


Line 64: Line 63:


See also https://www.tweag.io/blog/2020-05-25-flakes/
See also https://www.tweag.io/blog/2020-05-25-flakes/
====Generate flake.nix file====
To start the basic usage of flake, run the flake command in the project directory:
<syntaxHighlight lang=text>
nix flake init
</syntaxHighlight>


== Flake schema ==
== Flake schema ==