Nix Flakes is a feature of the Nix package manager that aims to improve project reproducibility and source code management. Flakes allow you to declare project dependencies, including Nix expressions, in a consistent and reproducible manner.
If you want to import (or reference) one flake from another, you typically use the inputs attribute of your flake's flake.nix to specify dependencies on other flakes.
Here's how you can reference another flake from your flake file:
# flake.nix
{
description = "My flake";
inputs = {
# Import another flake from a Git repository
someotherflake.url = "github:username/repo";
# Optionally, you can pin it to a specific revision
pinnedflake.url = "github:username/repo?rev=commit-hash-or-tag";
# If you're referencing a local flake, you can do so with a file path
localflake.url = "path:/path/to/local/flake";
# For fetching with a specific input from the flake (e.g., `nixpkgs`)
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
# To override the inputs of another flake, you can use "follows"
overriddenflake.url = "github:username/overriddenrepo";
overriddenflake.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, someotherflake, pinnedflake, localflake, overriddenflake, ... }: {
# You can now use inputs in your outputs definition
# Example on how you might use these flakes in your outputs
defaultPackage.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.hello;
somePackage.x86_64-linux = someotherflake.packages.x86_64-linux.somePackage;
localPackage.x86_64-linux = localflake.packages.x86_64-linux.localPackage;
overriddenPackage.x86_64-linux = overriddenflake.packages.x86_64-linux.overriddenPackage;
};
}
In the above example:
someotherflake: Demonstrates how to import a flake from a GitHub repository.
pinnedflake: Shows how to import a flake from a GitHub repository pinned to a specific commit or tag.
localflake: Shows how to import a flake from a local path.
nixpkgs: Demonstrates importing the nixpkgs flake from GitHub.
overriddenflake: Shows how to import a flake and override one of its inputs, such that overriddenflake's dependency of nixpkgs follows the version we specified in inputs.
After specifying inputs, you can use them in the outputs function to construct packages, applications, or any other Nix expressions that should be part of your flake.
Remember that after modifying flake.nix, you need to update your flake lock file with the following command:
nix flake update
This will fetch and lock the versions of the input flakes, ensuring reproducibility.