Flakes: Difference between revisions
imported>Mic92 extend based on https://zimbatm.com/NixFlakes |
imported>Mic92 No edit summary |
||
| Line 157: | Line 157: | ||
To switch a remote configuration, use: | To switch a remote configuration, use: | ||
<syntaxHighlight lang=console> | <syntaxHighlight lang=console> | ||
nixos-rebuild --flake .#mymachine \ | $ nixos-rebuild --flake .#mymachine \ | ||
--target-host mymachine-hostname --build-host localhost \ | --target-host mymachine-hostname --build-host localhost \ | ||
switch | switch | ||
| Line 163: | Line 163: | ||
{{warning|Remote building seems to be broken at the moment, which is why the build host is set to “localhost”.}} | {{warning|Remote building seems to be broken at the moment, which is why the build host is set to “localhost”.}} | ||
== Super fast nix-shell == | |||
One of the nix feature of the Flake edition is that Nix evaluations are cached. | |||
Let’s say that your project has a <code>shell.nix</code> file that looks like this: | |||
<syntaxHighlight lang=nix> | |||
{ pkgs ? import <nixpkgs> { } }: | |||
with pkgs; | |||
mkShell { | |||
buildInputs = [ | |||
nixpkgs-fmt | |||
]; | |||
shellHook = '' | |||
# ... | |||
''; | |||
} | |||
</syntaxHighlight> | |||
Running nix-shell can be a bit slow and take 1-3 seconds. | |||
Now create a <code>flake.nix</code> file in the same repository: | |||
<syntaxHighlight lang=nix> | |||
{ | |||
description = "my project description"; | |||
inputs.flake-utils.url = "github:numtide/flake-utils"; | |||
outputs = { self, nixpkgs, flake-utils }: | |||
flake-utils.lib.eachDefaultSystem | |||
(system: | |||
let pkgs = nixpkgs.legacyPackages.${system}; in | |||
{ | |||
devShell = import ./shell.nix { inherit pkgs; }; | |||
} | |||
); | |||
} | |||
</syntaxHighlight> | |||
Run git add flake.nix so that Nix recognizes it. | |||
And finally, run <code>nix develop</code>. This is what replaces the old nix-shell invocation. | |||
Exit and run again, this command should now be super fast. | |||
{{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.}} | |||
=== Direnv integration === | |||
Assuming that the flake defines a devShell output attribute and that you are using direnv. Here is how to replace the old use nix stdlib function with the faster flake version: | |||
<syntaxHighlight lang=sh> | |||
use_flake() { | |||
watch_file flake.nix | |||
watch_file flake.lock | |||
eval "$(nix print-dev-env --profile "$(direnv_layout_dir)/flake-profile")" | |||
} | |||
</syntaxHighlight> | |||
Copy this in ~/.config/direnv/lib/use_flake.sh or in ~/.config/direnv/direnvrc | |||
or directly in your project specific <code>.envrc</code>. | |||
With this in place, you can now replace the use nix invocation in the .envrc file with use flake. | |||
The nice thing about this approach is that evaluation is cached, and that the project’s shell is now protected from the nix garbage-collector. | |||
==== Optimize the reloads ==== | |||
Nix Flakes has a Nix evaluation caching mechanism. Is it possible to expose that somehow to automatically trigger direnv reloads? | |||
With the previous solution, direnv would only reload iff the flake.nix or flake.lock files have changed. This is not completely precise as the flake.nix file might import other files in the repository. | |||
== Pushing Flake inputs to Cachix == | |||
Flake inputs can also be cached in the Nix binary cache! | |||
<syntaxHighlight lang=console> | |||
$ nix flake archive --json \ | |||
| jq -r '.path,(.inputs|to_entries[].value.path)' \ | |||
| cachix push $cache_name | |||
</syntaxHighlight> | |||
== Build specific attributes in a flake repository == | |||
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. | |||
Eg, in nixpkgs: | |||
<syntaxHighlight lang=console> | |||
$ nix build .#hello | |||
</syntaxHighlight> | |||
== See also == | |||
* [https://zimbatm.com/NixFlakes/#direnv-integration zimbat's direnv article] | |||
* [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, Part 1: An introduction and tutorial] | |||
* [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, Part 2: Evaluation caching] | |||