Flake Parts: Difference between revisions

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.
 
Frontear (talk | contribs)
add an example of a flake-parts module
Line 43: Line 43:
   };
   };
}
}
</syntaxhighlight>
== Writing Modules ==
Modules are written in exactly the same way as NixOS modules. The main exposed attributes are <code>perSystem</code> and <code>flake</code>. You can reference the <code>self</code> and <code>inputs</code> attribute at the top-level, though generally you will likely not need it. An example of writing a module can be seen below:<syntaxhighlight lang="nix">
# flake/packages.nix
{ self, inputs, ... }: {
  perSystem = { pkgs, ... }: {
    packages.hello = pkgs.hello;
  };
}
</syntaxhighlight>The above module can then be imported and referenced in the main flake (or other modules)<syntaxhighlight lang="nix">
# flake.nix
{
  inputs = {
    flake-parts.url = "github:hercules-ci/flake-parts";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  outputs = { flake-parts, ... } @ inputs: flake-parts.lib.mkFlake { inherit inputs; } {
    imports = [
      ./flake/packages.nix
    ];
    systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
  };
}
</syntaxhighlight>On evaluation the flake will expose 4 distinct outputs under packages:<syntaxhighlight lang="console">
$ nix flake show --all-systems
path:/path/to/flake.nix?lastModified=omitted&narHash=omitted
└───packages
    ├───aarch64-darwin
    │  └───hello: package 'hello-2.12.1'
    ├───aarch64-linux
    │  └───hello: package 'hello-2.12.1'
    ├───x86_64-darwin
    │  └───hello: package 'hello-2.12.1'
    └───x86_64-linux
        └───hello: package 'hello-2.12.1'
</syntaxhighlight>
</syntaxhighlight>