Flakes: Difference between revisions

imported>Fricklerhandwerk
mNo edit summary
imported>Fricklerhandwerk
make a more dispassionate introduction
Line 3: Line 3:
== Introduction ==
== Introduction ==


Flakes allow you to specify your code's dependencies (e.g. remote Git repositories) in a declarative way, simply by listing them inside a <code>flake.nix</code> file:
The flakes experimental feature introduces a policy for managing dependencies between Nix expressions and adds supporting convenience functionality.


<syntaxHighlight lang=nix>
Technically, a [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#description flake] is a file system tree that contains a Nix file named <code>flake.nix</code> in its root directory.
{
  inputs = {
    home-manager.url = "github:nix-community/home-manager";
  };
}
</syntaxHighlight>


Each dependency gets pinned, that is: its commit hash gets automatically stored into a file - named <code>flake.lock</code> - making it easy to, say, upgrade it:
Flakes add the following behavior to Nix:


<syntaxHighlight lang=console>
# A <code>flake.nix</code> file offers a uniform [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-format schema], where:
$ nix flake lock --update-input home-manager
#* Other flakes can be referenced as dependencies providing Nix language code or other files.
</syntaxHighlight>
#* The values produced by the Nix expression in <code>flake.nix</code> are structured according to pre-defined use cases.
 
# References to other flakes can be specified using a dedicated [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references URL-like syntax]. A [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-registry.html flake registry] allows using symbolic identifiers for further brevity. References can be automatically locked to their current specific version and later updated programmatically.
''(if you're familiar with modern packages managers like <code>cargo</code> or <code>npm</code>, then the overall mechanism shouldn't surprise you - Nix works in a similar way, although without a centralized repository.)''
# A [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html new command line interface], implemented as a separate experimental feature, leverages flakes by accepting flake references in order to build, run, or deploy software defined as a flake.
 
Flakes replace the nix-channels command and things like ad-hoc invocations of <code>builtins.fetchgit</code> - no more worrying about keeping your channels in sync, no more worrying about forgetting about a dependency deep down in your tree: everything's at hand right inside <code>flake.lock</code>.


== Enable flakes ==
== Enable flakes ==