Jump to content

Flakes/ru: Difference between revisions

From NixOS Wiki
Golits (talk | contribs)
Created page with "См. также: https://www.tweag.io/blog/2020-05-25-flakes/"
Tags: Mobile edit Mobile web edit
Golits (talk | contribs)
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages />
<languages />
'''Nix flakes''' это [https://nixos.org/manual/nix/stable/contributing/experimental-Features.html экспериментальная особенность] которая была представлена с Nix 2.4 ([https://nixos.org/manual/nix/unstable/release-notes/rl-2.4.html см. Примечания к выпуску]).


<span id="Introduction"></span>
{{Cleanup}}
==== Введение ====


Nix flakes обеспечивают единую структуру для Nix-проектов, фиксируют версии зависимостей в лок-файле и делают более удобным написание воспроизводимых Nix-выражений.
'''Nix Flakes''' (Флейки Nix) — это [[Experimental Nix features|экспериментальная функция]], впервые представленная в релизе [[Nix]] 2.4{{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)}}, которая направлена на решение ряда задач по улучшению экосистемы Nix: Флейки предоставляют единую структуру для Nix-проектов, позволяя фиксировать конкретные версии каждой зависимости, делиться этими зависимостями с помощью lock-файлов и в целом делать запись репродуцируемых Nix-выражений более удобной.


* [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#description flake]  это файловая структура, корневая директория которой содержит спецификацию Nix в файле <code>flake.nix</code>.
Флейк — это каталог, который содержит файл Nix с именем <code>flake.nix</code>, который следует очень конкретной структуре. Флейки вводят похожий на URL синтаксис{{Cite manual|nix|command-ref/new-cli/nix3-flake|number=8.5.17|title=nix flake|subsection=url-like-syntax|subtitle=URL-like syntax}} для указания удалённых ресурсов. Чтобы упростить синтаксис, флейки используют реестр символических идентификаторов{{Cite manual|nix|command-ref/new-cli/nix3-registry|number=8.5.62|title=nix registry}}, что позволяет напрямую ссылаться на ресурсы в следующей форме: <code>github:NixOS/nixpkgs</code>.


* Содержимое файла <code>flake.nix</code> следует единой схеме наименования для объявления пакетов и их зависимостей на языке Nix.
Флейки также поддерживают фиксацию ссылок и версий, которые затем могут быть запрошены и обновлены программно через 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}}. Кроме того, экспериментальная утилита командной строки принимает flake-ссылки для выражений, которые собирают, выполняют и развёртывают пакеты{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}}.


Флейки вводят [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references синтаксис, похожий на URL] для указания удалённых источников.
== Структура flake-файла ==
В простейшем виде flake-файл содержит описание флейка, набор входных зависимостей (inputs) и выход (output). Вы можете сгенерировать шаблонный flake-файл в любой момент с помощью <code>nix flake init</code>. Это создаст в текущей директории файл <code>flake.nix</code>, содержащий примерно следующее:
{{File|3=<nowiki>{
  description = "A very basic flake";


* Чтобы упростить длинный синтаксис URL с помощью более коротких названий, [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-registry.html флейки используют реестр] символических идентификаторов.
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
 
  outputs = { self, nixpkgs }: {
 
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
 
    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;


* Флейки также позволяют фиксировать ссылки и версии, которые затем могут быть запрошены и обновлены программно.
  };
}</nowiki>|name=flake.nix|lang=nix}}
В примере выше вы можете видеть описание, вход (input), указанный как репозиторий GitHub с конкретной веткой (здесь <code>nixos/nixpkgs</code> на ветке <code>nixos-unstable</code>), и выход (output), который использует этот input. Output в этом примере просто определяет, что в флейке есть один пакет для архитектуры x86_64 под именем <code>hello</code>. Даже если output вашего флейка не использует его inputs (что на практике маловероятно), output всё равно должен быть Nix-функцией.
{{Note|Флейки требуют явного указания outputs для каждой архитектуры отдельно. Для дополнительной информации смотрите соответствующий раздел ниже.}}


