Flake Parts

From NixOS Wiki
Revision as of 21:42, 5 August 2024 by Frontear (talk | contribs) (Introduce flake-parts to the wiki, and exemplify its power while providing a little more explanation than what the flake-parts webpage itself provides.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Flake Parts is a framework that leverages the NixOS module system to write modular and organized flakes. It provides options that represent standard flake attributes and establishes an easy way to work with system. It is a minimal and very lightweight mirror of the flake schema.

The major benefit of flake-parts is being able to write modular flakes with the full power of the module system. It is a great option and alternative to flake-utils, a wrapper which is largely discouraged from being used.

Getting Started

It is very easy to introduce flake-parts into your already existing flake.nix, or to a completely new project as well. A minimal template is provided below:

{
  description = "Your new project, powered by flake-parts!";

  inputs = {
    flake-parts.url = "github:hercules-ci/flake-parts";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; # This should point to whichever nixpkgs rev you want.
  };

  outputs = { flake-parts, ... } @ inputs: flake-parts.lib.mkFlake { inherit inputs; } {
    imports = [
      # ./module.nix
      # inputs.foo.flakeModule
    ];

    perSystem = { config, self', inputs', pkgs, system, ... }: {
      # Allows definition of system-specific attributes
      # without needing to declare the system explicitly!
      #
      # Quick rundown of the provided arguments:
      # - config is a reference to the full configuration, lazily evaluated
      # - self' is the outputs as provided here, without system. (self'.packages.default)
      # - inputs' is the input without needing to specify system (inputs'.foo.packages.bar)
      # - pkgs is an instance of nixpkgs for your specific system
      # - system is the system this configuration is for

      # This is equivalent to packages.<system>.default
      packages.default = pkgs.hello;
    };

    flake = {
      # A fallback attribute for non-system specific
      # flake attributes (like check, formatter, nixosConfigurations, etc)
    };

    # Declared systems that your flake supports. These will be enumerated in perSystem
    systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
  };
}

See also