OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 028

External Flake references and collaboration

Connect external modules and packages, and check the relationships between dependency versions.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSFlakes: project and dependency management ↗
About 2 min readStart reading

How do you reference external resources?#

In flake.nix, you can define various external sources in the inputs section.

1. Common URL types

  • GitHub/GitLab: github:user/repo/rev
  • Local path: path:../other-flake
  • Tarball: https://example.com/archive.tar.gz

Practical examples: referencing someone else’s Nix configuration#

1. Referencing Hyprland’s official Flake

Hyprland evolves very quickly, and the official NixOS repository may not be recent enough. You can reference its Flake directly:

inputs.hyprland.url = "github:hyprwm/Hyprland";

outputs = { self, nixpkgs, hyprland, ... }: {
  nixosConfigurations.my-pc = nixpkgs.lib.nixosSystem {
    modules = [
      ./configuration.nix
      { programs.hyprland = {
          enable = true;
          package = hyprland.packages.x86_64-linux.hyprland;
          portalPackage = hyprland.packages.x86_64-linux.xdg-desktop-portal-hyprland;
        }; } # 官方文档的包与 portal 配对方式
    ];
  };
};

2. Referencing someone else’s Neovim configuration

Suppose someone has packaged their Neovim setup as a Flake:

inputs.my-neovim.url = "github:someone/neovim-flake";

# 在 configuration.nix 里:
environment.systemPackages = [ inputs.my-neovim.packages.x86_64-linux.default ];

🔄 Collaboration and dependency conflicts#

When you reference multiple Flakes, each may depend on a different version of nixpkgs. Check the external projects’ compatibility requirements before sharing a version.

Solve it with follows!

If the external Flake supports your current nixpkgs, you can make a specified input follow the main configuration’s version:

inputs = {
  nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  home-manager = {
    url = "github:nix-community/home-manager";
    inputs.nixpkgs.follows = "nixpkgs"; # 强制跟随主 nixpkgs
  };
};

Collaboration workflow#

  1. Add an Input: edit flake.nix.
  2. Lock versions: run nix flake lock (which updates flake.lock automatically).
  3. Use an Output: reference the corresponding output by variable name in your code.

Summary#

Learning to reference external Flakes puts the whole Nix community’s toolkit at your disposal. Desktop environments, editors, and specialized development tools can all be made to work “out of the box.”

References & further reading

  1. Nix Flake Manual ↗
  2. Nix flake update ↗
  3. Nix flake check ↗
  4. Official Hyprland NixOS Documentation ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Flakes: project and dependency management

  1. 01Flake structure: inputs and outputs
  2. 02flake.lock and reproducibility
  3. 03Flake Utils and project templates
  4. 04External Flake references and collaboration
  5. 05Migrating NixOS configuration to Flakes
  6. 06A tutorial on Flake projects and dependency management
  7. 07How Flake updates work
  8. 08Rolling back and manually editing flake.lock
CONTINUE READING / NEXT ARTICLEMigrating NixOS configuration to Flakes

Create a Flake entry point from an existing configuration, then perform checks, builds, and activation separately.