Flakes: Difference between revisions

m Minor typos
No edit summary
Line 103: Line 103:
nix flake init
nix flake init
</syntaxHighlight>
</syntaxHighlight>
<translate>
====Common structure====
The above command will provide a very simple flake file looking like:
</translate>
<syntaxHighlight lang=nix>
{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };
  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
  };
}
</syntaxHighlight>
<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:
</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>
<translate>