Flakes/en: Difference between revisions
Updating to match new version of source page Tags: Mobile edit Mobile web edit |
Updating to match new version of source page |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 10: | Line 10: | ||
== Flake file structure == | == Flake file structure == | ||
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 at any time using nix flake init. This will populate the current directory with a file called flake.nix that will contain something akin to: | 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 at any time using nix flake init. This will populate the current directory with a file called flake.nix that will contain something akin to: | ||
{{File|3=<nowiki>{ | {{File|3=<nowiki>{ | ||
description = "A very basic flake"; | description = "A very basic flake"; | ||
| Line 19: | Line 21: | ||
outputs = { self, nixpkgs }: { | outputs = { self, nixpkgs }: { | ||
packages.x86_64-linux = { | |||
packages.x86_64-linux | default = self.packages.x86_64-linux.hello; | ||
hello = nixpkgs.legacyPackages.x86_64-linux.hello; | |||
}; | |||
}; | }; | ||
}</nowiki>|name=flake.nix|lang=nix}} | }</nowiki>|name=flake.nix|lang=nix}} | ||
In the example above, you can see the description, the input specified as a GitHub repository with a specific branch (here <code>nixos/nixpkgs</code> on the <code>nixos-unstable</code> branch), and an output that makes use of the input. The output simply specifies that the flake contains one package for the x86_64 architecture called <code>hello</code>. Even if your flake's output wouldn't use its input (however, in practice, that is highly unlikely), the output still needs to be a Nix function. | In the example above, you can see the description, the input specified as a GitHub repository with a specific branch (here <code>nixos/nixpkgs</code> on the <code>nixos-unstable</code> branch), and an output that makes use of the input. The output simply specifies that the flake contains one package for the x86_64 architecture called <code>hello</code>. Even if your flake's output wouldn't use its input (however, in practice, that is highly unlikely), the output still needs to be a Nix function. | ||
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}} | {{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}} | ||
=== Nix configuration === | === Nix configuration === | ||
It is possible to override the global Nix configuration set in your <code>nix.conf</code> file for the purposes of evaluating a flake. This can be useful, for example, for setting up binary caches specific to certain projects, while keeping the global configuration untouched. The flake file can contain a nixConfig attribute with any relevant configuration settings supplied. For example, enabling the nix-community binary cache would be achieved by: | It is possible to override the global Nix configuration set in your <code>nix.conf</code> file for the purposes of evaluating a flake. This can be useful, for example, for setting up binary caches specific to certain projects, while keeping the global configuration untouched. The flake file can contain a nixConfig attribute with any relevant configuration settings supplied. For example, enabling the nix-community binary cache would be achieved by: | ||
{{File|3=<nowiki>{ | {{File|3=<nowiki>{ | ||
... | ... | ||
| Line 41: | Line 46: | ||
]; | ]; | ||
} | } | ||
}</nowiki>|name=flake.nix|lang=nix}}{{Note|If you are used to configuring your Nix settings via the NixOS configuration, these options are under <code>nix.settings</code> and not <code>nix</code>. For example, you cannot specify the automatic storage optimisation under <code>nix.optimisation.enable</code>.}} | }</nowiki>|name=flake.nix|lang=nix}} | ||
{{Note|If you are used to configuring your Nix settings via the NixOS configuration, these options are under <code>nix.settings</code> and not <code>nix</code>. For example, you cannot specify the automatic storage optimisation under <code>nix.optimisation.enable</code>.}} | |||
== Setup == | == Setup == | ||
| Line 48: | Line 55: | ||
When using any [[Nix command|<code>nix</code> command]], add the following command-line options: | When using any [[Nix command|<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> | ||
=== Enabling flakes permanently === | === Enabling flakes permanently === | ||
| Line 78: | Line 87: | ||
experimental-features = nix-command flakes | experimental-features = nix-command flakes | ||
</syntaxHighlight> | </syntaxHighlight> | ||
== Usage == | == Usage == | ||
| Line 87: | Line 97: | ||
=== The nix flakes command === | === The nix flakes command === | ||
{{Main|Nix (command)}} | {{Main|Nix (command)}} | ||
| Line 109: | Line 120: | ||
description = "Example flake with a devShell"; | description = "Example flake with a devShell"; | ||
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |||
outputs = { self, nixpkgs}: | |||
let | let | ||
system = "x86_64-linux"; | system = "x86_64-linux"; | ||
| Line 178: | Line 189: | ||
In this example the input would point to the `nixpkgs-unstable` channel. | 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 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. | ||
| Line 190: | Line 200: | ||
<code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code> | <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: | Using curly brackets (<code>{}</code>), we can shorten all of this and put it in a table. The code will look something like this: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 224: | Line 234: | ||
* <code><store-path></code> is a <code>/nix/store..</code> path | * <code><store-path></code> is a <code>/nix/store..</code> path | ||
< | <syntaxhighlight lang="nix"> | ||
{ self, ... }@inputs: | { self, ... }@inputs: | ||
{ | { | ||
| Line 237: | Line 247: | ||
type = "app"; | type = "app"; | ||
program = "<store-path>"; | program = "<store-path>"; | ||
meta = {description = "..."; inherit otherMetaAttrs; }; | |||
}; | }; | ||
# Executed by `nix run . -- <args?>` | # Executed by `nix run . -- <args?>` | ||
apps."<system>".default = { type = "app"; program = "..."; }; | apps."<system>".default = { type = "app"; program = "..."; meta = {description = "..."; inherit otherMetaAttrs; }; }; | ||
# Formatter (alejandra, nixfmt or nixpkgs-fmt) | # Formatter (alejandra, nixfmt, treefmt-nix 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 270: | Line 281: | ||
templates.default = { path = "<store-path>"; description = ""; }; | templates.default = { path = "<store-path>"; description = ""; }; | ||
} | } | ||
</ | </syntaxhighlight> | ||
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. | ||
| Line 294: | Line 305: | ||
description = "A flake targeting multiple architectures"; | description = "A flake targeting multiple architectures"; | ||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
}; | }; | ||
outputs = { self, nixpkgs }: let | |||
systems = [ "x86_64-linux" "aarch64-linux" ]; | systems = [ "x86_64-linux" "aarch64-linux" ]; | ||
forAllSystems = f: builtins.listToAttrs (map (system: { | forAllSystems = f: builtins.listToAttrs (map (system: { | ||
| Line 415: | Line 426: | ||
# Output should show ~/libdep-src-checkout/ so you know it worked | # Output should show ~/libdep-src-checkout/ so you know it worked | ||
</syntaxHighlight> | </syntaxHighlight> | ||
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. | ||
| Line 460: | Line 472: | ||
{{references}} | {{references}} | ||
[[Category:Software]] | [[Category:Software|Software]] | ||
[[Category:Nix]] | [[Category:Nix|Nix]] | ||
[[Category:Nix Language]] | [[Category:Nix Language|Nix Language]] | ||
[[Category:Flakes]] | [[Category:Flakes|Flakes]] | ||