Flakes: Difference between revisions
imported>Mic92 add nixos example |
imported>Mic92 extend based on https://zimbatm.com/NixFlakes |
||
Line 26: | Line 26: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
On non-nixos system install `nixUnstable` in your environment | On non-nixos system install `nixUnstable` in your environment: | ||
<syntaxHighlight lang=console> | |||
$ nix-env -iA nixUnstable | |||
</syntaxHighlight> | |||
Edit either ~/.config/nix/nix.conf or /etc/nix/nix.conf and add: | |||
<syntaxHighlight> | <syntaxHighlight> | ||
experimental-features = nix-command flakes | experimental-features = nix-command flakes | ||
</syntaxHighlight> | </syntaxHighlight> | ||
This is needed to expose the Nix 2.0 CLI and flakes support that are hidden behind feature-flags. | |||
Finally, if the Nix installation is in multi-user mode, don’t forget to restart the nix-daemon. | |||
There is no official installer yet, but it is possible to download the latest snapshot nix from hydra | There is no official installer yet, but it is possible to download the latest snapshot nix from hydra | ||
as shown in | as shown in here [https://github.com/Mic92/dotfiles/blob/27a5b81964ecb8722796018a8c4ed7b149f05b96/.github/workflows/build.yml#L33 github action]. | ||
== Basic project usage == | |||
{{warning|flake makes a strong assumption that the folder is a git repository. It doesn’t work outside of them.}} | |||
In your repo, run <code>nix flake init</code> to generate the flake.nix file. Then run <code>git add flake.nix</code> to add it to the git staging area, otherwise nix will not recognize that the file exists. | |||
See also https://www.tweag.io/blog/2020-05-25-flakes/ | |||
== Flake schema == | |||
The flake.nix file is a Nix file but that has special restrictions (more on that later). | |||
It has 3 top-level attributes: | |||
* <code>description</code> which is self…describing | |||
* <code>input</code> is an attribute set of all the dependencies of the flake. The schema is described below. | |||
* <code>output</code> is a function of one argument that takes an attribute set of all the realized inputs, and outputs another attribute set which schema is described below. | |||
== Input schema == | |||
This is not a complete schema but should be enough to get you started: | |||
<syntaxHighlight lang=nix> | |||
{ | |||
inputs.bar = { url = "github:foo/bar/branch"; flake = false; } | |||
} | |||
</syntaxHighlight> | |||
The bar input is then passes to the output schema | |||
== Output schema == | |||
This is described in the nix package manager [https://github.com/NixOS/nix/blob/master/src/nix/flake.cc src/nix/flake.cc] in CmdFlakeCheck. | |||
Where: | |||
* <code><system></code> is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin" | |||
* <code><attr></code> is an attribute name like "hello". | |||
* <code><flake></code> is a flake name like "nixpkgs". | |||
* <code><store-path></code> is a <code>/nix/store..</code> path | |||
<syntaxHighlight lang=nix> | |||
{ self, ... }@inputs: | |||
{ | |||
# Executed by `nix flake check` | |||
checks."<system>"."<attr>" = derivation; | |||
# Executed by `nix build .#<name>` | |||
packages."<system>"."<attr>" = derivation; | |||
# Executed by `nix build .` | |||
defaultPackage."<system>" = derivation; | |||
# Executed by `nix run .#<name> | |||
apps."<system>"."<attr>" = { | |||
type = "app"; | |||
program = "<store-path>"; | |||
}; | |||
defaultApp."<system>" = { type = "app"; program = "..."; }; | |||
# TODO: Not sure how it's being used | |||
legacyPackages = TODO; | |||
# TODO: Not sure how it's being used | |||
overlay = final: prev: { }; | |||
# TODO: Same idea as overlay but a list of them. | |||
overlays = []; | |||
# TODO: Not sure how it's being used | |||
nixosModule = TODO; | |||
# TODO: Same idea as nixosModule but a list of them. | |||
nixosModules = []; | |||
# TODO: Not sure how it's being used | |||
nixosConfigurations = TODO; | |||
# TODO: Same idea as nixosModule but a list of them. | |||
hydraJobs = TODO; | |||
# Used by `nix flake init -t <flake>` | |||
defaultTemplate = { | |||
path = "<store-path>"; | |||
description = "template description goes here?"; | |||
}; | |||
# Used by `nix flake init -t <flake>#<attr>` | |||
templates."<attr>" = { path = "<store-path>"; description = ""; ); | |||
} | |||
</syntaxHighlight> | |||
== Using nix flakes with NixOS == | == Using nix flakes with NixOS == | ||
nixos-rebuild switch will reads its configuration from <code>/etc/nixos/flake.nix</code> if is present. | nixos-rebuild switch will reads its configuration from <code>/etc/nixos/flake.nix</code> if is present. | ||
A basic nixos flake.nix could look like this: | A basic nixos flake.nix could look like this: | ||
Line 44: | Line 133: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
{ | { | ||
outputs = { self, nixpkgs }: { | outputs = { self, nixpkgs }: { | ||
# replace 'joes-desktop' with your hostname here. | # replace 'joes-desktop' with your hostname here. | ||
Line 66: | Line 154: | ||
$ sudo nixos-rebuild switch --flake '/etc/nixos#joes-desktop' | $ sudo nixos-rebuild switch --flake '/etc/nixos#joes-desktop' | ||
</syntaxHighlight> | </syntaxHighlight> | ||
To switch a remote configuration, use: | |||
<syntaxHighlight lang=console> | |||
nixos-rebuild --flake .#mymachine \ | |||
--target-host mymachine-hostname --build-host localhost \ | |||
switch | |||
</syntaxHighlight> | |||
{{warning|Remote building seems to be broken at the moment, which is why the build host is set to “localhost”.}} |