Flakes: Difference between revisions
m fix broken link |
m grammar fix |
||
| (13 intermediate revisions by 8 users not shown) | |||
| Line 5: | Line 5: | ||
<translate> | <translate> | ||
<!--T:182--> | <!--T:182--> | ||
'''Nix flakes''' are an [ | '''Nix flakes''' are an [https://nix.dev/manual/nix/stable/development/experimental-features experimental feature] first introduced in the 2.4 [[Nix]] release,{{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)}} aiming to address a number of areas of improvement for the Nix ecosystem: they provide a uniform structure for Nix projects, allow for pinning specific versions of each dependency, and sharing these dependencies via lock files, and overall make it more convenient to write reproducible Nix expressions. | ||
<!--T:183--> | <!--T:183--> | ||
| Line 11: | Line 11: | ||
<!--T:184--> | <!--T:184--> | ||
Flakes also allow for locking references and versions, which can then be queried and updated | Flakes also allow for locking references and versions, which can then be queried and updated programmatically via the 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}}. Additionally, an experimental CLI utility accepts flake references for expressions that build, run, and deploy packages.{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}} | ||
== Flake file structure == <!--T:185--> | == Flake file structure == <!--T:185--> | ||
<!--T:231--> | <!--T:231--> | ||
Minimally, a flake file contains a description of the flake, a set of input dependencies and an output. You can generate a very basic flake file | Minimally, a flake file contains a description of the flake, a set of input dependencies and an output. You can generate a very basic flake file after [[#Setup]] using <code>nix flake init</code>. This will populate the current directory with a file called flake.nix that will contain something akin to: | ||
</translate> | </translate> | ||
| Line 72: | Line 72: | ||
<syntaxhighlight lang="shell"> | <syntaxhighlight lang="shell"> | ||
--experimental-features 'nix-command flakes' | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 81: | Line 81: | ||
<!--T:8--> | <!--T:8--> | ||
Add the following to the [[ | Add the following to the [[NixOS system configuration#Usage|NixOS configuration]]: | ||
</translate> | </translate> | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
nix.settings.experimental-features = [ "nix-command" "flakes" ]; | |||
</syntaxHighlight> | </syntaxHighlight> | ||
| Line 96: | Line 96: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
nix.settings.experimental-features = [ "nix-command" "flakes" ]; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 103: | Line 103: | ||
<!--T:14--> | <!--T:14--> | ||
{{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default.}} | {{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default, but installs the proprietary Determinate Nix.}} | ||
<!--T:15--> | <!--T:15--> | ||
| Line 135: | Line 135: | ||
<!--T:194--> | <!--T:194--> | ||
This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system- | This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system-specific 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: | ||
</translate> | </translate> | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix flake show | $ nix flake show | ||
path:/path/to/flake | |||
└───packages | └───packages | ||
└───x86_64-linux | └───x86_64-linux | ||
├───default: package 'hello-2.12. | ├───default: package 'hello-2.12.3' | ||
└───hello: package 'hello-2.12. | └───hello: package 'hello-2.12.3' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 222: | Line 223: | ||
<!--T:149--> | <!--T:149--> | ||
* <code>nixConfig</code> is an attribute set of values which reflect the [https:// | * <code>nixConfig</code> is an attribute set of values which reflect the [https://nix.dev/manual/nix/stable/command-ref/conf-file.html values given to nix.conf]. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a [[Binary Cache|binary cache]]. | ||
=== Input schema === <!--T:31--> | === Input schema === <!--T:31--> | ||
<!--T:32--> | <!--T:32--> | ||
[https:// | [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs The nix flake inputs manual]. | ||
<!--T:150--> | <!--T:150--> | ||
[https:// | [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references The nix flake references manual]. | ||
<!--T:33--> | <!--T:33--> | ||
| Line 289: | Line 290: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
inputs.self.submodules = true; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 296: | Line 297: | ||
<!--T:151--> | <!--T:151--> | ||
The output schema is described the [https://nix.dev/manual/nix/ | The output schema is described the [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake-check.html#evaluation-checks nix flake check manual page]. | ||
<!--T:43--> | <!--T:43--> | ||
| Line 396: | Line 397: | ||
description = "A flake targeting multiple architectures"; | description = "A flake targeting multiple architectures"; | ||
inputs = { | inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
}; | }; | ||
outputs = { self, nixpkgs }: let | outputs = { self, nixpkgs }: let | ||
systems = [ "x86_64-linux" "aarch64-linux" ]; | |||
forAllSystems = f: builtins.listToAttrs (map (system: { | |||
name = system; | |||
value = f system; | |||
}) systems); | |||
in { | in { | ||
hello = pkgs.hello; | packages = forAllSystems (system: let | ||
pkgs = nixpkgs.legacyPackages.${system}; | |||
in { | |||
hello = pkgs.hello; | |||
default = pkgs.hello; | |||
}); | |||
}; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 429: | Line 430: | ||
<!--T:218--> | <!--T:218--> | ||
To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config. | To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config.allowUnfree = true;</code> when importing Nixpkgs. | ||
</translate> | </translate> | ||
| Line 438: | Line 439: | ||
let | let | ||
system = "x86_64-linux"; | system = "x86_64-linux"; | ||
pkgs = import nixpkgs { inherit system; config.allowUnfree = true;}; | pkgs = import nixpkgs { inherit system; config.allowUnfree = true; }; | ||
in { | in { | ||
... | ... | ||
| Line 468: | Line 469: | ||
<translate> | <translate> | ||
=== Flake support in projects without flakes === <!--T:50--> | === Flake support in projects without flakes === <!--T:50--> | ||
{{Tip|Consider building your project in a manner where the <code>flake.nix</code> is merely <code>import</code>ing or <code>callPackage</code>ing stable Nix code to maximize compatibility for everyone as well as avoiding adding workaround dependencies to your project}} | |||
<!--T:51--> | <!--T:51--> | ||
| Line 488: | Line 491: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang=console> | ||
nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE> | $ nix build github:nixos/nixpkgs?ref=pull/<PR_NUMBER>/head#<PACKAGE> | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
| Line 500: | Line 503: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang=console> | ||
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> | ||
<translate> | <translate> | ||
| Line 514: | Line 517: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang=console> | ||
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> | ||
<translate> | <translate> | ||
| Line 529: | Line 532: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang=console> | ||
cd ~/libdep-src-checkout/ | $ cd ~/libdep-src-checkout/ | ||
nix develop # Or `nix-shell` if applicable. | $ nix develop # Or `nix-shell` if applicable. | ||
export prefix="./install" # configure nix to install it here | $ export prefix="./install" # configure nix to install it here | ||
buildPhase # build it like nix does | $ buildPhase # build it like nix does | ||
installPhase # install it like nix does | $ installPhase # install it like nix does | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
| Line 542: | Line 545: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang=console> | ||
cd ~/consumexe-src-checkout/ | $ cd ~/consumexe-src-checkout/ | ||
nix develop --redirect libdep ~/libdep-src-checkout/install | $ nix develop --redirect libdep ~/libdep-src-checkout/install | ||
echo $buildInputs | tr " " "\n" | grep libdep | $ echo $buildInputs | tr " " "\n" | grep libdep | ||
# Output should show ~/libdep-src-checkout/ so you know it worked | $ # Output should show ~/libdep-src-checkout/ so you know it worked | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
| Line 562: | Line 565: | ||
<!--T:176--> | <!--T:176--> | ||
* [https:// | * [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-flake.html Nix flake command reference manual] - Many additional details about flakes, and their parts. | ||
<!--T:178--> | <!--T:178--> | ||