[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html Экспериментальный интерфейс командной строки] принимает ссылки на флейки для выражений, которые создают, запускают и развёртывают пакеты.
<div class="mw-translate-fuzzy">
=== Конфигурация Nix ===
Можно переопределить глобальную конфигурацию Nix, заданную в файле <code>nix.conf</code>, чтобы оценить работу флейка. Это может быть полезно, например, для установки бинарных кэшей, специфичных для проекта, пока глобальная конфигурация останется нетронутой. Flake-файл может содержать атрибут <code>nixConfig</code> с любыми релевантными настройками. Например, чтобы включить бинарный кэш nix-community, можно добавить:
{{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|Если вы привыкли настраивать Nix через конфигурацию NixOS, эти опции находятся под <code>nix.settings</code>, а не под <code>nix</code>. Например, вы не сможете указать автоматическую оптимизацию хранилища через <code>nix.optimisation.enable</code>.}}
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
== Setup ==
</div>


<span id="Enable_flakes_temporarily"></span>
<span id="Enabling_flakes_temporarily"></span>
<div class="mw-translate-fuzzy">
==== Временно включить поддержку Flakes ====
==== Временно включить поддержку Flakes ====
</div>


<div class="mw-translate-fuzzy">
При использовании любой команды <code>nix</code> добавьте следующие параметры командной строки:
При использовании любой команды <code>nix</code> добавьте следующие параметры командной строки:
</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="Enable_flakes_permanently_in_NixOS"></span>
<span id="NixOS"></span>
<div class="mw-translate-fuzzy">
====Включить flakes на постоянной основе в NixOS====
====Включить flakes на постоянной основе в NixOS====
</div>


Добавьте следующее в [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |Конфигурацию NixOS]]:
Добавьте следующее в [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |Конфигурацию NixOS]]:
Line 36: Line 74:
</syntaxHighlight>
</syntaxHighlight>


<span id="Other_Distros,_with_Home-Manager"></span>
<span id="Home_Manager"></span>
<div class="mw-translate-fuzzy">
===== В других Дистрибутивах, с Home-Manager =====
===== В других Дистрибутивах, с Home-Manager =====
</div>


<div class="mw-translate-fuzzy">
Добавьте следующее в свою конфигурацию Home-Manager:
Добавьте следующее в свою конфигурацию Home-Manager:
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 45: Line 87:
</syntaxhighlight>
</syntaxhighlight>


<span id="Other_Distros,_without_Home-Manager"></span>
<span id="Nix_standalone"></span>
<div class="mw-translate-fuzzy">
=====Другие Дистрибутивы, без Home-Manager=====
=====Другие Дистрибутивы, без Home-Manager=====
</div>


