Flakes: Difference between revisions

No edit summary
Pigs (talk | contribs)
Moved flake-utils usage to its own page; formatting and wording improvements
Line 26: Line 26:
<!--T:145-->
<!--T:145-->
* An [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html experimental command-line interface] accepts flake references for expressions that build, run, and deploy packages.
* An [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html experimental command-line interface] accepts flake references for expressions that build, run, and deploy packages.
=== Enabling flakes ===


====Enable flakes temporarily==== <!--T:5-->
====Enable flakes temporarily==== <!--T:5-->
Line 48: Line 50:
<translate>
<translate>


====Other Distros, with Home-Manager==== <!--T:10-->
====With Home Manager==== <!--T:10-->


<!--T:11-->
<!--T:11-->
Add the following to your home-manager config:
Add the following to your [[Home Manager|home manager]] config:


</translate>
</translate>
Line 59: Line 61:
<translate>
<translate>


====Other Distros, without Home-Manager==== <!--T:13-->
====Other Distros, without Home Manager==== <!--T:13-->


<!--T:14-->
<!--T:14-->
Line 74: Line 76:


===Basic Usage of Flake=== <!--T:17-->
===Basic Usage of Flake=== <!--T:17-->
<!--T:18-->
Before running any nix commands at this point, please note the two warnings below: one for encryption and the other for git.
====Encryption WARNING==== <!--T:19-->


<!--T:20-->
<!--T:20-->
{{Warning | Since contents of flake files are copied to the world-readable Nix store folder, do not put any unencrypted secrets in flake files. You should instead use a [[Comparison of secret managing schemes|secret managing scheme]].}}
{{Warning | Since contents of flake files are copied to the world-readable Nix store folder, do not put any unencrypted secrets in flake files. You should instead use a [[Comparison of secret managing schemes|secret managing scheme]].}}
====Git WARNING==== <!--T:21-->


<!--T:146-->
<!--T:146-->
For flakes in git repos, only files in the working tree will be copied to the store.
{{Note | For flakes in [[git]] repositories, only files in the working tree will be copied to the store.


<!--T:22-->
<!--T:22-->
Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.
Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}}
 
<!--T:23-->
See also https://www.tweag.io/blog/2020-05-25-flakes/


====Generate flake.nix file==== <!--T:24-->
====Generate flake.nix file==== <!--T:24-->


<!--T:25-->
<!--T:25-->
To start the basic usage of flake, run the flake command in the project directory:
To initialize a flake, run the following flake command in the project directory:


</translate>
</translate>
 
<syntaxhighlight lang="console">
<syntaxHighlight lang=text>
$ nix flake init
nix flake init
</syntaxhighlight>
</syntaxHighlight>
 
<translate>
<translate>


Line 112: Line 102:


</translate>
</translate>
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{
{
Line 130: Line 119:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>
<translate>


As you can see, it forces you to specify a program for each supported architecture. To avoid this, third-parties projects like flake-utils or flake.parts automatically provide code to avoid this boilerplate. Moreover, in some cases, you may want to also provide an architecture and nixpkgs-independent overlay, allowing for instance the users of the flake to install the program in their system while using their own version of nixpkgs (anything inside <code>packages</code> will always pin all libraries to a fixed version, unless you use <code>yourflake.inputs.nixpkgs.follows = "nixpkgs";</code>). To avoid re-defining the program multiple times (once in the overlay, once for each architecture etc), you can instead write you flake as follows:
Flakes force you to specify a program for each supported architecture. To avoid this, third-parties projects like [[Flake Utils|flake-utils]] or [[Flake Parts|flake-parts]] automatically provide code to avoid this boilerplate. To avoid re-defining the program multiple times, refer to [[Flake Utils#Defining a flake for multiple architectures]]
 
</translate>
 
<syntaxHighlight lang=nix>
{
  description = "My flake description";
 
  # To easily generate a derivation per architecture
  inputs.flake-utils.url = "github:numtide/flake-utils";
 
  outputs = { self, nixpkgs, flake-utils }: {
    # We define first an overlay, i.e. a definition of new packages as recommended in
    # https://discourse.nixos.org/t/how-to-consume-a-eachdefaultsystem-flake-overlay/19420/9
    overlays.default = final: prev: {
      yourprogram = final.callPackage ({stdenv, pkgs, ...}:
        # You can put here the derivation to build your program, for instance:
        stdenv.mkDerivation {
          src = ./.;
          pname = "yourprogram";
          version = "unstable";
          buildInputs = with pkgs; [
            # libraries needed by your program
            # ...
          ];
          # Write here additional phases, e.g. buildPhase, installPhase etc.
          # ...
        }
      ) {};
    };
  } // # We now add the package defined in the above overlay for all architectures
  (flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        system = system;
        overlays = [ self.overlays.default ];
      };
      lib = nixpkgs.lib;
    in
      {
        # Create a new package
        packages = {
          yourprogram = pkgs.yourprogram;
          default = self.packages.${system}.yourprogram; # default program: this way, typing "nix develop" will directly put you in a shell needed to develop the above your program, running "nix build/run" will directly build/run this program etc.
        };
      }
  ))
  ;
}
</syntaxHighlight>
 
<translate>


== Flake schema == <!--T:27-->
== Flake schema == <!--T:27-->