Convert Figma logo to code with AI

hercules-ci logoflake-parts

❄️ Simplify Nix Flakes with the module system

1,008
55
1,008
67

Top Related Projects

21,292

Nix Packages collection & NixOS

Pure Nix flake utility functions [maintainer=@zimbatm]

Simplified nix packaging for various programming language ecosystems [maintainer=@DavHau]

Generate Nix packages from URLs with hash prefetching, dependency inference, license detection, and more [maintainer=@figsoda]

Quick Overview

Flake-parts is a Nix library that simplifies the creation and management of Nix flakes. It provides a modular approach to flake configuration, allowing users to compose their flakes from reusable parts. This library aims to reduce boilerplate code and improve the organization of Nix projects.

Pros

  • Modular design allows for easy composition and reuse of flake components
  • Reduces boilerplate code in flake configurations
  • Improves maintainability and readability of Nix projects
  • Provides a consistent structure for flake-based projects

Cons

  • Requires learning a new abstraction layer on top of Nix flakes
  • May add complexity for simple projects that don't require modularity
  • Limited documentation and examples compared to vanilla Nix flakes

Code Examples

  1. Basic flake-parts configuration:
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [ "x86_64-linux" "aarch64-darwin" ];
      perSystem = { config, self', inputs', pkgs, system, ... }: {
        # Per-system configuration
      };
      flake = {
        # Flake-wide configuration
      };
    };
}
  1. Adding a custom module:
{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [
        ./my-custom-module.nix
      ];
      # ... rest of the configuration
    };
}
  1. Defining a development shell:
{
  perSystem = { config, self', inputs', pkgs, ... }: {
    devShells.default = pkgs.mkShell {
      packages = with pkgs; [
        nodejs
        yarn
      ];
    };
  };
}

Getting Started

To use flake-parts in your project:

  1. Add flake-parts as an input in your flake.nix:

    {
      inputs.flake-parts.url = "github:hercules-ci/flake-parts";
    }
    
  2. Use flake-parts.lib.mkFlake in your outputs:

    outputs = inputs@{ flake-parts, ... }:
      flake-parts.lib.mkFlake { inherit inputs; } {
        systems = [ "x86_64-linux" "aarch64-darwin" ];
        perSystem = { config, self', inputs', pkgs, system, ... }: {
          # Your per-system configuration here
        };
        flake = {
          # Your flake-wide configuration here
        };
      };
    
  3. Run nix flake update to fetch flake-parts and update your lock file.

Competitor Comparisons

21,292

Nix Packages collection & NixOS

Pros of nixpkgs

  • Comprehensive package collection with a vast ecosystem
  • Well-established and widely used in the Nix community
  • Provides a stable foundation for system configurations

Cons of nixpkgs

  • Can be complex and overwhelming for newcomers
  • Slower to iterate and make changes due to its size and scope
  • May include unnecessary dependencies for smaller projects

Code Comparison

nixpkgs:

{ pkgs ? import <nixpkgs> {} }:

pkgs.stdenv.mkDerivation {
  name = "my-package";
  # ... other attributes
}

flake-parts:

{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      # ... flake modules
    };
}

Summary

While nixpkgs offers a comprehensive package collection and is widely used in the Nix ecosystem, flake-parts provides a more modular and flexible approach to building Nix flakes. flake-parts allows for easier composition of flake modules and can be more suitable for smaller projects or those requiring a more customized setup. However, nixpkgs remains the go-to solution for larger, more complex systems and benefits from its extensive community support and package availability.

Pure Nix flake utility functions [maintainer=@zimbatm]

Pros of flake-utils

  • Lightweight and simple to use
  • Provides basic utility functions for common Nix flake tasks
  • Easy to integrate into existing projects

Cons of flake-utils

  • Limited functionality compared to more comprehensive solutions
  • Requires more manual configuration for complex setups
  • Less flexibility for customizing flake outputs

Code Comparison

flake-utils:

{
  inputs.flake-utils.url = "github:numtide/flake-utils";
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system: {
      # Your flake outputs here
    });
}

flake-parts:

{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = { self, nixpkgs, flake-parts }:
    flake-parts.lib.mkFlake { inherit self; } {
      systems = [ "x86_64-linux" "aarch64-darwin" ];
      perSystem = { config, self', inputs', pkgs, system, ... }: {
        # Your flake outputs here
      };
    };
}

