Nix Flakes

Nix Flakes

Summary

  • Nix flake is a unit of modularization in the Nix package manager
  • It allows users to define reusable package and environment configurations that can be shared and composed with other flakes
  • One of the main problems that Nix flakes aim to solve is the difficulty of managing complex software projects that have multiple dependencies and configuration options
  • With Nix flakes, users can define a single, self-contained configuration that specifies all of the dependencies, build instructions, and other necessary information for a given project
  • Another problem that Nix flakes can help to solve is the challenge of reproducing software environments
    • By using Nix flakes to define a specific environment configuration
    • users can ensure that their project can be built and run consistently across different machines and operating systems
    • this can be particularly useful for collaborative software development, as it allows different team members to work on the same project with the same environment setup
  • nix copies everything to the store so files have to be in git index

CLI

# initialize project with flake
nix flake init

# update all flake inputs
nix flake update

# update specific inputs
nix flake update-input nixpkgs

Input Schema

{
  inputs = {
    # github example, also supported gitlab:
    nixpkgs.url = "github:Mic92/nixpkgs/master";
    # git urls
    git-example.url = "git+https://git.somehost.tld/user/path?ref=branch&rev=fdc8ef970de2b4634e1b3dca296e1ed918459a9e";
    # local directories (for absolute paths you can omit 'path:')
    directory-example.url = "path:/path/to/repo";
    # Use this for non-flakes
    bar.url = "github:foo/bar/branch";
    bar.flake = false;
    # Overwrite inputs in a flake
    # This is useful to use the same nixpkgs version in both flakes
    sops-nix.url = "github:Mic92/sops-nix";
    sops-nix.inputs.nixpkgs.follows = "nixpkgs";
    # Pin flakes to a specific revision
    nix-doom-emacs.url = "github:vlaci/nix-doom-emacs?rev=238b18d7b2c8239f676358634bfb32693d3706f3";
    nix-doom-emacs.flake = false;
    # To use a subdirectory of a repo, pass dir=
    nixpkgs.url = "github:foo/bar?dir=shu";
  }
}

Output Schema

{ self, ... }@inputs:
{
  # Executed by `nix flake check`
  checks."<system>"."<name>" = derivation;
  # Executed by `nix build .#<name>`
  packages."<system>"."<name>" = derivation;
  # Executed by `nix build .`
  packages."<system>".default = derivation;
  # Executed by `nix run .#<name>`
  apps."<system>"."<name>" = {
    type = "app";
    program = "<store-path>";
  };
  # Executed by `nix run . -- <args?>`
  apps."<system>".default = { type = "app"; program = "..."; };

  # Used for nixpkgs packages, also accessible via `nix build .#<name>`
  legacyPackages."<system>"."<name>" = derivation;
  # Overlay, consumed by other flakes
  overlays."<name>" = final: prev: { };
  # Default overlay
  overlays.default = {};
  # Nixos module, consumed by other flakes
  nixosModules."<name>" = { config }: { options = {}; config = {}; };
  # Default module
  nixosModules.default = {};
  # Used with `nixos-rebuild --flake .#<hostname>`
  # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
  nixosConfigurations."<hostname>" = {};
  # Used by `nix develop .#<name>`
  devShells."<system>"."<name>" = derivation;
  # Used by `nix develop`
  devShells."<system>".default = derivation;
  # Hydra build jobs
  hydraJobs."<attr>"."<system>" = derivation;
  # Used by `nix flake init -t <flake>#<name>`
  templates."<name>" = {
    path = "<store-path>";
    description = "template description goes here?";
  };
  # Used by `nix flake init -t <flake>`
  templates.default = { path = "<store-path>"; description = ""; };
}

Notes mentioning this note