{{Note | [https://github.com/DeterminateSystems/nix-installer Детерминированный Установщик Nix (Determinate Nix Installer)] включает Flakes по умолчанию.}}
{{Note | [https://github.com/DeterminateSystems/nix-installer Детерминированный Установщик Nix (Determinate Nix Installer)] включает Flakes по умолчанию.}}
Line 55: Line 99:
experimental-features = nix-command flakes
experimental-features = nix-command flakes
</syntaxHighlight>
</syntaxHighlight>
<span id="Usage"></span>
<div class="mw-translate-fuzzy">
=== Основное Использование Flake ===
</div>
<div class="mw-translate-fuzzy">
{{Warning | Так как содержимое файлов Flake скопировано в общедоступную папку хранилища Nix, не храните никаких незашифрованных секретов в файлах Flake. Вам следует использовать [[Comparison of secret managing schemes|схемы управления секретами]].}}
</div>
<div class="mw-translate-fuzzy">
Для флейков в репозиториях git, только файлы из рабочего дерева (Working tree) будут скопированы в хранилище
</div>
<div class="mw-translate-fuzzy">
Поэтому если вы используете git для ваших флейков, не забывайте применять <code>git add</code> ко всем файлам репозитория, после того как вы их создаёте
</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>


<span id="Basic_Usage_of_Flake"></span>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Основное Использование Flake ===
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 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>


Прежде чем выполнять какие-либо команды nix на этом этапе, обратите внимание на два предупреждения ниже: одно для шифрования, другое для git.
<div lang="en" dir="ltr" class="mw-content-ltr">
==== Development shells ====
</div>


<span id="Encryption_WARNING"></span>
<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>


{{Warning | Так как содержимое файлов Flake скопировано в общедоступную папку хранилища Nix, не храните никаких незашифрованных секретов в файлах Flake. Вам следует использовать [[Comparison of secret managing schemes|схемы управления секретами]].}}
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
{
  description = "Example flake with a devShell";
</div>


<span id="Git_WARNING"></span>
  <div lang="en" dir="ltr" class="mw-content-ltr">
====Внимание: Git====
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
</div>


Для флейков в репозиториях git, только файлы из рабочего дерева (Working tree) будут скопированы в хранилище
  <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>


Поэтому если вы используете git для ваших флейков, не забывайте применять <code>git add</code> ко всем файлам репозитория, после того как вы их создаёте
<div lang="en" dir="ltr" class="mw-content-ltr">
To enter the development shell environment:
</div>


См. также: https://www.tweag.io/blog/2020-05-25-flakes/
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
$ nix develop
</syntaxhighlight>
</div>


<span id="Generate_flake.nix_file"></span>
<div lang="en" dir="ltr" class="mw-content-ltr">
====Создание файла flake.nix====
{{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>


Чтобы начать базовое использование Flakes, выполните команду flake в каталоге проекта:
<div lang="en" dir="ltr" class="mw-content-ltr">
==== Build specific attributes in a flake repository ====
</div>


<syntaxHighlight lang=text>
<div lang="en" dir="ltr" class="mw-content-ltr">
nix flake init
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 lang="en" dir="ltr" class="mw-content-ltr">
<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>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 107: Line 232:


<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 126: Line 251:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Nixpkgs can be defined using the following code:
[[Nixpkgs]] can be defined using the following code:
</div>
</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 160: Line 292:
};
};
</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 242: Line 384:
You can also define additional arbitrary attributes, but these are the outputs that Nix knows about.
You can also define additional arbitrary attributes, but these are the outputs that Nix knows about.
</div>
</div>
<span id="nix_run"></span>
==== `nix run` ====
<div lang="en" dir="ltr" class="mw-content-ltr">
When output <code>apps.<system>.myapp</code> is not defined, <code>nix run myapp</code> runs <code><packages or legacyPackages.<system>.myapp>/bin/<myapp.meta.mainProgram or myapp.pname or myapp.name (the non-version part)></code>
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
== Using flakes with stable Nix ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
There exists the [https://github.com/edolstra/flake-compat flake-compat] library that you can use to shim <code>default.nix</code> and <code>shell.nix</code> files. It will download the inputs of the flake, pass them to the flake’s <code>outputs</code> function and return an attribute set containing <code>defaultNix</code> and <code>shellNix</code> attributes. The attributes will contain the output attribute set with an extra <code>default</code> attribute pointing to current platform’s <code>defaultPackage</code> (resp. <code>devShell</code> for <code>shellNix</code>).
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
Place the following into <code>default.nix</code> (for <code>shell.nix</code>, replace <code>defaultNix</code> with <code>shellNix</code>) to use the shim:
</div>
<syntaxHighlight lang=nix>
(import (
  fetchTarball {
    url = "https://github.com/edolstra/flake-compat/archive/12c64ca55c1014cdc1b16ed5a804aa8576601ff2.tar.gz";
    sha256 = "0jm6nzb83wa6ai17ly9fzpqc40wg1viib8klq8lby54agpl213w5"; }
) {
  src =  ./.;
}).defaultNix
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
You can also use the lockfile to make updating the hashes easier using <code>nix flake lock --update-input flake-compat</code>. Add the following to your <code>flake.nix</code>:
== Core usage patterns ==
</div>
</div>
<syntaxHighlight lang=nix>
  inputs.flake-compat = {
    url = "github:edolstra/flake-compat";
    flake = false;
  };
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
and add <code>flake-compat</code> to the arguments of <code>outputs</code> attribute. Then you will be able to use <code>default.nix</code> like the following:
=== Making your evaluations pure ===
</div>
</div>
<syntaxhighlight lang="nix">
(import (
  let
    lock = builtins.fromJSON (builtins.readFile ./flake.lock);
    nodeName = lock.nodes.root.inputs.flake-compat;
  in
  fetchTarball {
    url =
      lock.nodes.${nodeName}.locked.url
        or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz";
    sha256 = lock.nodes.${nodeName}.locked.narHash;
  }
) { src = ./.; }).defaultNix
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Accessing flakes from Nix expressions ==
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">
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>.
* {{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">
== Making your evaluations pure ==
* <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">
Nix flakes run in pure evaluation mode, which is underdocumented. Some tips for now:
*  <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
* fetchurl and fetchtar [https://github.com/NixOS/nix/blob/36c4d6f59247826dde32ad2e6b5a9471a9a1c911/src/libexpr/primops/fetchTree.cc#L201 require] a sha256 argument to be considered pure.
=== 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">
* builtins.currentSystem is non-hermetic and impure. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it.
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">
* Imports from channels like <code><nixpkgs></code> can be made pure by instead importing from the <code>output</code> function in <code>flake.nix</code>, where the arguments provide the store path to the flake's inputs:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
outputs = { self, nixpkgs, ... }:
{
   {
   description = "A flake targeting multiple architectures";
    nixosConfigurations.machine = nixpkgs.lib.nixosSystem {
      modules = [
        "${nixpkgs}/nixos/modules/<some-module>.nix"
        ./machine.nix
      ];
    };
  };
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
== The nix flakes command ==
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
  <div lang="en" dir="ltr" class="mw-content-ltr">
The {{ic|nix flake}} subcommand is described in [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html command reference page of the unstable manual].
inputs = {
</div>
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
 
<span id="Install_packages_with_`nix_profile`"></span>
== Установка пакетов с помощью `nix-profile` ==
 
[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-profile-install.html <code>nix profile install</code> в документации]
 
<div lang="en" dir="ltr" class="mw-content-ltr">
== Using nix flakes with NixOS ==
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
{{Ic|nixos-rebuild switch}} will read its configuration from <code>/etc/nixos/flake.nix</code> if it is present.
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
A basic nixos flake.nix could look like this:
</div>
 
<syntaxhighlight lang="nix">
{
  inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
  outputs = { self, nixpkgs }: {
    # replace 'joes-desktop' with your hostname here.
    nixosConfigurations.joes-desktop = nixpkgs.lib.nixosSystem {
      modules = [ ./configuration.nix ];
    };
   };
   };
}
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
If you want to pass on the flake inputs to external configuration files, you can use the <code>specialArgs</code> attribute:
</div>
</div>


<syntaxhighlight lang="nix">
  <div lang="en" dir="ltr" class="mw-content-ltr">
{
outputs = { self, nixpkgs }: let
  inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
    systems = [ "x86_64-linux" "aarch64-linux" ];
  inputs.home-manager.url = github:nix-community/home-manager;
    forAllSystems = f: builtins.listToAttrs (map (system: {
 
      name = system;
   outputs = { self, nixpkgs, ... }@inputs: {
      value = f system;
     nixosConfigurations.fnord = nixpkgs.lib.nixosSystem {
    }) systems);
       specialArgs = { inherit inputs; };
   in {
       modules = [ ./configuration.nix ];
    packages = forAllSystems (system: let
     };
      pkgs = nixpkgs.legacyPackages.${system};
     in {
       hello = pkgs.hello;
       default = pkgs.hello;
     });
   };
   };
}
}
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
Then, you can access the flake inputs from the file <code>configuration.nix</code> like this:
</div>
<syntaxhighlight lang="nix">
{ config, lib, inputs, ... }: {
  # do something with home-manager here, for instance:
  imports = [ inputs.home-manager.nixosModules.default ];
  ...
}
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
{{Ic|nixos-rebuild}} also allows to specify different flake using the <code>--flake</code> flag (# is optional):
</div>
</div>
<syntaxhighlight lang="console">
$ sudo nixos-rebuild switch --flake .
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
By default nixos-rebuild will use the currents system hostname to lookup the right nixos configuration in <code>nixosConfigurations</code>. You can also override this by using appending it to the flake parameter:
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>
<syntaxhighlight lang="console">
$ sudo nixos-rebuild switch --flake /etc/nixos#joes-desktop
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
To switch a remote host you can use:
=== Using overlays ===
</div>
</div>  
 
<syntaxhighlight lang="bash">
$ nixos-rebuild --flake .#mymachine \
  --target-host mymachine-hostname \
  --build-host mymachine-hostname --fast \
  switch
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
{{warning|Remote building seems to have an issue that's [https://github.com/NixOS/nixpkgs/issues/134952#issuecomment-1367056358 resolved by setting the <code>--fast</code> flag].}}
To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Enable_unfree_software"></span>
== Pinning the registry on NixOS ==
<div class="mw-translate-fuzzy">
</div>
== Включить несвободное ПО ==
 
<syntaxhighlight lang="nix">
{ inputs, ... }:
{
nix.registry = {
    nixpkgs.flake = inputs.nixpkgs;
  };
}
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
To make sure the registry entry is "locked", use the following:
</div>
</div>
<syntaxHighlight lang=nix>
  nix.registry = {
    nixpkgs.to = {
      type = "path";
      path = pkgs.path;
      narHash = builtins.readFile
          (pkgs.runCommandLocal "get-nixpkgs-hash"
            { nativeBuildInputs = [ pkgs.nix ]; }
            "nix-hash --type sha256 --sri ${pkgs.path} > $out");
    };
  };
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
This has the unfortunate side-effect of requiring import-from-derivation and slowing down build times, however it may greatly speed up almost every eval. Full-time flakes users may be able to just use <code>narHash = pkgs.narHash</code>.
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>
<span id="Super_fast_nix-shell"></span>
== Супер быстрая nix-shell ==


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
A feature of the nix Flake edition is that Nix evaluations are cached.
</div>
Допустим, в вашем проекте есть файл <code>shell.nix</code>, который выглядит следующим образом:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
  pkgs ? import <nixpkgs> { },
   inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
}:
   outputs = { self, nixpkgs, flake-compat }:
pkgs.mkShell {
     let
  packages = [ pkgs.nixfmt ];
       system = "x86_64-linux";
 
       pkgs = import nixpkgs { inherit system; config.allowUnfree = true;};
  shellHook = ''
    in {
    # ...
      ...
  '';
}
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Running nix-shell can be a bit slow and take 1-3 seconds.
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Now create a <code>flake.nix</code> file in the same repository:
</div>
 
<syntaxhighlight lang="nix">
{
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
 
   outputs =
    { nixpkgs, ... }:
     {
       /*
        This example assumes your system is x86_64-linux
        change as neccesary
      */
       devShells.x86_64-linux =
        let
          pkgs = nixpkgs.legacyPackages.x86_64-linux;
        in
        {
          default = pkgs.mkShell {
            packages = [ pkgs.hello ];
          };
        };
     };
     };
}
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
( If you're in a git repository run `git add flake.nix` so that Nix recognizes it. )
== NixOS configuration with flakes ==
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
And finally, run <code>nix develop</code>. This is what replaces the old nix-shell invocation.
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">
Exit and run again, this command should now be super fast.
For details and examples, see [[NixOS system configuration#Defining NixOS as a flake]].
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Development_tricks"></span>
{{warning|TODO: there is an alternative version where the defaultPackage is a pkgs.buildEnv that contains all the dependencies. And then nix shell is used to open the environment.}}
== Трюки для разработки ==
</div>


<span id="Automatically_switch_nix_shells_with_nix-direnv"></span>
<span id="Automatically_switch_nix_shells_with_direnv"></span>
<div class="mw-translate-fuzzy">
=== Автоматическое переключение оболочек nix с nix-direnv ===
=== Автоматическое переключение оболочек nix с nix-direnv ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
You can easily switch nix shells when you cd into different projects with [https://github.com/nix-community/nix-direnv nix-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 [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 560: Line 515:


<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].
=== 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">
== Build specific attributes in a flake repository ==
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">
When in the repository top-level, run <code>nix build .#<attr></code>. It will look in the <code>legacyPackages</code> and <code>packages</code> output attributes for the corresponding derivation.
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">
Eg, in nixpkgs:
=== Accessing flakes from Nix expressions ===
</div>
</div>
<syntaxHighlight lang=console>
$ nix build .#hello
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Building flakes from a Git repo url with submodules ===
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 lang="en" dir="ltr" class="mw-content-ltr">
As per nix 2.9.1, git submodules in package <code>src</code>'s won't get copied to the nix store, this may cause the build to fail. To workaround this, use:
</div>
</div>
<syntaxhighlight lang="console">
nix build '.?submodules=1#hello'
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
See: https://github.com/NixOS/nix/pull/5434
=== Efficiently build multiple flake outputs ===
</div>
</div>
<span id="Importing_packages_from_multiple_nixpkgs_branches"></span>
== Импорт пакетов из нескольких веток nixpkgs ==


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
A NixOS config flake could be as follows:
To push ''all'' flake outputs automatically, checkout [https://github.com/srid/devour-flake#usage devour-flake].
</div>
 
<syntaxhighlight lang="nix">
{
  description = "NixOS configuration with two or more channels";
 
inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
 
  outputs =
    { nixpkgs, nixpkgs-unstable, ... }:
    {
      nixosConfigurations."<hostname>" = nixpkgs.lib.nixosSystem {
        modules = [
          {
            nixpkgs.overlays = [
              (final: prev: {
                unstable = nixpkgs-unstable.legacyPackages.${prev.system};
                # use this variant if unfree packages are needed:
                # unstable = import nixpkgs-unstable {
                #  inherit system;
                #  config.allowUnfree = true;
                # };
              })
            ];
          }
          ./configuration.nix
        ];
      };
    };
}
</syntaxhighlight>
 
<syntaxhighlight lang="nix">
# NixOS configuration.nix, can now use "pkgs.package" or "pkgs.unstable.package"
{ pkgs, ... }:
{
  environment.systemPackages = [
    pkgs.firefox
    pkgs.unstable.chromium
  ];
  # ...
}
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
If the variable <code>nixpkgs</code> points to the flake, you can also define <code>pkgs</code> with overlays with:
</div>
 
<syntaxhighlight lang="nix">
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ /*the overlay in question*/ ]; };
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
== Getting ''Instant'' System Flakes Repl ==
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
How to get a nix repl out of your system flake:
</div>
 
<syntaxhighlight lang="text">
$ nix repl
 
nix-repl> :lf /path/to/flake
Added 18 variables.
 
nix-repl> nixosConfigurations.myHost.config.networking.hostName
"myHost"
 
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
However, this won't be instant upon evaluation if any file changes have been done since your last configuration rebuild. Instead, if one puts:
</div>
 
<syntaxHighlight lang=nix>
nix.nixPath = let path = toString ./.; in [ "repl=${path}/repl.nix" "nixpkgs=${inputs.nixpkgs}" ];
</syntaxHighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
In their system <code>flake.nix</code> configuration file, and includes the following file in their root directory flake as <code>repl.nix</code>:
</div>
</div>
<syntaxHighlight lang=nix>
let
  flake = builtins.getFlake (toString ./.);
  nixpkgs = import <nixpkgs> { };
in
{ inherit flake; }
// flake
// builtins
// nixpkgs
// nixpkgs.lib
// flake.nixosConfigurations
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
(Don't forget to <code>git add repl.nix && nixos-rebuild  switch --flake "/etc/nixos"</code>)
Then one can run (or bind a shell alias):
</div>
<syntaxHighlight lang=bash>
source /etc/set-environment && nix repl $(echo $NIX_PATH | perl -pe 's|.*(/nix/store/.*-source/repl.nix).*|\1|')</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
This will launch a repl with access to <code>nixpkgs</code>, <code>lib</code>, and the <code>flake</code> options in a split of a second.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
An alternative approach to the above shell alias is omitting <code>repl</code> from <code>nix.nixPath</code> and creating a shell script:
</div>
<syntaxHighlight lang=nix>
nix.nixPath = [ "nixpkgs=${inputs.nixpkgs}" ];
environment.systemPackages = let
  repl_path = toString ./.;
  my-nix-fast-repl = pkgs.writeShellScriptBin "my-nix-fast-repl" ''
    source /etc/set-environment
    nix repl "${repl_path}/repl.nix" "$@"
  '';
in [
  my-nix-fast-repl
];
</syntaxHighlight>
<span id="Enable_unfree_software"></span>
== Включить несвободное ПО ==
<div lang="en" dir="ltr" class="mw-content-ltr">
Refer to [[Unfree software|Unfree Software]].
</div>
<span id="Development_tricks"></span>
== Трюки для разработки ==


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 767: Line 571:


<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 812: Line 616:
<span id="See_also"></span>
<span id="See_also"></span>
== См. также ==
== См. также ==
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Official sources ===
</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 850: Line 670:


<div lang="en" dir="ltr" class="mw-content-ltr">
<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.
* [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">
* [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020)
=== 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">
* [https://github.com/NixOS/nix/blob/master/src/nix/flake.md spec describing flake inputs in more detail]
* [[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">
* [https://github.com/numtide/flake-utils flake-utils: Library to avoid some boiler-code when writing flakes]
* [[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://zimbatm.com/NixFlakes/#direnv-integration zimbat's direnv article]
* [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">
* [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes]
{{references}}
</div>
</div>


[[Category:Software]]
[[Category:Software]]
[[Category:Nix]]
[[Category:Nix]]
[[Category:Nix Language]]
[[Category:Flakes]]
[[Category:Flakes]]

Latest revision as of 13:50, 10 August 2025

⚟︎
This article or section needs cleanup. Please edit the article, paying special attention to fixing any formatting issues, inconsistencies, grammar, or phrasing. Make sure to consult the Manual of Style for guidance.

Nix Flakes (Флейки Nix) — это экспериментальная функция, впервые представленная в релизе Nix 2.4[1][2], которая направлена на решение ряда задач по улучшению экосистемы Nix: Флейки предоставляют единую структуру для Nix-проектов, позволяя фиксировать конкретные версии каждой зависимости, делиться этими зависимостями с помощью lock-файлов и в целом делать запись репродуцируемых Nix-выражений более удобной.

Флейк — это каталог, который содержит файл Nix с именем flake.nix, который следует очень конкретной структуре. Флейки вводят похожий на URL синтаксис[3] для указания удалённых ресурсов. Чтобы упростить синтаксис, флейки используют реестр символических идентификаторов[4], что позволяет напрямую ссылаться на ресурсы в следующей форме: github:NixOS/nixpkgs.

Флейки также поддерживают фиксацию ссылок и версий, которые затем могут быть запрошены и обновлены программно через inputs[5][6]. Кроме того, экспериментальная утилита командной строки принимает flake-ссылки для выражений, которые собирают, выполняют и развёртывают пакеты[7].

Структура flake-файла

В простейшем виде flake-файл содержит описание флейка, набор входных зависимостей (inputs) и выход (output). Вы можете сгенерировать шаблонный flake-файл в любой момент с помощью nix flake init. Это создаст в текущей директории файл flake.nix, содержащий примерно следующее:

❄︎ flake.nix
{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }: {

    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;

    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;

  };
}

В примере выше вы можете видеть описание, вход (input), указанный как репозиторий GitHub с конкретной веткой (здесь nixos/nixpkgs на ветке nixos-unstable), и выход (output), который использует этот input. Output в этом примере просто определяет, что в флейке есть один пакет для архитектуры x86_64 под именем hello. Даже если output вашего флейка не использует его inputs (что на практике маловероятно), output всё равно должен быть Nix-функцией.

Note: Флейки требуют явного указания outputs для каждой архитектуры отдельно. Для дополнительной информации смотрите соответствующий раздел ниже.

Конфигурация Nix

Можно переопределить глобальную конфигурацию Nix, заданную в файле nix.conf, чтобы оценить работу флейка. Это может быть полезно, например, для установки бинарных кэшей, специфичных для проекта, пока глобальная конфигурация останется нетронутой. Flake-файл может содержать атрибут nixConfig с любыми релевантными настройками. Например, чтобы включить бинарный кэш nix-community, можно добавить:

❄︎ flake.nix
{
  ...
  nixConfig = {
    extra-substituters = [
      "https://nix-community.cachix.org"
    ];
    extra-trusted-public-keys = [
      "nix-community.cachix.org-1:...="
    ];
}
Note: Если вы привыкли настраивать Nix через конфигурацию NixOS, эти опции находятся под nix.settings, а не под nix. Например, вы не сможете указать автоматическую оптимизацию хранилища через nix.optimisation.enable.

Setup

Временно включить поддержку Flakes

При использовании любой команды nix добавьте следующие параметры командной строки:

 --experimental-features 'nix-command flakes'

Enabling flakes permanently

Включить flakes на постоянной основе в NixOS

Добавьте следующее в Конфигурацию NixOS:

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

В других Дистрибутивах, с Home-Manager

Добавьте следующее в свою конфигурацию Home-Manager:

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

Другие Дистрибутивы, без Home-Manager

Добавьте следующее в ~/.config/nix/nix.conf или /etc/nix/nix.conf:

experimental-features = nix-command flakes

Основное Использование Flake

⚠︎
Warning: Так как содержимое файлов Flake скопировано в общедоступную папку хранилища Nix, не храните никаких незашифрованных секретов в файлах Flake. Вам следует использовать схемы управления секретами.

Для флейков в репозиториях git, только файлы из рабочего дерева (Working tree) будут скопированы в хранилище

Поэтому если вы используете git для ваших флейков, не забывайте применять git add ко всем файлам репозитория, после того как вы их создаёте

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
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).

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.

Flake schema

The flake.nix file is a Nix file but that has special restrictions (more on that later).

It has 4 top-level attributes:

  • description это строка описывающая flake.
  • inputs is an attribute set of all the dependencies of the flake. The schema is described below.
  • outputs is a function of one argument that takes an attribute set of all the realized inputs, and outputs another attribute set whose schema is described below.
  • 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.

Входная схема

The inputs attribute defines the dependencies of the flake. For example, nixpkgs has to be defined as a dependency for a system flake in order for the system to build properly.

Nixpkgs can be defined using the following code:

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.

В которой:

  • <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.

Включить несвободное ПО

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.

Трюки для разработки

Автоматическое переключение оболочек nix с nix-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.

См. также

Official sources

  • RFC 49 (2019) - Original flakes specification

Guides

  • NixOS & Flakes Book(Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners.

Useful flake modules

  • flake-utils: Library to avoid some boiler-code when writing flakes
  • flake-parts: Library to help write modular and organized flakes

References

  1. Nix Reference Manual, §13.8. Experimental Features, 📖︎ flakes subsection
  2. Nix Reference Manual, §14.27. 📖︎ Release 2.4 (2021-11-01)
  3. Nix Reference Manual, §8.5.17. nix flake, 📖︎ URL-like syntax subsection
  4. Nix Reference Manual, §8.5.62. 📖︎ nix registry
  5. Nix Reference Manual, §7.5.19. 📖︎ nix flake lock
  6. Nix Reference Manual, §7.5.17. 📖︎ nix flake info
  7. Nix Reference Manual, §8.5.1. 📖︎ nix