Flakes/ja: Difference between revisions

Natsukium (talk | contribs)
Created page with "Nix flakesはNixのプロジェクトに一定の構造を強制し、ロックファイルを用いて依存するプロジェクトのバージョンを指定することでより便利に再現可能なNix式を記述できるようにします。"
 
Natsukium (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit
 
(40 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<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 リリースノートを参照してください])。
'''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 リリースノートを参照してください])。


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Introduction"></span>
====Introduction====
====概要====
</div>


Nix flakesはNixのプロジェクトに一定の構造を強制し、ロックファイルを用いて依存するプロジェクトのバージョンを指定することでより便利に再現可能なNix式を記述できるようにします。
Nix flakesはNixのプロジェクトに一定の構造を強制し、ロックファイルを用いて依存するプロジェクトのバージョンを指定することでより便利に再現可能なNix式を記述できるようにします。


<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#description Flake]とは<code>flake.nix</code>というのNixファイルをルートに持つファイルシステムツリーを指します。
* A [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#description flake] refers to a file-system tree whose root directory contains the Nix file specification called <code>flake.nix</code>.
 
* The contents of <code>flake.nix</code> file follow a uniform naming schema for declaring packages and their dependencies in the Nix language.
* <code>flake.nix</code>ファイルの内容は、Nix言語でパッケージとその依存関係を宣言するための統一された命名スキーマに従っています。
Flakes introduce a [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references URL-like syntax] for specifying remote sources.
 
* To simplify the long URL syntax with shorter names, [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-registry.html flakes uses a registry] of symbolic identifiers.
Flakeは外部のソースを指定するために[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references URLのような構文]を提供しています。
* Flakes also allow for locking references and versions that can then be queried and updated programmatically.
 
* An [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html experimental command-line interface] accepts flake references for expressions that build, run, and deploy packages.
* 長いURL構文を簡易にするために[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-registry.html Flakeはレジストリを使用]して短い記号として登録できます。
</div>
 
* また、Flakesは参照(Gitのrefsの事)とバージョンを固定することができ、それらをプログラムによって照会したり更新したりすることができます。
 
* [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html 実験的なコマンドラインインタフェース]はFlakeの参照を受け取りパッケージのビルド、実行やデプロイができます。


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Enable_flakes_temporarily"></span>
====Enable flakes temporarily====
====flakesを一時的に有効にする====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
任意の<code>nix</code>コマンドを使用する場合は、次のコマンドラインオプションを追加します:
When using any <code>nix</code> command, add the following command-line options:
<syntaxhighlight lang="shell">
<syntaxhighlight lang="shell">
  --experimental-features 'nix-command flakes'
  --experimental-features 'nix-command flakes'
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Enable_flakes_permanently_in_NixOS"></span>
====Enable flakes permanently in NixOS====
====NixOSでflakesを永続的に有効にする====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |NixOS configuration]]に以下を追加します
Add the following to the [[Overview_of_the_NixOS_Linux_distribution#Declarative_Configuration system configuration |NixOS configuration]]:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
   nix.settings.experimental-features = [ "nix-command" "flakes" ];
   nix.settings.experimental-features = [ "nix-command" "flakes" ];
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Other_Distros,_with_Home-Manager"></span>
=====Other Distros, with Home-Manager=====
=====その他ディストリビューション、Home-Managerあり=====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
home-managerの設定に以下を追加します:
Add the following to your home-manager config:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
   nix.settings.experimental-features = [ "nix-command" "flakes" ];
   nix.settings.experimental-features = [ "nix-command" "flakes" ];
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Other_Distros,_without_Home-Manager"></span>
=====Other Distros, without Home-Manager=====
=====その他ディストリビューション、Home-Managerなし=====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{注記: | [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer]ではデフォルトでflakesを有効化しています。}}
{{Note | The  [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default.}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
次の内容を<code>~/.config/nix/nix.conf</code>または<code>/etc/nix/nix.conf</code>に追記してください:
Add the following to <code>~/.config/nix/nix.conf</code> or <code>/etc/nix/nix.conf</code>:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
experimental-features = nix-command flakes
experimental-features = nix-command flakes
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Basic_Usage_of_Flake"></span>
===Basic Usage of Flake===
===基本的なFlakeの使用方法===
</div>
 
この時点でnixコマンドを実行する前に、以下の2つの警告に注意してください。1つは暗号化に関するもの、もう1つはgitに関するものです。


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Encryption_WARNING"></span>
Before running any nix commands at this point, please note the two warnings below: one for encryption and the other for git.
====暗号化に関する警告====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{Warning | flakeファイルの内容は世界的に読み取り可能なNix storeフォルダにコピーされるため、暗号化されていない機密情報はflakeファイルに置かないでください。代わりに[[Comparison of secret managing schemes|secret managing scheme]]を使うべきです。}}
====Encryption WARNING====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Git_WARNING"></span>
{{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]].}}
====Gitに関する警告====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Gitリポジトリ内のフレークでは、作業ツリー内のファイルのみがNix storeにコピーされます。
====Git WARNING====
For flakes in git repos, only files in the working tree will be copied to the store.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
したがって、flakeに<code>git</code>を使用する場合は、プロジェクトファイルを作成した後、必ず<code>git add</code>を実行してください。
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">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 102: Line 77:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Generate_flake.nix_file"></span>
====Generate flake.nix file====
====flake.nixファイルを生成する====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
基本的な方法でFlakeを使い始めるにはプロジェクトディレクトリでflakeコマンドを実行します:
To start the basic usage of flake, run the flake command in the project directory:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
nix flake init
nix flake init
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
== Flake schema ==
== Flake schema ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
flake.nixファイルはNixファイルですが、特殊な制限が設けられています(これについては後述します)。
The flake.nix file is a Nix file but that has special restrictions (more on that later).
 
</div>
Flakeは4つのトップレベルアトリビュートを持ちます:
 
* <code>description</code>はflakeを説明する文字列です。
 
* <code>inputs</code>はflakeの依存関係をアトリビュートセットとして記述したものです。下にスキーマを記述しています。
 
* <code>outputs</code>はすべてのrealizeされたinputを受け取り、下に記述されたスキーマのようにアトリビュートセットを返す関数です。


<div lang="en" dir="ltr" class="mw-content-ltr">
* <code>nixConfig</code>は[https://nixos.org/manual/nix/stable/command-ref/conf-file.html nix.confに使用できる値]を反映しています。これを用いてこのflake特有の設定を反映させることができます。例としてバイナリキャッシュを設定できます。
It has 4 top-level attributes:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Input_schema"></span>
* <code>description</code> is a string describing the flake.
=== Input スキーマ ===
* <code>inputs</code> is an attribute set of all the dependencies of the flake. The schema is described below.
* <code>outputs</code> 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.
* <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.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs Nixのflake inputsのマニュアル].
=== Input schema ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references Nix flake referencesのマニュアル].
[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs The nix flake inputs manual].
[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references The nix flake references manual].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
inputsアトリビュートはflakeの依存関係を定義します。例えば、システムflakeではシステムをビルドするためにnixpkgsに依存しています。
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.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Nixpkgsは以下のコードで定義できます:
Nixpkgs can be defined using the following code:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 164: Line 121:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<code>inputs.hyprland.url = "github:hyprwm/Hyprland";</code>
<code>inputs.hyprland.url = "github:hyprwm/Hyprland";</code>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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:
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:
</div>
</div>
<code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code>
Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this:<syntaxhighlight lang="nix">
inputs = {
inputs = {
   nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";
   nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";
Line 186: Line 142:
};
};
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Output schema ===
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Output schema ===
This is described in the nix package manager [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md].
This is described in the nix package manager [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md].
</div>
</div>
Line 203: Line 161:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
* <code><system></code> is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin"
* <code><system></code> is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin"
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* <code><name></code> is an attribute name like "hello".
* <code><name></code> is an attribute name like "hello".
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* <code><flake></code> is a flake name like "nixpkgs".
* <code><flake></code> is a flake name like "nixpkgs".
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* <code><store-path></code> is a <code>/nix/store..</code> path
* <code><store-path></code> is a <code>/nix/store..</code> path
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{ self, ... }@inputs:
{ self, ... }@inputs:
Line 225: Line 191:
   # Executed by `nix run . -- <args?>`
   # Executed by `nix run . -- <args?>`
   apps."<system>".default = { type = "app"; program = "..."; };
   apps."<system>".default = { type = "app"; program = "..."; };
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   # Formatter (alejandra, nixfmt or nixpkgs-fmt)
# Formatter (alejandra, nixfmt or nixpkgs-fmt)
   formatter."<system>" = derivation;
   formatter."<system>" = derivation;
   # Used for nixpkgs packages, also accessible via `nix build .#<name>`
   # Used for nixpkgs packages, also accessible via `nix build .#<name>`
Line 258: Line 222:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 266: Line 229:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
==== nix run ====
==== nix run ====
</div>
<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>
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>
Line 281: Line 247:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
(import (
(import (
Line 291: Line 256:
}).defaultNix
}).defaultNix
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 297: Line 261:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
   inputs.flake-compat = {
   inputs.flake-compat = {
Line 304: Line 267:
   };
   };
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 310: Line 272:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
(import (
(import (
Line 325: Line 286:
) { src = ./.; }).defaultNix
) { src = ./.; }).defaultNix
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 345: Line 305:
<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.
* fetchurl and fetchtar [https://github.com/NixOS/nix/blob/36c4d6f59247826dde32ad2e6b5a9471a9a1c911/src/libexpr/primops/fetchTree.cc#L201 require] a sha256 argument to be considered pure.
</div>
<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.
* 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.
</div>
<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:
* 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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  outputs = { self, nixpkgs, ... }:
  outputs = { self, nixpkgs, ... }:
Line 361: Line 326:
   };
   };
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 391: Line 355:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
Line 403: Line 366:
}
}
</syntaxhighlight>
</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:
If you want to pass on the flake inputs to external configuration files, you can use the <code>specialArgs</code> attribute:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
   inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
   inputs.home-manager.url = github:nix-community/home-manager;
   inputs.home-manager.url = github:nix-community/home-manager;
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   outputs = { self, nixpkgs, ... }@inputs: {
outputs = { self, nixpkgs, ... }@inputs: {
     nixosConfigurations.fnord = nixpkgs.lib.nixosSystem {
     nixosConfigurations.fnord = nixpkgs.lib.nixosSystem {
       specialArgs = { inherit inputs; };
       specialArgs = { inherit inputs; };
Line 419: Line 384:
}
}
</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:
Then, you can access the flake inputs from the file <code>configuration.nix</code> like this:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, lib, inputs, ... }: {
{ config, lib, inputs, ... }: {
Line 427: Line 396:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>
 


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 433: Line 402:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ sudo nixos-rebuild switch --flake .
$ sudo nixos-rebuild switch --flake .
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 443: Line 410:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ sudo nixos-rebuild switch --flake /etc/nixos#joes-desktop
$ sudo nixos-rebuild switch --flake /etc/nixos#joes-desktop
</syntaxhighlight>
</syntaxhighlight>
</div>


<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:
To switch a remote host you can use:
</div>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ nixos-rebuild --flake .#mymachine \
$ nixos-rebuild --flake .#mymachine \
Line 457: Line 424:
   switch
   switch
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 467: Line 433:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ inputs, ... }:
{ inputs, ... }:
Line 476: Line 441:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 482: Line 446:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
   nix.registry = {
   nix.registry = {
Line 495: Line 458:
   };
   };
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 513: Line 475:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
Line 520: Line 481:
pkgs.mkShell {
pkgs.mkShell {
   packages = [ pkgs.nixfmt ];
   packages = [ pkgs.nixfmt ];
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   shellHook = ''
shellHook = ''
     # ...
     # ...
   '';
   '';
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 538: Line 496:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   outputs =
outputs =
     { nixpkgs, ... }:
     { nixpkgs, ... }:
     {
     {
       /*  
       /*
         This example assumes your system is x86_64-linux
         This example assumes your system is x86_64-linux
         change as neccesary
         change as neccesary
Line 565: Line 520:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 595: Line 549:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
https://docs.cachix.org/pushing#flakes
https://docs.cachix.org/pushing#flakes
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 615: Line 567:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix build .#hello
$ nix build .#hello
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 629: Line 579:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
nix build '.?submodules=1#hello'
nix build '.?submodules=1#hello'
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 645: Line 593:
<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:
A NixOS config flake could be as follows:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   description = "NixOS configuration with two or more channels";
   description = "NixOS configuration with two or more channels";
</div>


  <div lang="en" dir="ltr" class="mw-content-ltr">
inputs = {
inputs = {
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
     nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
     nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
   };
   };
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   outputs =
outputs =
     { nixpkgs, nixpkgs-unstable, ... }:
     { nixpkgs, nixpkgs-unstable, ... }:
     {
     {
Line 681: Line 627:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# NixOS configuration.nix, can now use "pkgs.package" or "pkgs.unstable.package"
# NixOS configuration.nix, can now use "pkgs.package" or "pkgs.unstable.package"
Line 694: Line 638:
   # ...
   # ...
}
}
</syntaxhighlight>If the variable <code>nixpkgs</code> points to the flake, you can also define <code>pkgs</code> with overlays with:
</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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ /*the overlay in question*/ ]; };
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ /*the overlay in question*/ ]; };
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 711: Line 656:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
$ nix repl
$ nix repl
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
nix-repl> :lf /path/to/flake
nix-repl> :lf /path/to/flake
Added 18 variables.
Added 18 variables.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
nix-repl> nixosConfigurations.myHost.config.networking.hostName
nix-repl> nixosConfigurations.myHost.config.networking.hostName
"myHost"
"myHost"
</div>


</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
</syntaxhighlight>
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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
However, this won't be instant upon evaluation if any file changes have been done since your last configuration rebuild. Instead, if one puts:<syntaxHighlight lang=nix>
nix.nixPath = let path = toString ./.; in [ "repl=${path}/repl.nix" "nixpkgs=${inputs.nixpkgs}" ];
nix.nixPath = let path = toString ./.; in [ "repl=${path}/repl.nix" "nixpkgs=${inputs.nixpkgs}" ];
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 741: Line 679:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
let
let
Line 754: Line 691:
// flake.nixosConfigurations
// flake.nixosConfigurations
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 761: Line 697:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
source /etc/set-environment && nix repl $(echo $NIX_PATH | perl -pe 's|.*(/nix/store/.*-source/repl.nix).*|\1|')</syntaxHighlight>
source /etc/set-environment && nix repl $(echo $NIX_PATH | perl -pe 's|.*(/nix/store/.*-source/repl.nix).*|\1|')</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 772: Line 706:
<div lang="en" dir="ltr" class="mw-content-ltr">
<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:
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>
<syntaxHighlight lang=nix>
nix.nixPath = [ "nixpkgs=${inputs.nixpkgs}" ];
nix.nixPath = [ "nixpkgs=${inputs.nixpkgs}" ];
Line 784: Line 720:
];
];
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 796: Line 731:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Development tricks ==
== Development tricks ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Build a package added in a PR ===
=== Build a package added in a PR ===
</div>
<syntaxHighlight>
<syntaxHighlight>
nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE>
nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE>
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
this allows building a package that has not yet been added to nixpkgs.
this allows building a package that has not yet been added to nixpkgs.
</div>
</div>
Line 805: Line 747:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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:
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:
</div>
<syntaxHighlight>
<syntaxHighlight>
git fetch upstream pull/<PR_NUMBER>/head && git checkout FETCH_HEAD && nix build .#PACKAGE
git fetch upstream pull/<PR_NUMBER>/head && git checkout FETCH_HEAD && nix build .#PACKAGE
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
this allows building a package that has not yet been added to nixpkgs.
this allows building a package that has not yet been added to nixpkgs.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== How to add a file locally in git but not include it in commits ===
=== How to add a file locally in git but not include it in commits ===
</div>
</div>
Line 816: Line 765:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight>
<syntaxHighlight>
git add --intent-to-add extra/flake.nix
git add --intent-to-add extra/flake.nix
git update-index --skip-worktree --assume-unchanged extra/flake.nix
git update-index --skip-worktree --assume-unchanged extra/flake.nix
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Rapid iteration of a direct dependency ===
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== 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 <code>nix develop --redirect <flake> <directory></code> command allows you to provide a mutable dependency to your shell as if it were built by Nix.
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 <code>nix develop --redirect <flake> <directory></code> command allows you to provide a mutable dependency to your shell as if it were built by Nix.
</div>
</div>
Line 832: Line 782:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
cd ~/libdep-src-checkout/
cd ~/libdep-src-checkout/
Line 840: Line 789:
installPhase # install it like nix does
installPhase # install it like nix does
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
Now that you've built the dependency, <code>consumexe</code> can take it as an input. '''In another terminal''':
Now that you've built the dependency, <code>consumexe</code> can take it as an input. '''In another terminal''':
</div>
<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
cd ~/consumexe-src-checkout/
cd ~/consumexe-src-checkout/
Line 847: Line 800:
# Output should show ~/libdep-src-checkout/ so you know it worked
# Output should show ~/libdep-src-checkout/ so you know it worked
</syntaxHighlight>
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
If Nix warns you that your redirected flake isn't actually used as an input to the evaluated flake, try using the <code>--inputs-from .</code> flag. If all worked well you should be able to <code>buildPhase && installPhase</code> when the dependency changes and rebuild your consumer with the new version ''without'' exiting the development shell.
If Nix warns you that your redirected flake isn't actually used as an input to the evaluated flake, try using the <code>--inputs-from .</code> flag. If all worked well you should be able to <code>buildPhase && installPhase</code> when the dependency changes and rebuild your consumer with the new version ''without'' exiting the development shell.
</div>
</div>
Line 856: Line 811:
<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://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">
* [https://jade.fyi/blog/flakes-arent-real/ Flakes aren't real and can't hurt you] (Jade Lovelace, 2024)
* [https://jade.fyi/blog/flakes-arent-real/ Flakes aren't real and can't hurt you] (Jade Lovelace, 2024)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book](Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners.
* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book](Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://xeiaso.net/blog/nix-flakes-1-2022-02-21 Nix Flakes: an Introduction] (Xe Iaso, 2022)
* [https://xeiaso.net/blog/nix-flakes-1-2022-02-21 Nix Flakes: an Introduction] (Xe Iaso, 2022)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://serokell.io/blog/practical-nix-flakes Practical Nix Flakes] (Alexander Bantyev, 2021) - Intro article on working with Nix and Flakes
* [https://serokell.io/blog/practical-nix-flakes Practical Nix Flakes] (Alexander Bantyev, 2021) - Intro article on working with Nix and Flakes
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, Part 1: An introduction and tutorial] (Eelco Dolstra, 2020)
* [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, Part 1: An introduction and tutorial] (Eelco Dolstra, 2020)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, Part 2: Evaluation caching] (Eelco Dolstra, 2020)
* [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, Part 2: Evaluation caching] (Eelco Dolstra, 2020)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://www.tweag.io/blog/2020-07-31-nixos-flakes/ Nix Flakes, Part 3: Managing NixOS systems] (Eelco Dolstra, 2020)
* [https://www.tweag.io/blog/2020-07-31-nixos-flakes/ Nix Flakes, Part 3: Managing NixOS systems] (Eelco Dolstra, 2020)
</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.
* [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://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020)
* [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020)
</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]
* [https://github.com/NixOS/nix/blob/master/src/nix/flake.md spec describing flake inputs in more detail]
</div>
<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]
* [https://github.com/numtide/flake-utils flake-utils: Library to avoid some boiler-code when writing flakes]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://zimbatm.com/NixFlakes/#direnv-integration zimbat's direnv article]
* [https://zimbatm.com/NixFlakes/#direnv-integration zimbat's direnv article]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes]
* [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes]
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Category:Software]]
[[Category:Software]]
[[Category:Nix]]
[[Category:Nix]]
[[Category:Flakes]]
[[Category:Flakes]]
</div>