Flakes: Difference between revisions

imported>Ryan4yin
mNo edit summary
imported>Artturin
m add values to .default
Line 32: Line 32:


<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
experimental-features = nix-command flakes  
experimental-features = nix-command flakes
</syntaxHighlight>
</syntaxHighlight>


Line 39: Line 39:
<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
mkdir -p ~/.config/nix
mkdir -p ~/.config/nix
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf  
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
</syntaxHighlight>
</syntaxHighlight>


Line 127: Line 127:


Where:
Where:
 
* <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"
* <code><name></code> is an attribute name like "hello".
* <code><name></code> is an attribute name like "hello".
Line 157: Line 157:
   overlays."<name>" = final: prev: { };
   overlays."<name>" = final: prev: { };
   # Default overlay
   # Default overlay
   overlays.default = {};
   overlays.default = final: prev: { };
   # Nixos module, consumed by other flakes
   # Nixos module, consumed by other flakes
   nixosModules."<name>" = { config }: { options = {}; config = {}; };
   nixosModules."<name>" = { config }: { options = {}; config = {}; };
   # Default module
   # Default module
   nixosModules.default = {};
   nixosModules.default = { config }: { options = {}; config = {}; };
   # Used with `nixos-rebuild --flake .#<hostname>`
   # Used with `nixos-rebuild --flake .#<hostname>`
   # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
   # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
Line 289: Line 289:
   inputs.nixpkgs.url = github:NixOS/nixpkgs;
   inputs.nixpkgs.url = github:NixOS/nixpkgs;
   inputs.home-manager.url = github:nix-community/home-manager;
   inputs.home-manager.url = github:nix-community/home-manager;
 
 
   outputs = { self, nixpkgs, ... }@attrs: {
   outputs = { self, nixpkgs, ... }@attrs: {
     nixosConfigurations.fnord = nixpkgs.lib.nixosSystem {
     nixosConfigurations.fnord = nixpkgs.lib.nixosSystem {
Line 326: Line 326:
   switch
   switch
</syntaxHighlight>
</syntaxHighlight>
 
{{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”.}}


Line 404: Line 404:
The nice thing about this approach is that evaluation is cached.
The nice thing about this approach is that evaluation is cached.


==== Optimize the reloads ====  
==== Optimize the reloads ====


Nix Flakes has a Nix evaluation caching mechanism. Is it possible to expose that somehow to automatically trigger direnv reloads?
Nix Flakes has a Nix evaluation caching mechanism. Is it possible to expose that somehow to automatically trigger direnv reloads?
Line 410: Line 410:
With the previous solution, direnv would only reload if 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.
With the previous solution, direnv would only reload if 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.


==== Setting the bash prompt like nix-shell ====  
==== Setting the bash prompt like nix-shell ====


A [https://github.com/NixOS/nix/pull/4189 new experimental feature of flakes] allow to setup a bash-prompt per flake:  
A [https://github.com/NixOS/nix/pull/4189 new experimental feature of flakes] allow to setup a bash-prompt per flake:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{
{
Line 457: Line 457:


   inputs = {
   inputs = {
     nixpkgs.url = "nixpkgs/nixos-21.11";  
     nixpkgs.url = "nixpkgs/nixos-21.11";
     nixpkgs-unstable.url = "nixpkgs/nixos-unstable";  
     nixpkgs-unstable.url = "nixpkgs/nixos-unstable";
   };
   };


Line 491: Line 491:
   environment.systemPackages = [pkgs.firefox pkgs.unstable.chromium];
   environment.systemPackages = [pkgs.firefox pkgs.unstable.chromium];
   # ...
   # ...
}  
}
</syntaxHighlight>
</syntaxHighlight>
Same can be done with the NURs, as it already has an ''overlay'' attribute in the flake.nix of the project, you can just add <syntaxHighlight lang=nix>nixpkgs.overlays = [ nur.overlay ];</syntaxHighlight>
Same can be done with the NURs, as it already has an ''overlay'' attribute in the flake.nix of the project, you can just add <syntaxHighlight lang=nix>nixpkgs.overlays = [ nur.overlay ];</syntaxHighlight>
Line 506: Line 506:


<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
# nix repl  
# nix repl
>> :lf /etc/nixos
>> :lf /etc/nixos
>> nixosConfigurations.myhost.config
>> nixosConfigurations.myhost.config
Line 514: Line 514:
Or out of your current flake:
Or out of your current flake:
<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
# nix repl  
# nix repl
>> :lf .#
>> :lf .#
</syntaxHighlight>
</syntaxHighlight>
Line 520: Line 520:
You can then access to the inputs, outputs… For instance if you would like to check the default version of the kernel present in nixpgs:
You can then access to the inputs, outputs… For instance if you would like to check the default version of the kernel present in nixpgs:
<syntaxHighlight lang=text>
<syntaxHighlight lang=text>
nix-repl> inputs.nixpkgs.legacyPackages.x86_64-linux.linuxPackages.kernel.version      
nix-repl> inputs.nixpkgs.legacyPackages.x86_64-linux.linuxPackages.kernel.version
"5.15.74"
"5.15.74"
</syntaxHighlight>
</syntaxHighlight>
Line 603: Line 603:


=== Rapid iteration of a direct dependency ===
=== 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.


Consider a situation where your executable, <code>consumexe</code>, depends on a library, <code>libdep</code>. You're trying to work on both at the same time, where changes to <code>libdep</code> are reflected in real time for <code>consumexe</code>. This workflow can be achieved like so:
Consider a situation where your executable, <code>consumexe</code>, depends on a library, <code>libdep</code>. You're trying to work on both at the same time, where changes to <code>libdep</code> are reflected in real time for <code>consumexe</code>. This workflow can be achieved like so:
Line 628: Line 628:
* [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)
* [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)
* [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.
* [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.
* [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)