Summary

flake-utils is a simpler, more lightweight option for basic flake utilities, while flake-parts offers a more comprehensive and flexible approach to structuring Nix flakes. flake-parts provides a modular system for organizing flake outputs and supports more advanced configurations, making it better suited for larger or more complex projects. However, flake-utils may be preferable for smaller projects or those requiring minimal flake functionality.

Simplified nix packaging for various programming language ecosystems [maintainer=@DavHau]

Pros of dream2nix

  • Focuses on automatically generating Nix expressions from various ecosystems (Python, Node.js, etc.)
  • Provides a more comprehensive solution for packaging non-Nix projects
  • Offers a wider range of language and ecosystem support

Cons of dream2nix

  • May have a steeper learning curve for users new to Nix
  • Could be overkill for simpler projects or those already well-suited for Nix
  • Potentially less flexible for custom Nix configurations

Code Comparison

dream2nix:

{
  inputs.dream2nix.url = "github:nix-community/dream2nix";
  outputs = { self, dream2nix }:
    dream2nix.lib.makeFlakeOutputs {
      systems = ["x86_64-linux"];
      config.projectRoot = ./.;
      source = ./.;
    };
}

flake-parts:

{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = { self, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit self; } {
      systems = ["x86_64-linux"];
      perSystem = { config, self', inputs', pkgs, system, ... }: {
        # Your module configurations here
      };
    };
}

The code examples demonstrate the basic setup for each project. dream2nix focuses on automatic derivation generation, while flake-parts provides a modular structure for organizing Nix configurations.

Generate Nix packages from URLs with hash prefetching, dependency inference, license detection, and more [maintainer=@figsoda]

Pros of nix-init

  • Focuses on project initialization and scaffolding
  • Provides a user-friendly CLI interface for creating new Nix projects
  • Supports multiple project types and templates

Cons of nix-init

  • Limited to project initialization, lacks advanced configuration options
  • May require additional setup for complex project structures
  • Less flexibility for customizing flake outputs

Code Comparison

nix-init:

{
  description = "A basic Nix flake";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs }: {
    # Basic default output
  };
}

flake-parts:

{
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [ "x86_64-linux" "aarch64-darwin" ];
      perSystem = { config, self', inputs', pkgs, system, ... }: {
        # Customizable per-system configuration
      };
    };
}

flake-parts offers a more modular and extensible approach to flake configuration, allowing for easier management of complex multi-system setups. nix-init, on the other hand, provides a simpler starting point for new projects but may require more manual configuration for advanced use cases.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Flake Parts

Core of a distributed framework for writing Nix Flakes.

flake-parts provides the options that represent standard flake attributes and establishes a way of working with system. Opinionated features are provided by an ecosystem of modules that you can import.

flake-parts itself has the goal to be a minimal mirror of the Nix flake schema. Used by itself, it is very lightweight.


Documentation: flake.parts


Why Modules?

Flakes are configuration. The module system lets you refactor configuration into modules that can be shared.

It reduces the proliferation of custom Nix glue code, similar to what the module system has done for NixOS configurations.

Unlike NixOS, but following Flakes' spirit, flake-parts is not a monorepo with the implied goal of absorbing all of open source, but rather a single module that other repositories can build upon, while ensuring a baseline level of compatibility: the core attributes that constitute a flake.

Features

  • Split your flake.nix into focused units, each in their own file.

  • Take care of system.

  • Allow users of your library flake to easily integrate your generated flake outputs into their flake.

  • Reuse project logic written by others

Getting Started

If your project does not have a flake yet:

nix flake init -t github:hercules-ci/flake-parts

Migrate

Otherwise, add the input,

    flake-parts.url = "github:hercules-ci/flake-parts";

then slide mkFlake between your outputs function head and body,

  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      flake = {
        # Put your original flake attributes here.
      };
      systems = [
        # systems for which you want to build the `perSystem` attributes
        "x86_64-linux"
        # ...
      ];
    };

Now you can add the remaining module attributes like in the the template.

Templates

See the template.

Examples

See the examples/ directory.

Projects using flake-parts

Options Reference

See flake.parts options